Skip to content

Commit 6d7d6a5

Browse files
committed
Merge branch 'MAGETWO-71701' into 2.1.13-PR-0.5
2 parents bd50538 + 116acd0 commit 6d7d6a5

File tree

1 file changed

+150
-8
lines changed

1 file changed

+150
-8
lines changed

lib/web/mage/menu.js

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

@@ -29,6 +35,9 @@ define([
2935
});
3036
},
3137

38+
/**
39+
* @private
40+
*/
3241
_init: function () {
3342
this._super();
3443
this.delay = this.options.delay;
@@ -39,7 +48,7 @@ define([
3948

4049
if (this.options.responsive === true) {
4150
mediaCheck({
42-
media: '(max-width: 640px)',
51+
media: this.options.mediaBreakpoint,
4352
entry: $.proxy(function () {
4453
this._toggleMobileMode();
4554
}, this),
@@ -50,8 +59,13 @@ define([
5059
}
5160

5261
this._assignControls()._listen();
62+
this._setActiveMenu();
5363
},
5464

65+
/**
66+
* @return {Object}
67+
* @private
68+
*/
5569
_assignControls: function () {
5670
this.controls = {
5771
toggleBtn: $('[data-action="toggle-nav"]'),
@@ -61,14 +75,24 @@ define([
6175
return this;
6276
},
6377

78+
/**
79+
* @private
80+
*/
6481
_listen: function () {
65-
var controls = this.controls;
66-
var toggle = this.toggle;
82+
var controls = this.controls,
83+
toggle = this.toggle;
6784

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

93+
/**
94+
* Toggle.
95+
*/
7296
toggle: function () {
7397
if ($('html').hasClass('nav-open')) {
7498
$('html').removeClass('nav-open');
@@ -83,24 +107,142 @@ define([
83107
}
84108
},
85109

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

91222
expandedMenus.addClass('expanded');
92223
},
93224

225+
/**
226+
* @param {jQuery.Event} event
227+
* @private
228+
*/
94229
_activate: function (event) {
95230
window.location.href = this.active.find('> a').attr('href');
96231
this.collapseAll(event);
97232
},
98233

234+
/**
235+
* @param {jQuery.Event} event
236+
* @private
237+
*/
99238
_keydown: function (event) {
100-
101239
var match, prev, character, skip, regex,
102240
preventDefault = true;
103241

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

0 commit comments

Comments
 (0)