2018-01-23 12:26:40 +00:00
|
|
|
const frappe = require('frappejs');
|
|
|
|
const BaseControl = require('./base');
|
|
|
|
const Awesomplete = require('awesomplete');
|
|
|
|
|
|
|
|
class LinkControl extends BaseControl {
|
|
|
|
make() {
|
|
|
|
super.make();
|
2018-01-23 12:31:09 +00:00
|
|
|
this.input.setAttribute('type', 'text');
|
2018-02-15 09:53:28 +00:00
|
|
|
this.setupAwesomplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupAwesomplete() {
|
2018-01-23 12:31:09 +00:00
|
|
|
this.awesomplete = new Awesomplete(this.input, {
|
|
|
|
minChars: 0,
|
2018-02-13 10:42:44 +00:00
|
|
|
maxItems: 99,
|
2018-02-13 11:54:57 +00:00
|
|
|
filter: () => true,
|
2018-01-23 12:31:09 +00:00
|
|
|
});
|
2018-01-23 12:26:40 +00:00
|
|
|
|
2018-01-23 12:31:09 +00:00
|
|
|
// rebuild the list on input
|
|
|
|
this.input.addEventListener('input', async (event) => {
|
2018-02-13 10:42:44 +00:00
|
|
|
let list = await this.getList(this.input.value);
|
|
|
|
|
|
|
|
// action to add new item
|
|
|
|
list.push({
|
2018-02-16 13:13:46 +00:00
|
|
|
label: frappe._('+ New {0}', this.label),
|
2018-02-13 10:42:44 +00:00
|
|
|
value: '__newItem',
|
|
|
|
});
|
|
|
|
|
|
|
|
this.awesomplete.list = list;
|
|
|
|
});
|
|
|
|
|
|
|
|
// new item action
|
|
|
|
this.input.addEventListener('awesomplete-select', async (e) => {
|
|
|
|
if (e.text && e.text.value === '__newItem') {
|
|
|
|
e.preventDefault();
|
2018-03-27 13:55:26 +00:00
|
|
|
const newDoc = await frappe.getNewDoc(this.getTarget());
|
|
|
|
const formModal = await frappe.desk.showFormModal(this.getTarget(), newDoc.name);
|
2018-02-19 06:31:07 +00:00
|
|
|
if (formModal.form.doc.meta.hasField('name')) {
|
|
|
|
formModal.form.doc.set('name', this.input.value);
|
|
|
|
}
|
|
|
|
|
2018-02-27 16:29:14 +00:00
|
|
|
formModal.once('save', async () => {
|
2018-02-13 11:54:57 +00:00
|
|
|
await this.updateDocValue(formModal.form.doc.name);
|
2018-02-19 06:31:07 +00:00
|
|
|
});
|
2018-02-13 10:42:44 +00:00
|
|
|
}
|
2018-01-23 12:31:09 +00:00
|
|
|
});
|
2018-02-15 09:53:28 +00:00
|
|
|
|
2018-01-23 12:26:40 +00:00
|
|
|
}
|
2018-01-23 12:48:27 +00:00
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
async getList(query) {
|
2018-02-08 06:46:38 +00:00
|
|
|
return (await frappe.db.getAll({
|
2018-03-27 13:55:26 +00:00
|
|
|
doctype: this.getTarget(),
|
2018-03-08 13:31:22 +00:00
|
|
|
filters: this.getFilters(query, this),
|
2018-01-23 12:48:27 +00:00
|
|
|
limit: 50
|
|
|
|
})).map(d => d.name);
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
getFilters(query) {
|
2018-01-23 12:48:27 +00:00
|
|
|
return { keywords: ["like", query] }
|
|
|
|
}
|
2018-03-27 13:55:26 +00:00
|
|
|
|
|
|
|
getTarget() {
|
|
|
|
return this.target;
|
|
|
|
}
|
2018-01-23 12:26:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = LinkControl;
|