lmc/dns/domain_slave/domain_slave.js

150 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2020-01-30 18:48:19 -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, apiPut, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.domain = "domain";
elements.domainLabel = "domain-label";
elements.domainTag = "domain-tag";
elements.domainTagLink = "domain-tag-link";
elements.masters = "masters";
elements.saveButton = "save-button";
elements.tags = "tags";
elements.transfers = "transfers";
// Data recieved from API calls
var data = {};
data.domain = {};
// Static references to UI elements
var ui = {};
ui.domain = {};
ui.domainLabel = {};
ui.domainTag = {};
ui.domainTagLink = {};
ui.masters = {};
ui.saveButton = {};
ui.tags = {};
ui.transfers = {};
// Callback for domain details API call
var displayDetails = function(response)
{
data.domain = response;
// Redirect if this is a master zone
if (data.domain.type == "master") {
location.href = "/dns/domain?did=" + data.domain.id;
return;
}
// Set page title and header stuff
document.title += " // " + data.domain.domain;
ui.domainLabel.innerHTML = data.domain.domain;
if (data.domain.tags.length == 1) {
ui.domainTagLink.href = "/dns?tag=" + data.domain.tags[0];
ui.domainTagLink.innerHTML = "(" + data.domain.tags[0] + ")";
ui.domainTag.style.display = "inline";
}
// Set form info
ui.domain.value = data.domain.domain;
ui.tags.value = data.domain.tags.join(",");
ui.masters.value = data.domain.master_ips.join("\n");
ui.transfers.value = data.domain.axfr_ips.join("\n");
ui.saveButton.disabled = false;
};
// Click handler for save button
var handleSave = function(event)
{
if (event.currentTarget.disabled)
return;
// This request can take a few seconds,
// so disable the button to prevent antsy users from double-submitting
ui.saveButton.disabled = true;
var req = {
"domain": ui.domain.value,
"tags": [],
"master_ips": [],
"axfr_ips": []
};
if (ui.tags.value.length)
req.tags = ui.tags.value.split(",");
if (ui.masters.value.length)
var masterLines = ui.masters.value.split("\n");
for (var i = 0; i < masterLines.length; i++)
req.master_ips = req.master_ips.concat(masterLines[i].split(";"));
if (ui.transfers.value.length) {
var transferLines = ui.transfers.value.split("\n");
for (var i = 0; i < transferLines.length; i++)
req.axfr_ips = req.axfr_ips.concat(transferLines[i].split(";"));
}
apiPut("/domains/" + data.params.did, req, function(response)
{
location.href = "/dns";
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
// We need a domain ID, so die if we don't have it
if (!data.params.did) {
alert("No domain ID supplied!");
return;
}
setupHeader();
// Update links on page to include proper Linode ID
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++)
anchors[i].href = anchors[i].href.replace("did=0", "did=" + data.params.did);
// Get element references
ui.domain = document.getElementById(elements.domain);
ui.domainLabel = document.getElementById(elements.domainLabel);
ui.domainTag = document.getElementById(elements.domainTag);
ui.domainTagLink = document.getElementById(elements.domainTagLink);
ui.masters = document.getElementById(elements.masters);
ui.saveButton = document.getElementById(elements.saveButton);
ui.tags = document.getElementById(elements.tags);
ui.transfers = document.getElementById(elements.transfers);
// Attach event handlers
ui.saveButton.addEventListener("click", handleSave);
// Get data from API
apiGet("/domains/" + data.params.did, displayDetails, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();