2
0
mirror of https://github.com/frappe/books.git synced 2024-12-25 04:06:04 +00:00
books/client/desk/menu.js

42 lines
1.2 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-02-15 09:53:28 +00:00
let item = frappe.ui.add('div', 'list-row', this.listGroup);
2018-02-14 16:44:50 +00:00
item.textContent = label;
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);
}
}
}