2
0
mirror of https://github.com/frappe/books.git synced 2024-12-25 12:10:06 +00:00
books/client/view/controls/select.js

52 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-01-12 12:25:07 +00:00
const BaseControl = require('./base');
2018-02-08 11:45:32 +00:00
const frappe = require('frappejs');
2018-01-12 12:25:07 +00:00
class SelectControl extends BaseControl {
2018-02-08 11:45:32 +00:00
makeInput() {
2018-03-08 13:31:22 +00:00
this.input = frappe.ui.add('select', 'form-control', this.getInputParent());
this.addOptions();
}
refresh() {
this.addOptions();
super.refresh();
}
2018-01-12 12:25:07 +00:00
2018-03-08 13:31:22 +00:00
addOptions() {
const options = this.getOptions();
if (this.areOptionsSame(options)) return;
frappe.ui.empty(this.input);
for (let value of options) {
let option = frappe.ui.add('option', null, this.input, value.label || value);
option.setAttribute('value', value.value || value);
}
this.lastOptions = options;
}
getOptions() {
2018-01-12 12:25:07 +00:00
let options = this.options;
if (typeof options==='string') {
options = options.split('\n');
}
2018-03-08 13:31:22 +00:00
return options;
}
2018-01-12 12:25:07 +00:00
2018-03-08 13:31:22 +00:00
areOptionsSame(options) {
let same = false;
if (this.lastOptions && options.length===this.lastOptions.length) {
same = options.every((v ,i) => {
const v1 = this.lastOptions[i];
return (v.value || v) === (v1.value || v1)
});
2018-01-12 12:25:07 +00:00
}
2018-03-08 13:31:22 +00:00
return same;
2018-01-12 12:25:07 +00:00
}
2018-03-08 13:31:22 +00:00
2018-01-12 12:25:07 +00:00
make() {
super.make();
this.input.setAttribute('row', '3');
}
};
module.exports = SelectControl;