-
Notifications
You must be signed in to change notification settings - Fork 204
/
search.vue
610 lines (525 loc) · 21.2 KB
/
search.vue
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<template>
<div
ref="search-container"
class="search-container w-full space-y-4"
>
<slot name="before-search">
<div class="list-summary-wrapper flex justify-center text-center text-sm">
<ListSummary
:app-list="appList"
class="max-w-4xl"
/>
</div>
</slot>
<div class="search-input relative space-y-4">
<div>
<input
id="search"
ref="search"
:autofocus="autofocus"
v-model="query"
aria-label="Type here to Search"
class="appearance-none w-full text-white font-hairline sm:text-5xl outline-none bg-transparent p-3"
type="search"
placeholder="Type to Search"
autocomplete="off"
@keyup="queryResults(query); scrollInputToTop()"
>
<div class="search-input-separator border-white border-t-2" />
</div>
<div class="quick-buttons overflow-x-auto whitespace-no-wrap space-x-2">
<button
v-for="button in quickButtons"
:key="button.query"
:class="[
'inline-block text-xs rounded-lg py-1 px-2',
'border-2 border-white focus:outline-none',
query.includes(button.query) ? 'border-opacity-50 bg-darkest' : 'border-opacity-0 neumorphic-shadow-inner'
]"
:aria-label="`Filter list by ${button.label}`"
@click="toggleFilter(button.query); queryResults(query)"
>{{ button.label }}</button>
</div>
</div>
<Carbon class="carbon-inline-wide" />
<div
ref="search-container"
class="search-container relative divide-y divide-gray-700 w-full rounded-lg border border-gray-700 bg-gradient-to-br from-darker to-dark my-8 px-5"
>
<svg style="display: none;">
<defs>
<path
id="chevron-right"
fill-rule="evenodd"
d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z"
clip-rule="evenodd"
/>
</defs>
</svg>
<!-- hasStartedAnyQuery: {{ hasStartedAnyQuery }} -->
<div
v-if="chunkedResults.length === 0"
class="text-center py-4"
>
No apps found
</div>
<ul
v-for="(results, i) in chunkedResults"
:key="`results-chunk-${i}`"
class="results-container divide-y divide-gray-700"
>
<li
v-for="(app, i) in results"
:key="`${app.slug}-${i}`"
:ref="`${app.slug}-row`"
:data-app-slug="app.slug"
class="relative"
>
<!-- app.endpoint: {{ app.endpoint }} -->
<a
:href="getAppEndpoint(app)"
class="flex flex-col justify-center inset-x-0 hover:bg-darkest border-2 border-white border-opacity-0 hover:border-opacity-50 focus:outline-none focus:bg-gray-50 duration-300 ease-in-out rounded-lg space-y-2 -mx-5 pl-5 md:pl-20 pr-6 md:pr-64 py-5"
style="transition-property: border;"
>
<template v-if="seenItems[app.slug] === false && hasStartedAnyQuery === false">
{{ getAppCategory(app).icon ? `${getAppCategory(app).icon} ${app.name}` : app.name }}
<div class="text-sm leading-5 font-bold">
{{ app.text }}
</div>
<!-- app.endpoint: {{ app.endpoint }} -->
</template>
<template v-else>
<client-only>
<div class="absolute hidden left-0 h-12 w-12 rounded-full md:flex items-center justify-center bg-darker">
{{ app.name.charAt(0) }}
</div>
</client-only>
{{ getAppCategory(app).icon ? `${getAppCategory(app).icon} ${app.name}` : app.name }}
<div class="text-sm leading-5 font-bold">
{{ app.text }}
</div>
<!-- app.lastUpdated: {{ app.lastUpdated }} -->
<client-only v-if="app.lastUpdated">
<small
class="text-xs opacity-50"
>
<RelativeTime
:timestamp="app.lastUpdated.timestamp"
class="text-xs opacity-50"
/>
</small>
<small
slot="placeholder"
class="text-xs opacity-50"
>
⏳
</small>
</client-only>
<!-- app.endpoint: {{ app.endpoint }} -->
</template>
</a>
<div
class="search-item-options relative md:absolute md:inset-0 w-full pointer-events-none"
>
<div class="search-item-options-container h-full flex justify-center md:justify-end items-center pb-4 md:py-4 md:px-4">
<LinkButton
v-for="link in getSearchLinks(app)"
:key="`${app.slug}-${link.label.toLowerCase()}`"
:href="link.href"
:class="[
'px-3 py-2 rounded-md text-sm pointer-events-auto focus:outline-none focus:text-white focus:bg-gray-700 transition duration-150 ease-in-out',
'text-gray-300 hover:bg-darker hover:neumorphic-shadow'
]"
:class-groups="{
// general: 'relative inline-flex items-center rounded-md px-4 py-2',
// font: 'leading-5 font-bold',
// text: 'text-white',
// border: 'border border-transparent focus:outline-none focus:border-indigo-600',
shadow: 'hover:neumorphic-shadow',
bg: 'hover:bg-darker',
// transition: 'transition duration-150 ease-in-out'
}"
:label="link.label"
/>
<LinkButton
:href="getAppEndpoint(app)"
:class="[
'px-3 py-2 rounded-md text-sm pointer-events-auto focus:outline-none focus:text-white focus:bg-gray-700 transition duration-150 ease-in-out',
'text-gray-300 hover:bg-darker hover:neumorphic-shadow'
]"
:class-groups="{
// general: 'relative inline-flex items-center rounded-md px-4 py-2',
// font: 'leading-5 font-bold',
// text: 'text-white',
// border: 'border border-transparent focus:outline-none focus:border-indigo-600',
shadow: 'hover:neumorphic-shadow',
bg: 'hover:bg-darker',
// transition: 'transition duration-150 ease-in-out'
}"
label="Details ͐"
/>
</div>
</div>
</li>
</ul>
</div>
</div>
</template>
<script>
import scrollIntoView from 'scroll-into-view-if-needed'
import { getAppCategory } from '~/helpers/categories.js'
import { getAppEndpoint } from '~/helpers/app-derived.js'
// import appList from '~/static/app-list.json'
import Carbon from '~/components/carbon-inline.vue'
import LinkButton from '~/components/link-button.vue'
// import RelativeTime from '~/components/relative-time.vue'
import ListSummary from '~/components/list-summary.vue'
export default {
components: {
Carbon,
// EmailSubscribe: () => process.client ? import('~/components/email-subscribe.vue') : null,
ListSummary,
LinkButton,
RelativeTime: () => process.client ? import('~/components/relative-time.vue') : null
},
props: {
appList: {
type: Array,
required: true
},
noEmailSubscribe: {
type: Boolean,
default: false
},
initialLimit: {
type: Number,
default: null
},
autofocus: {
type: Boolean,
default: true
},
quickButtons: {
type: Array,
default: () => [
{
label: '✅ Native Support',
query: 'status:native'
},
{
label: '✳️ Rosetta',
query: 'status:rosetta'
},
{
label: '🚫 Unsupported',
query: 'status:no'
},
{
label: 'Music Tools',
query: 'Music'
},
{
label: 'Developer Tools',
query: 'Developer'
},
{
label: 'Photo Tools',
query: 'Photo'
},
{
label: 'Video Tools',
query: 'Video'
},
{
label: 'Productivity Tools',
query: 'Productivity'
},
]
}
},
data: function () {
return {
// appList,
query: '',
hasStartedAnyQuery: false,
observer: null,
seenItems: Object.fromEntries(this.appList.map(app => {
return [app.slug, false]
})),
// results: [],
titleStartsWithResults: [],
titleContainsResults: [],
categoryContainsResults: [],
statusResults: [],
// store: overlayStore.state
}
},
computed: {
initialList () {
return this.initialLimit !== null ? this.appList.slice(0, this.initialLimit) : this.appList
},
results () {
if (!this.hasSearchInputText) return this.initialList
return [
...this.titleStartsWithResults,
...this.titleContainsResults,
...this.categoryContainsResults,
...this.statusResults
]
},
// Chunk results to avoid having a parent node with more than 60 child nodes.
chunkedResults () {
const results = [
...this.results
]
const size = 25
const chunks = []
while (results.length > 0)
chunks.push(results.splice(0, size))
return chunks
},
hasSearchInputText () {
return this.query.length > 0
},
},
beforeDestroy() {
this.observer.disconnect()
},
// watch: {
// 'store.mode': function (newMode) {
// // If we're showing the search
// // then focus on the search input
// // on the next tick when our input
// // exists
// if (newMode === 'search') {
// this.$nextTick(() => {
// this.$refs.search.focus()
// })
// }
// }
// },
mounted () {
// console.log(this.$el)
this.observer = new IntersectionObserver(this.onElementObserved, {
// root: this.$el,
threshold: 1.0,
})
// Start observing all search rows
this.initialList.forEach(app => {
if (this.$refs.hasOwnProperty(`${app.slug}-row`) === false) {
console.log('App Row not found', app)
return
}
// console.log('this.$refs[`${app.slug}-row`]', this.$refs[`${app.slug}-row`])
this.observer.observe(this.$refs[`${app.slug}-row`][0])
})
},
methods: {
getAppCategory,
getAppEndpoint,
getSearchLinks (app) {
return app?.searchLinks || []
},
// Search priorities
titleStartsWith (query, app) {
const matches = app.name.toLowerCase().startsWith(query)
if (matches) {
this.titleStartsWithResults.push(app)
}
return matches
},
titleContains (query, app) {
const matches = app.name.toLowerCase().includes(query)
if (matches) {
this.titleContainsResults.push(app)
}
return matches
},
categoryContains (query, app) {
const matches = getAppCategory(app).label.toLowerCase().includes(query)
if (matches) {
this.categoryContainsResults.push(app)
}
return matches
},
statusIs (query, app) {
// console.log('query', query)
if (!query.includes('status:')) return
const [_, status] = query.split(':')
const matches = app.status.includes(status) || app.status === status
// if (matches) {
// this.statusResults.push(app)
// }
return matches
},
// Search tools
pluck (array, index) {
const pluckedItem = array[index]
array.splice(index, 1)
return pluckedItem
},
toggleFilter ( newFilterQuery ) {
// Get the key and value from our filter
const [
newFilterKey, // This will always have a value
newFilterValue = null
] = newFilterQuery.split(':')
const oldQueryWords = this.query.split(' ')
let oldHasStatus = false
let oldHasWords = false
oldQueryWords.forEach( word => {
if (word.includes('status:')) {
oldHasStatus = true
return
}
if (word.trim().length !== 0) oldHasWords = true
})
// If this filter is already present
// then remove it
if (this.query.includes(newFilterQuery)) {
this.query = this.query.replace(newFilterQuery, '').trim()
return
}
// If there is only an existing status and we're adding a plain words
// and the newQuery is not
// then prepend the words to the existing status
if (oldHasStatus && !oldHasWords && newFilterValue === null) {
this.query = [
newFilterQuery,
this.query.trim()
].join(' ')
return
}
// There is no filter key
// then update the whole query
if (newFilterValue === null) {
this.query = newFilterQuery
return
}
// However, if the query already has a status
// then update the existing status
if (this.query.includes(newFilterKey)) {
const queryWords = this.query.split(' ')
this.query = queryWords.map( word => {
// If this the filter word
// then update it to the new one
if (word.includes(newFilterKey)) return newFilterQuery
return word.trim()
}).join(' ')
return
// Otherwise add to the end of our current query
} else {
const queryWords = []
// If the query is not empty
// then add it to our updated query
if (this.query.trim().length) queryWords.push(this.query.trim())
// Append the new filter
queryWords.push(newFilterQuery)
// Update the query
this.query = queryWords.join(' ')
}
},
filterStatusFromText (rawQuery) {
const statusText = []
const searchWords = []
// Look through each word and separate the status words from the normal query words
rawQuery.split(' ').forEach(word => {
if (word.includes('status:')) {
statusText.push(word)
return
}
searchWords.push(word)
})
return [
statusText.join(' '),
searchWords.join(' ').trim()
]
},
scrollInputToTop () {
scrollIntoView(this.$refs['search-container'], {
block: 'start',
behavior: 'smooth'
})
},
onElementObserved(entries) {
entries.forEach(({ target, isIntersecting }) => {
if (!isIntersecting) {
return
}
this.observer.unobserve(target)
// console.log('Observed target', target)
const appSlug = target.getAttribute('data-app-slug')
this.seenItems[appSlug] = true
});
},
queryResults (rawQuery) {
// Clear any results from before
this.titleStartsWithResults = []
this.titleContainsResults = []
this.categoryContainsResults = []
this.statusResults = []
// Snap results scroll position back to top
this.$refs['search-container'].scrollTop = 0
this.$emit('update:query', rawQuery)
// If our query is empty
// then bail
if (rawQuery.length === 0) return
// Separate status filters from the actual query text
const [
statusText,
query
] = this.filterStatusFromText(rawQuery.toLowerCase())
// Declare that at least one query has been made
this.hasStartedAnyQuery = true
// Search App List
this.appList.filter( app => {
const filters = [
{
key: 'status',
method: this.statusIs,
queryArg: statusText
}
]
// Does this app match every active filter
const filtersMatched = filters.every( ({ key, method, queryArg }) => {
// If the query does not contain the key for the filter
// then filter automatically passes
if (queryArg.includes(key) === false) return true
// console.log(queryArg, 'method', method)
return method(queryArg, app)
})
return filtersMatched
}).forEach(app => {
// console.log('app', app)
const matchers = [
{
method: this.titleStartsWith,
queryArg: query
},
{
method: this.titleContains,
queryArg: query
},
{
method: this.categoryContains,
queryArg: query
}
]
// Run through our search priorities
for (const { method, queryArg } of matchers){
// iterations++
const appMatches = method(queryArg, app)
if (appMatches) {
// We've found a match for this app
// so let's stop trying match methods
// and search the next app
break
}
}
})
// console.log('query', query)
// console.log('iterations', iterations)
}
}
}
</script>