@@ -16,7 +16,6 @@ define([
16
16
*/
17
17
$ . widget ( 'mage.menu' , $ . ui . menu , {
18
18
options : {
19
- useCategoryPathInUrl : false ,
20
19
categoryLayoutClass : 'catalog-product-view' ,
21
20
responsive : false ,
22
21
expanded : false ,
@@ -200,61 +199,56 @@ define([
200
199
/**
201
200
* Determines if the current page is a product page.
202
201
* It checks the catalog product view related class in the body tag of the document.
203
- * The result is cached after the first check to optimize performance for subsequent calls.
204
202
*
205
203
* @return {Boolean } True if the current page is a product page, false otherwise.
206
204
* @private
207
205
*/
208
206
_isProductPage : function ( ) {
209
- // Cache the result if called multiple times
210
- if ( this . _cachedIsProductPage === undefined ) {
211
- this . _cachedIsProductPage = document . body . classList . contains ( this . options . categoryLayoutClass ) ;
212
- }
213
- return this . _cachedIsProductPage ;
207
+ return document . body . classList . contains ( this . options . categoryLayoutClass ) ;
214
208
} ,
215
209
216
210
/**
217
- * Sets the active state in the menu for a product page.
218
- * It first tries to determine the category URL based on the referrer or the current URL.
219
- * If 'useCategoryPathInUrl' is enabled, it uses the current URL and appends the dynamically determined category URL suffix.
220
- * Otherwise, it uses the referrer URL or the current URL with the dynamically determined category URL suffix.
221
- * If a valid category URL is found, it sets the corresponding category as active in the menu.
222
- * If no valid category URL is found, it clears the active state.
211
+ * Sets the active state in the menu for a product page. Determines the category URL from either
212
+ * the referrer URL or the current URL, using the URL extension to identify the category.
213
+ * Sets the corresponding category as active in the menu if a valid category URL is found.
214
+ * Clears the active state if no valid category URL is found or if it's not a product page.
223
215
*
224
- * @param {String } currentUrl - current page URL without parameters
225
- * @return {Boolean }
216
+ * @param {String } currentUrl - The current page URL without parameters.
217
+ * @return {Boolean } - True if a valid category is set, false otherwise.
226
218
* @private
227
219
*/
228
220
_setActiveMenuForProduct : function ( currentUrl ) {
229
221
var firstCategoryUrl = this . element . find ( '> li a' ) . attr ( 'href' ) ;
230
222
231
- // If no category URL is found in the menu, return false .
223
+ // Return false if no category URL is found in the menu.
232
224
if ( ! firstCategoryUrl ) {
225
+ this . _clearActiveState ( ) ;
233
226
return false ;
234
227
}
235
228
236
229
var categoryUrlExtension = this . _getUrlExtension ( firstCategoryUrl ) ;
237
230
var categoryUrl ;
231
+ var isProductPage = this . _isProductPage ( ) ;
238
232
239
- if ( this . options . useCategoryPathInUrl ) {
240
- // Derive the category URL from the current URL and append the dynamically determined URL extension
241
- categoryUrl = currentUrl . substring ( 0 , currentUrl . lastIndexOf ( '/' ) ) + categoryUrlExtension ;
242
- } else {
243
- var referrer = new URL ( document . referrer ) ;
244
- var isProductPage = this . _isProductPage ( ) ;
233
+ if ( isProductPage ) {
234
+ var currentHostname = window . location . hostname ;
245
235
246
- if ( referrer . hostname === window . location . hostname && referrer . pathname . endsWith ( categoryUrlExtension ) && isProductPage ) {
247
- categoryUrl = referrer . href . split ( '?' ) [ 0 ] ;
248
- } else if ( isProductPage ) {
249
- categoryUrl = currentUrl . substring ( 0 , currentUrl . lastIndexOf ( '/' ) ) + categoryUrlExtension ;
236
+ // Check if the referrer's hostname matches the current hostname
237
+ // and if the referrer's pathname ends with the category URL extension
238
+ if ( document . referrer . includes ( currentHostname ) && document . referrer . endsWith ( categoryUrlExtension ) ) {
239
+ categoryUrl = document . referrer . split ( '?' ) [ 0 ] ;
250
240
} else {
251
- this . _clearActiveState ( ) ;
252
- return false ;
241
+ // Fallback to using the current URL
242
+ categoryUrl = currentUrl . substring ( 0 , currentUrl . lastIndexOf ( '/' ) ) + categoryUrlExtension ;
253
243
}
244
+
245
+ this . _setActiveMenuForCategory ( categoryUrl ) ;
246
+ return true ;
254
247
}
255
248
256
- this . _setActiveMenuForCategory ( categoryUrl ) ;
257
- return true ;
249
+ // Clear active state if not a product page
250
+ this . _clearActiveState ( ) ;
251
+ return false ;
258
252
} ,
259
253
260
254
/**
0 commit comments