commento/frontend/js/dashboard-domain.js

150 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-06-04 00:06:17 +08:00
(function (global, document) {
"use strict";
2018-06-04 00:06:17 +08:00
// Selects a domain.
global.domainSelect = function(domain) {
var data = global.dashboard.$data;
var domains = data.domains;
for (var i = 0; i < domains.length; i++) {
if (domains[i].domain === domain) {
global.vs("frozen", domains[i].state === "frozen");
2018-06-04 00:06:17 +08:00
domains[i].selected = true;
data.cd = i;
data.importedComments = domains[i].importedComments;
} else {
2018-06-04 00:06:17 +08:00
domains[i].selected = false;
}
2018-06-04 00:06:17 +08:00
}
data.showSettings = true;
global.settingDeselectAll();
2018-06-04 00:06:17 +08:00
$(".view").hide();
};
// Deselects all domains.
global.domainDeselectAll = function() {
var data = global.dashboard.$data;
var domains = data.domains;
for (var i = 0; i < domains.length; i++) {
2018-06-04 00:06:17 +08:00
domains[i].selected = false;
}
2018-06-04 00:06:17 +08:00
}
// Creates a new domain.
global.domainNewHandler = function() {
var json = {
"ownerToken": global.cookieGet("commentoOwnerToken"),
"name": $("#new-domain-name").val(),
"domain": $("#new-domain-domain").val(),
2018-06-04 00:06:17 +08:00
}
global.buttonDisable("#add-site-button");
global.post(global.origin + "/api/domain/new", json, function(resp) {
2018-06-04 00:06:17 +08:00
global.buttonEnable("#add-site-button");
$("#new-domain-name").val("");
$("#new-domain-domain").val("");
document.location.hash = "#modal-close";
if (!resp.success) {
global.globalErrorShow(resp.message);
return;
}
global.domainRefresh(function() {
global.domainSelect(resp.domain);
global.domainDeselectAll();
global.settingSelect("installation");
});
});
}
// Refreshes the list of domains.
global.domainRefresh = function(callback) {
var json = {
"ownerToken": global.cookieGet("commentoOwnerToken"),
2018-06-04 00:06:17 +08:00
};
global.post(global.origin + "/api/domain/list", json, function(resp) {
2018-06-04 00:06:17 +08:00
if (!resp.success) {
global.globalErrorShow(resp.message);
return;
}
resp.domains = resp.domains.sort(function(a, b) {
var x = a.creationDate; var y = b.creationDate;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
2018-06-04 00:06:17 +08:00
});
for (var i = 0; i < resp.domains.length; i++) {
resp.domains[i].show = true;
resp.domains[i].selected = false;
resp.domains[i].origName = resp.domains[i].name;
resp.domains[i].origDomain = resp.domains[i].domain;
resp.domains[i].viewsLast30Days = global.numberify(0);
resp.domains[i].commentsLast30Days = global.numberify(0);
for (var j = 0; j < resp.domains[i].moderators.length; j++) {
resp.domains[i].moderators[j].timeAgo = global.timeSince(
Date.parse(resp.domains[i].moderators[j].addDate));
}
}
global.vs("domains", resp.domains);
if (callback !== undefined) {
2018-06-04 00:06:17 +08:00
callback();
}
2018-06-04 00:06:17 +08:00
});
};
// Updates a domain with the backend.
global.domainUpdate = function(domain, callback) {
var json = {
"ownerToken": global.cookieGet("commentoOwnerToken"),
"domain": domain,
2018-06-04 00:06:17 +08:00
};
global.post(global.origin + "/api/domain/update", json, function(resp) {
if (callback !== undefined) {
2018-06-04 00:06:17 +08:00
callback(resp.success);
}
2018-06-04 00:06:17 +08:00
if (!resp.success) {
global.globalErrorShow(resp.message);
return;
}
});
}
// Deletes a domain.
global.domainDelete = function(domain, callback) {
var json = {
"ownerToken": global.cookieGet("commentoOwnerToken"),
"domain": domain,
2018-06-04 00:06:17 +08:00
};
global.post(global.origin + "/api/domain/delete", json, function(resp) {
2018-06-04 00:06:17 +08:00
if (!resp.success) {
global.globalErrorShow(resp.message);
return;
}
if (callback !== undefined) {
2018-06-04 00:06:17 +08:00
callback(resp.success);
}
2018-06-04 00:06:17 +08:00
});
}
} (window.commento, document));