2
0
mirror of https://github.com/frappe/books.git synced 2024-09-21 03:39:02 +00:00
books/client/view/controls/link.js

62 lines
1.7 KiB
JavaScript
Raw Normal View History

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, {
autoFirst: true,
minChars: 0,
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) => {
let list = await this.getList(this.input.value);
// action to add new item
list.push({
label:frappe._('+ New {0}', this.target),
value: '__newItem',
action: () => {
}
});
this.awesomplete.list = list;
});
// new item action
this.input.addEventListener('awesomplete-select', async (e) => {
if (e.text && e.text.value === '__newItem') {
e.preventDefault();
const newDoc = await frappe.getNewDoc(this.target);
const formModal = frappe.desk.showFormModal(this.target, newDoc.name);
2018-02-13 11:54:57 +00:00
formModal.form.once('submit', async () => {
await this.updateDocValue(formModal.form.doc.name);
})
}
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) {
return (await frappe.db.getAll({
2018-01-31 12:56:21 +00:00
doctype: this.target,
2018-02-08 09:38:47 +00:00
filters: this.getFilters(query),
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-01-23 12:26:40 +00:00
};
module.exports = LinkControl;