PageSourceSearch

https://megaom05.ru/bitrix/templates/aspro_mshop/js/jqModal.js

js megaom05.ru collected 2026-10-04 00:12:53 UTC 13,122 bytes, 363 lines download raw bytes

1/*
2 * jqModal - Minimalist Modaling with jQuery
3 *
4 * Copyright (c) 2007-2014 Brice Burgess @iceburg_net
5 * Dual licensed under the MIT and GPL licenses:
6 *   http://www.opensource.org/licenses/mit-license.php
7 *   http://www.gnu.org/licenses/gpl.html
8 *
9 * $Version: 1.0.2 (2014.04.10 +r19)
10 * Requires: jQuery 1.2.3+
11 */
12
13(function ($) {
14    /**
15     * Initialize a set of elements as "modals". Modals typically are popup dialogs,
16     * notices, modal windows, &c.
17     *
18     * @name jqm
19     * @param options user defined options, augments defaults.
20     * @type jQuery
21     * @cat Plugins/jqModal
22     */
23  
24    $.fn.jqm = function (options) {
25      var o = $.extend({}, $.jqm.params, options);
26  
27      return this.each(function () {
28        var e = $(this),
29          jqm = $(this).data("jqm");
30  
31        if (!jqm) jqm = { ID: I++ };
32  
33        // add/extend options to modal and mark as initialized
34        e.data("jqm", $.extend(o, jqm)).addClass("jqm-init");
35  
36        // ... Attach events to trigger showing of this modal
37        o.trigger && $(this).jqmAddTrigger(o.trigger);
38      });
39    };
40  
41    /**
42     * Matching modals will have their jqmShow() method fired by attaching a
43     *   onClick event to elements matching `trigger`.
44     *
45     * @name jqmAddTrigger
46     * @param trigger a selector String, jQuery collection of elements, or a DOM element.
47     */
48    $.fn.jqmAddTrigger = function (trigger) {
49      return this.each(function () {
50        if (!addTrigger($(this), "jqmShow", trigger)) err("jqmAddTrigger must be called on initialized modals");
51      });
52    };
53  
54    /**
55     * Matching modals will have their jqmHide() method fired by attaching an
56     *   onClick event to elements matching `trigger`.
57     *
58     * @name jqmAddClose
59     * @param trigger a selector String, jQuery collection of elements, or a DOM element.
60     */
61    $.fn.jqmAddClose = function (trigger) {
62      return this.each(function () {
63        if (!addTrigger($(this), "jqmHide", trigger)) err("jqmAddClose must be called on initialized modals");
64      });
65    };
66  
67    /**
68     * Open matching modals (if not shown)
69     */
70    $.fn.jqmShow = function (trigger) {
71      return this.each(function () {
72        !this._jqmShown && show($(this), trigger);
73      });
74    };
75  
76    /**
77     * Close matching modals
78     */
79    $.fn.jqmHide = function (trigger) {
80      return this.each(function () {
81        this._jqmShown && hide($(this), trigger);
82      });
83    };
84  
85    // utility functions
86  
87    var err = function (msg) {
88        if (window.console && window.console.error) window.console.error(msg);
89      },
90      show = function (e, t) {
91        /**
92         * e = modal element (as jQuery object)
93         * t = triggering element
94         *
95         * o = options
96         * z = z-index of modal
97         * v = overlay element (as jQuery object)
98         * h = hash (for jqModal <= r15 compatibility)
99         */
100  
101        var o = e.data("jqm"),
102          t = t || window.event,
103          z = parseInt(e.css("z-index"));
104  
105        if (o.noOverlay === undefined || (o.noOverlay !== undefined && !o.noOverlay)) {
106          if (isNaN(z)) {
107            if (window["lastJqmZindex"]) {
108              window["lastJqmZindex"] = z = window["lastJqmZindex"] + 2;
109            } else {
110              z = 3000;
111            }
112          }
113          window["lastJqmZindex"] = z;
114          var v = $("<div></div>")
115            .addClass(o.overlayClass)
116            .css({
117              height: "100%",
118              width: "100%",
119              position: "fixed",
120              left: 0,
121              top: 0,
122              "z-index": z - 1,
123              opacity: o.overlay / 100,
124            });
125          e.css("z-index", z);
126        } else {
127          var v = $("");
128          e.css("z-index", 2999);
129        }
130  
131        // maintain legacy "hash" construct
132        var h = { w: e, c: o, o: v, t: t };
133  
134        if (o.ajax) {
135          var target = o.target || e,
136            url = o.ajax;
137  
138          target = typeof target == "string" ? $(target, e) : $(target);
139          if (url.substr(0, 1) == "@") url = $(t).attr(url.substring(1));
140  
141          // Load the Ajax Content (and once loaded);
142          // Fire the onLoad callback (if exists),
143          // Attach closing events to elements inside the modal that match the closingClass,
144          // and Execute the jqModal default Open Callback
145          target.html(o.ajaxText).load(url, function () {
146            o.onLoad && o.onLoad.call(this, h);
147            open(h);
148          });
149        } else {
150          open(h);
151        }
152      },
153      hide = function (e, t) {
154        /**
155         * e = modal element (as jQuery object)
156         * t = triggering element
157         *
158         * o = options
159         * h = hash (for jqModal <= r15 compatibility)
160         */
161  
162        var o = e.data("jqm"),
163          t = t || window.event,
164          // maintain legacy "hash" construct
165          h = { w: e, c: o, o: e.data("jqmv"), t: t };
166  
167        close(h);
168      },
169      onShow = function (hash) {
170        // onShow callback. Responsible for showing a modal and overlay.
171        //  return false to stop opening modal.
172  
173        // hash object;
174        //  w: (jQuery object) The modal element
175        //  c: (object) The modal's options object
176        //  o: (jQuery object) The overlay element
177        //  t: (DOM object) The triggering element
178  
179        // display the overlay (prepend to body) if not disabled
180  
181        // prevent double click
182        if (hash.w[0]._jqmShown) return false;
183  
184        if (hash.c.overlay > 0) hash.o.prependTo("body");
185  
186        // make modal visible
187        hash.w.show();
188  
189        // call focusFunc (attempts to focus on first input in modal)
190        $.jqm.focusFunc(hash.w);
191  
192        return true;
193      },
194      onHide = function (hash) {
195        // onHide callback. Responsible for hiding a modal and overlay.
196        //  return false to stop closing modal.
197  
198        // hash object;
199        //  w: (jQuery object) The modal element
200        //  c: (object) The modal's options object
201        //  o: (jQuery object) The overlay element
202        //  t: (DOM object) The triggering element
203  
204        // hide modal and if overlay, remove overlay.
205        hash.w.hide() && hash.o && hash.o.remove();
206  
207        return true;
208      },
209      addTrigger = function (e, key, trigger) {
210        // addTrigger: Adds a jqmShow or jqmHide (key) for a modal (e)
211        //  all elements that match trigger string (trigger)\
212  
213        // return false if e is not an initialized modal element
214        if (!e.data("jqm")) return false;
215  
216        return $(trigger)
217          .each(function () {
218            // register modal to trigger elements
219            this[key] = this[key] || [];
220            this[key].push(e);
221          })
222          .click(function () {
223            var trigger = this;
224  
225            // foreadh modal registered to this trigger, call jqmShow ||
226            //   jqmHide (key) on modal passing trigger element (e)
227            $.each(this[key], function (i, e) {
228              e[key](trigger);
229            });
230  
231            // stop trigger click event from bubbling
232            return false;
233          });
234      },
235      open = function (h) {
236        // open: executes the onOpen callback + performs common tasks if successful
237  
238        // transform legacy hash into new var shortcuts
239        var e = h.w,
240          v = h.o,
241          o = h.c;
242  
243        // execute onShow callback
244        if (o.onShow(h) !== false) {
245          // mark modal as shown
246          e[0]._jqmShown = true;
247  
248          // if modal dialog
249          //
250          // Bind the Keep Focus Function [F] if no other Modals are open (!A[0]) +
251          // Add this modal to the opened modals stack (A) for nested modal support
252          //
253          // else, close dialog when overlay is clicked
254          if (o.modal) {
255            !A[0] && F("bind");
256            A.push(e);
257          } else e.jqmAddClose(v);
258  
259          //  Attach closing events to elements inside the modal that match the closingClass
260          o.closeClass && e.jqmAddClose($("." + o.closeClass, e));
261  
262          // IF toTop is true and overlay exists;
263          //  Add placeholder element <span id="jqmP#ID_of_modal"/> before modal to
264          //  remember it's position in the DOM and move it to a child of the body tag (after overlay)
265          o.toTop && v && e.before('<span id="jqmP' + o.ID + '"></span>').insertAfter(v);
266  
267          // remember overlay (for closing function)
268          e.data("jqmv", v);
269        }
270      },
271      close = function (h) {
272        // close: executes the onHide callback + performs common tasks if successful
273  
274        // transform legacy hash into new var shortcuts
275        var e = h.w,
276          v = h.o,
277          o = h.c;
278  
279        // execute onShow callback
280        if (o.onHide(h) !== false) {
281          // mark modal as !shown
282          e[0]._jqmShown = false;
283  
284          // If modal, remove from modal stack.
285          // If no modals in modal stack, unbind the Keep Focus Function
286          if (o.modal) {
287            A.pop();
288            !A[0] && F("unbind");
289          }
290  
291          // IF toTop was passed and an overlay exists;
292          //  Move modal back to its "remembered" position.
293          o.toTop &&
294            v &&
295            $("#jqmP" + o.ID)
296              .after(e)
297              .remove();
298        }
299      },
300      F = function (t) {
301        // F: The Keep Focus Function (for modal: true dialos)
302        // Binds or Unbinds (t) the Focus Examination Function (X) to keypresses and clicks
303  
304        $(document)[t]("keypress keydown mousedown", X);
305      },
306      X = function (e) {
307        // X: The Focus Examination Function (for modal: true dialogs)
308  
309        var modal = $(e.target).data("jqm") || $(e.target).parents(".jqm-init:first").data("jqm"),
310          activeModal = A[A.length - 1].data("jqm");
311  
312        // allow bubbling if event target is within active modal dialog
313        if (modal && modal.ID == activeModal.ID) return true;
314  
315        // else, trigger focusFunc (focus on first input element and halt bubbling)
316        return $.jqm.focusFunc(activeModal);
317      },
318      I = 0, // modal ID increment (for nested modals)
319      A = []; // array of active modals (used to lock interactivity to appropriate modal)
320  
321    // $.jqm, overridable defaults
322    $.jqm = {
323      /**
324       *  default options
325       *
326       * (Integer)   overlay      - [0-100] Translucency percentage (opacity) of the body covering overlay. Set to 0 for NO overlay, and up to 100 for a 100% opaque overlay.
327       * (String)    overlayClass - Applied to the body covering overlay. Useful for controlling overlay look (tint, background-image, &c) with CSS.
328       * (String)    closeClass   - Children of the modal element matching `closeClass` will fire the onHide event (to close the modal).
329       * (Mixed)     trigger      - Matching elements will fire the onShow event (to display the modal). Trigger can be a selector String, a jQuery collection of elements, a DOM element, or a False boolean.
330       * (String)    ajax         - URL to load content from via an AJAX request. False to disable ajax. If ajax begins with a "@", the URL is extracted from the attribute of the triggering element (e.g. use '@data-url' for; <a href="#" class="jqModal" data-url="modal.html">...)
331       * (Mixed)     target       - Children of the modal element to load the ajax response into. If false, modal content will be overwritten by ajax response. Useful for retaining modal design.
332       *                            Target may be a selector string, jQuery collection of elements, or a DOM element -- and MUST exist as a child of the modal element.
333       * (String)    ajaxText     - Text shown while waiting for ajax return. Replaces HTML content of `target` element.
334       * (Boolean)   modal        - If true, user interactivity will be locked to the modal window until closed.
335       * (Boolean)   toTop        - If true, modal will be posistioned as a first child of the BODY element when opened, and its DOM posistion restored when closed. Useful for overcoming z-Index container issues.
336       * (Function)  onShow       - User defined callback function fired when modal opened.
337       * (Function)  onHide       - User defined callback function fired when modal closed.
338       * (Function)  onLoad       - User defined callback function fired when ajax content loads.
339       */
340      params: {
341        overlay: 50,
342        overlayClass: "jqmOverlay",
343        closeClass: "jqmClose",
344        trigger: ".jqModal",
345        ajax: false,
346        target: false,
347        ajaxText: "",
348        modal: false,
349        toTop: false,
350        onShow: onShow,
351        onHide: onHide,
352        onLoad: false,
353      },
354  
355      // focusFunc is fired when a modal is shown, or when interaction occurs outside
356      // a modal enabled dialog. Passed the modal element.
357      focusFunc: function (e) {
358        $(":input:visible:first", e).focus();
359        return false;
360      },
361    };
362  })(jQuery);
363  

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.