2016-08-01 20:07:49 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const ICON_CLASS_OPENED = 'fa-chevron-down';
|
|
|
|
const ICON_CLASS_CLOSED = 'fa-chevron-up';
|
|
|
|
|
2016-08-22 00:53:53 +02:00
|
|
|
const views = require('../util/views.js');
|
|
|
|
|
|
|
|
const template = views.getTemplate('expander');
|
|
|
|
|
2016-08-01 20:07:49 +02:00
|
|
|
class ExpanderControl {
|
2016-08-23 23:09:07 +02:00
|
|
|
constructor(name, title, nodes) {
|
|
|
|
this._name = name;
|
2016-08-01 20:07:49 +02:00
|
|
|
|
|
|
|
nodes = Array.from(nodes).filter(n => n);
|
|
|
|
if (!nodes.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-22 00:53:53 +02:00
|
|
|
const expanderNode = template({title: title});
|
|
|
|
const toggleLinkNode = expanderNode.querySelector('a');
|
|
|
|
const toggleIconNode = expanderNode.querySelector('i');
|
|
|
|
const expanderContentNode = expanderNode.querySelector('div');
|
2016-08-01 20:07:49 +02:00
|
|
|
toggleLinkNode.addEventListener('click', e => this._evtToggleClick(e));
|
|
|
|
|
|
|
|
nodes[0].parentNode.insertBefore(expanderNode, nodes[0]);
|
|
|
|
|
|
|
|
for (let node of nodes) {
|
|
|
|
expanderContentNode.appendChild(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._expanderNode = expanderNode;
|
|
|
|
this._toggleIconNode = toggleIconNode;
|
|
|
|
|
|
|
|
expanderNode.classList.toggle(
|
|
|
|
'collapsed',
|
2016-08-23 23:09:07 +02:00
|
|
|
this._allStates[this._name] === undefined ?
|
2016-08-01 20:07:49 +02:00
|
|
|
false :
|
2016-08-23 23:09:07 +02:00
|
|
|
!this._allStates[this._name]);
|
2016-08-01 20:07:49 +02:00
|
|
|
this._syncIcon();
|
|
|
|
}
|
|
|
|
|
2020-06-04 20:09:35 +02:00
|
|
|
// eslint-disable-next-line accessor-pairs
|
2016-08-23 23:09:07 +02:00
|
|
|
set title(newTitle) {
|
2016-08-27 22:15:30 +02:00
|
|
|
if (this._expanderNode) {
|
|
|
|
this._expanderNode
|
|
|
|
.querySelector('header span')
|
|
|
|
.textContent = newTitle;
|
|
|
|
}
|
2016-08-23 23:09:07 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 20:07:49 +02:00
|
|
|
get _isOpened() {
|
|
|
|
return !this._expanderNode.classList.contains('collapsed');
|
|
|
|
}
|
|
|
|
|
|
|
|
get _allStates() {
|
|
|
|
try {
|
|
|
|
return JSON.parse(localStorage.getItem('expander')) || {};
|
|
|
|
} catch (e) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_save() {
|
|
|
|
const newStates = Object.assign({}, this._allStates);
|
2016-08-23 23:09:07 +02:00
|
|
|
newStates[this._name] = this._isOpened;
|
2016-08-01 20:07:49 +02:00
|
|
|
localStorage.setItem('expander', JSON.stringify(newStates));
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtToggleClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this._expanderNode.classList.toggle('collapsed');
|
|
|
|
this._save();
|
|
|
|
this._syncIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
_syncIcon() {
|
|
|
|
if (this._isOpened) {
|
|
|
|
this._toggleIconNode.classList.add(ICON_CLASS_OPENED);
|
|
|
|
this._toggleIconNode.classList.remove(ICON_CLASS_CLOSED);
|
|
|
|
} else {
|
|
|
|
this._toggleIconNode.classList.add(ICON_CLASS_CLOSED);
|
|
|
|
this._toggleIconNode.classList.remove(ICON_CLASS_OPENED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ExpanderControl;
|