2
0
mirror of https://github.com/frappe/books.git synced 2025-01-24 15:48:25 +00:00
books/client/desk/menu.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

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