2
0
mirror of https://github.com/frappe/books.git synced 2025-01-26 16:48:28 +00:00
books/client/ui/dropdown.js

54 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-01-16 11:39:17 +05:30
const frappe = require('frappejs');
2018-02-14 18:20:56 +05:30
const bootstrap = require('bootstrap');
const $ = require('jquery');
2018-01-12 17:55:07 +05:30
class Dropdown {
2018-02-15 15:23:28 +05:30
constructor({parent, label, items = [], right, cssClass='btn-secondary'}) {
2018-01-12 17:55:07 +05:30
Object.assign(this, arguments[0]);
2018-02-14 18:20:56 +05:30
Dropdown.instances += 1;
this.id = 'dropdownMenuButton-' + Dropdown.instances;
2018-01-12 17:55:07 +05:30
this.make();
// init items
if (this.items) {
2018-02-14 18:20:56 +05:30
for (let item of this.items) {
this.addItem(item.label, item.action);
2018-01-12 17:55:07 +05:30
}
}
}
make() {
2018-02-14 18:20:56 +05:30
this.$dropdown = $(`<div class="dropdown ${this.right ? 'float-right' : ''}">
2018-02-15 15:23:28 +05:30
<button class="btn ${this.cssClass} dropdown-toggle"
2018-02-14 18:20:56 +05:30
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) {
2018-03-08 19:01:22 +05:30
let item = frappe.ui.add('button', 'dropdown-item', this.dropdownMenu, label);
2018-01-16 11:39:17 +05:30
item.setAttribute('type', 'button');
2018-01-12 17:55:07 +05:30
if (typeof action === 'string') {
2018-01-16 11:39:17 +05:30
item.addEventListener('click', async () => {
2018-02-08 15:08:47 +05:30
await frappe.router.setRoute(action);
2018-01-12 17:55:07 +05:30
});
} else {
item.addEventListener('click', async () => {
await action();
});
}
}
2018-02-14 18:20:56 +05:30
floatRight() {
frappe.ui.addClass(this.dropdown, 'float-right');
2018-01-12 17:55:07 +05:30
}
}
2018-02-14 18:20:56 +05:30
Dropdown.instances = 0;
2018-01-12 17:55:07 +05:30
module.exports = Dropdown;