|
1 | | -// jQuery.leanModal2.js v2.3.1 |
2 | | -// MIT Licensed by eustasy http://eustasy.org |
| 1 | +//// jQuery.leanModal2.js v2.3.2 |
| 2 | +// MIT Licensed by eustasy https://eustasy.org |
3 | 3 | // Based on leanModal v1.1 by Ray Stone - http://finelysliced.com.au |
4 | 4 |
|
5 | | -// Wrap in an anonymous function. |
| 5 | +// ANONFUNC Wrap in an anonymous function. |
6 | 6 | (function($){ |
7 | 7 |
|
8 | | - // Function: Fade out the Overlay and a passed identifier |
| 8 | + //// Close the Modal |
| 9 | + // FUNCTION: Fade out the overlay and a passed identifier. |
9 | 10 | function leanModal_Close(modal_id) { |
10 | 11 | $('.js-leanmodal-overlay').fadeOut(300); |
11 | 12 | $(modal_id).fadeOut(200); |
12 | 13 | } |
13 | 14 |
|
14 | | - // Define a new Extension. |
| 15 | + //// Extend jQuery |
| 16 | + // EXTENDFUNC |
15 | 17 | $.fn.extend({ |
| 18 | + // EXTENDFUNC_LEANMODAL |
16 | 19 | leanModal: function(options) { |
17 | 20 |
|
| 21 | + //// Default Options |
18 | 22 | // Set some Defaults. |
19 | 23 | var defaults = { |
20 | 24 | top: 100, |
|
23 | 27 | disableCloseOnOverlayClick: false, |
24 | 28 | disableCloseOnEscape: false, |
25 | 29 | }; |
26 | | - |
27 | | - // Merge in the passed options. |
| 30 | + // Merge in any passed options. |
28 | 31 | options = $.extend(defaults, options); |
29 | 32 |
|
30 | | - // If there isn't an overlay, add one. |
| 33 | + //// There can be only one. |
| 34 | + // Overlay. If there isn't an overlay, add one. |
31 | 35 | if ( $('.js-leanmodal-overlay').length == 0 ) { |
32 | | - var style = 'background: #000; display: none; height: 100%; left: 0px; position: fixed; top: 0px; width: 100%; z-index: 100;'; |
| 36 | + var style = 'background: #000; display: none; height: 100%; left: 0; position: fixed; top: 0; width: 100%; z-index: 100;'; |
33 | 37 | var overlay = $('<div class="js-leanmodal-overlay" style="' + style + '"></div>'); |
34 | 38 | $('body').append(overlay); |
35 | 39 | } |
36 | 40 |
|
37 | | - // FORLINK For each targetted link. |
| 41 | + //// Everything is Linked |
| 42 | + // FOREACHLINK For each targeted link. |
38 | 43 | return this.each(function() { |
39 | | - |
| 44 | + // Force this to look like a link. |
40 | 45 | $(this).css({ 'cursor': 'pointer' }); |
41 | 46 |
|
| 47 | + //// Command Override |
| 48 | + // Override the existing click command, |
| 49 | + // and insert this new one. |
| 50 | + // CLICKOVERRIDE |
42 | 51 | $(this).unbind('click').click(function(e) { |
43 | 52 |
|
| 53 | + //// Select the Modal Identifier |
44 | 54 | // IFHREF Use data-open-modal if available |
45 | 55 | if ( $(this).attr('data-modal-id') ) { |
46 | 56 | var modal_id = $(this).attr('data-modal-id'); |
47 | 57 | // IFHREF Fall back to href |
48 | 58 | } else if ( $(this).attr('href') ) { |
49 | 59 | var modal_id = $(this).attr('href'); |
| 60 | + // IFHREF Fail entirely. |
50 | 61 | } else { |
51 | 62 | return false; |
52 | 63 | } // IFHREF |
53 | 64 |
|
54 | | - // Set the function to close the overlay if you click it. |
55 | | - $('.js-leanmodal-overlay').click(function() { |
56 | | - if ( !options.disableCloseOnOverlayClick ) { |
57 | | - leanModal_Close(modal_id); |
58 | | - } |
59 | | - }); |
60 | | - |
61 | | - // If a close button is set, link it to the close command. |
| 65 | + //// Close with closeButton |
| 66 | + // If `closeButton` is set, |
| 67 | + // use it to call the close command. |
62 | 68 | if ( options.closeButton ) { |
63 | 69 | $(options.closeButton).click(function() { |
64 | 70 | leanModal_Close(modal_id); |
65 | 71 | }); |
66 | 72 | } |
67 | 73 |
|
68 | | - // Close the modal on escape |
69 | | - $(document).on('keyup', function(evt) { |
70 | | - if ( |
71 | | - !options.disableCloseOnEscape && |
72 | | - evt.keyCode == 27 |
73 | | - ) { |
| 74 | + //// Escape with `Esc` |
| 75 | + // Close the modal when someone presses `Esc`, |
| 76 | + // except when `disableCloseOnEscape` is set to `true` |
| 77 | + if ( !options.disableCloseOnEscape ) { |
| 78 | + $(document).on('keyup', function(evt) { |
| 79 | + if ( evt.keyCode == 27 ) { |
| 80 | + leanModal_Close(modal_id); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + //// Envelope in Darkness |
| 86 | + // Close the modal when someone clicks on the overlay, |
| 87 | + // except when `disableCloseOnOverlayClick` is set to `true` |
| 88 | + if ( !options.disableCloseOnOverlayClick ) { |
| 89 | + $('.js-leanmodal-overlay').click(function() { |
74 | 90 | leanModal_Close(modal_id); |
75 | | - } |
76 | | - }); |
| 91 | + }); |
| 92 | + } |
77 | 93 |
|
| 94 | + //// Modal Positioning |
| 95 | + // Position the modal centrally using JavaScript, because CSS sucks. |
| 96 | + // Actually it doesn't, but it is hard to globally position. |
78 | 97 | var modal_height = $(modal_id).innerHeight(); |
79 | 98 | var modal_width = $(modal_id).innerWidth(); |
80 | 99 | $(modal_id).css({ |
81 | 100 | 'display': 'block', |
82 | | - 'position': 'fixed', |
83 | | - 'opacity': 0, |
84 | | - 'z-index': 11000, |
85 | 101 | 'left': 50 + '%', |
86 | | - 'margin-left': -(modal_width/2) + 'px', |
| 102 | + 'margin-left': - ( modal_width / 2 ) + 'px', |
| 103 | + 'opacity': 0, |
| 104 | + 'position': 'fixed', |
87 | 105 | 'top': options.top + 'px' |
| 106 | + 'z-index': 11000, |
88 | 107 | }); |
89 | 108 |
|
| 109 | + //// Curtain Up |
| 110 | + // Fade in the modal and overlay. |
90 | 111 | $('.js-leanmodal-overlay').css({ 'display': 'block', opacity: 0 }); |
91 | 112 | $('.js-leanmodal-overlay').fadeTo(300, options.overlayOpacity); |
92 | 113 | $(modal_id).fadeTo(200, 1); |
93 | 114 |
|
| 115 | + //// Default Prevention |
94 | 116 | // Prevent whatever the default was (probably scrolling). |
95 | 117 | e.preventDefault(); |
96 | | - |
97 | | - }); |
98 | | - }); // FORLINK |
99 | | - |
100 | | - } |
101 | | - }); |
102 | | - |
103 | | -// Apparently this jQuery is important. |
104 | | -})(jQuery); |
| 118 | + |
| 119 | + }); // CLICKOVERRIDE |
| 120 | + }); // FOREACHLINK |
| 121 | + } // EXTENDFUNC_LEANMODAL |
| 122 | + }); // EXTENDFUNC |
| 123 | +})(jQuery); // ANONFUNC This passes in `jQuery` as `$` |
0 commit comments