@@ -122,7 +122,7 @@ define([
122
122
var currentUrl = window . location . href . split ( '?' ) [ 0 ] ;
123
123
124
124
if ( ! this . _setActiveMenuForCategory ( currentUrl ) ) {
125
- this . _setActiveMenuForProduct ( currentUrl ) ;
125
+ this . _setActiveMenuForProduct ( ) ;
126
126
}
127
127
} ,
128
128
@@ -135,6 +135,9 @@ define([
135
135
* @private
136
136
*/
137
137
_setActiveMenuForCategory : function ( url ) {
138
+ // Clear active state from all categories
139
+ this . _clearActiveState ( ) ;
140
+
138
141
var activeCategoryLink = this . element . find ( 'a[href="' + url + '"]' ) ,
139
142
classes ,
140
143
classNav ;
@@ -156,6 +159,19 @@ define([
156
159
return true ;
157
160
} ,
158
161
162
+ /**
163
+ * Clears the active state from all menu items within the navigation element.
164
+ * It removes 'active' and 'has-active' classes from all list items (li elements),
165
+ * which are used to indicate the currently selected or parent of a selected item.
166
+ * This method is typically used to reset the menu's state before applying new active states.
167
+ *
168
+ * @return void
169
+ * @private
170
+ */
171
+ _clearActiveState : function ( ) {
172
+ this . element . find ( 'li' ) . removeClass ( 'active has-active' ) ;
173
+ } ,
174
+
159
175
/**
160
176
* Sets 'has-active' CSS class to all parent categories which have part of provided class in childClassName
161
177
*
@@ -182,33 +198,46 @@ define([
182
198
} ,
183
199
184
200
/**
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
201
+ * Activates the category in the menu corresponding to the current product page.
202
+ * It resolves the category URL based on the referrer (if it's from the same domain),
203
+ * and then sets this category as active in the menu.
204
+ * If no suitable referrer URL is found, no category is set as active.
192
205
*
193
- * @param {String } currentUrl - current page URL without parameters
194
206
* @return void
195
207
* @private
196
208
*/
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 ) ;
209
+ _setActiveMenuForProduct : function ( ) {
210
+ var categoryUrl = this . _resolveCategoryUrl ( ) ;
211
+
212
+ if ( categoryUrl ) {
213
+ this . _setActiveMenuForCategory ( categoryUrl ) ;
214
+ }
215
+ } ,
216
+
217
+ /**
218
+ * Resolves the category URL based on the referrer URL.
219
+ * Checks if the referrer URL belongs to the same domain and is a likely category page.
220
+ * If so, returns the referrer URL after stripping any query parameters.
221
+ * Returns null if no suitable referrer is found or if it cannot be determined to be a category page.
222
+ *
223
+ * @return {String|null } The URL of the category, or null if it cannot be determined.
224
+ * @private
225
+ */
226
+ _resolveCategoryUrl : function ( ) {
227
+ var categoryUrl = document . referrer ;
228
+
229
+ // Ensure the referrer is from the same domain
230
+ if ( categoryUrl && new URL ( categoryUrl ) . hostname === window . location . hostname ) {
231
+ // Remove any query parameters from the referrer URL
232
+ var queryParamIndex = categoryUrl . indexOf ( '?' ) ;
233
+ if ( queryParamIndex > 0 ) {
234
+ categoryUrl = categoryUrl . substring ( 0 , queryParamIndex ) ;
235
+ }
236
+ return categoryUrl ;
211
237
}
238
+
239
+ // Fallback or default behavior if no suitable referrer is found
240
+ return null ;
212
241
} ,
213
242
214
243
/**
0 commit comments