2
2
* Copyright © Magento, Inc. All rights reserved.
3
3
* See COPYING.txt for license details.
4
4
*/
5
+
5
6
define ( [
6
7
"jquery" ,
7
8
"matchMedia" ,
@@ -18,8 +19,13 @@ define([
18
19
options : {
19
20
responsive : false ,
20
21
expanded : false ,
21
- delay : 300
22
+ delay : 300 ,
23
+ mediaBreakpoint : '(max-width: 768px)'
22
24
} ,
25
+
26
+ /**
27
+ * @private
28
+ */
23
29
_create : function ( ) {
24
30
var self = this ;
25
31
@@ -29,6 +35,9 @@ define([
29
35
} ) ;
30
36
} ,
31
37
38
+ /**
39
+ * @private
40
+ */
32
41
_init : function ( ) {
33
42
this . _super ( ) ;
34
43
this . delay = this . options . delay ;
@@ -39,7 +48,7 @@ define([
39
48
40
49
if ( this . options . responsive === true ) {
41
50
mediaCheck ( {
42
- media : '(max-width: 640px)' ,
51
+ media : this . options . mediaBreakpoint ,
43
52
entry : $ . proxy ( function ( ) {
44
53
this . _toggleMobileMode ( ) ;
45
54
} , this ) ,
@@ -50,8 +59,13 @@ define([
50
59
}
51
60
52
61
this . _assignControls ( ) . _listen ( ) ;
62
+ this . _setActiveMenu ( ) ;
53
63
} ,
54
64
65
+ /**
66
+ * @return {Object }
67
+ * @private
68
+ */
55
69
_assignControls : function ( ) {
56
70
this . controls = {
57
71
toggleBtn : $ ( '[data-action="toggle-nav"]' ) ,
@@ -61,14 +75,24 @@ define([
61
75
return this ;
62
76
} ,
63
77
78
+ /**
79
+ * @private
80
+ */
64
81
_listen : function ( ) {
65
- var controls = this . controls ;
66
- var toggle = this . toggle ;
82
+ var controls = this . controls ,
83
+ toggle = this . toggle ;
67
84
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
+ } ) ;
70
91
} ,
71
92
93
+ /**
94
+ * Toggle.
95
+ */
72
96
toggle : function ( ) {
73
97
if ( $ ( 'html' ) . hasClass ( 'nav-open' ) ) {
74
98
$ ( 'html' ) . removeClass ( 'nav-open' ) ;
@@ -83,24 +107,142 @@ define([
83
107
}
84
108
} ,
85
109
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 ( / ( n a v \- ) [ 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
+ */
87
218
isExpanded : function ( ) {
88
219
var subMenus = this . element . find ( this . options . menus ) ,
89
220
expandedMenus = subMenus . find ( 'ul' ) ;
90
221
91
222
expandedMenus . addClass ( 'expanded' ) ;
92
223
} ,
93
224
225
+ /**
226
+ * @param {jQuery.Event } event
227
+ * @private
228
+ */
94
229
_activate : function ( event ) {
95
230
window . location . href = this . active . find ( '> a' ) . attr ( 'href' ) ;
96
231
this . collapseAll ( event ) ;
97
232
} ,
98
233
234
+ /**
235
+ * @param {jQuery.Event } event
236
+ * @private
237
+ */
99
238
_keydown : function ( event ) {
100
-
101
239
var match , prev , character , skip , regex ,
102
240
preventDefault = true ;
103
241
242
+ /* eslint-disable max-depth */
243
+ /**
244
+ * @param {String } value
245
+ */
104
246
function escape ( value ) {
105
247
return value . replace ( / [ \- \[ \] { } ( ) * + ? . , \\ \^ $ | # \s ] / g, "\\$&" ) ;
106
248
}
0 commit comments