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
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* @api
*/
define([
'jquery',
'jquery/ui'
], function ($) {
'use strict';
$.widget('mage.recentlyViewedProducts', {
options: {
localStorageKey: 'recently-viewed-products',
productBlock: '#widget_viewed_item',
viewedContainer: 'ol'
},
/**
* Bind events to the appropriate handlers.
* @private
*/
_create: function () {
var productHtml = $(this.options.productBlock).html(),
productSku = $(this.options.productBlock).data('sku'),
products = JSON.parse(window.localStorage.getItem(this.options.localStorageKey)),
productsLength, maximum, showed, index;
if (products) {
productsLength = products.sku.length;
maximum = $(this.element).data('count');
showed = 0;
for (index = 0; index <= productsLength; index++) {
if (products.sku[index] == productSku || showed >= maximum) { //eslint-disable-line
products.sku.splice(index, 1);
products.html.splice(index, 1);
} else {
$(this.element).find(this.options.viewedContainer).append(products.html[index]);
$(this.element).show();
showed++;
}
}
$(this.element).find(this.options.productBlock).show();
} else {
products = {};
products.sku = [];
products.html = [];
}
products.sku.unshift(productSku);
products.html.unshift(productHtml);
window.localStorage.setItem(this.options.localStorageKey, JSON.stringify(products));
}
});
return $.mage.recentlyViewedProducts;
});