generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
spotlight.js
132 lines (114 loc) · 4.93 KB
/
spotlight.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import Fuse from 'fuse.js'
window.LivewireUISpotlight = (config) => {
return {
inputPlaceholder: config.placeholder,
searchEngine: 'commands',
commands: config.commands,
commandSearch: null,
selectedCommand: null,
dependencySearch: null,
dependencyQueryResults: window.Livewire.find(config.componentId).entangle('dependencyQueryResults'),
requiredDependencies: [],
currentDependency: null,
resolvedDependencies: {},
showResultsWithoutInput: config.showResultsWithoutInput,
init() {
this.commandSearch = new Fuse(this.commands, {threshold: 0.3, keys: ['name', 'description', 'synonyms']});
this.dependencySearch = new Fuse([], {threshold: 0.3, keys: ['name', 'description', 'synonyms']});
this.$watch('dependencyQueryResults', value => { this.dependencySearch.setCollection(value) });
this.$watch('input', value => {
if (value.length === 0) {
this.selected = 0;
}
if(this.selectedCommand !== null && this.currentDependency !== null && this.currentDependency.type === 'search'){
this.$wire.searchDependency(this.selectedCommand.id, this.currentDependency.id, value, this.resolvedDependencies);
}
});
this.$watch('isOpen', value => {
if (value === false) {
setTimeout(() => {
this.input = '';
this.inputPlaceholder = config.placeholder;
this.searchEngine = 'commands';
this.resolvedDependencies = {};
this.selectedCommand = null;
this.currentDependency = null;
this.selectedCommand = null;
this.requiredDependencies = [];
}, 300);
}
});
},
isOpen: false,
toggleOpen() {
if (this.isOpen) {
this.isOpen = false;
return;
}
this.input = ''
this.isOpen = true
setTimeout(() => {
this.$refs.input.focus()
}, 100)
},
input: '',
filteredItems() {
if (this.searchEngine === 'commands') {
if (! this.input && this.showResultsWithoutInput) {
return this.commandSearch.getIndex().docs.map((item, i) => [{item: item}, i]);
}
return this.commandSearch.search(this.input).map((item, i) => [item, i])
}
if (this.searchEngine === 'search') {
if (! this.input && this.showResultsWithoutInput) {
return this.dependencySearch.getIndex().docs.map((item, i) => [{item: item}, i]);
}
return this.dependencySearch.search(this.input).map((item, i) => [item, i])
}
return [];
},
selectUp() {
this.selected = Math.max(0, this.selected - 1)
this.$nextTick(() => {
this.$refs.results.children[this.selected + 1].scrollIntoView({
block: 'nearest',
})
})
},
selectDown() {
this.selected = Math.min(this.filteredItems().length - 1, this.selected + 1)
this.$nextTick(() => {
this.$refs.results.children[this.selected + 1].scrollIntoView({
block: 'nearest',
})
})
},
go(id) {
if (this.selectedCommand === null) {
this.selectedCommand = this.commands.find((command) => {
return command.id === (id ? id : this.filteredItems()[this.selected][0].item.id);
});
this.requiredDependencies = JSON.parse(JSON.stringify(this.selectedCommand.dependencies));
}
if (this.currentDependency !== null) {
let dependencyValue;
if(this.currentDependency.type === 'search') {
dependencyValue = id ? id : this.filteredItems()[this.selected][0].item.id;
} else {
dependencyValue = this.input;
}
this.resolvedDependencies[this.currentDependency.id] = dependencyValue;
}
if (this.requiredDependencies.length > 0) {
this.input = '';
this.currentDependency = this.requiredDependencies.pop();
this.inputPlaceholder = this.currentDependency.placeholder;
this.searchEngine = (this.currentDependency.type === 'search') ? 'search' : false;
} else {
this.isOpen = false;
this.$wire.execute(this.selectedCommand.id, this.resolvedDependencies);
}
},
selected: 0,
};
};