2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 01:14:03 +00:00
books/ui/components/controls/Link.vue

166 lines
3.8 KiB
Vue
Raw Normal View History

2018-06-27 14:38:27 +00:00
<script>
import frappe from 'frappejs';
import feather from 'feather-icons';
import Awesomplete from 'awesomplete';
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 {
extends: Autocomplete,
watch: {
value(newValue) {
this.$refs.input.value = newValue;
}
},
methods: {
async getList(query) {
const list = await frappe.db.getAll({
doctype: this.getTarget(),
filters: {
keywords: ['like', query]
},
fields: ['name'],
limit: 50
});
2018-06-27 14:38:27 +00:00
const plusIcon = feather.icons.plus.toSvg({
class: 'm-1',
width: 16,
height: 16
});
2018-06-27 14:38:27 +00:00
return list
.map(d => ({
label: d.name,
value: d.name
}))
.concat({
label: plusIcon + ' New ' + this.getTarget(),
value: '__newItem'
});
},
getWrapperElement(h) {
return h(
'div',
{
class: ['form-group', ...this.wrapperClass],
attrs: {
'data-fieldname': this.docfield.fieldname
}
2018-07-09 16:36:02 +00:00
},
[
this.getLabelElement(h),
2018-07-16 12:03:59 +00:00
this.getInputGroupElement(h)
]
);
},
2018-07-16 12:03:59 +00:00
getInputGroupElement(h) {
return h('div', {
class: ['input-group']
}, [
this.getInputElement(h),
h('div', {
class: ['input-group-append']
}, [this.getFollowLink(h)])
]);
},
getFollowLink(h) {
const doctype = this.getTarget();
const name = this.value;
2018-07-09 16:36:02 +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, {
props: {
2018-07-16 12:03:59 +00:00
name: 'arrow-right'
2018-07-09 16:36:02 +00:00
},
class: ['text-muted'],
style: {
2018-07-16 12:03:59 +00:00
height: '16px'
}
});
return h('button', {
class: ['btn btn-sm btn-outline-light border d-flex'],
attrs: {
type: 'button'
2018-06-27 14:38:27 +00:00
},
2018-07-16 12:03:59 +00:00
on: {
click: () => {
this.$router.push(`/edit/${doctype}/${name}`);
}
}
2018-07-16 12:03:59 +00:00
}, [arrow])
},
getTarget() {
return this.docfield.target;
},
sort() {
return (a, b) => {
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;
}
return Awesomplete.FILTER_CONTAINS(suggestion, txt);
};
},
bindEvents() {
const input = this.$refs.input;
2018-06-27 14:38:27 +00:00
input.addEventListener('awesomplete-select', async e => {
// new item action
if (e.text && e.text.value === '__newItem') {
e.preventDefault();
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
}
}
}
);
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-06-27 14:38:27 +00:00
}
}
};
2018-06-27 14:38:27 +00:00
</script>