-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsearch-config.js
107 lines (90 loc) · 2.34 KB
/
search-config.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
// Everything is in the asynchronous function. It is called once search-data file is loaded.
$.getJSON("data/search-data.json", function (json) {
//console.log(json);
var rows = json
var configuration = {
searchableFields: ['name', 'tag', 'summary', 'description'],
aggregations: {
category: {
title: 'Categories',
size: 15
},
type: {
title: 'Types',
size: 15
},
tag: {
title: 'Tags',
size: 30
}
}
}
itemsjs = itemsjs(rows, configuration);
var vm = new Vue({
el: '#el',
data: function () {
// making it more generic
var filters = {};
Object.keys(configuration.aggregations).map(function (v) {
filters[v] = [];
})
return {
query: '',
// initializing filters with empty arrays
filters: filters,
selected_filters: [],
sort_keys: [],
sort: '',
per_page: 500
}
},
methods: {
remove_filter: function (facet, name) {
this.filters[facet] = this.filters[facet].filter(v => {
return v !== name;
});
},
reset: function () {
var filters = {};
Object.keys(configuration.aggregations).map(function (v) {
filters[v] = [];
})
this.filters = filters;
this.query = '';
}
},
computed: {
searchResult: function () {
var result = itemsjs.search({
query: this.query,
filters: this.filters,
per_page: this.per_page,
sort: this.sort
})
// creating selected filters flat array
this.selected_filters = [];
for (const [key, value] of Object.entries(this.filters)) {
for (const name in value) {
this.selected_filters.push({
name: value[name],
facet: key
})
}
}
result.data.items.sort(function(a, b) {
var nameA = a.name.toUpperCase(); // ignore upper and lowercase
var nameB = b.name.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
})
return result
}
}
});
});