-
Notifications
You must be signed in to change notification settings - Fork 16
/
navigation-form.js
480 lines (427 loc) · 16 KB
/
navigation-form.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
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
/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
* @copyright Copyright (c) 2017-2022 Tweakwise.com B.V. (https://www.tweakwise.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
define([
'jquery',
'mage/cookies'
], function ($) {
$.widget('tweakwise.navigationForm', {
options: {
ajaxFilters: false,
formFilters: false,
seoEnabled: false,
ajaxEndpoint: '/tweakwise/ajax/navigation',
filterSelector: '#layered-filter-block',
productListSelector: '.products.wrapper',
toolbarSelector: '.toolbar.toolbar-products',
productsGridSelector: '.products-grid',
mainColumnSelector: '.column.main',
emptyInfoMessageSelector: '.message.info.empty',
noteMessageSelector: '.message.note',
noticeMessageSelector: '.message.notice',
isLoading: false,
ajaxCache: true,
},
currentXhr: null,
_create: function () {
this._hookEvents();
this._fixAjaxHistory();
return this._superApply(arguments);
},
/**
* Fix first page history on page load when using ajax filters
*
* @private
*/
_fixAjaxHistory: function () {
if(this.options.ajaxFilters && this.options.ajaxCache && (!window.history.state || !window.history.state.html))
{
//if window history is empty, do an ajax request to fill it.
this.currentXhr = $.ajax({
url: this.options.ajaxEndpoint,
data: this._getFilterParameters(),
cache: this.options.ajaxCache,
success: function (response) {
this._replaceState(response);
}.bind(this)
});
}
},
/**
* Bind filter events, these are filter click and filter remove
*
* @private
*/
_hookEvents: function () {
this._bindFilterClickEvents();
this._bindFilterRemoveEvents();
if (this.options.ajaxFilters) {
this._bindPopChangeHandler();
}
},
/**
* Bind filter click events
*
* @private
*/
_bindFilterClickEvents: function () {
if (this.options.formFilters) {
this.element.on('click', '.js-btn-filter', this._getFilterClickHandler().bind(this));
} else {
this.element.on('change', this._getFilterClickHandler().bind(this));
}
},
/**
* Filter remove events are only relevant for ajax filtering. If ajaxFilters is false then we just navigate
* to the url specified in the a.
*
* @private
*/
_bindFilterRemoveEvents: function () {
if (this.options.ajaxFilters) {
this.element.on('click', 'a.remove', this._ajaxClearHandler.bind(this));
}
},
/**
*
* @private
*/
_bindPopChangeHandler: function () {
window.onpopstate = function (event) {
if (event.state && event.state.html) {
this._updateBlocks(event.state.html);
}
}.bind(this);
},
/**
* Should return the handler for the filter event, depends on config options.
* Supported options are ajax filtering and form filters and any combination of those options.
* Note that the ajaxHandler also handles the case ajax enabled AND form filters enabled
*
* @returns {tweakwise.navigationFilterAjax._ajaxHandler|tweakwise.navigationFilterAjax._defaultHandler}
* @private
*/
_getFilterClickHandler: function () {
if (this.options.ajaxFilters) {
return this._ajaxHandler;
}
if (this.options.formFilters) {
return this._formFilterHandler;
}
return this._defaultHandler;
},
/**
* Serialize the form element but skip unwanted inputs
*
* @returns {*}
* @private
*/
_getFilterParameters: function () {
var serializedData = this.element.find(':not(.js-skip-submit)').serialize();
let params = new URLSearchParams(serializedData);
let keysForDel = [];
params.forEach((value, key) => {
if (value == '') {
keysForDel.push(key);
}
});
//remove empty parameters
keysForDel.forEach(key => {
params.delete(key);
});
return params.toString();
},
// ------- Default filter handling (i.e. no ajax and no filter form)
/**
* Navigate to the selected filter url
*
* @param event
* @returns {boolean}
* @private
*/
_defaultHandler: function (event) {
var a = $(event.target).closest('a');
var href = this._findHref(a);
if (href) {
window.location.href = href;
return false;
}
},
/**
* Should return the url to navigate to
*
* @param aElement
* @returns {*}
* @private
*/
_findHref: function (aElement) {
var href = aElement.attr('href');
if (this.options.seoEnabled) {
var seoHref = aElement.data('seo-href');
return seoHref ? seoHref : href;
}
return href;
},
/**
* This provides a means to disable ajax filtering.
* If you dont want ajax filtering for certain filters add a data-no-ajax attribute.
*
* @param event
* @returns {boolean}
* @private
*/
_allowAjax: function (event) {
var a = $(event.target).closest('a');
return !a.data('no-ajax');
},
// ------- End of default filter handling
// ------- Handling for ajax filtering (i.e. only ajax filtering)
/**
* Handle Ajax request for new content
*
* @param event
* @private
*/
_ajaxHandler: function (event) {
event.preventDefault();
if(event.target.name == 'tw_filtersearch') {
return;
}
if (this.currentXhr) {
this.currentXhr.abort();
}
if (!this._allowAjax(event)) {
this._defaultHandler(event);
return;
}
this._startLoader();
this.currentXhr = $.ajax({
url: this.options.ajaxEndpoint,
data: this._getFilterParameters(),
cache: this.options.ajaxCache,
success: function (response) {
this._updateBlocks(response.html);
this._updateState(response);
}.bind(this),
error: function (jqXHR, errorStatus) {
if (errorStatus !== 'abort') {
// Something went wrong, try to navigate to the selected filter
this._defaultHandler(event);
}
}.bind(this),
complete: function () {
this._stopLoader();
}.bind(this)
});
},
/**
* Handle filter clear events
*
* @param event
* @private
*/
_ajaxClearHandler: function (event) {
var filterId = '#' + $(event.target).data('js-filter-id');
var filter = this.element.find(filterId);
if (filter && filter.length) {
event.preventDefault();
filter = $(filter);
// Set filter disabled so that it will not be submitted when change is triggered
filter.attr('disabled', true);
if (this.options.formFilters) {
// Simulate click so that the form will be submitted
this.element.find('.js-btn-filter').first().trigger('click');
} else {
filter.trigger('change');
}
}
},
/**
* Update all relevant html with response data, trigger contentUpdated to 'trigger' data-mage-init
* @param htmlResponse
* @private
*/
_updateBlocks: function (htmlResponse) {
var filterSelector = this.options.filterSelector;
var productListSelector = this.options.productListSelector;
var toolbarSelector = this.options.toolbarSelector;
var wrapper = document.createElement('div');
wrapper.innerHTML = htmlResponse;
var parsedHtml = $(wrapper);
var newFilters = parsedHtml.find(filterSelector);
var newProductList = parsedHtml.find(productListSelector);
var newToolbar = parsedHtml.find(toolbarSelector);
// Toolbar is included twice in the response
// We use this first().get(0) construction to access outerHTML
// The reason for this is that we need to replace the entire toolbar because otherwise
// the data-mage-init attribute is no longer available on the toolbar and hence the toolbar
// would not be initialized when $('body').trigger('contentUpdated'); is called
var newToolbarFirst = newToolbar.first().get(0);
var newToolbarLast = newToolbar.last().get(0);
if (newFilters.length) {
$(filterSelector).replaceWith(newFilters);
}
var toolbar = $(toolbarSelector);
const productsGrid = $(this.options.productsGridSelector);
const mainColumn = $(this.options.mainColumnSelector);
const emptyInfo = $(this.options.emptyInfoMessageSelector);
const note = $(this.options.noteMessageSelector);
const notice = $(this.options.noticeMessageSelector);
emptyInfo.remove();
note.remove();
notice.remove();
/*
The product list comes after the toolbar.
We use this construction as there could be more product lists on the page
and we dont want to replace them all
*/
if (newProductList.length) {
toolbar
.siblings(productListSelector)
.replaceWith(newProductList);
} else {
/*
It happens that a filter yields no result.
In that case magento returns a message in a div
that needs to rendered correctly and also removed if not required
*/
toolbar.hide();
productsGrid.hide();
$('script[data-role=\'msrp-popup-template\']').remove();
$('script[data-role=\'msrp-info-template\']').remove();
mainColumn.append(htmlResponse);
}
if (newToolbarFirst) {
toolbar
.first()
.replaceWith(newToolbarFirst.outerHTML);
}
if (newToolbarLast) {
var scripts = '';
$(newToolbarLast).siblings('script[type="text/x-magento-init"]').map(
function (index, element) {
scripts += element.outerHTML;
}
);
toolbar
.last()
.replaceWith(newToolbarLast.outerHTML + scripts);
}
const primaryActionsDiv = $(".actions-primary")
primaryActionsDiv.find('form').
each(function (i, form) {
$(form).append('<input name="form_key" type="hidden" ' +
' value="' + $.mage.cookies.get('form_key') + '" />');
});
$('body').trigger('contentUpdated');
},
/**
* @param response
* @private
*/
_updateState: function (response) {
const newUrl = this._buildUrlWithQueryString(response, true);
window.history.pushState({html: response.html}, '', newUrl);
},
/**
* @param response
* @private
*/
_replaceState: function (response) {
const newUrl = this._buildUrlWithQueryString(response);
window.history.replaceState({html: response.html}, '', newUrl);
},
/**
* Merges existed query parameters with the ones get from AJAX response if needed
*
* @param response
* @param flip
* @returns string
* @private
*/
_buildUrlWithQueryString: function (response, flip = false) {
const baseUrl = window.location.origin;
const resultUrl = new URL(response.url, baseUrl);
const queryParams = new URLSearchParams(window.location.search ?? '');
let queryParamsString = queryParams.toString();
if (resultUrl.search) {
if (flip) {
queryParamsString = this._combineQueryStrings(resultUrl.searchParams, queryParams);
} else {
queryParamsString = this._combineQueryStrings(queryParams, resultUrl.searchParams);
}
}
resultUrl.search = queryParamsString;
let result = resultUrl.toString();
result = this._normalizeQueryString(result);
return result;
},
/**
* Removes `=` sign for parameters without value
*
* @param queryString
* @returns string
* @private
*/
_normalizeQueryString: function (queryString) {
return queryString.replace(/=$|=(?=&)/g, '');
},
/**
* Combines the original query string parameters with the ones in the AJAX response
*
* @param origQueryString
* @param responseQueryString
* @returns string
* @private
*/
_combineQueryStrings: function (origQueryString, responseQueryString) {
const uniqueQueryParams = new URLSearchParams();
origQueryString.forEach((value, key) => {
if (false === uniqueQueryParams.has(key)) {
uniqueQueryParams.append(key, value);
}
});
responseQueryString.forEach((value, key) => {
if (false === uniqueQueryParams.has(key)) {
uniqueQueryParams.append(key, value);
}
});
return uniqueQueryParams.toString();
},
/**
* Start loader targeting body relevant for ajax filtering
* @private
*/
_startLoader: function () {
$(this.options.productListSelector).trigger('processStart');
this.options.isLoading = true;
},
/**
* Stop Loader targeting body relevant for ajax filtering
* @private
*/
_stopLoader: function () {
$(this.options.productListSelector).trigger('processStop');
this.options.isLoading = false;
},
// ------- End of handling for ajax filtering
// ------- Handling for form filters.
// ------- Note that is only used when ajax is not enabled and form filters is enabled
/**
* This just handles the filter button click
*
* @private
*/
_formFilterHandler: function () {
var filterUrl = this._getFilterParameters();
if (filterUrl) {
window.location = '?' + filterUrl;
}
}
// ------- End of handling for form filters
});
return $.tweakwise.navigationForm;
});