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