123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- /**
- * 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;
- });
|