lmc/dns/resource/resource.js

318 lines
9.5 KiB
JavaScript

/*
* 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, apiPost, apiPut, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.a = "a";
elements.aHostname = "a-hostname";
elements.aIP = "a-ip";
elements.caa = "caa";
elements.caaSubdomain = "caa-subdomain";
elements.caaTag = "caa-tag";
elements.caaValue = "caa-value";
elements.cname = "cname";
elements.cnameHostname = "cname-hostname";
elements.cnameAlias = "cname-alias";
elements.domainLabel = "domain-label";
elements.domainTag = "domain-tag";
elements.domainTagLink = "domain-tag-link";
elements.mx = "mx";
elements.mxServer = "mx-server";
elements.mxPriority = "mx-priority";
elements.mxSubdomain = "mx-subdomain";
elements.ns = "ns";
elements.nsNameserver = "ns-nameserver";
elements.nsSubdomain = "ns-subdomain";
elements.saveButton = "save-button";
elements.srv = "srv";
elements.srvService = "srv-service";
elements.srvProtocol = "srv-protocol";
elements.srvTarget = "srv-target";
elements.srvPriority = "srv-priority";
elements.srvWeight = "srv-weight";
elements.srvPort = "srv-port";
elements.ttl = "ttl";
elements.txt = "txt";
elements.txtName = "txt-name";
elements.txtValue = "txt-value";
// Data recieved from API calls
var data = {};
data.domain = {};
data.record = {};
// Static references to UI elements
var ui = {};
ui.a = [];
ui.aaaa = [];
ui.aHostname = {};
ui.aIP = {};
ui.caa = [];
ui.caaSubdomain = {};
ui.caaTag = {};
ui.caaValue = {};
ui.cname = [];
ui.cnameHostname = {};
ui.cnameAlias = {};
ui.domainLabel = {};
ui.domainTag = {};
ui.domainTagLink = {};
ui.mx = [];
ui.mxServer = {};
ui.mxPriority = {};
ui.mxSubdomain = {};
ui.ns = [];
ui.nsNameserver = {};
ui.nsSubdomain = {};
ui.saveButton = {};
ui.srv = [];
ui.srvService = {};
ui.srvProtocol = {};
ui.srvTarget = {};
ui.srvPriority = {};
ui.srvWeight = {};
ui.srvPort = {};
ui.ttl = {};
ui.txt = [];
ui.txtName = {};
ui.txtValue = {};
// Callback for domain details API call
var displayDetails = function(response)
{
data.domain = response;
// 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";
}
};
// Callback for record details API call
var displayRecord = function(response)
{
data.record = response;
// Display controls for record type
data.record.type = data.record.type.toLowerCase();
for (var i = 0; i < ui[data.record.type].length; i++)
ui[data.record.type][i].style.display = "table-row-group";
// Fill in form data
switch (data.record.type) {
case "a":
case "aaaa":
ui.aHostname.value = data.record.name;
ui.aIP.value = data.record.target;
break;
case "caa":
ui.caaSubdomain.value = data.record.name;
ui.caaTag.value = data.record.tag;
ui.caaValue.value = data.record.target;
break;
case "cname":
ui.cnameHostname.value = data.record.name;
ui.cnameAlias.value = data.record.target;
break;
case "mx":
ui.mxServer.value = data.record.target;
ui.mxPriority.value = data.record.priority;
ui.mxSubdomain.value = data.record.name;
break;
case "ns":
ui.nsNameserver.value = data.record.target;
ui.nsSubdomain.value = data.record.name;
break;
case "srv":
if (data.record.service.charAt(0) == "_")
ui.srvService.value = data.record.service;
else
ui.srvService.value = "_" + data.record.service;
ui.srvProtocol.value = data.record.protocol;
ui.srvTarget.value = data.record.target;
ui.srvPriority.value = data.record.priority;
ui.srvWeight.value = data.record.weight;
ui.srvPort.value = data.record.port;
break;
case "txt":
ui.txtName.value = data.record.name;
ui.txtValue.value = data.record.target;
break;
}
ui.ttl.value = data.record.ttl_sec;
ui.saveButton.disabled = false;
};
// Click handler for save button
var handleSave = function(event)
{
if (event.currentTarget.disabled)
return;
var type;
if (data.record.type)
type = data.record.type;
else
type = data.params.type;
var req = {
"ttl_sec": parseInt(ui.ttl.value),
"type": type.toUpperCase()
};
switch (type) {
case "a":
case "aaaa":
req.name = ui.aHostname.value;
req.target = ui.aIP.value;
break;
case "caa":
req.name = ui.caaSubdomain.value;
req.tag = ui.caaTag.value;
req.target = ui.caaValue.value;
break;
case "cname":
req.name = ui.cnameHostname.value;
req.target = ui.cnameAlias.value;
break;
case "mx":
req.target = ui.mxServer.value;
req.priority = parseInt(ui.mxPriority.value);
req.name = ui.mxSubdomain.value;
break;
case "ns":
req.target = ui.nsNameserver.value;
req.name = ui.nsSubdomain.value;
break;
case "srv":
req.service = ui.srvService.value;
req.protocol = ui.srvProtocol.value;
req.target = ui.srvTarget.value;
req.priority = parseInt(ui.srvPriority.value);
req.weight = parseInt(ui.srvWeight.value);
req.port = parseInt(ui.srvPort.value);
break;
case "txt":
req.name = ui.txtName.value;
req.target = ui.txtValue.value;
break;
}
var callback = function(response)
{
location.href = "/dns/domain?did=" + data.params.did;
};
if (data.params.rid == "0")
apiPost("/domains/" + data.params.did + "/records", req, callback);
else
apiPut("/domains/" + data.params.did + "/records/" + data.params.rid, req, callback);
};
// 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;
}
// We also need a resource ID
if (!data.params.rid) {
alert("No resource ID supplied!");
return;
}
// If this is a new resource, we also need a resource type
if (data.params.rid == "0" && !data.params.type) {
alert("No resource type 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.a = document.getElementsByClassName(elements.a);
ui.aaaa = ui.a;
ui.aHostname = document.getElementById(elements.aHostname);
ui.aIP = document.getElementById(elements.aIP);
ui.caa = document.getElementsByClassName(elements.caa);
ui.caaSubdomain = document.getElementById(elements.caaSubdomain);
ui.caaTag = document.getElementById(elements.caaTag);
ui.caaValue = document.getElementById(elements.caaValue);
ui.cname = document.getElementsByClassName(elements.cname);
ui.cnameHostname = document.getElementById(elements.cnameHostname);
ui.cnameAlias = document.getElementById(elements.cnameAlias);
ui.domainLabel = document.getElementById(elements.domainLabel);
ui.domainTag = document.getElementById(elements.domainTag);
ui.domainTagLink = document.getElementById(elements.domainTagLink);
ui.mx = document.getElementsByClassName(elements.mx);
ui.mxServer = document.getElementById(elements.mxServer);
ui.mxPriority = document.getElementById(elements.mxPriority);
ui.mxSubdomain = document.getElementById(elements.mxSubdomain);
ui.ns = document.getElementsByClassName(elements.ns);
ui.nsNameserver = document.getElementById(elements.nsNameserver);
ui.nsSubdomain = document.getElementById(elements.nsSubdomain);
ui.saveButton = document.getElementById(elements.saveButton);
ui.srv = document.getElementsByClassName(elements.srv);
ui.srvService = document.getElementById(elements.srvService);
ui.srvProtocol = document.getElementById(elements.srvProtocol);
ui.srvTarget = document.getElementById(elements.srvTarget);
ui.srvPriority = document.getElementById(elements.srvPriority);
ui.srvWeight = document.getElementById(elements.srvWeight);
ui.srvPort = document.getElementById(elements.srvPort);
ui.ttl = document.getElementById(elements.ttl);
ui.txt = document.getElementsByClassName(elements.txt);
ui.txtName = document.getElementById(elements.txtName);
ui.txtValue = document.getElementById(elements.txtValue);
// Display controls for given type
if (data.params.rid == "0") {
for (var i = 0; i < ui[data.params.type].length; i++)
ui[data.params.type][i].style.display = "table-row-group";
ui.saveButton.disabled = false;
}
// Attach event handlers
ui.saveButton.addEventListener("click", handleSave);
// Get data from API
apiGet("/domains/" + data.params.did, displayDetails, null);
if (data.params.rid != "0")
apiGet("/domains/" + data.params.did + "/records/" + data.params.rid, displayRecord, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();