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"
9 :aria-label="placeholder"
11 <ul class="suggestion-box" v-if="showSuggestions">
12 <li v-for="(suggestion, i) in suggestions"
13 @click="selectSuggestion(suggestion)"
14 :class="{active: (i === active)}">{{suggestion}}</li>
23 showSuggestions: false,
30 const props = ['url', 'type', 'value', 'placeholder', 'name'];
32 function getNameInputVal(valInput) {
33 let parentRow = valInput.parentNode.parentNode;
34 let nameInput = parentRow.querySelector('[autosuggest-type="name"]');
35 return (nameInput === null) ? '' : nameInput.value;
40 inputUpdate(inputValue) {
41 this.$emit('input', inputValue);
44 if (this.type === 'value') {
45 let nameVal = getNameInputVal(this.$el);
46 if (nameVal !== "") params.name = nameVal;
49 this.getSuggestions(inputValue.slice(0, 3), params).then(suggestions => {
50 if (inputValue.length === 0) {
51 this.displaySuggestions(suggestions.slice(0, 6));
54 // Filter to suggestions containing searched term
55 suggestions = suggestions.filter(item => {
56 return item.toLowerCase().indexOf(inputValue.toLowerCase()) !== -1;
58 this.displaySuggestions(suggestions);
65 this.showSuggestions = false;
70 if (event.key === 'Enter') event.preventDefault();
71 if (!this.showSuggestions) return;
74 if (event.key === 'ArrowDown') {
75 this.active = (this.active === this.suggestions.length - 1) ? 0 : this.active+1;
78 else if (event.key === 'ArrowUp') {
79 this.active = (this.active === 0) ? this.suggestions.length - 1 : this.active-1;
82 else if ((event.key === 'Enter') && !event.shiftKey) {
83 this.selectSuggestion(this.suggestions[this.active]);
86 else if (event.key === 'Escape') {
87 this.showSuggestions = false;
91 displaySuggestions(suggestions) {
92 if (suggestions.length === 0) {
93 this.suggestions = [];
94 this.showSuggestions = false;
98 this.suggestions = suggestions;
99 this.showSuggestions = true;
103 selectSuggestion(suggestion) {
104 this.$refs.input.value = suggestion;
105 this.$refs.input.focus();
106 this.$emit('input', suggestion);
107 this.showSuggestions = false;
111 * Get suggestions from BookStack. Store and use local cache if already searched.
112 * @param {String} input
113 * @param {Object} params
115 getSuggestions(input, params) {
116 params.search = input;
117 const cacheKey = `${this.url}:${JSON.stringify(params)}`;
119 if (typeof ajaxCache[cacheKey] !== "undefined") {
120 return Promise.resolve(ajaxCache[cacheKey]);
123 return this.$http.get(this.url, params).then(resp => {
124 ajaxCache[cacheKey] = resp.data;
131 export default {template, data, props, methods};