PageSourceSearch

https://wfc14.com/modules/contrib/modal_page/js/modal-page.js?t0j0y8

js wfc14.com collected 2026-10-08 12:13:38 UTC 10,367 bytes, 287 lines download raw bytes

1/**
2 * @file
3 * Default JavaScript file for Modal Page.
4 */
5
6(function ($, cookies, Drupal, drupalSettings, once) {
7  'use strict';
8
9  Drupal.behaviors.modalPage = {
10    attach: function (context, settings) {
11
12      // Get Modals to Show.
13      var modals = $('.js-modal-page-show', context);
14
15      // Verify if there is Modal.
16      if (!modals.length) {
17        return false;
18      }
19
20      // Verify if this project should load Bootstrap automatically.
21      var verify_load_bootstrap_automatically = true;
22      if (typeof settings.modal_page != 'undefined' && settings.modal_page.verify_load_bootstrap_automatically != 'undefined') {
23        verify_load_bootstrap_automatically = settings.modal_page.verify_load_bootstrap_automatically;
24      }
25
26      // If Bootstrap is automatic enable it only if its necessary.
27      if (!$.fn.modal && !window.bootstrap && verify_load_bootstrap_automatically) {
28        $.ajax({
29          url: "/modal-page/ajax/enable-bootstrap",
30          dataType: "json", // Specify expected data type of the response to be JSON.
31          success: function (result) {
32            // Check the 'success' flag in the JSON response.
33            if (result.success) {
34              location.reload();
35            } else {
36              console.error('Failed to enable Bootstrap:', result.message);
37            }
38          },
39          error: function (xhr, status, error) {
40            // Handle Ajax error
41            console.error('Ajax error occurred:', error);
42          }
43        });
44      }
45
46      // Foreach in all Modals.
47      $(modals).each(function (index) {
48
49        // Get default variables.
50        var modal = $(this);
51        var checkbox_please_do_not_show_again = $('.modal-page-please-do-not-show-again', modal);
52        var id_modal = $('#modal_id', modal).val();
53
54        var show_once = $(modal).find('#show_once').val();
55
56        var prevent_default = modal.data('modal-options').prevent_default || false;
57
58        // Remove the cookie if show only once option is disabled.
59        if (show_once != "1") {
60          cookies.remove('hide_modal_id_' + id_modal);
61        }
62
63        // Get cookies for do not show again option and show only once.
64        var hide_modal_cookie = cookies.get('hide_modal_id_' + id_modal) || cookies.get('please_do_not_show_again_modal_id_' + id_modal);
65
66        // Verify don't show again and show only once options.
67        if (hide_modal_cookie) {
68          return;
69        }
70
71        // Verify auto-open.
72        var auto_open = true;
73
74        if (typeof modal.data('modal-options').auto_open != 'undefined' && typeof modal.data('modal-options').auto_open != 'undefined') {
75          auto_open = modal.data('modal-options').auto_open;
76        }
77
78        modal.on('shown.bs.modal', function () {
79          $(this).find(".js-modal-page-ok-buttom").first().focus();
80          var auto_hide = $(modal).find('#auto_hide').val();
81          var auto_hide_delay = $(modal).find('#auto_hide_delay').val();
82          if (auto_hide == "1") {
83            setTimeout(function () {
84              $(modal).modal('hide');
85            }, auto_hide_delay * 1000);
86          }
87        });
88
89        modal.on('hide.bs.modal', function () {
90          if (show_once == "1") {
91            cookies.set('hide_modal_id_' + id_modal, true, { expires: 365 * 20, path: '/' });
92          }
93        });
94
95        modal.on('keydown', function (e) {
96          var eventKey = e.key || e.which;
97          var lastElement = $(this).find('.js-modal-page-ok-buttom').last().is(':focus');
98          var firstElement = $(this).find(".js-modal-page-ok-buttom").first().is(':focus');
99
100          if (eventKey === 'Escape' && !e.shiftKey && lastElement) {
101            e.preventDefault();
102            $(this).find(".js-modal-page-ok-buttom").first().focus();
103          } else if (eventKey === 'Escape' && e.shiftKey && firstElement) {
104            e.preventDefault();
105            $(this).find(".js-modal-page-ok-buttom").last().focus();
106          }
107        });
108
109        // Open Modal on Auto Open.
110        if (auto_open == true) {
111
112          // Verify if the modal should be trigged by height instead of time
113          var offsetHeight = $(modal).find('#height_offset').val();
114
115          if (offsetHeight) {
116
117            // If page has no scrollbar show the modal.
118            if ($(document).height() == $(window).height()) {
119              modal.modal();
120              $(document).off(namespace);
121              return;
122            }
123
124            // Namespace guarantee we are only working with events of one modal, even on the DOM document.
125            var namespace = '.' + $(modal).attr('aria-describedby');
126            // Check if is using pixels or percentage, if using percentage
127            // remove the % symbol, if using pixels convert to percentage.
128            if (!$.isNumeric(offsetHeight)) {
129              offsetHeight = offsetHeight.slice(0, -1);
130            } else {
131              offsetHeight = (offsetHeight / $(document).height()) * 100;
132            }
133            $(document).on('scroll' + namespace, function () {
134              // Account for the offset touch option
135              // 0 to start, 0.5 to center, 1 to end.
136              var offsetTouch = $(modal).find('#height_offset').attr('offset-type');
137
138              // Getting the position of the scroll on the page according to
139              // the page total size and window size.
140              var positionY = Math.round(
141                ($(window).scrollTop() / ($(document).height() - $(window)
142                  .height())) * 100
143              );
144
145              var windowPercentage = Math.round(($(window).height() / $(document).height()) * 100);
146
147              // Adjusting the scroll position by the offsetTouch.
148              positionY = positionY + (offsetTouch * windowPercentage);
149
150              if (positionY >= offsetHeight) {
151                modal.modal();
152                $(document).off(namespace);
153              }
154            });
155          } else {
156            // Verify if there is a delay to show Modal.
157            var delay = $(modal).find('#delay_display').val() * 1000;
158
159            setTimeout(function () {
160              if (drupalSettings.modal_page.bootstrap_version == "3x") {
161                modal.modal();
162              } else {
163                modal.modal('show');
164              }
165            }, delay);
166          }
167        }
168
169        // Open Modal Page clicking on "open-modal-page" class.
170        $('.open-modal-page', modal).on('click', function (e) {
171          if (prevent_default) {
172            e.preventDefault();
173          }
174          if (drupalSettings.modal_page.bootstrap_version == "3x") {
175            modal.modal();
176          } else {
177            modal.modal('show');
178          }
179        });
180
181        // Open Modal Page clicking on user custom element.
182        if (typeof modal.data('modal-options').open_modal_on_element_click != 'undefined' && modal.data('modal-options').open_modal_on_element_click) {
183
184          var link_open_modal = modal.data('modal-options').open_modal_on_element_click;
185          $(link_open_modal).on('click', function (e) {
186            if (prevent_default) {
187              e.preventDefault();
188            }
189
190            if (drupalSettings.modal_page.bootstrap_version == "3x") {
191              modal.modal();
192            } else {
193              modal.modal('show');
194            }
195          });
196        }
197
198        var ok_button = $('.js-modal-page-ok-button', modal);
199
200        ok_button.on('click', function () {
201
202          if (checkbox_please_do_not_show_again.is(':checked')) {
203            var cookieTime = $(modal).find('#cookie_expiration').val();
204            var cookieSettings = { path: '/' };
205            // If not set at all, uses an arbitraty time (never show up again)
206            if (!cookieTime) {
207              cookieSettings.expires = 10000;
208            } else {
209              // If it's 0, don't add expire date. Expire at end of session.
210              if (cookieTime > 0) {
211                cookieSettings.expires = parseInt(cookieTime);
212              }
213            }
214
215            cookies.set('please_do_not_show_again_modal_id_' + id_modal, true, cookieSettings);
216
217          }
218
219          modal.modal('hide');
220
221          var modalElement = $('.js-modal-page-ok-button').parents('#js-modal-page-show-modal');
222
223          // URL to send data.
224          var urlModalSubmit = "/modal/ajax/hook-modal-submit";
225
226          // Get Modal Options.
227          var modalOptions = modalElement.data('modal-options');
228
229          // Get Modal ID.
230          var modalId = modalOptions.id;
231
232          var dontShowAgainOption = modalElement.find('.modal-page-please-do-not-show-again').is(':checked');
233
234          var modalState = new Object();
235
236          modalState.dont_show_again_option = dontShowAgainOption;
237
238          // Params to be sent.
239          var params = new Object();
240
241          // Send Modal ID.
242          params.id = modalId;
243
244          // Send Modal State.
245          params.modal_state = modalState;
246
247          $.post(urlModalSubmit, params, function (result) {
248
249            // Check the 'success' flag in the JSON response.
250            if (result.success) {
251
252              var redirect = modalElement.find('.modal-buttons .js-modal-page-ok-button').attr('data-redirect');
253
254              if (typeof redirect != 'undefined' && redirect.length > 0) {
255                window.location.assign(redirect);
256              }
257            }
258            else {
259              console.error('Modal Ajax submit error:', result.message);
260            }
261          }, "json"); // Specify expected data type of the response to be JSON.
262        });
263      });
264    }
265  };
266  Drupal.behaviors.modalPageMaximize = {
267    attach: function (context) {
268      let modals = $('.js-modal-page-show', context);
269
270      if (!modals.length) {
271        return false;
272      }
273
274      let maximize = once('modalPageMaximize', '.js-modal-page-maximize-button', context);
275
276      maximize.forEach(function (el) {
277        $(el).on('click', function () {
278          $(el).closest('.modal', context).toggleClass('maximized-container');
279          $(el).closest('.modal-page-dialog', context).toggleClass('maximized');
280
281          $(el).find('#modal-maximize', context).toggle();
282          $(el).find('#modal-minimize', context).toggle();
283        });
284      });
285    }
286  };
287})(jQuery, window.Cookies, Drupal, drupalSettings, once);

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.