lmc/dns/dns.js

238 lines
7.3 KiB
JavaScript
Raw Permalink Normal View History

2020-01-18 17:50:00 -05:00
/*
* This file is part of Linode Manager Classic.
*
* Linode Manager Classic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Linode Manager Classic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Linode Manager Classic. If not, see <https://www.gnu.org/licenses/>.
*/
import { settings, elements, apiGet, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.centerCell = "center-cell";
elements.domains = "domains";
elements.domainTagPrefix = "domain-tag-";
elements.lmcRow = "lmc-tr1";
elements.lmcRowAlt = "lmc-tr2";
elements.lmcTable = "lmc-table";
elements.loading = "loading";
elements.sublinks = "sub-links";
// Data recieved from API calls
var data = {};
data.domains = [];
data.domainTags = [];
data.noTag = false;
// Static references to UI elements
var ui = {};
ui.domains = {};
ui.domainTables = {};
ui.loading = {};
var createDomainRow = function(domain, alt)
{
var row = document.createElement("tr");
if (alt)
row.className = elements.lmcRowAlt;
else
row.className = elements.lmcRow;
var name = document.createElement("td");
var nameLink = document.createElement("a");
2020-01-30 18:48:19 -05:00
if (domain.type == "master")
nameLink.href = "/dns/domain?did=" + domain.id;
else
nameLink.href = "/dns/domain_slave?did=" + domain.id;
2020-01-18 17:50:00 -05:00
nameLink.innerHTML = domain.domain;
name.appendChild(nameLink);
row.appendChild(name);
var type = document.createElement("td");
type.innerHTML = domain.type;
row.appendChild(type);
2022-10-19 13:07:52 -04:00
var modified = document.createElement("td");
var modifiedDate = new Date(domain.updated + "Z");
modified.innerHTML = modifiedDate.toLocaleString();
row.appendChild(modified);
2020-01-18 17:50:00 -05:00
var status = document.createElement("td");
status.innerHTML = domain.status.charAt(0).toUpperCase() + domain.status.slice(1).replace(/_/g, " ");
row.appendChild(status);
var options = document.createElement("td");
options.className = elements.centerCell;
var editLink = document.createElement("a");
2020-01-30 18:48:19 -05:00
if (domain.type == "master")
editLink.href = "/dns/domain?did=" + domain.id;
else
editLink.href = "/dns/domain_slave?did=" + domain.id;
2020-01-18 17:50:00 -05:00
editLink.innerHTML = "Edit";
var separator = document.createElement("span");
separator.innerHTML = " | ";
var removeLink = document.createElement("a");
removeLink.href = "/dns/domain_delete?did=" + domain.id;
removeLink.innerHTML = "Remove";
2022-10-19 13:56:40 -04:00
var zoneFileLink = document.createElement("a");
zoneFileLink.href = "/dns/domain_zonefile?did=" + domain.id;
zoneFileLink.innerHTML = "Zone file";
2020-01-18 17:50:00 -05:00
options.appendChild(editLink);
options.appendChild(separator);
options.appendChild(removeLink);
2022-10-19 13:56:40 -04:00
options.appendChild(separator.cloneNode(true));
options.appendChild(zoneFileLink);
2020-01-18 17:50:00 -05:00
row.appendChild(options);
return row;
};
var createDomainTable = function(tag)
{
var table = document.createElement("table");
table.id = elements.domainTagPrefix + tag;
table.className = elements.lmcTable;
var thead = document.createElement("thead");
var headRow1 = document.createElement("tr");
var title = document.createElement("td");
if (!tag.length)
title.innerHTML = "Domains";
else
title.innerHTML = tag;
headRow1.appendChild(title);
var headRow2 = document.createElement("tr");
2022-10-19 13:07:52 -04:00
var cells = ["Domain Zone", "Type", "Last Modified", "Status", "Options"];
2020-01-18 17:50:00 -05:00
title.colspan = cells.length;
for (var i = 0; i < cells.length; i++) {
var cell = document.createElement("td");
if (cells[i] == "Options")
cell.className = elements.centerCell;
cell.innerHTML = cells[i];
headRow2.appendChild(cell);
}
thead.appendChild(headRow1);
thead.appendChild(headRow2);
var tbody = document.createElement("tbody");
table.appendChild(thead);
table.appendChild(tbody);
ui.domainTables[tag] = tbody;
var sublinks = document.createElement("p");
sublinks.className = elements.sublinks;
var importZone = document.createElement("a");
importZone.href = "/dns/domain_import";
importZone.innerHTML = "Import a zone";
var separator = document.createElement("span");
separator.innerHTML = " | ";
var clone = document.createElement("a");
clone.href = "/dns/domain_clone";
clone.innerHTML = "Clone an existing zone";
var add = document.createElement("a");
add.href = "/dns/domain_add";
add.innerHTML = "Add a domain zone";
sublinks.appendChild(importZone);
sublinks.appendChild(separator);
sublinks.appendChild(clone);
sublinks.appendChild(separator.cloneNode(true));
sublinks.appendChild(add);
ui.domains.appendChild(table);
ui.domains.appendChild(sublinks);
};
// Callback for domains API call
var displayDomains = function(response)
{
// Add domains to array
data.domains = data.domains.concat(response.data);
// Add new tags to array
for (var i = 0; i < response.data.length; i++) {
if (!response.data[i].tags.length)
data.noTag = true;
for (var j = 0; j < response.data[i].tags.length; j++) {
if (!data.domainTags.includes(response.data[i].tags[j]))
data.domainTags.push(response.data[i].tags[j]);
}
}
// Request the next page if there are more pages
if (response.page != response.pages) {
var progress = (response.page / response.pages) * 100;
progress = progress.toFixed(0);
ui.loading.innerHTML = "Loading " + progress + "%...";
var filters = null;
if (data.params.tag)
filters = { "tags": data.params.tag };
apiGet("/domains?page=" + (response.page + 1), displayDomains, filters);
return;
}
// Remove tag filter if there are no domains with given tag
if (!data.domains.length && data.params.tag)
location.href = "/dns";
// Redirect to add page if there are no domains
if (!data.domains.length && !data.params.tag)
location.href = "/dns/domain_add";
// Sort
data.domainTags.sort();
data.domains.sort(function(a, b)
{
return a.domain.toLowerCase().localeCompare(b.domain.toLowerCase());
});
// Create tables
ui.loading.remove();
if (data.noTag)
createDomainTable("");
for (var i = 0; i < data.domainTags.length; i++)
createDomainTable(data.domainTags[i]);
// Insert domains
for (var i = 0; i < data.domains.length; i++) {
if (!data.domains[i].tags.length)
ui.domainTables[""].appendChild(createDomainRow(data.domains[i], ui.domainTables[""].children.length % 2));
for (var j = 0; j < data.domains[i].tags.length; j++)
ui.domainTables[data.domains[i].tags[j]].appendChild(createDomainRow(data.domains[i], ui.domainTables[data.domains[i].tags[j]].children.length % 2));
}
};
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.domains = document.getElementById(elements.domains);
ui.loading = document.getElementById(elements.loading);
// Get linode and transfer info
var filters = null;
if (data.params.tag)
filters = { "tags": data.params.tag };
apiGet("/domains", displayDomains, filters);
};
// Attach onload handler
window.addEventListener("load", setup);
})();