PageSourceSearch

https://indiacinehub.gov.in/modules/contrib/modal_page/js/modal-page.js?tm2lgj

js indiacinehub.gov.in collected 2026-10-02 17:37:04 UTC 9,149 bytes, 254 lines download raw bytes

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

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.