|
6 | 6 | define([
|
7 | 7 | 'jquery',
|
8 | 8 | 'underscore',
|
9 |
| - 'jquery/ui' |
| 9 | + 'jquery/ui', |
| 10 | + 'jquery/jquery.parsequery' |
10 | 11 | ], function ($, _) {
|
11 | 12 | 'use strict';
|
12 | 13 |
|
13 |
| - /** |
14 |
| - * Parse params |
15 |
| - * @param {String} query |
16 |
| - * @returns {{}} |
17 |
| - */ |
18 |
| - function parseParams(query) { |
19 |
| - var re = /([^&=]+)=?([^&]*)/g, |
20 |
| - decodeRE = /\+/g, // Regex for replacing addition symbol with a space |
21 |
| - |
22 |
| - /** |
23 |
| - * Return decoded URI component |
24 |
| - * |
25 |
| - * @param {String} str |
26 |
| - * @returns {String} |
27 |
| - */ |
28 |
| - decode = function (str) { |
29 |
| - return decodeURIComponent(str.replace(decodeRE, ' ')); |
30 |
| - }, |
31 |
| - params = {}, |
32 |
| - e, |
33 |
| - k, |
34 |
| - v; |
35 |
| - |
36 |
| - while ((e = re.exec(query)) != null) { |
37 |
| - |
38 |
| - k = decode(e[1]); |
39 |
| - v = decode(e[2]); |
40 |
| - |
41 |
| - if (k.substring(k.length - 2) === '[]') { |
42 |
| - k = k.substring(0, k.length - 2); |
43 |
| - (params[k] || (params[k] = [])).push(v); |
44 |
| - } else { |
45 |
| - params[k] = v; |
46 |
| - } |
47 |
| - } |
48 |
| - |
49 |
| - return params; |
50 |
| - } |
51 |
| - |
52 | 14 | /**
|
53 | 15 | * Render tooltips by attributes (only to up).
|
54 | 16 | * Required element attributes:
|
@@ -341,7 +303,8 @@ define([
|
341 | 303 | $widget._Rewind(container);
|
342 | 304 |
|
343 | 305 | //Emulate click on all swatches from Request
|
344 |
| - $widget._EmulateSelected(); |
| 306 | + $widget._EmulateSelected($.parseQuery()); |
| 307 | + $widget._EmulateSelected($widget._getSelectedAttributes()); |
345 | 308 | },
|
346 | 309 |
|
347 | 310 | /**
|
@@ -747,7 +710,7 @@ define([
|
747 | 710 | .find('.price-box.price-final_price').attr('data-product-id');
|
748 | 711 | }
|
749 | 712 |
|
750 |
| - additional = parseParams(window.location.search.substring(1)); |
| 713 | + additional = $.parseQuery(); |
751 | 714 |
|
752 | 715 | $widget._XhrKiller();
|
753 | 716 | $widget._EnableProductMediaLoader($this);
|
@@ -893,18 +856,36 @@ define([
|
893 | 856 |
|
894 | 857 | /**
|
895 | 858 | * Emulate mouse click on all swatches that should be selected
|
896 |
| - * |
| 859 | + * @param {Object} [selectedAttributes] |
897 | 860 | * @private
|
898 | 861 | */
|
899 |
| - _EmulateSelected: function () { |
900 |
| - var $widget = this, |
901 |
| - $this = $widget.element, |
902 |
| - request = parseParams(window.location.search.substring(1)); |
| 862 | + _EmulateSelected: function (selectedAttributes) { |
| 863 | + $.each(selectedAttributes, $.proxy(function (attributeCode, optionId) { |
| 864 | + this.element.find('.' + this.options.classes.attributeClass + |
| 865 | + '[attribute-code="' + attributeCode + '"] [option-id="' + optionId + '"]').trigger('click'); |
| 866 | + }, this)); |
| 867 | + }, |
903 | 868 |
|
904 |
| - $.each(request, function (key, value) { |
905 |
| - $this.find('.' + $widget.options.classes.attributeClass + |
906 |
| - '[attribute-code="' + key + '"] [option-id="' + value + '"]').trigger('click'); |
907 |
| - }); |
| 869 | + /** |
| 870 | + * Get default options values settings with either URL query parameters |
| 871 | + * @private |
| 872 | + */ |
| 873 | + _getSelectedAttributes: function () { |
| 874 | + var hashIndex = window.location.href.indexOf('#'), |
| 875 | + selectedAttributes = {}, |
| 876 | + params; |
| 877 | + |
| 878 | + if (hashIndex !== -1) { |
| 879 | + params = $.parseQuery(window.location.href.substr(hashIndex + 1)); |
| 880 | + |
| 881 | + selectedAttributes = _.invert(_.mapObject(_.invert(params), function (attributeId) { |
| 882 | + var attribute = this.options.jsonConfig.attributes[attributeId]; |
| 883 | + |
| 884 | + return attribute ? attribute.code : attributeId; |
| 885 | + }.bind(this))); |
| 886 | + } |
| 887 | + |
| 888 | + return selectedAttributes; |
908 | 889 | }
|
909 | 890 | });
|
910 | 891 | });
|
0 commit comments