@@ -59,6 +59,7 @@ define([
59
59
}
60
60
61
61
this . _assignControls ( ) . _listen ( ) ;
62
+ this . _setActiveMenu ( ) ;
62
63
} ,
63
64
64
65
/**
@@ -108,6 +109,111 @@ define([
108
109
}
109
110
} ,
110
111
112
+ /**
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
+ *
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.
120
+ *
121
+ * @return void
122
+ * @private
123
+ */
124
+ _setActiveMenu : function ( ) {
125
+ var currentUrl = window . location . href . split ( '?' ) [ 0 ] ;
126
+
127
+ if ( ! this . _setActiveMenuForCategory ( currentUrl ) ) {
128
+ this . _setActiveMenuForProduct ( currentUrl ) ;
129
+ }
130
+ } ,
131
+
132
+ /**
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.
135
+ *
136
+ * @param {String } url - possible category URL
137
+ * @returns {Boolean } - true if active category was founded by provided URL, otherwise return false
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
+ //category was not found by provided URL
148
+ return false ;
149
+ } else if ( ! activeCategoryLink . parent ( ) . hasClass ( 'active' ) ) {
150
+ activeCategoryLink . parent ( ) . addClass ( 'active' ) ;
151
+ classes = activeCategoryLink . parent ( ) . attr ( 'class' ) ;
152
+ classNav = classes . match ( / ( n a v \- ) [ 0 - 9 ] + ( \- [ 0 - 9 ] + ) + / gi) ;
153
+
154
+ if ( classNav ) {
155
+ this . _setActiveParent ( classNav [ 0 ] ) ;
156
+ }
157
+ }
158
+
159
+ return true ;
160
+ } ,
161
+
162
+ /**
163
+ * Sets 'has-active' CSS class to all parent categories which have part of provided class in childClassName
164
+ *
165
+ * @example
166
+ * childClassName - 'nav-1-2-3'
167
+ * CSS class 'has-active' will be added to categories have 'nav-1-2' and 'nav-1' classes
168
+ *
169
+ * @param {String } childClassName - Class name of active category <li> element
170
+ * @return void
171
+ * @private
172
+ */
173
+ _setActiveParent : function ( childClassName ) {
174
+ var parentElement ,
175
+ parentClass = childClassName . substr ( 0 , childClassName . lastIndexOf ( '-' ) ) ;
176
+
177
+ if ( parentClass . lastIndexOf ( '-' ) !== - 1 ) {
178
+ parentElement = this . element . find ( '.' + parentClass ) ;
179
+
180
+ if ( parentElement ) {
181
+ parentElement . addClass ( 'has-active' ) ;
182
+ }
183
+ this . _setActiveParent ( parentClass ) ;
184
+ }
185
+ } ,
186
+
187
+ /**
188
+ * Tries to retrieve category URL from current URL and mark this category as active
189
+ * @see _setActiveMenuForCategory(url)
190
+ *
191
+ * @example
192
+ * currentUrl - http://magento.com/category1/category12/product.html,
193
+ * category URLs has extensions .phtml - http://magento.com/category1.phtml
194
+ * method sets active category which has URL http://magento.com/category1/category12.phtml
195
+ *
196
+ * @param {String } currentUrl - current page URL without parameters
197
+ * @return void
198
+ * @private
199
+ */
200
+ _setActiveMenuForProduct : function ( currentUrl ) {
201
+ var categoryUrlExtension ,
202
+ lastUrlSection ,
203
+ possibleCategoryUrl ,
204
+ //retrieve first category URL to know what extension is used for category URLs
205
+ firstCategoryUrl = this . element . find ( '> li a' ) . attr ( 'href' ) ;
206
+
207
+ if ( firstCategoryUrl ) {
208
+ lastUrlSection = firstCategoryUrl . substr ( firstCategoryUrl . lastIndexOf ( '/' ) ) ;
209
+ categoryUrlExtension = ( lastUrlSection . lastIndexOf ( '.' ) !== - 1 )
210
+ ? lastUrlSection . substr ( lastUrlSection . lastIndexOf ( '.' ) ) : '' ;
211
+
212
+ possibleCategoryUrl = currentUrl . substr ( 0 , currentUrl . lastIndexOf ( '/' ) ) + categoryUrlExtension ;
213
+ this . _setActiveMenuForCategory ( possibleCategoryUrl ) ;
214
+ }
215
+ } ,
216
+
111
217
/**
112
218
* Add class for expanded option.
113
219
*/
0 commit comments