-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoogle_main.js
233 lines (201 loc) · 7.33 KB
/
woogle_main.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
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
window.onload = function(){
/* User clicks search button */
document.querySelector('#search-button').onclick = function() {
getResults();
}
/* User clicks enter */
document.body.onkeydown = function(e) {
if (e.keyCode == 13)
getResults();
};
try {
document.querySelector('#order').onchange = function() {
getResults();
}
} catch (error) {}
try {
document.querySelector('.overview-iframe').width = screen.width;
document.querySelector('.overview-iframe').height = screen.height;
} catch (error) {
}
try {
href_value = href.searchParams.get('order') ? href.searchParams.get('order') : 'relevance-desc';
const $select = document.querySelector('#order');
$select.value = href_value;
} catch (error) {}
/* set background of current tab */
let current_url = document.location;
document.querySelectorAll(".navbar .nav-link").forEach(function(e){
if(e.href == current_url){
e.classList += " current";
}
});
/* Change page */
$('.woogle-pagination-nr').each(function() {
$(this).click(function() {
href.searchParams.set('page', this.value);
window.location.href = href;
})
})
/* Change page */
$('.woo-facet-value').each(function() {
$(this).click(function() {
href.searchParams.set('page', 1);
parameterToSet = this.className.split(" ")[1];
href.searchParams.set(parameterToSet, this.value);
window.location.href = href;
})
})
/* Expand facets */
$('.woo-expand').each(function() {
$(this).click(function() {
facetType = this.className.split(" ")[1];
const collection = document.getElementsByClassName('facet-disabled ' + facetType)
for (let i = 0; i < collection.length; i++) {
collection[i].style.display = "block";
}
document.getElementsByClassName('woo-collapse ' + facetType)[0].style.display = 'block';
this.style.display = 'none';
})
})
/* Collapse facets */
$('.woo-collapse').each(function() {
$(this).click(function() {
facetType = this.className.split(" ")[1];
const collection = document.getElementsByClassName('facet-disabled ' + facetType)
for (let i = 0; i < collection.length; i++) {
collection[i].style.display = "none";
}
document.getElementsByClassName('woo-expand ' + facetType)[0].style.display = 'block';
this.style.display = 'none';
})
})
/* Expand page hits */
$('.woogle-expand-hits').each(function() {
$(this).click(function() {
dossierId = this.className.split(" ")[1];
const collection = document.getElementsByClassName('woogle-page-hits ' + dossierId);
collection[0].style.display = 'block';
document.getElementsByClassName('woogle-collapse-hits ' + dossierId)[0].style.display = 'block';
this.style.display = 'none';
})
})
/* Collapse page hits */
$('.woogle-collapse-hits').each(function() {
$(this).click(function() {
dossierId = this.className.split(" ")[1];
const collection = document.getElementsByClassName('woogle-page-hits ' + dossierId);
collection[0].style.display = 'none';
document.getElementsByClassName('woogle-expand-hits ' + dossierId)[0].style.display = 'block';
this.style.display = 'none';
})
})
$('.crumb').each(function() {
$(this).click(function() {
handleDeleteCrumb(this);
})
})
$('.register-click').each(function() {
$(this).click(function() {
registerClick(this);
})
})
}
href = new URL(window.location.href);
function getDataListSelectedOption(txt_input, data_list_options)
{
var shownVal = document.getElementById(txt_input).value;
var docvalue = document.querySelector("#" + data_list_options + ' option[value="' + shownVal + '"]');
if (docvalue == null) {
return null;
}
var value2send = document.querySelector("#" + data_list_options + ' option[value="' + shownVal + '"]').dataset.value;
return value2send;
}
function getResults() {
queryvalue = document.querySelector('#query').value
query = queryvalue.replace(/“|”/g, '"');
publisher = getDataListSelectedOption('publisher', 'woo-creators');
country = getDataListSelectedOption('country', 'woo-countries');
year_min = document.querySelector('#year_min').value;
year_max = document.querySelector('#year_max').value;
publication_type = getDataListSelectedOption('type', 'woo-types');
pid = document.querySelector('#pid').value;
page = 1;
try {
order = document.querySelector('#order').value;
} catch (error) { order = null; }
href.pathname = "/search"
if (query) {
href.searchParams.set('q', query);
}
if (publisher) {
href.searchParams.set('publisher', publisher);
}
if (year_min) {
href.searchParams.set('year-min', year_min);
}
if (year_max) {
href.searchParams.set('year-max', year_max);
}
if (publication_type) {
href.searchParams.set('type', publication_type);
}
if (pid) {
href.searchParams.set('pid', pid);
}
if (order) {
href.searchParams.set('order', order);
}
if (page) {
href.searchParams.set('page', page);
}
if (country) {
href.searchParams.set('country', country);
}
window.location.href = href;
}
/* Register click */
function registerClick(resource) {
var click_data = {
'resourceID': resource.getAttribute('data-resource'),
'query': document.querySelector('#query').value,
'publisher': getDataListSelectedOption('publisher', 'woo-creators'),
'year_min': document.querySelector('#year_min').value,
'year_max': document.querySelector('#year_max').value,
'publication_type': getDataListSelectedOption('type', 'woo-types')
};
/* Send click data to server */
$.ajax({
url: '/register_click',
type: 'POST',
data: JSON.stringify(click_data), // converts js value to JSON string
contentType: 'application/json', // sends json
})
.done(function(){ // on success get the return object from server
return true // do whatever with it. In this case see it in console
})
}
function handleDeleteCrumb(element) {
var crumb = element.getAttribute('data-crumb').split('_')[1];
href.searchParams.delete(crumb);
href.searchParams.set('page', 1);
window.location.href = href;
}
function homepage(){
var query = '*';
window.location.href = "/search?q=" + SchoolId;
}
window.addEventListener( "pageshow", function ( event ) {
var historyTraversal = event.persisted ||
( typeof window.performance != "undefined" &&
window.performance.navigation.type === 2 );
if ( historyTraversal ) {
// Handle page restore.
window.location.reload();
}
var perfEntries = performance.getEntriesByType("navigation");
if (perfEntries[0].type === "back_forward") {
location.reload();
}
});