2018-01-10 21:54:08 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2018-01-18 00:01:44 +00:00
|
|
|
var cache = { recordsMap: {} };
|
|
|
|
|
2018-01-12 09:03:41 +00:00
|
|
|
if (!Element.prototype.matches) {
|
|
|
|
Element.prototype.matches = Element.prototype.msMatchesSelector;
|
|
|
|
}
|
2018-01-12 09:46:17 +00:00
|
|
|
function $qs(qs, el) {
|
|
|
|
return (el||document).querySelector(qs);
|
2018-01-10 21:54:08 +00:00
|
|
|
}
|
2018-01-17 09:17:39 +00:00
|
|
|
function $qsa(qs, el) {
|
2018-01-21 08:04:29 +00:00
|
|
|
return Array.prototype.slice.call((el||document).querySelectorAll(qs));
|
2018-01-17 09:17:39 +00:00
|
|
|
}
|
2018-01-12 09:03:41 +00:00
|
|
|
function $on(selector, eventname, cb) {
|
|
|
|
if (!$on._events[eventname]) {
|
|
|
|
$on._events[eventname] = $on._dispatcher(eventname);
|
|
|
|
document.addEventListener(eventname, $on._events[eventname]);
|
2018-01-10 21:54:08 +00:00
|
|
|
}
|
2018-01-12 09:03:41 +00:00
|
|
|
if (!$on._handlers[eventname]) {
|
|
|
|
$on._handlers[eventname] = {};
|
2018-01-10 21:54:08 +00:00
|
|
|
}
|
2018-01-12 09:03:41 +00:00
|
|
|
if (!$on._handlers[eventname][selector]) {
|
|
|
|
$on._handlers[eventname][selector] = [];
|
|
|
|
}
|
|
|
|
$on._handlers[eventname][selector].push(cb);
|
|
|
|
}
|
|
|
|
$on._events = {};
|
|
|
|
$on._handlers = {};
|
|
|
|
$on._dispatcher = function (eventname) {
|
|
|
|
return function (ev) {
|
|
|
|
//console.log('event: ' + ev.type);
|
|
|
|
if (!$on._handlers[eventname]) {
|
|
|
|
console.warn('no handlers for event');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var matches = Object.keys($on._handlers[eventname]).some(function (selector) {
|
|
|
|
if (ev.target.matches(selector)) {
|
|
|
|
$on._handlers[eventname][selector].forEach(function (cb) { cb(ev); });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2018-01-12 09:46:17 +00:00
|
|
|
if (matches) {
|
2018-01-12 09:03:41 +00:00
|
|
|
console.warn("no handlers for selector");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-01-12 09:46:17 +00:00
|
|
|
function verifyAuth(/*ev*/) {
|
2018-01-12 09:03:41 +00:00
|
|
|
auth = $qs('input.js-jwt').value;
|
|
|
|
return window.fetch(
|
|
|
|
'/api/verify-auth'
|
|
|
|
, { method: 'GET'
|
|
|
|
, headers: new window.Headers({ 'Authorization': 'Bearer ' + auth })
|
|
|
|
}
|
|
|
|
).then(function (resp) {
|
|
|
|
return resp.json().then(function (data) {
|
|
|
|
if (data.error) {
|
2018-01-12 09:46:17 +00:00
|
|
|
localStorage.removeItem('auth');
|
2018-01-12 09:03:41 +00:00
|
|
|
console.error(data.error);
|
|
|
|
window.alert('Bad HTTP Response: ' + data.error.message);
|
|
|
|
throw new Error('Bad HTTP Response: ' + data.error.message);
|
|
|
|
}
|
|
|
|
|
2018-01-12 09:46:17 +00:00
|
|
|
console.log('verify-auth:');
|
2018-01-12 09:03:41 +00:00
|
|
|
console.log(data);
|
|
|
|
|
|
|
|
localStorage.setItem('auth', auth);
|
2018-01-12 09:46:17 +00:00
|
|
|
|
|
|
|
return fetchPeers(auth).then(function () {
|
|
|
|
return fetchZones(auth);
|
|
|
|
});
|
2018-01-12 09:03:41 +00:00
|
|
|
});
|
|
|
|
});
|
2018-01-12 09:46:17 +00:00
|
|
|
}
|
|
|
|
function fetchPeers(auth) {
|
|
|
|
return window.fetch('/api/peers', {
|
|
|
|
method: 'GET'
|
|
|
|
, headers: new window.Headers({ 'Authorization': 'Bearer ' + auth })
|
|
|
|
}).then(function (resp) {
|
|
|
|
return resp.json().then(function (data) {
|
|
|
|
var tpl = '';
|
|
|
|
var el;
|
|
|
|
if (!tpls.peer) {
|
|
|
|
tpls.peer = $qs('.js-peer-tpl').innerHTML;
|
|
|
|
}
|
|
|
|
data.peers.forEach(function (peer) {
|
|
|
|
el = document.createElement('div');
|
|
|
|
el.innerHTML = tpls.peer;
|
|
|
|
console.log(el);
|
|
|
|
console.log($qs('.js-peer-name', el));
|
|
|
|
$qs('.js-peer-name', el).innerText = peer.name;
|
|
|
|
$qs('.js-peer-name', el).dataset.id = peer.name;
|
|
|
|
console.log(el.innerHTML);
|
|
|
|
tpl += el.innerHTML;
|
|
|
|
console.log(tpl);
|
|
|
|
});
|
|
|
|
$qs('.js-peer-tpl').innerHTML = tpl;
|
|
|
|
console.log($qs('.js-peer-tpl').innerHTML);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function fetchZones(auth) {
|
|
|
|
return window.fetch('/api/zones', {
|
|
|
|
method: 'GET'
|
|
|
|
, headers: new window.Headers({ 'Authorization': 'Bearer ' + auth })
|
|
|
|
}).then(function (resp) {
|
|
|
|
return resp.json().then(function (data) {
|
|
|
|
var tpl = '';
|
|
|
|
var el;
|
|
|
|
if (!tpls.zone) {
|
|
|
|
tpls.zone = $qs('.js-zone-tpl').innerHTML;
|
|
|
|
}
|
|
|
|
data.zones.forEach(function (zone) {
|
|
|
|
el = document.createElement('div');
|
|
|
|
el.innerHTML = tpls.zone;
|
|
|
|
console.log(el);
|
|
|
|
console.log($qs('.js-zone-name', el));
|
2018-01-21 08:47:37 +00:00
|
|
|
$qs('.js-zone-name', el).innerText = zone.name;
|
2018-01-12 09:46:17 +00:00
|
|
|
$qs('.js-zone-name', el).dataset.id = zone.id;
|
2018-01-21 08:47:37 +00:00
|
|
|
$qs('.js-zone-name', el).dataset.name = zone.name;
|
2018-01-12 09:46:17 +00:00
|
|
|
console.log(el.innerHTML);
|
|
|
|
tpl += el.innerHTML;
|
|
|
|
console.log(tpl);
|
|
|
|
});
|
|
|
|
$qs('.js-zone-tpl').innerHTML = tpl;
|
|
|
|
console.log($qs('.js-zone-tpl').innerHTML);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-01-21 08:04:29 +00:00
|
|
|
function renderRecords() {
|
|
|
|
var tpl = '';
|
|
|
|
var el;
|
|
|
|
if (!tpls.recordsMap) {
|
|
|
|
//tpls.recordTypes = Array.prototype.slice.call($qsa('.js-record-tpl li'));
|
|
|
|
//.innerHTML;
|
|
|
|
tpls.recordsMap = {};
|
|
|
|
tpls.recordsMap.soa = $qs('.js-record-soa').outerHTML;
|
|
|
|
tpls.recordsMap.ns = $qs('.js-record-ns').outerHTML;
|
|
|
|
tpls.recordsMap.a = $qs('.js-record-a').outerHTML;
|
|
|
|
tpls.recordsMap.aaaa = $qs('.js-record-aaaa').outerHTML;
|
|
|
|
tpls.recordsMap.aname = $qs('.js-record-aname').outerHTML;
|
|
|
|
tpls.recordsMap.cname = $qs('.js-record-cname').outerHTML;
|
|
|
|
tpls.recordsMap.caa = $qs('.js-record-caa').outerHTML;
|
|
|
|
tpls.recordsMap.ptr = $qs('.js-record-ptr').outerHTML;
|
|
|
|
tpls.recordsMap.mx = $qs('.js-record-mx').outerHTML;
|
|
|
|
tpls.recordsMap.txt = $qs('.js-record-txt').outerHTML;
|
|
|
|
tpls.recordsMap.srv = $qs('.js-record-srv').outerHTML;
|
|
|
|
//tpls.recordsMap.typex = $qs('.js-record-typex').outerHTML;
|
|
|
|
}
|
|
|
|
console.log('tpls.recordsMap:');
|
|
|
|
console.log(tpls.recordsMap);
|
|
|
|
cache.records.forEach(function (record) {
|
|
|
|
el = document.createElement('div');
|
|
|
|
console.log('record.type:');
|
|
|
|
console.log(record.type);
|
|
|
|
el.innerHTML = tpls.recordsMap[record.type.toLowerCase()];
|
|
|
|
console.log(el);
|
|
|
|
console.log($qs('.js-record-name', el));
|
|
|
|
Object.keys(record).sort().forEach(function (key) {
|
|
|
|
if ('id' === key) { return; }
|
|
|
|
var x = $qs('.js-record-' + key, el);
|
|
|
|
if (x) {
|
|
|
|
x.innerText = record[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
el.dataset.recordId = record.id;
|
|
|
|
$qs('input.js-record-id', el).value = record.id;
|
|
|
|
cache.recordsMap[record.id] = record;
|
|
|
|
|
|
|
|
//$qs('.js-record-type', el).innerText = record.type;
|
|
|
|
//$qs('.js-record-name', el).innerText = record.name;
|
|
|
|
//$qs('.js-record-address', el).innerText = record.address;
|
|
|
|
console.log('el.innerHTML:');
|
|
|
|
console.log(el.innerHTML);
|
|
|
|
tpl += el.innerHTML;
|
|
|
|
console.log(tpl);
|
|
|
|
});
|
|
|
|
$qs('.js-record-tpl').innerHTML = tpl;
|
|
|
|
console.log($qs('.js-record-tpl').innerHTML);
|
|
|
|
}
|
2018-01-12 09:46:17 +00:00
|
|
|
function fetchRecords(zone) {
|
|
|
|
return window.fetch('/api/zones/' + zone + '/records', {
|
|
|
|
method: 'GET'
|
|
|
|
, headers: new window.Headers({ 'Authorization': 'Bearer ' + auth })
|
|
|
|
}).then(function (resp) {
|
|
|
|
return resp.json().then(function (data) {
|
2018-01-18 00:01:44 +00:00
|
|
|
cache.records = data.records;
|
2018-01-21 08:04:29 +00:00
|
|
|
renderRecords();
|
2018-01-12 09:46:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// BEGIN
|
|
|
|
var tpls = {};
|
|
|
|
var auth = localStorage.getItem('auth');
|
|
|
|
if (auth) {
|
|
|
|
$qs('input.js-jwt').value = auth;
|
|
|
|
verifyAuth();
|
|
|
|
}
|
|
|
|
|
|
|
|
$on('body', 'click', function () {
|
|
|
|
console.log('woo-hoo, that tickles my body!');
|
2018-01-12 09:03:41 +00:00
|
|
|
});
|
|
|
|
|
2018-01-12 09:46:17 +00:00
|
|
|
$on('button.js-jwt', 'click', verifyAuth);
|
|
|
|
|
2018-01-12 09:03:41 +00:00
|
|
|
$on('input.js-zone', 'keyup', function (ev) {
|
|
|
|
$qs('code.js-zone').innerHTML = ev.target.value || ':zone';
|
|
|
|
// $qs('a.js-zone').setAttribute('data-href', ...)
|
|
|
|
$qs('a.js-zone').href =
|
|
|
|
$qs('a.js-zone').dataset.href.replace(/:zone/, ev.target.value || ':zone');
|
|
|
|
});
|
|
|
|
|
|
|
|
$on('input.js-name', 'keyup', function (ev) {
|
|
|
|
$qs('code.js-name').innerHTML = ev.target.value || ':name';
|
|
|
|
$qs('a.js-name').href =
|
|
|
|
$qs('a.js-name').dataset.href.replace(/:name/, ev.target.value || ':name');
|
2018-01-10 21:54:08 +00:00
|
|
|
});
|
|
|
|
|
2018-01-12 09:46:17 +00:00
|
|
|
$on('button.js-zone-name', 'click', function (ev) {
|
2018-01-21 08:47:37 +00:00
|
|
|
var zone = ev.target.dataset.name;
|
2018-01-12 09:46:17 +00:00
|
|
|
return fetchRecords(zone);/*.then(function () {
|
|
|
|
});*/
|
|
|
|
});
|
|
|
|
|
2018-01-18 00:01:44 +00:00
|
|
|
$on('select.js-record-form-type', 'change', function (ev) {
|
|
|
|
var type = ev.target.value;
|
|
|
|
console.log("form type:", type);
|
|
|
|
if (!tpls.formsMap) {
|
|
|
|
tpls.formsMap = {};
|
|
|
|
tpls.formsMap.soa = $qs('.js-record-form-soa').outerHTML;
|
|
|
|
tpls.formsMap.ns = $qs('.js-record-form-ns').outerHTML;
|
|
|
|
tpls.formsMap.a = $qs('.js-record-form-a').outerHTML;
|
|
|
|
tpls.formsMap.aaaa = $qs('.js-record-form-aaaa').outerHTML;
|
|
|
|
tpls.formsMap.aname = $qs('.js-record-form-aname').outerHTML;
|
|
|
|
tpls.formsMap.caa = $qs('.js-record-form-caa').outerHTML;
|
|
|
|
tpls.formsMap.cname = $qs('.js-record-form-cname').outerHTML;
|
|
|
|
tpls.formsMap.mx = $qs('.js-record-form-mx').outerHTML;
|
|
|
|
tpls.formsMap.ptr = $qs('.js-record-form-ptr').outerHTML;
|
|
|
|
tpls.formsMap.srv = $qs('.js-record-form-srv').outerHTML;
|
|
|
|
tpls.formsMap.txt = $qs('.js-record-form-txt').outerHTML;
|
|
|
|
tpls.formsMap.typex = $qs('.js-record-form-typex').outerHTML;
|
|
|
|
}
|
|
|
|
$qs('.js-record-form-tpl').innerHTML = tpls.formsMap[type] || '';
|
|
|
|
});
|
|
|
|
|
|
|
|
$on('button.js-record-edit', 'click', function (ev) {
|
|
|
|
var id = ev.target.parentElement.querySelector('.js-record-id').value;
|
|
|
|
var record = cache.recordsMap[id];
|
2018-01-21 04:23:59 +00:00
|
|
|
var formTpl = tpls.formsMap[record.type.toLowerCase()];
|
2018-01-21 04:08:29 +00:00
|
|
|
if (!formTpl) {
|
|
|
|
formTpl = tpls.formsMap.typex;
|
|
|
|
}
|
|
|
|
|
2018-01-21 04:23:59 +00:00
|
|
|
console.log(ev.target);
|
|
|
|
console.log(id);
|
|
|
|
console.log(record);
|
|
|
|
|
2018-01-21 08:04:29 +00:00
|
|
|
formTpl = tpls.formsMap[(record.type||'typex').toLowerCase()];
|
|
|
|
$qs('select.js-record-form-type').value = (record.type||'typex').toLowerCase();
|
2018-01-21 04:08:29 +00:00
|
|
|
$qs('select.js-record-form-type').dispatchEvent(new Event('change', { bubbles: true }));
|
|
|
|
$qs('.js-record-form-tpl').innerHTML = formTpl || '';
|
|
|
|
|
2018-01-21 04:23:59 +00:00
|
|
|
record.host = record.name.replace(new RegExp('\\.?' + (record.zone || record.name).replace(/\./g, '\\.') + '$'), '');
|
2018-01-21 04:08:29 +00:00
|
|
|
|
|
|
|
console.log('record.type:');
|
|
|
|
console.log(record.type.toLowerCase());
|
2018-01-21 04:23:59 +00:00
|
|
|
|
2018-01-21 04:08:29 +00:00
|
|
|
Object.keys(record).forEach(function (key) {
|
|
|
|
var $el = $qs('.js-record-' + key, $qs('.js-record-form-tpl'));
|
|
|
|
if (!$el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$el.value = record[key];
|
|
|
|
});
|
2018-01-21 04:23:59 +00:00
|
|
|
if (!record.host) { $qs('.js-record-host').placeholder = '@'; }
|
|
|
|
$qs('.js-record-type', $qs('.js-record-form-tpl')).innerHTML = record.type;
|
2018-01-18 00:01:44 +00:00
|
|
|
});
|
|
|
|
|
2018-01-21 08:04:29 +00:00
|
|
|
$on('button.js-record-save', 'click', function (ev) {
|
|
|
|
console.log('save');
|
|
|
|
var $pel = ev.target.parentElement;
|
|
|
|
var id = $qs('.js-record-id', $pel).value;
|
|
|
|
var existingRecord = cache.recordsMap[id];
|
2018-01-21 08:47:37 +00:00
|
|
|
var record = {};
|
2018-01-21 08:04:29 +00:00
|
|
|
var change;
|
|
|
|
|
|
|
|
$qsa('input[class*="js-record-"]', $pel).forEach(function ($el) {
|
|
|
|
var key;
|
|
|
|
Array.prototype.some.call($el.classList, function (str) {
|
|
|
|
if (/^js-record-/.test(str)) {
|
|
|
|
key = str.replace(/^js-record-(.*)/, '$1');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!key) { return; }
|
|
|
|
if ('undefined' === typeof $el.value || 'INPUT' !== $el.tagName) { return; }
|
|
|
|
record[key] = $el.value;
|
|
|
|
});
|
|
|
|
|
2018-01-21 08:47:37 +00:00
|
|
|
if (!record.id) {
|
|
|
|
record.id = '';
|
2018-01-21 08:04:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
record.type = record.type || $qs('.js-record-type', $pel).innerHTML || $qs('.js-record-type', $pel).value;
|
|
|
|
console.log('record.type:', record.type);
|
|
|
|
/*
|
|
|
|
if (!tpls.recordsMap[record.type.toLowerCase()]) {
|
|
|
|
record.typex = 'typex';
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!existingRecord) {
|
|
|
|
change = true;
|
|
|
|
cache.recordsMap[record.id] = record;
|
|
|
|
cache.records.push(record);
|
|
|
|
} else {
|
|
|
|
console.log('keys:', Object.keys(record));
|
|
|
|
Object.keys(record).forEach(function (key) {
|
|
|
|
console.log(key);
|
|
|
|
if (existingRecord[key].toString() !== record[key].toString()) {
|
|
|
|
change = true;
|
|
|
|
existingRecord[key] = record[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change) {
|
|
|
|
renderRecords();
|
|
|
|
|
|
|
|
return window.fetch(
|
|
|
|
'/api/records/' + record.id
|
|
|
|
, { method: 'POST'
|
|
|
|
, headers: new window.Headers({ 'Authorization': 'Bearer ' + auth })
|
|
|
|
, body: JSON.stringify(record)
|
|
|
|
}
|
|
|
|
).then(function (resp) {
|
|
|
|
return resp.json().then(function (data) {
|
|
|
|
console.log('result:', data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-18 00:01:44 +00:00
|
|
|
$qs('select.js-record-form-type').value = '';
|
|
|
|
// Create a new 'change' event and dispatch it.
|
|
|
|
$qs('select.js-record-form-type').dispatchEvent(new Event('change', { bubbles: true }));
|
|
|
|
|
2018-01-10 21:54:08 +00:00
|
|
|
}());
|