export class Application { constructor(records) {// {{{ this.records = this.parseRecords(records) this.folders = this.createFolders() this.render() }// }}} parseRecords(recordsData) {// {{{ const records = recordsData.map(d => new Record(d)) return records }// }}} createFolders() {// {{{ this.records.sort(this.sortRecords) const topFolder = new Folder(this, 'root') for (const rec of this.records) { const labels = rec.labels().reverse().slice(0, -1) let currFolder = topFolder let accFolderLabels = [] for (const i in labels) { const label = labels[i] accFolderLabels.push(label) const accFolderName = accFolderLabels.map(v => v).reverse().join('.') 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) } return topFolder }// }}} 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() {// {{{ const tree = document.getElementById('records-tree') tree.replaceChildren() // Top root folder doesn't have to be shown. const folders = Array.from(this.folders.subfolders.values()) folders.sort(this.sortFolders) for (const folder of folders) tree.append(folder.toElement()) }// }}} 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') } }// }}} } 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 = `