2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 19:29:02 +00:00
books/client/ui/dropdown.js

55 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-01-16 06:09:17 +00:00
const frappe = require('frappejs');
2018-02-14 12:50:56 +00:00
const bootstrap = require('bootstrap');
const $ = require('jquery');
2018-01-12 12:25:07 +00:00
class Dropdown {
2018-02-15 09:53:28 +00:00
constructor({parent, label, items = [], right, cssClass='btn-secondary'}) {
2018-01-12 12:25:07 +00:00
Object.assign(this, arguments[0]);
2018-02-14 12:50:56 +00:00
Dropdown.instances += 1;
this.id = 'dropdownMenuButton-' + Dropdown.instances;
2018-01-12 12:25:07 +00:00
this.make();
// init items
if (this.items) {
2018-02-14 12:50:56 +00:00
for (let item of this.items) {
this.addItem(item.label, item.action);
2018-01-12 12:25:07 +00:00
}
}
}
make() {
2018-02-14 12:50:56 +00:00
this.$dropdown = $(`<div class="dropdown ${this.right ? 'float-right' : ''}">
2018-02-15 09:53:28 +00:00
<button class="btn ${this.cssClass} dropdown-toggle"
2018-02-14 12:50:56 +00:00
type="button" id="${this.id}" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">${this.label}
</button>
<div class="dropdown-menu ${this.right ? 'dropdown-menu-right' : ''}" aria-labelledby="${this.id}"></div>
</div>`).appendTo(this.parent)
this.dropdown = this.$dropdown.get(0);
this.dropdownMenu = this.dropdown.querySelector('.dropdown-menu');
}
addItem(label, action) {
let item = frappe.ui.add('button', 'dropdown-item', this.dropdownMenu);
2018-01-12 12:25:07 +00:00
item.textContent = label;
2018-01-16 06:09:17 +00:00
item.setAttribute('type', 'button');
2018-01-12 12:25:07 +00:00
if (typeof action === 'string') {
2018-01-16 06:09:17 +00:00
item.addEventListener('click', async () => {
2018-02-08 09:38:47 +00:00
await frappe.router.setRoute(action);
2018-01-12 12:25:07 +00:00
});
} else {
item.addEventListener('click', async () => {
await action();
});
}
}
2018-02-14 12:50:56 +00:00
floatRight() {
frappe.ui.addClass(this.dropdown, 'float-right');
2018-01-12 12:25:07 +00:00
}
}
2018-02-14 12:50:56 +00:00
Dropdown.instances = 0;
2018-01-12 12:25:07 +00:00
module.exports = Dropdown;