-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchFilter.js
More file actions
413 lines (331 loc) · 13.3 KB
/
Copy pathsearchFilter.js
File metadata and controls
413 lines (331 loc) · 13.3 KB
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
/**
* Filter module. Currently used to filter eventListing events.
* This module is reponsible for showing/hiding filter module and switching
* between desktop and mobile versions.
*
* @module searchFilter
*/
mod.create_module("search-filter", function(sb) {
'use strict';
var self;
return {
el: {
"container": sb.getContainer(),
"toggle": null,
"filterContainer": null,
"pageContainer": null,
"headers": null,
"mobileHeader": null,
"mobileMenuContents": null,
"filterTags": null
},
events: {
'click headers': 'clickToggle',
'tap headers': 'clickToggle',
'click mobileMenuContents': 'modulesClickHandler'
},
mobileMenuToggleClass: 'is-closed',
isMenuVisible: false,
isMobileMenuVisible: false,
isChecked: 'is-active',
/**
* Standard init function.
*/
init: function() {
self = this;
self.view.updateElementBindings();
self.bindNotifiers();
self.updateElements();
self.view.shouldBeVisible();
},
/**
* Event handler for clicking on the filter menu
*/
clickToggle: function(event){
event.stopPropagation();
event.preventDefault();
self.view.toggleMenu();
},
/**
* Listen for updates from filter modules.
*/
bindNotifiers: function() {
sb.library({
'name': 'polling'
}, sb);
sb.listen({
'update-search-filter-modules': self.view.updateContents,
'switch-to-mobile': self.updateElements,
'view-resized': self.view.viewResized,
'polling': self.library.polling.listen,
'server-error': self.handleServerError
});
},
/**
* This function is called when the mobile menu is opened, and rebinds itself to the recently moved
*/
updateElements: function() {
var pollingContainer = null;
// Get new data holders
self.bindHolderElements();
pollingContainer = self.getPreloaderContainer();
self.library.polling.updateContainer(pollingContainer);
self.bindHolderElementEvents();
},
getPreloaderContainer: function() {
return (self.isMobileMenuVisible) ? sb.find(".mobilemenu--right", document)[0] : sb.find('.mobile-menu-contents', self.el.container)[0];
},
/**
* So we have always pointers to all data holder objects.
*/
bindHolderElements: function() {
self.el.dataHolder = sb.find('[data-type="holder"]', document);
self.el.filterTags = sb.find('[data-module="search-filter-tags"]', document);
},
/**
* Binding events and checking for actual container.
*
* @param {Boolean} isMobile If the call is from mobile version
*/
bindHolderElementEvents: function() {
sb.removeEvent(self.el.dataHolder, 'click', self.modulesClickHandler);
self.bindHolderElements();
sb.addEvent(self.el.dataHolder, 'click', self.modulesClickHandler);
},
/**
* Event handler for clicking on the links in filter module.
* Using event delegation.
*/
modulesClickHandler: function(event) {
var target = event.target;
event.stopPropagation();
event.preventDefault();
if (target.hasAttribute('href')) {
self.view.toggleCheckbox(target);
}
},
/**
* Get href attribute from object
*
* @param {Object} object actual clicked link object
* @return {String} href attribute without query mark
*/
getLink: function(object) {
var link = '';
if (!object.hasAttribute('href')) {
return false;
} else {
link = object.href;
}
return link;
},
/**
*
*
* Calling publisher to notify other components.
*
* @param {Object} object Actual clicked object for data collection.
*/
updateData: function(object) {
var link = self.getLink(object);
if (typeof link === 'string') {
// Call publisher
self.publishData(link);
}
},
/**
* Triggering sandbox publisher.
*
* @param {Object} data Data object injection.
*/
publishData: function(link) {
// Notify Events Listing module that we need new data
sb.notify({
type: 'update-event-listing',
data: link
});
// Notify preloader
self.library.polling.start();
// Calculate new spinner position
self.view.updateSpinnerPosition();
},
/**
* Triggered when there's a error in the ajax event listing
*/
handleServerError: function() {
if (self.isMenuVisible === true) {
self.view.toggleMenu();
}
},
/**
* Method is returning actual Tags section height for spinner to be
* positioned accurately.
*
* It's adding tags section height and filter headers height + 10px
* so the spinner is not sticked to the headers but has small
* gap in between.
*
* @param {Object} tagsHolder Tags section object (Message coupling)
* @param {Object} headersHolder Headers object (Message coupling)
* @return {Number} Tags section height (px).
*/
getTagsSectionHeight: function(tagsHolder, headersHolder) {
return sb.outerHeight(tagsHolder) + sb.outerHeight(headersHolder) + 10;
},
view: {
updateElementBindings: function(){
self.el.toggle = sb.find('.searchfilter__togglebtn')[0];
self.el.filterContainer = sb.find('.searchfilter__holder')[0];
self.el.mobileMenuContents = sb.find('.mobile-menu-contents')[0];
self.el.pageContainer = sb.find('.container', document.body)[0];
self.el.headers = sb.find("[data-action='toggle-filter']");
self.el.filterTags = sb.find('[data-module="search-filter-tags"]');
sb.bindElementEvents(self);
},
/**
* To create checkbox like behavior for links
*
* @param {Object} object Clicked object
*/
toggleCheckbox: function(object) {
if (typeof object === 'object' && object !== null) {
if (sb.hasClass(object, self.isChecked)) {
sb.removeClass(object, self.isChecked);
} else {
sb.addClass(object, self.isChecked);
}
/**
* Send object for data extraction
*/
self.updateData(object);
}
},
/**
* Shows or hides the filter area. On mobile, this means the right-hand-side menu. On
* desktop, this means the boxes above the search area.
*
* @returns {boolean|.searchFilter.isMenuVisible}
*/
toggleMenu: function (){
var contents;
if (self.isMenuVisible){
self.isMenuVisible = false;
if (self.isMobileMenuVisible){
contents = sb.find('.mobilemenu--right', document.body)[0].innerHTML;
self.el.mobileMenuContents.innerHTML = contents;
self.view.hideMobileMenu();
self.view.updateElementBindings();
}
sb.addClass(self.el.filterContainer, self.mobileMenuToggleClass);
} else {
self.isMenuVisible = true;
sb.removeClass(self.el.filterContainer, self.mobileMenuToggleClass);
if (sb.checkMatchMedia('smallScreen') &&
typeof self.el.filterContainer !== "undefined"){
contents = self.el.mobileMenuContents.innerHTML;
self.el.mobileMenuContents.innerHTML = '';
self.view.showMobileMenu(contents);
}
}
return self.isMenuVisible;
},
/**
* Binds a click handler to the main document when the mobile menu is open, used to hide
* it again. This click handler will only be bound when the menu is open.
*
* @param {Boolean} shouldListen Toggle listening on or off
*/
shouldListenForContainerClicks: function(shouldListen){
if (shouldListen === true){
sb.addEvent(self.el.pageContainer, 'click' , self.clickToggle);
} else {
sb.removeEvent(self.el.pageContainer, 'click', self.clickToggle);
}
},
/**
* Displays the filter area in a mobile menu, coming in from the page right
*
* @param html Appends the mobile menu to the document body
* @return {Boolean} True if the menu is visible
*/
showMobileMenu: function(html){
var body = sb.find(document.body, document)[0],
container = sb.create_element('div', {'class': 'mobilemenu--right'}),
titleElement;
container.innerHTML = html;
body.appendChild(container);
sb.addClass(self.el.pageContainer, 'is-mobilemenuopen');
self.view.shouldListenForContainerClicks(true);
titleElement = sb.find('.searchfilter__togglebtn--mobiletitle', document.body);
sb.addEvent(titleElement, 'click', self.view.toggleMenu);
self.isMobileMenuVisible = true;
self.updateElements();
return self.isMenuVisible;
},
/**
* Hides the mobile menu that is shown with showMobileMenu
*
* @return {Boolean} False if the menu is correctly hidden
*/
hideMobileMenu: function(){
// Remove the menu dom element
sb.remove(sb.find('.mobilemenu--right', document));
// Class management
sb.addClass(self.el.filterContainer, self.mobileMenuToggleClass);
sb.removeClass(self.el.pageContainer, 'is-mobilemenuopen');
// When the menu is open, clicking on the left would close the menu
self.view.shouldListenForContainerClicks(false);
self.isMobileMenuVisible = false;
self.updateElements();
return self.isMenuVisible;
},
/**
* When the mobile menu is open, we watch for resize events. If the user
* moves from small-screen to large-screen, hide the mobile menu.
*/
viewResized: function(){
if (sb.checkMatchMedia('largeScreen') && self.isMobileMenuVisible){
self.view.toggleMenu();
}
},
/**
* Update the containers with new HTML
*
* @param data The json response containing HTML fragments for the new results and pagination
*/
updateContents: function(data) {
sb.foreach(self.el.dataHolder, function(index) {
var datafetch = self.el.dataHolder[index].getAttribute("data-fetch");
self.el.dataHolder[index].innerHTML = data.filters[datafetch];
});
// Check if module should hide itself
self.view.shouldBeVisible();
// Notify preloader
self.library.polling.end();
},
/**
* Method is checking if we have any results in tags module. If there
* are no tags we are hiding whole module.
*/
shouldBeVisible: function() {
if (sb.find('a', self.el.filterTags).length === 0) {
sb.addClass(self.el.filterTags, 'is-hidden');
} else {
sb.removeClass(self.el.filterTags, 'is-hidden');
}
},
/**
* Update CSS for spinner.
*/
updateSpinnerPosition: function() {
var spinner = sb.find('.inactiveOverlay');
sb.setCSS(spinner, {
"background-position": "50% " + self.getTagsSectionHeight(self.el.filterTags, self.el.headers) + "px"
});
}
},
destroy: function() {
self = null;
}
};
});