Skip to content

Commit 13e7469

Browse files
committed
MAGETWO-71701: [Backport] - Catalog top nav, CSS class not set to active when using Varnish - for 2.1
1 parent 8390ef4 commit 13e7469

File tree

1 file changed

+149
-8
lines changed

1 file changed

+149
-8
lines changed

lib/web/mage/menu.js

Lines changed: 149 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
define([
67
"jquery",
78
"matchMedia",
@@ -18,8 +19,12 @@ define([
1819
options: {
1920
responsive: false,
2021
expanded: false,
21-
delay: 300
22+
mediaBreakpoint: '(max-width: 768px)'
2223
},
24+
25+
/**
26+
* @private
27+
*/
2328
_create: function () {
2429
var self = this;
2530

@@ -29,6 +34,9 @@ define([
2934
});
3035
},
3136

37+
/**
38+
* @private
39+
*/
3240
_init: function () {
3341
this._super();
3442
this.delay = this.options.delay;
@@ -39,7 +47,7 @@ define([
3947

4048
if (this.options.responsive === true) {
4149
mediaCheck({
42-
media: '(max-width: 640px)',
50+
media: this.options.mediaBreakpoint,
4351
entry: $.proxy(function () {
4452
this._toggleMobileMode();
4553
}, this),
@@ -50,8 +58,13 @@ define([
5058
}
5159

5260
this._assignControls()._listen();
61+
this._setActiveMenu();
5362
},
5463

64+
/**
65+
* @return {Object}
66+
* @private
67+
*/
5568
_assignControls: function () {
5669
this.controls = {
5770
toggleBtn: $('[data-action="toggle-nav"]'),
@@ -61,14 +74,24 @@ define([
6174
return this;
6275
},
6376

77+
/**
78+
* @private
79+
*/
6480
_listen: function () {
65-
var controls = this.controls;
66-
var toggle = this.toggle;
81+
var controls = this.controls,
82+
toggle = this.toggle;
6783

68-
this._on(controls.toggleBtn, {'click': toggle});
69-
this._on(controls.swipeArea, {'swipeleft': toggle});
84+
this._on(controls.toggleBtn, {
85+
'click': toggle
86+
});
87+
this._on(controls.swipeArea, {
88+
'swipeleft': toggle
89+
});
7090
},
7191

92+
/**
93+
* Toggle.
94+
*/
7295
toggle: function () {
7396
if ($('html').hasClass('nav-open')) {
7497
$('html').removeClass('nav-open');
@@ -83,24 +106,142 @@ define([
83106
}
84107
},
85108

86-
//Add class for expanded option
109+
/**
110+
* Tries to figure out the active category for current page and add appropriate classes:
111+
* - 'active' class for active category
112+
* - 'has-active' class for all parents of active category
113+
*
114+
* First, checks whether current URL is URL of category page,
115+
* otherwise tries to retrieve category URL in case of current URL is product view page URL
116+
* which has category tree path in it.
117+
*
118+
* @return void
119+
* @private
120+
*/
121+
_setActiveMenu: function () {
122+
var currentUrl = window.location.href.split('?')[0];
123+
124+
if (!this._setActiveMenuForCategory(currentUrl)) {
125+
this._setActiveMenuForProduct(currentUrl);
126+
}
127+
},
128+
129+
/**
130+
* Looks for category with provided URL and adds 'active' CSS class to it if it was not set before.
131+
* If menu item has parent categories, sets 'has-active' class to all af them.
132+
*
133+
* @param {String} url - possible category URL
134+
* @returns {Boolean} - true if active category was founded by provided URL, otherwise return false
135+
* @private
136+
*/
137+
_setActiveMenuForCategory: function (url) {
138+
var activeCategoryLink = this.element.find('a[href="' + url + '"]'),
139+
classes,
140+
classNav;
141+
142+
if (!activeCategoryLink || !activeCategoryLink.hasClass('ui-corner-all')) {
143+
144+
//category was not found by provided URL
145+
return false;
146+
} else if (!activeCategoryLink.parent().hasClass('active')) {
147+
activeCategoryLink.parent().addClass('active');
148+
classes = activeCategoryLink.parent().attr('class');
149+
classNav = classes.match(/(nav\-)[0-9]+(\-[0-9]+)+/gi);
150+
151+
if (classNav) {
152+
this._setActiveParent(classNav[0]);
153+
}
154+
}
155+
156+
return true;
157+
},
158+
159+
/**
160+
* Sets 'has-active' CSS class to all parent categories which have part of provided class in childClassName
161+
*
162+
* @example
163+
* childClassName - 'nav-1-2-3'
164+
* CSS class 'has-active' will be added to categories have 'nav-1-2' and 'nav-1' classes
165+
*
166+
* @param {String} childClassName - Class name of active category <li> element
167+
* @return void
168+
* @private
169+
*/
170+
_setActiveParent: function (childClassName) {
171+
var parentElement,
172+
parentClass = childClassName.substr(0, childClassName.lastIndexOf('-'));
173+
174+
if (parentClass.lastIndexOf('-') !== -1) {
175+
parentElement = this.element.find('.' + parentClass);
176+
177+
if (parentElement) {
178+
parentElement.addClass('has-active');
179+
}
180+
this._setActiveParent(parentClass);
181+
}
182+
},
183+
184+
/**
185+
* Tries to retrieve category URL from current URL and mark this category as active
186+
* @see _setActiveMenuForCategory(url)
187+
*
188+
* @example
189+
* currentUrl - http://magento.com/category1/category12/product.html,
190+
* category URLs has extensions .phtml - http://magento.com/category1.phtml
191+
* method sets active category which has URL http://magento.com/category1/category12.phtml
192+
*
193+
* @param {String} currentUrl - current page URL without parameters
194+
* @return void
195+
* @private
196+
*/
197+
_setActiveMenuForProduct: function (currentUrl) {
198+
var categoryUrlExtension,
199+
lastUrlSection,
200+
possibleCategoryUrl,
201+
//retrieve first category URL to know what extension is used for category URLs
202+
firstCategoryUrl = this.element.find('> li a').attr('href');
203+
204+
if (firstCategoryUrl) {
205+
lastUrlSection = firstCategoryUrl.substr(firstCategoryUrl.lastIndexOf('/'));
206+
categoryUrlExtension = lastUrlSection.lastIndexOf('.') !== -1 ?
207+
lastUrlSection.substr(lastUrlSection.lastIndexOf('.')) : '';
208+
209+
possibleCategoryUrl = currentUrl.substr(0, currentUrl.lastIndexOf('/')) + categoryUrlExtension;
210+
this._setActiveMenuForCategory(possibleCategoryUrl);
211+
}
212+
},
213+
214+
/**
215+
* Add class for expanded option.
216+
*/
87217
isExpanded: function () {
88218
var subMenus = this.element.find(this.options.menus),
89219
expandedMenus = subMenus.find('ul');
90220

91221
expandedMenus.addClass('expanded');
92222
},
93223

224+
/**
225+
* @param {jQuery.Event} event
226+
* @private
227+
*/
94228
_activate: function (event) {
95229
window.location.href = this.active.find('> a').attr('href');
96230
this.collapseAll(event);
97231
},
98232

233+
/**
234+
* @param {jQuery.Event} event
235+
* @private
236+
*/
99237
_keydown: function (event) {
100-
101238
var match, prev, character, skip, regex,
102239
preventDefault = true;
103240

241+
/* eslint-disable max-depth */
242+
/**
243+
* @param {String} value
244+
*/
104245
function escape(value) {
105246
return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
106247
}

0 commit comments

Comments
 (0)