Skip to content

Commit 4d9be10

Browse files
committed
MAGETWO-49796: Catalog top nav, CSS class not set to active when using Varnish
- cover case for product view page
1 parent 80811fc commit 4d9be10

File tree

1 file changed

+77
-21
lines changed

1 file changed

+77
-21
lines changed

lib/web/mage/menu.js

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ define([
5959
}
6060

6161
this._assignControls()._listen();
62-
6362
this._setActiveMenu();
6463
},
6564

@@ -111,48 +110,105 @@ define([
111110
},
112111

113112
/**
114-
* Sets 'active' CSS class to menu item for active category if it was not set before.
115-
* If menu item has parent categories, sets 'has-active' class to all af them
113+
* Tries to figure out the active category for current page and add appropriate classes:
114+
* - 'active' class for active category
115+
* - 'has-active' class for all parents of active category
116116
*
117-
* @private
117+
* First, checks whether current URL is URL of category page,
118+
* otherwise tries to retrieve category URL in case of current URL is product view page URL
119+
* which has category tree path in it.
118120
*
119121
* @return void
122+
* @private
120123
*/
121124
_setActiveMenu: function () {
122-
var currentUrl = window.location.href.split('?')[0],
123-
activeCategoryLink = this.element.find('a[href="' + currentUrl + '"]');
124-
125-
if (activeCategoryLink && activeCategoryLink.parent().hasClass('active') !== true
126-
&& activeCategoryLink.hasClass('ui-corner-all')
127-
) {
128-
activeCategoryLink.parent().addClass('active');
129-
var classes = activeCategoryLink.parent().attr('class'),
130-
classStartPosition = classes.indexOf('nav-'),
131-
classNav = classes.substring(classStartPosition, classes.indexOf(' ', classStartPosition));
132-
133-
this._setActiveParent(classNav);
125+
var currentUrl = window.location.href.split('?')[0];
126+
127+
if (!this._setActiveMenuForCategory(currentUrl)) {
128+
this._setActiveMenuForProduct(currentUrl);
134129
}
135130
},
136131

137132
/**
138-
* Sets 'has-active' CSS class to all parent categories in menu has active category
133+
* Looks for category with provided URL and adds 'active' CSS class to it if it was not set before.
134+
* If menu item has parent categories, sets 'has-active' class to all af them.
139135
*
136+
* @param {String} url - possible category URL
137+
* @returns {boolean} - true if active category was founded by provided URL, otherwise return false
140138
* @private
139+
*/
140+
_setActiveMenuForCategory: function (url) {
141+
var activeCategoryLink = this.element.find('a[href="' + url + '"]'),
142+
classes,
143+
classNav;
144+
145+
if (activeCategoryLink && activeCategoryLink.hasClass('ui-corner-all')) {
146+
147+
if (!activeCategoryLink.parent().hasClass('active')) {
148+
activeCategoryLink.parent().addClass('active');
149+
classes = activeCategoryLink.parent().attr('class');
150+
classNav = classes.match(/(nav\-)[0-9]+(\-[0-9]+)+/gi);
151+
152+
if (classNav) {
153+
this._setActiveParent(classNav[0]);
154+
}
155+
}
156+
return true;
157+
}
158+
return false;
159+
},
160+
161+
/**
162+
* Sets 'has-active' CSS class to all parent categories which have part of provided class in childClassName
163+
*
164+
* @example:
165+
* childClassName - 'nav-1-2-3'
166+
* CSS class 'has-active' will be added to categories have 'nav-1-2' and 'nav-1' classes
141167
*
142168
* @param {String} childClassName - Class name of active category <li> element
143169
* @return void
170+
* @private
144171
*/
145172
_setActiveParent: function (childClassName) {
146-
var parentClass = childClassName.substr(0, childClassName.lastIndexOf('-'));
173+
var parentElement,
174+
parentClass = childClassName.substr(0, childClassName.lastIndexOf('-'));
175+
147176
if (parentClass.lastIndexOf('-') !== -1) {
148-
var parent = this.element.find('.' + parentClass);
149-
if (parent) {
150-
parent.addClass('has-active');
177+
parentElement = this.element.find('.' + parentClass);
178+
179+
if (parentElement) {
180+
parentElement.addClass('has-active');
151181
}
152182
this._setActiveParent(parentClass);
153183
}
154184
},
155185

186+
/**
187+
* Tries to retrieve category URL from current URL and mark this category as active
188+
* @see _setActiveMenuForCategory(url)
189+
*
190+
* @example:
191+
* currentUrl - http://magento.com/category1/category12/product.html,
192+
* category URLs has extensions .phtml - http://magento.com/category1.phtml
193+
* method sets active category which has URL http://magento.com/category1/category12.phtml
194+
*
195+
* @param {String} currentUrl - current page URL without parameters
196+
* @return void
197+
* @private
198+
*/
199+
_setActiveMenuForProduct: function (currentUrl) {
200+
var categoryUrlExtension,
201+
possibleCategoryUrl,
202+
//retrieve first category URL to know what extension is used for category URLs
203+
firstCategoryUrl = this.element.find('> li a').attr('href');
204+
205+
if (firstCategoryUrl) {
206+
categoryUrlExtension = firstCategoryUrl.substr(firstCategoryUrl.lastIndexOf('.'));
207+
possibleCategoryUrl = currentUrl.substr(0, currentUrl.lastIndexOf('/')) + categoryUrlExtension;
208+
this._setActiveMenuForCategory(possibleCategoryUrl);
209+
}
210+
},
211+
156212
/**
157213
* Add class for expanded option.
158214
*/

0 commit comments

Comments
 (0)