2018-06-27 14:38:27 +00:00
|
|
|
<script>
|
|
|
|
import frappe from 'frappejs';
|
|
|
|
import feather from 'feather-icons';
|
|
|
|
import Autocomplete from './Autocomplete';
|
2018-07-09 16:36:02 +00:00
|
|
|
import FeatherIcon from 'frappejs/ui/components/FeatherIcon';
|
2018-06-27 14:38:27 +00:00
|
|
|
import Form from '../Form/Form';
|
|
|
|
import { _ } from 'frappejs/utils';
|
|
|
|
|
|
|
|
export default {
|
2018-07-10 13:35:43 +00:00
|
|
|
extends: Autocomplete,
|
|
|
|
methods: {
|
|
|
|
async getList(query) {
|
2019-07-16 12:40:47 +00:00
|
|
|
let filters = this.docfield.getFilters
|
|
|
|
? this.docfield.getFilters(query, this.doc)
|
|
|
|
: null;
|
2018-10-22 20:50:50 +00:00
|
|
|
|
|
|
|
if (query) {
|
|
|
|
if (!filters) filters = {};
|
|
|
|
filters.keywords = ['like', query];
|
|
|
|
}
|
|
|
|
|
2019-02-27 07:18:30 +00:00
|
|
|
let target = this.getTarget();
|
|
|
|
let titleField = frappe.getMeta(target).titleField;
|
|
|
|
|
2018-07-10 13:35:43 +00:00
|
|
|
const list = await frappe.db.getAll({
|
2019-02-27 07:18:30 +00:00
|
|
|
doctype: target,
|
2018-10-22 20:50:50 +00:00
|
|
|
filters,
|
2019-02-27 07:18:30 +00:00
|
|
|
fields: ['name', titleField],
|
2018-07-10 13:35:43 +00:00
|
|
|
limit: 50
|
|
|
|
});
|
2018-06-27 14:38:27 +00:00
|
|
|
|
2018-07-10 13:35:43 +00:00
|
|
|
const plusIcon = feather.icons.plus.toSvg({
|
|
|
|
class: 'm-1',
|
|
|
|
width: 16,
|
|
|
|
height: 16
|
|
|
|
});
|
2018-06-27 14:38:27 +00:00
|
|
|
|
2018-07-10 13:35:43 +00:00
|
|
|
return list
|
|
|
|
.map(d => ({
|
2019-02-27 07:18:30 +00:00
|
|
|
label: d[titleField],
|
2018-07-10 13:35:43 +00:00
|
|
|
value: d.name
|
|
|
|
}))
|
|
|
|
.concat({
|
|
|
|
label: plusIcon + ' New ' + this.getTarget(),
|
|
|
|
value: '__newItem'
|
|
|
|
});
|
|
|
|
},
|
2018-09-26 14:20:35 +00:00
|
|
|
getChildrenElement(h) {
|
|
|
|
return [
|
2018-09-26 15:07:59 +00:00
|
|
|
this.onlyInput ? null : this.getLabelElement(h),
|
2018-09-26 14:20:35 +00:00
|
|
|
this.getInputGroupElement(h),
|
|
|
|
this.getDropdownElement(h)
|
|
|
|
];
|
|
|
|
},
|
|
|
|
getInputGroupElement(h) {
|
2018-07-10 13:35:43 +00:00
|
|
|
return h(
|
|
|
|
'div',
|
|
|
|
{
|
2018-09-26 14:20:35 +00:00
|
|
|
class: ['input-group']
|
2018-07-09 16:36:02 +00:00
|
|
|
},
|
2018-07-10 13:35:43 +00:00
|
|
|
[
|
2018-09-26 14:20:35 +00:00
|
|
|
this.getInputElement(h),
|
|
|
|
h(
|
|
|
|
'div',
|
|
|
|
{
|
|
|
|
class: ['input-group-append']
|
|
|
|
},
|
|
|
|
[this.getFollowLink(h)]
|
|
|
|
)
|
2018-07-10 13:35:43 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
},
|
|
|
|
getFollowLink(h) {
|
|
|
|
const doctype = this.getTarget();
|
|
|
|
const name = this.value;
|
2018-07-09 16:36:02 +00:00
|
|
|
|
2018-07-10 13:35:43 +00:00
|
|
|
if (!name) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-07-09 16:36:02 +00:00
|
|
|
|
2018-07-16 12:03:59 +00:00
|
|
|
const arrow = h(FeatherIcon, {
|
2018-07-10 13:35:43 +00:00
|
|
|
props: {
|
2018-07-16 12:03:59 +00:00
|
|
|
name: 'arrow-right'
|
2018-07-09 16:36:02 +00:00
|
|
|
},
|
2018-07-10 13:35:43 +00:00
|
|
|
class: ['text-muted'],
|
|
|
|
style: {
|
2018-07-16 12:03:59 +00:00
|
|
|
height: '16px'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-26 14:20:35 +00:00
|
|
|
return h(
|
|
|
|
'button',
|
|
|
|
{
|
|
|
|
class: ['btn btn-sm btn-outline-light border d-flex'],
|
|
|
|
attrs: {
|
|
|
|
type: 'button'
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
click: () => {
|
|
|
|
this.$router.push(`/edit/${doctype}/${name}`);
|
|
|
|
}
|
2018-07-10 13:35:43 +00:00
|
|
|
}
|
2018-09-26 14:20:35 +00:00
|
|
|
},
|
|
|
|
[arrow]
|
|
|
|
);
|
2018-07-10 13:35:43 +00:00
|
|
|
},
|
|
|
|
getTarget() {
|
|
|
|
return this.docfield.target;
|
|
|
|
},
|
|
|
|
sort() {
|
|
|
|
return (a, b) => {
|
2018-09-07 10:08:15 +00:00
|
|
|
a = a.toLowerCase();
|
|
|
|
b = b.toLowerCase();
|
2018-09-26 14:20:35 +00:00
|
|
|
|
2018-07-10 13:35:43 +00:00
|
|
|
if (a.value === '__newItem') {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (b.value === '___newItem') {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.value === b.value) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (a.value < b.value) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.value > b.value) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
filter() {
|
|
|
|
return (suggestion, txt) => {
|
|
|
|
if (suggestion.value === '__newItem') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2018-09-26 14:20:35 +00:00
|
|
|
onItemClick(item) {
|
|
|
|
if (item.value === '__newItem') {
|
|
|
|
this.openFormModal();
|
|
|
|
} else {
|
|
|
|
this.handleChange(item.value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async openFormModal() {
|
2018-07-10 13:35:43 +00:00
|
|
|
const input = this.$refs.input;
|
2018-09-26 14:20:35 +00:00
|
|
|
const newDoc = await frappe.getNewDoc(this.getTarget());
|
|
|
|
this.$formModal.open(newDoc, {
|
|
|
|
defaultValues: {
|
|
|
|
name: input.value !== '__newItem' ? input.value : null
|
|
|
|
},
|
|
|
|
onClose: () => {
|
|
|
|
// if new doc was not created
|
|
|
|
// then reset the input value
|
|
|
|
if (this.value === '__newItem') {
|
|
|
|
this.handleChange('');
|
|
|
|
}
|
2018-06-27 14:38:27 +00:00
|
|
|
}
|
2018-07-10 13:35:43 +00:00
|
|
|
});
|
2018-09-26 14:20:35 +00:00
|
|
|
|
|
|
|
newDoc.on('afterInsert', data => {
|
|
|
|
// if new doc was created
|
|
|
|
// then set the name of the doc in input
|
|
|
|
this.handleChange(newDoc.name);
|
|
|
|
this.$formModal.close();
|
|
|
|
});
|
2018-06-27 14:38:27 +00:00
|
|
|
}
|
2018-07-10 13:35:43 +00:00
|
|
|
}
|
|
|
|
};
|
2018-06-27 14:38:27 +00:00
|
|
|
</script>
|