@@ -59,7 +59,6 @@ define([
59
59
}
60
60
61
61
this . _assignControls ( ) . _listen ( ) ;
62
-
63
62
this . _setActiveMenu ( ) ;
64
63
} ,
65
64
@@ -111,48 +110,105 @@ define([
111
110
} ,
112
111
113
112
/**
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
116
116
*
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.
118
120
*
119
121
* @return void
122
+ * @private
120
123
*/
121
124
_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 ) ;
134
129
}
135
130
} ,
136
131
137
132
/**
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.
139
135
*
136
+ * @param {String } url - possible category URL
137
+ * @returns {boolean } - true if active category was founded by provided URL, otherwise return false
140
138
* @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 ( / ( n a v \- ) [ 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
141
167
*
142
168
* @param {String } childClassName - Class name of active category <li> element
143
169
* @return void
170
+ * @private
144
171
*/
145
172
_setActiveParent : function ( childClassName ) {
146
- var parentClass = childClassName . substr ( 0 , childClassName . lastIndexOf ( '-' ) ) ;
173
+ var parentElement ,
174
+ parentClass = childClassName . substr ( 0 , childClassName . lastIndexOf ( '-' ) ) ;
175
+
147
176
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' ) ;
151
181
}
152
182
this . _setActiveParent ( parentClass ) ;
153
183
}
154
184
} ,
155
185
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
+
156
212
/**
157
213
* Add class for expanded option.
158
214
*/
0 commit comments