2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/client/desk/menu.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

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);
}
}
}