PageSourceSearch

https://www.nestlehealthscience.com.hk/themes/custom/nhsc_com/js/main-menu.js?v=11.3.17

js nestlehealthscience.com.hk collected 2026-10-06 07:59:08 UTC 14,506 bytes, 315 lines download raw bytes

1/**
2 * @file
3 * JavaScript behaviors for Main Menu integration.
4 */
5
6(function ($, Drupal) {
7  'use strict';
8  Drupal.behaviors.main_menu = {
9    attach: function (context, settings) {
10      $(once('main_menu', '.main-navigation-wrapper', context)).each(function () {
11        var $context = $(context);
12        var initial_text_back_button = $context.find('.back-menu').text();
13        var initial_text_back_button2;
14        var timeout = 0;
15        var timeout_hover = 0;
16        var url;
17
18        function isMobile() {
19          return $(window).width() <= 1240;
20        }
21
22        function getScrollbarWidth() {
23          return window.innerWidth - document.documentElement.clientWidth;
24        }
25
26        if ($context.find('.main-navigation-wrapper').length) {
27          if ($context.find('.main-navigation-wrapper').hasClass('new-style-header')) {
28            $context.find('.main-navigation-revamp').find('a[href^=http]').on('click', function (e) {
29              if(this.href.indexOf(location.hostname) == -1) {
30                e.preventDefault();
31
32                url = Drupal.checkPlain($(this).attr('href'));
33                window.open(url,'_blank');
34              }
35            });
36
37            $context.find('.navbar-toggle').on('click', function (e) {
38              e.stopImmediatePropagation();
39
40              clearTimeout(timeout);
41              $context.find('body, .top-menu').toggleClass('menu-open');
42
43              if (isMobile()) {
44                $context.find('.back-menu').removeClass('menu-open');
45              }
46
47              timeout = setTimeout(function () {
48                if (isMobile()) {
49                  $context.find('.back-menu').text(initial_text_back_button);
50                  $context.find('.field--name-field-three-category-name.clicked').removeClass('clicked');
51                  $context.find('.field--name-field-four-category-name.clicked').removeClass('clicked');
52                  $context.find('.field--name-field-four-three-category-name.clicked').removeClass('clicked');
53                  $context.find('li.we-mega-menu-li.dropdown-menu').removeClass('clicked');
54                } else {
55                  $context.find('li.we-mega-menu-li').removeClass('hovering');
56                }
57              }, 250);
58            });
59
60            if (isMobile()) {
61              $context.find('.we-mega-menu-li').on('click', function () {
62                $context.find('li.we-mega-menu-li.dropdown-menu').removeClass('active');
63              });
64            }
65
66            $context.find('li.we-mega-menu-li').on('mouseenter', function () {
67              if (!isMobile()) {
68                var $this = $(this);
69
70                clearTimeout(timeout_hover);
71
72                timeout_hover = setTimeout(function () {
73                  $context.find('li.we-mega-menu-li').removeClass('hovering');
74
75                  if ($this.hasClass('dropdown-menu')) {
76                    $context.find('body').css({'padding-right': getScrollbarWidth() + 'px'});
77                    $context.find('body').addClass('menu-open');
78                  $context.find('.field--name-field-three-category-link > a.hovering').removeClass('hovering');
79                  $context.find('.field--name-field-four-link > a.hovering').removeClass('hovering');
80                  $context.find('.field--name-field-four-three-link > a.hovering').removeClass('hovering');
81                    $context.find('.field--name-field-three-category-name.hovering').removeClass('hovering');
82                    $context.find('.field--name-field-four-category-name.hovering').removeClass('hovering');
83                    $context.find('.field--name-field-four-three-category-name.hovering').removeClass('hovering');
84                  } else {
85                    $context.find('body').removeClass('menu-open');
86                  }
87
88                  $this.addClass('hovering');
89                }, 250);
90              }
91            });
92
93            $context.find('li.we-mega-menu-li').on('mouseleave', function () {
94              if (!isMobile()) {
95
96                clearTimeout(timeout_hover);
97
98                timeout_hover = setTimeout(function () {
99                  $context.find('body').css({'padding-right': ''});
100                  $context.find('body').removeClass('menu-open');
101                  $context.find('li.we-mega-menu-li').removeClass('hovering');
102                }, 250);
103              }
104            });
105
106            $context.find('li.we-mega-menu-li.dropdown-menu > a').on('click', function (e) {
107              if (isMobile()) {
108                e.preventDefault();
109
110                $context.find('li.we-mega-menu-li.dropdown-menu').removeClass('clicked');
111                $(this).parent().addClass('clicked');
112                $context.find('.back-menu').addClass('menu-open');
113              }
114            });
115
116            $context.find('.field--name-field-three-category-link').on('mouseenter', function () {
117              if (!isMobile()) {
118                $context.find('.field--name-field-three-category-link > a.hovering').removeClass('hovering');
119                $context.find('.field--name-field-three-category-name.hovering').removeClass('hovering');
120                $context.find('.field--name-field-four-category-name.hovering').removeClass('hovering');
121                $context.find('.field--name-field-four-three-category-name.hovering').removeClass('hovering');
122                $(this).find('> a').addClass('hovering');
123              }
124            });
125
126            $context.find('.field--name-field-four-link').on('mouseenter', function () {
127              if (!isMobile()) {
128                $context.find('.field--name-field-four-link > a.hovering').removeClass('hovering');
129                $context.find('.field--name-field-four-category-name.hovering').removeClass('hovering');
130                $context.find('.field--name-field-four-three-category-name.hovering').removeClass('hovering');
131                $(this).find('> a').addClass('hovering');
132              }
133            });
134
135            $context.find('.field--name-field-four-three-link').on('mouseenter', function () {
136              if (!isMobile()) {
137                $context.find('.field--name-field-four-three-link > a.hovering').removeClass('hovering');
138                $context.find('.field--name-field-four-three-category-name.hovering').removeClass('hovering');
139                $(this).find('> a').addClass('hovering');
140              }
141            });
142
143            $context.find('.field--name-field-three-category-name').on('mouseenter', function () {
144              if (!isMobile()) {
145                $context.find('.field--name-field-three-category-link > a.hovering').removeClass('hovering');
146                $context.find('.field--name-field-three-category-name.hovering').removeClass('hovering');
147                $context.find('.field--name-field-four-category-name.hovering').removeClass('hovering');
148                $context.find('.field--name-field-four-three-category-name.hovering').removeClass('hovering');
149                $(this).addClass('hovering');
150              }
151            });
152
153            $context.find('.field--name-field-four-category-name').on('mouseenter', function () {
154              if (!isMobile()) {
155                $context.find('.field--name-field-four-link > a.hovering').removeClass('hovering');
156                $context.find('.field--name-field-four-category-name.hovering').removeClass('hovering');
157                $context.find('.field--name-field-four-three-category-name.hovering').removeClass('hovering');
158                $(this).addClass('hovering');
159              }
160            });
161
162            $context.find('.field--name-field-four-three-category-name').on('mouseenter', function () {
163              if (!isMobile()) {
164                $context.find('.field--name-field-four-three-link > a.hovering').removeClass('hovering');
165                $context.find('.field--name-field-four-three-category-name.hovering').removeClass('hovering');
166                $(this).addClass('hovering');
167              }
168            });
169
170            $context.find('.field--name-field-three-category-name, .field--name-field-four-category-name, .field--name-field-four-three-category-name').on('click', function () {
171              if (isMobile()) {
172                $(this).addClass('clicked');
173                $context.find('.back-menu').text($(this).text());
174
175                if ($(this).hasClass('field--name-field-four-category-name')) {
176                  initial_text_back_button2 = $(this).text();
177                }
178              }
179            });
180
181            $context.find('.back-menu').on('click', function () {
182              if ($context.find('.field--name-field-four-three-category-name.clicked').length) {
183                $context.find('.field--name-field-four-three-category-name.clicked').removeClass('clicked');
184                $context.find('.back-menu').text(initial_text_back_button2);
185              } else if ($context.find('.field--name-field-four-category-name.clicked').length) {
186                $context.find('.field--name-field-four-category-name.clicked').removeClass('clicked');
187                $context.find('.back-menu').text(initial_text_back_button);
188              } else if ($context.find('.field--name-field-three-category-name.clicked').length) {
189                $context.find('.field--name-field-three-category-name.clicked').removeClass('clicked');
190                $context.find('.back-menu').text(initial_text_back_button);
191              } else if ($context.find('.we-mega-menu-li.clicked').length) {
192                $context.find('.we-mega-menu-li.clicked').removeClass('clicked');
193                $(this).removeClass('menu-open');
194              }
195            });
196
197            $context.find('.main-navigation-wrapper .we-mega-menu-submenu a').on('click', function () {
198              if (typeof $(this).attr('href') !== 'undefined' && $(this).attr('href').indexOf('#') !== -1) {
199                $context.find('body, .top-menu').removeClass('menu-open');
200
201                if (isMobile()) {
202                  $context.find('.back-menu').removeClass('menu-open');
203                }
204
205                timeout = setTimeout(function () {
206                  if (isMobile()) {
207                    $context.find('li.we-mega-menu-li.dropdown-menu').removeClass('clicked');
208                    $context.find('.field--name-field-three-category-name.clicked').removeClass('clicked');
209                    $context.find('.back-menu').text(initial_text_back_button);
210                  } else {
211                    $context.find('li.we-mega-menu-li').removeClass('hovering');
212                  }
213                }, 250);
214              }
215            });
216          } else if ($context.find('.main-navigation-wrapper').hasClass('old-style-header')) {
217            $('body').addClass('old-style-header');
218            // Open main menu and set active indicator
219            if ($context.find('li.we-mega-menu-li.active').length === 0) {
220              $context.find('li.we-mega-menu-li').first().addClass('active');
221            }
222
223            $context.find('#navbarToggle').click(function (e) {
224              e.stopImmediatePropagation();
225              if (!$('body').hasClass("menu-open")) {
226                $context.find('li.we-mega-menu-li').removeClass('hovering');
227              }
228
229              const isMobile = window.matchMedia("only screen and (max-width: 768px)").matches;
230
231              if (isMobile) {
232                $context.find(".close-sub-menu").on("click", function () {
233                  $context.find('li.we-mega-menu-li').removeClass('active');
234                });
235
236                $context.find(".we-mega-menu-li").on("click", function () {
237                  $context.find('li.we-mega-menu-li').removeClass('active');
238                });
239              }
240
241              $('body').toggleClass('menu-open');
242              var activeItem = $context.find('li.we-mega-menu-li.active')[0];
243              if (typeof activeItem !== 'undefined') {
244                const {
245                  left,
246                  top,
247                  width,
248                  height
249                } = activeItem.getBoundingClientRect();
250                const position = (left + width / 2) + window.pageXOffset;
251                $context.find('.indicator').css('left', `${position}px`);
252              }
253            });
254
255            $context.find('#navbarToggleMobile').off().on('click', function () {
256              $('body').toggleClass('menu-open');
257            });
258
259            // Add hover class and move indicator
260            $context.find("li.we-mega-menu-li").on("mouseenter", function () {
261              $context.find('li.we-mega-menu-li').removeClass('hovering');
262              $(this).addClass('hovering');
263              const {
264                left,
265                top,
266                width,
267                height
268              } = this.getBoundingClientRect();
269              const position = (left + width / 2) + window.pageXOffset;
270              $context.find('.indicator').css('left', `${position}px`)
271            });
272
273
274            // device detection
275            $context.find('li.we-mega-menu-li > a').on("click", function () {
276              let isMobile = window.matchMedia("only screen and (max-width: 760px)").matches;
277
278              if (isMobile) {
279                $context.find('li.we-mega-menu-li').removeClass('clicked');
280                $(this).parent().addClass('clicked');
281                return false;
282              } else {
283                return true;
284              }
285            });
286
287            $context.find('li.we-mega-menu-li > a').on("mouseenter", function () {
288              $(this).click();
289            });
290
291
292            $context.find('.close-sub-menu').click(function () {
293              $context.find('li.we-mega-menu-li').removeClass('clicked');
294            });
295
296            // Equal height for dropdown menu
297            var submenuHeight = 0;
298            $context.find(".we-mega-menu-submenu").each(function () {
299              if ($(this).height() > submenuHeight) {
300                submenuHeight = $(this).height();
301              }
302            });
303            $context.find(".we-mega-menu-submenu").height(submenuHeight);
304
305            $context.find('.main-navigation-wrapper .we-mega-menu-submenu a[href*=\\#]').on('click', function (event) {
306              if (this.pathname === window.location.pathname) {
307                $('body').removeClass('menu-open');
308              }
309            });
310          }
311        }
312      });
313    }
314  }
315})(jQuery, Drupal);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.