4 <input :value="value" :autosuggest-type="type" ref="input"
5 :placeholder="placeholder"
8 @input="inputUpdate($event.target.value)"
9 @focus="inputUpdate($event.target.value)"
11 @keydown="inputKeydown"
12 :aria-label="placeholder"
15 <ul class="suggestion-box" v-if="showSuggestions">
16 <li v-for="(suggestion, i) in suggestions"
17 @click="selectSuggestion(suggestion)"
18 :class="{active: (i === active)}">{{suggestion}}</li>
26 showSuggestions: false,
33 const props = ['url', 'type', 'value', 'placeholder', 'name'];
35 function getNameInputVal(valInput) {
36 let parentRow = valInput.parentNode.parentNode;
37 let nameInput = parentRow.querySelector('[autosuggest-type="name"]');
38 return (nameInput === null) ? '' : nameInput.value;
43 inputUpdate(inputValue) {
44 this.$emit('input', inputValue);
47 if (this.type === 'value') {
48 let nameVal = getNameInputVal(this.$el);
49 if (nameVal !== "") params.name = nameVal;
52 this.getSuggestions(inputValue.slice(0, 3), params).then(suggestions => {
53 if (inputValue.length === 0) {
54 this.displaySuggestions(suggestions.slice(0, 6));
57 // Filter to suggestions containing searched term
58 suggestions = suggestions.filter(item => {
59 return item.toLowerCase().indexOf(inputValue.toLowerCase()) !== -1;
61 this.displaySuggestions(suggestions);
68 this.showSuggestions = false;
73 if (event.key === 'Enter') event.preventDefault();
74 if (!this.showSuggestions) return;
77 if (event.key === 'ArrowDown') {
78 this.active = (this.active === this.suggestions.length - 1) ? 0 : this.active+1;
81 else if (event.key === 'ArrowUp') {
82 this.active = (this.active === 0) ? this.suggestions.length - 1 : this.active-1;
85 else if ((event.key === 'Enter') && !event.shiftKey) {
86 this.selectSuggestion(this.suggestions[this.active]);
89 else if (event.key === 'Escape') {
90 this.showSuggestions = false;
94 displaySuggestions(suggestions) {
95 if (suggestions.length === 0) {
96 this.suggestions = [];
97 this.showSuggestions = false;
101 this.suggestions = suggestions;
102 this.showSuggestions = true;
106 selectSuggestion(suggestion) {
107 this.$refs.input.value = suggestion;
108 this.$refs.input.focus();
109 this.$emit('input', suggestion);
110 this.showSuggestions = false;
114 * Get suggestions from BookStack. Store and use local cache if already searched.
115 * @param {String} input
116 * @param {Object} params
118 getSuggestions(input, params) {
119 params.search = input;
120 const cacheKey = `${this.url}:${JSON.stringify(params)}`;
122 if (typeof ajaxCache[cacheKey] !== "undefined") {
123 return Promise.resolve(ajaxCache[cacheKey]);
126 return this.$http.get(this.url, params).then(resp => {
127 ajaxCache[cacheKey] = resp.data;
134 export default {template, data, props, methods};