4 <input :value="value" :autosuggest-type="type" ref="input"
5 :placeholder="placeholder" :name="name"
6 @input="inputUpdate($event.target.value)" @focus="inputUpdate($event.target.value)"
8 @keydown="inputKeydown"
10 <ul class="suggestion-box" v-if="showSuggestions">
11 <li v-for="(suggestion, i) in suggestions"
12 @click="selectSuggestion(suggestion)"
13 :class="{active: (i === active)}">{{suggestion}}</li>
22 showSuggestions: false,
29 const props = ['url', 'type', 'value', 'placeholder', 'name'];
31 function getNameInputVal(valInput) {
32 let parentRow = valInput.parentNode.parentNode;
33 let nameInput = parentRow.querySelector('[autosuggest-type="name"]');
34 return (nameInput === null) ? '' : nameInput.value;
39 inputUpdate(inputValue) {
40 this.$emit('input', inputValue);
43 if (this.type === 'value') {
44 let nameVal = getNameInputVal(this.$el);
45 if (nameVal !== "") params.name = nameVal;
48 this.getSuggestions(inputValue.slice(0, 3), params).then(suggestions => {
49 if (inputValue.length === 0) {
50 this.displaySuggestions(suggestions.slice(0, 6));
53 // Filter to suggestions containing searched term
54 suggestions = suggestions.filter(item => {
55 return item.toLowerCase().indexOf(inputValue.toLowerCase()) !== -1;
57 this.displaySuggestions(suggestions);
64 this.showSuggestions = false;
69 if (event.keyCode === 13) event.preventDefault();
70 if (!this.showSuggestions) return;
73 if (event.keyCode === 40) {
74 this.active = (this.active === this.suggestions.length - 1) ? 0 : this.active+1;
77 else if (event.keyCode === 38) {
78 this.active = (this.active === 0) ? this.suggestions.length - 1 : this.active-1;
81 else if ((event.keyCode === 13 || event.keyCode === 9) && !event.shiftKey) {
82 this.selectSuggestion(this.suggestions[this.active]);
85 else if (event.keyCode === 27) {
86 this.showSuggestions = false;
90 displaySuggestions(suggestions) {
91 if (suggestions.length === 0) {
92 this.suggestions = [];
93 this.showSuggestions = false;
97 this.suggestions = suggestions;
98 this.showSuggestions = true;
102 selectSuggestion(suggestion) {
103 this.$refs.input.value = suggestion;
104 this.$refs.input.focus();
105 this.$emit('input', suggestion);
106 this.showSuggestions = false;
110 * Get suggestions from BookStack. Store and use local cache if already searched.
111 * @param {String} input
112 * @param {Object} params
114 getSuggestions(input, params) {
115 params.search = input;
116 let cacheKey = `${this.url}:${JSON.stringify(params)}`;
118 if (typeof ajaxCache[cacheKey] !== "undefined") return Promise.resolve(ajaxCache[cacheKey]);
120 return this.$http.get(this.url, {params}).then(resp => {
121 ajaxCache[cacheKey] = resp.data;
128 module.exports = {template, data, props, methods};