2018-02-14 16:44:50 +00:00
|
|
|
const frappe = require('frappejs');
|
|
|
|
|
|
|
|
module.exports = class DeskMenu {
|
|
|
|
constructor(parent) {
|
|
|
|
this.parent = parent;
|
|
|
|
this.routeItems = {};
|
|
|
|
this.make();
|
|
|
|
}
|
|
|
|
|
|
|
|
make() {
|
2018-02-15 09:53:28 +00:00
|
|
|
this.listGroup = frappe.ui.add('div', 'list-body', this.parent);
|
2018-02-14 16:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addItem(label, action) {
|
2018-03-08 13:31:22 +00:00
|
|
|
let item = frappe.ui.add('div', 'list-row', this.listGroup, label);
|
2018-02-14 16:44:50 +00:00
|
|
|
if (typeof action === 'string') {
|
|
|
|
this.routeItems[action] = item;
|
|
|
|
}
|
2018-02-15 09:53:28 +00:00
|
|
|
item.addEventListener('click', async () => {
|
|
|
|
if (typeof action === 'string') {
|
|
|
|
await frappe.router.setRoute(action);
|
|
|
|
} else {
|
|
|
|
action();
|
|
|
|
}
|
|
|
|
this.setActive(item);
|
|
|
|
});
|
2018-02-14 16:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setActive() {
|
|
|
|
if (this.routeItems[window.location.hash]) {
|
|
|
|
let item = this.routeItems[window.location.hash];
|
2018-02-15 09:53:28 +00:00
|
|
|
let className = 'active';
|
2018-02-14 16:44:50 +00:00
|
|
|
let activeItem = this.listGroup.querySelector('.' + className);
|
|
|
|
|
|
|
|
if (activeItem) {
|
|
|
|
activeItem.classList.remove(className);
|
|
|
|
}
|
|
|
|
item.classList.add(className);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|