-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.js
142 lines (130 loc) · 3.73 KB
/
search.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
133
134
135
136
137
138
139
140
141
142
---
layout: null
---
var documents = [
{% for book in site.books %}
{
"title": "{{ book.title | escape }}",
"titleShort": "{{ book.titleShort | escape }}",
"author": "{{ book.author | escape }}",
"url": "{{ book.url | relative_url }}",
"pillars": {{ book.pillars | jsonify }},
"topics": {{ book.topics | jsonify}},
"tags": {{ book.tags | jsonify }},
"audiences": {{ book.audiences | jsonify }},
"date": "{{ book.date }}",
"imageUrl": "{{ book.imageUrl | relative_url }}",
"excerpt": "{{ book.excerpt | strip | strip_newlines | strip_html }}"
} {% unless forloop.last %}, {% endunless %}
{% endfor %}
];
var configuration = {
sortings: {
title_asc: {
// field name in data
field: 'title',
// possible values asc or desc
order: 'asc'
}
},
aggregations: {
pillars: {
title: 'Engineering Pillar',
// conjunctive facet (AND)
conjunction: true,
// the default is 10
size: 8
},
audiences: {
title: 'Audience',
// conjunctive facet (AND)
conjunction: true
},
topics: {
title: 'Topic',
// conjunctive facet (AND)
conjunction: true
},
tags: {
title: 'Tag',
// conjunctive facet (AND)
conjunction: true,
// the default is 10
size: 20
}
},
searchableFields: ['title', 'pillars','audiences', 'excerpt', 'author'],
};
var itemsjs = itemsjs(documents, configuration);
// add vue component for pagination
const { createApp } = Vue
createApp({
data: function () {
// making it more generic
var filters = {};
Object.keys(configuration.aggregations).map(function (v) {
filters[v] = [];
})
// adding pagination variables
var page = this.page || 1;
var per_page = this.per_page || 12;
return {
query: '',
// initializing filters with empty arrays
filters: filters,
selected_filters: [],
sort_keys: Object.keys(configuration.sortings),
sort: '',
// setting pagination variables
page: page,
per_page: per_page
}
},
methods: {
reset: function () {
var filters = {};
Object.keys(configuration.aggregations).map(function (v) {
filters[v] = [];
})
this.filters = filters;
this.query = '';
this.page = 1;
},
goToPage: function (page) {
this.page = page;
return page;
},
remove_filter: function (facet, name) {
this.filters[facet] = this.filters[facet].filter(v => {
return v !== name;
});
}
},
watch: {
query: function () {
this.page = 1;
}
},
computed: {
searchResult: function () {
var result = itemsjs.search({
query: this.query,
filters: this.filters,
page: this.page,
per_page: 12,
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
})
}
}
return result
}
}
}).mount('#el')