import { MessageBus } from '@mbus' export class Application { constructor(records) {// {{{ window._mbus = new MessageBus() this.settings = new Settings() this.records = this.parseRecords(records) this.topFolder = new Folder(this, 'root') this.recordsTree = null this.settingsIcon = null this.renderFolders() this.render() }// }}} parseRecords(recordsData) {// {{{ const records = recordsData.map(d => new Record(d)) return records }// }}} // cleanFolders removes all records from all folders. // renderFolders can then put moved records in the correct // (or newly created) folders again when record names are updated. cleanFolders(folder) {// {{{ if (folder === undefined) folder = this.topFolder folder.records = [] folder.subfolders.forEach((folder, _label) => { this.cleanFolders(folder) }) }// }}} renderFolders() {// {{{ this.records.sort(this.sortRecords) // rec: for example www.google.com for (const rec of this.records) { // com.google (reverse and remove wwww) const labels = rec.labels().reverse().slice(0, -1) // Start each record from the top and iterate through all its labels // except the first one since that would be the actual record. let currFolder = this.topFolder let accFolderLabels = [] for (const i in labels) { const label = labels[i] // The accumulated name is used to create each folder as the record progresses. accFolderLabels.push(label) const accFolderName = accFolderLabels.map(v => v).reverse().join('.') // A new folder is created only when it doesn't exist // to be able to update them when necessary. let folder = currFolder.subfolders.get(label) if (folder === undefined) { folder = new Folder(this, accFolderName) currFolder.subfolders.set(label, folder) } currFolder = folder } // Add the record to the innermost folder currFolder.addRecord(rec) } }// }}} sortFolders(a, b) {// {{{ const aLabels = a.labels().reverse() const bLabels = b.labels().reverse() for (let i = 0; i < aLabels.length && i < bLabels.length; i++) { if (aLabels[i] < bLabels[i]) return -1 if (aLabels[i] > bLabels[i]) return 1 } if (a.length < b.length) return 1 if (a.length > b.length) return -1 return 0 }// }}} sortRecords(a, b) {// {{{ const aLabels = a.labels().reverse() const bLabels = b.labels().reverse() for (let i = 0; i < aLabels.length && i < bLabels.length; i++) { if (aLabels[i] < bLabels[i]) return -1 if (aLabels[i] > bLabels[i]) return 1 } return 0 }// }}} render() {// {{{ if (this.recordsTree == null) { this.recordsTree = document.createElement('div') this.recordsTree.id = 'records-tree' this.createIcon = document.createElement('img') this.createIcon.id = 'create-icon' this.createIcon.src = `/images/${_VERSION}/icon_create.svg` this.createIcon.addEventListener('click', () => new RecordDialog(new Record()).show()) this.settingsIcon = document.createElement('img') this.settingsIcon.id = 'settings-icon' this.settingsIcon.src = `/images/${_VERSION}/icon_settings.svg` this.settingsIcon.addEventListener('click', () => new SettingsDialog(this).show()) document.body.appendChild(this.recordsTree) document.body.appendChild(this.settingsIcon) document.body.appendChild(this.createIcon) } // Top root folder doesn't have to be shown. const folders = Array.from(this.topFolder.subfolders.values()) folders.sort(this.sortFolders) for (const folder of folders) this.recordsTree.append(folder.toElement()) // Subscribe to settings update since the elements they will change // exists now. _mbus.subscribe('settings_updated', event => this.handlerSettingsUpdated(event.detail)) this.setBoxedFolders(this.settings.get('boxed_folders')) }// }}} handlerTop(event) {// {{{ const topEl = event.target.closest('.top') let records, types, values if (topEl.classList.contains('open')) { records = document.querySelectorAll(`[data-top$="${topEl.dataset.self}"]`) types = document.querySelectorAll(`[data-top$="${topEl.dataset.self}"] + .type`) values = document.querySelectorAll(`[data-top$="${topEl.dataset.self}"] + .type + .value`) for (const r of records) { r.classList.remove('show') r.classList.remove('open') } for (const r of types) r.classList.remove('show') for (const r of values) r.classList.remove('show') topEl.classList.remove('open') } else { if (event.shiftKey) { records = document.querySelectorAll(`[data-top$="${topEl.dataset.self}"]`) types = document.querySelectorAll(`[data-top$="${topEl.dataset.self}"] + .type`) values = document.querySelectorAll(`[data-top$="${topEl.dataset.self}"] + .type + .value`) } else { records = document.querySelectorAll(`[data-top="${topEl.dataset.self}"]`) types = document.querySelectorAll(`[data-top="${topEl.dataset.self}"] + .type`) values = document.querySelectorAll(`[data-top="${topEl.dataset.self}"] + .type + .value`) console.log(records) } for (const r of records) { r.classList.add('show') if (event.shiftKey && r.classList.contains('top')) r.classList.add('open') } for (const r of types) r.classList.add('show') for (const r of values) r.classList.add('show') topEl.classList.add('open') } }// }}} handlerSettingsUpdated({ key, value }) {// {{{ if (key == 'boxed_folders') { this.setBoxedFolders(value) } }// }}} setBoxedFolders(state) {// {{{ if (state) document.body.classList.add('boxed-folders') else document.body.classList.remove('boxed-folders') }// }}} } class Folder { constructor(app, name) {// {{{ this.application = app this.open = false this.folderName = name this.subfolders = new Map() this.records = [] this.div = null this.divSubfolders = null this.divRecords = null }// }}} name() {// {{{ return this.folderName.toLowerCase() }// }}} labels() {// {{{ return this.name().split('.') }// }}} addRecord(rec) {// {{{ this.records.push(rec) }// }}} openFolder(recursive) {// {{{ this.open = true this.div.classList.add('open') this.div.classList.remove('closed') if (recursive) this.subfolders.forEach(folder => folder.openFolder(recursive)) }// }}} closeFolder(recursive) {// {{{ this.open = false this.div.classList.remove('open') this.div.classList.add('closed') if (recursive) this.subfolders.forEach(folder => folder.closeFolder(recursive)) }// }}} toggleFolder(event) {// {{{ event.stopPropagation() if (this.open) this.closeFolder(event.shiftKey) else this.openFolder(event.shiftKey) }// }}} toElement() {// {{{ if (this.div === null) { this.div = document.createElement('div') this.div.classList.add('folder') this.div.classList.add(this.open ? 'open' : 'closed') if (this.labels().length == 1) this.div.classList.add('top-most') this.div.dataset.top = this.labels().slice(1).join('.') this.div.dataset.self = this.name() const firstLabel = this.labels()[0] const restLabels = this.labels().slice(1).join('.') this.div.innerHTML = `
${firstLabel}${restLabels != '' ? '.' + restLabels : ''}
` this.divSubfolders = this.div.querySelector('.subfolders') this.divRecords = this.div.querySelector('.records') this.div.querySelector('.label').addEventListener('click', event => this.toggleFolder(event)) } // Subfolders are refreshed. this.divSubfolders.replaceChildren() const subfolders = Array.from(this.subfolders.values()) subfolders.sort(this.application.sortFolders) for (const folder of subfolders) this.divSubfolders.append(folder.toElement()) // Records are refreshed. this.divRecords.replaceChildren() for (const rec of Array.from(this.records)) this.divRecords.append(...rec.render()) return this.div }// }}} } class Record { constructor(data) {// {{{ this.data = data || {} this.imgIcon = null this.divFQDN = null this.divType = null this.divValue = null this.divSeparator = null }// }}} id() {// {{{ return this.data['.id'] || '' }// }}} disabled() {// {{{ return this.data.Disabled === 'true' }// }}} dynamic() {// {{{ return this.data.Dynamic === 'true' }// }}} name() {// {{{ return this.data.Name?.toLowerCase() || '' }// }}} ttl() {// {{{ return this.data.TTL || '30m' }// }}} type() {// {{{ return this.data.Type?.toUpperCase() || 'A' }// }}} value() {// {{{ return this.data.ParsedValue || '' }// }}} matchSubdomain() {// {{{ return this.data.MatchSubdomain === 'true' }// }}} labels() {// {{{ return this.name().split('.') }// }}} copy(el, text) {// {{{ el.classList.add('copy') navigator.clipboard.writeText(text) setTimeout(() => el.classList.remove('copy'), 200) }// }}} edit() {// {{{ new RecordDialog(this).show() }// }}} set(key, value) {// {{{ if (key == 'Name') { if (value.slice(0, 2) == '*.') { this.data['Name'] = value.slice(2) this.data['MatchSubdomain'] = 'true' } else { this.data['Name'] = value this.data['MatchSubdomain'] = 'false' } return } this.data[key] = value }// }}} render() {// {{{ if (this.divFQDN === null) { this.imgIcon = document.createElement('img') this.divFQDN = document.createElement('div') this.divType = document.createElement('div') this.divValue = document.createElement('div') this.divSeparator = document.createElement('div') this.imgIcon.src = `/images/${_VERSION}/icon_record.svg` this.divFQDN.classList.add('fqdn') this.divType.classList.add('type') this.divValue.classList.add('value') this.divSeparator.classList.add('separator') this.divFQDN.innerHTML = ` *. ` this.divFQDN.addEventListener('click', event => { if (event.shiftKey) this.copy(event.target.closest('.fqdn'), this.name()) else this.edit() }) this.divValue.addEventListener('click', event => { if (event.shiftKey) this.copy(event.target.closest('.value'), this.value()) else this.edit() }) } // FQDN is updated. if (this.matchSubdomain()) this.divFQDN.classList.add('match-subdomains') else this.divFQDN.classList.remove('match-subdomains') const fl = this.labels()[0] const rl = this.labels().slice(1).join('.') this.divFQDN.querySelector('.first-label').innerText = fl this.divFQDN.querySelector('.rest-label').innerText = rl != '' ? `.${rl}` : '' this.divType.innerText = this.type() this.divValue.innerText = this.value() return [this.imgIcon, this.divFQDN, this.divType, this.divValue, this.divSeparator] }// }}} save() {// {{{ const created = (this.id() == '') fetch('/record/save', { method: 'POST', body: JSON.stringify(this.data), }) .then(data => data.json()) .then(json => { if (!json.OK) { alert(json.Error) return } // The data is read from the server/routeros device // since it could have manipulated the data. this.data = json.Record if (created) _app.records.push(this) _app.cleanFolders() _app.renderFolders() _app.render() }) }// }}} } class RecordDialog { constructor(record) {// {{{ this.record = record }// }}} show() {// {{{ this.dlg = document.createElement('dialog') this.dlg.id = "record-dialog" this.dlg.innerHTML = `
Name
Type
Value
TTL
` if (this.record.matchSubdomain()) this.dlg.querySelector('.name').value = '*.' + this.record.name() else this.dlg.querySelector('.name').value = this.record.name() this.dlg.querySelector('.type').value = this.record.type() this.dlg.querySelector('.value').value = this.record.value() this.dlg.querySelector('.ttl').value = this.record.ttl(); ['.name', '.type', '.value', '.ttl'].forEach(v => this.dlg.querySelector(v).addEventListener('keydown', event => this.enterKeyHandler(event)) ) this.dlg.querySelector('.save').addEventListener('click', () => this.save()) this.dlg.querySelector('.close').addEventListener('click', () => this.dlg.close()) this.dlg.addEventListener('close', () => this.dlg.remove()) document.body.appendChild(this.dlg) this.dlg.showModal() }// }}} enterKeyHandler(event) {// {{{ if (event.key == "Enter") this.save() }// }}} save() {// {{{ this.record.set('Name', this.dlg.querySelector('.name').value) this.record.set('Type', this.dlg.querySelector('.type').value) this.record.set('ParsedValue', this.dlg.querySelector('.value').value) this.record.set('TTL', this.dlg.querySelector('.ttl').value) this.record.render() this.record.save() this.dlg.close() }// }}} } class Settings { constructor() {// {{{ this.settings = new Map([ ['boxed_folders', true], ]) // Read any configured settings from local storage, but keeping default value // if not set. this.settings.forEach((_v, key) => { const configuredValue = localStorage.getItem(key) if (configuredValue !== null) this.settings.set(key, JSON.parse(configuredValue)) }) }// }}} set(key, value) {// {{{ this.settings.set(key, value) localStorage.setItem(key, JSON.stringify(value)) _mbus.dispatch('settings_updated', { key, value }) }// }}} get(key) {// {{{ return this.settings.get(key) }// }}} } class SettingsDialog { constructor(app) {// {{{ this.application = app this.dlg = null this.elBoxedFolders = null }// }}} show() {// {{{ this.dlg = document.createElement('dialog') this.dlg.id = 'settings-dialog' this.dlg.innerHTML = `
` const boxedFolders = this.application.settings.get('boxed_folders') this.elBoxedFolders = this.dlg.querySelector('#boxed-folders') this.elBoxedFolders.checked = boxedFolders // Event listeners are connected. this.dlg.querySelector('.save').addEventListener('click', () => this.save()) this.dlg.addEventListener('close', () => this.dlg.remove()) // Can't show a dialog that doesn't exist in DOM. document.body.appendChild(this.dlg) this.dlg.showModal() }// }}} save() {// {{{ this.application.settings.set('boxed_folders', this.elBoxedFolders.checked) this.dlg.close() }// }}} }