Skip to content

Commit 97bfa24

Browse files
authored
Additional commenting, better option handling, prioritise data attr.
1 parent b3218fc commit 97bfa24

File tree

1 file changed

+59
-40
lines changed

1 file changed

+59
-40
lines changed

jQuery.leanModal2.js

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
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
33
// Based on leanModal v1.1 by Ray Stone - http://finelysliced.com.au
44

5-
// Wrap in an anonymous function.
5+
// ANONFUNC Wrap in an anonymous function.
66
(function($){
77

8-
// Function: Fade out the Overlay and a passed identifier
8+
//// Close the Modal
9+
// FUNCTION: Fade out the overlay and a passed identifier.
910
function leanModal_Close(modal_id) {
1011
$('.js-leanmodal-overlay').fadeOut(300);
1112
$(modal_id).fadeOut(200);
1213
}
1314

14-
// Define a new Extension.
15+
//// Extend jQuery
16+
// EXTENDFUNC
1517
$.fn.extend({
18+
// EXTENDFUNC_LEANMODAL
1619
leanModal: function(options) {
1720

21+
//// Default Options
1822
// Set some Defaults.
1923
var defaults = {
2024
top: 100,
@@ -23,82 +27,97 @@
2327
disableCloseOnOverlayClick: false,
2428
disableCloseOnEscape: false,
2529
};
26-
27-
// Merge in the passed options.
30+
// Merge in any passed options.
2831
options = $.extend(defaults, options);
2932

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.
3135
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;';
3337
var overlay = $('<div class="js-leanmodal-overlay" style="' + style + '"></div>');
3438
$('body').append(overlay);
3539
}
3640

37-
// FORLINK For each targetted link.
41+
//// Everything is Linked
42+
// FOREACHLINK For each targeted link.
3843
return this.each(function() {
39-
44+
// Force this to look like a link.
4045
$(this).css({ 'cursor': 'pointer' });
4146

47+
//// Command Override
48+
// Override the existing click command,
49+
// and insert this new one.
50+
// CLICKOVERRIDE
4251
$(this).unbind('click').click(function(e) {
4352

53+
//// Select the Modal Identifier
4454
// IFHREF Use data-open-modal if available
4555
if ( $(this).attr('data-modal-id') ) {
4656
var modal_id = $(this).attr('data-modal-id');
4757
// IFHREF Fall back to href
4858
} else if ( $(this).attr('href') ) {
4959
var modal_id = $(this).attr('href');
60+
// IFHREF Fail entirely.
5061
} else {
5162
return false;
5263
} // IFHREF
5364

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.
6268
if ( options.closeButton ) {
6369
$(options.closeButton).click(function() {
6470
leanModal_Close(modal_id);
6571
});
6672
}
6773

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() {
7490
leanModal_Close(modal_id);
75-
}
76-
});
91+
});
92+
}
7793

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.
7897
var modal_height = $(modal_id).innerHeight();
7998
var modal_width = $(modal_id).innerWidth();
8099
$(modal_id).css({
81100
'display': 'block',
82-
'position': 'fixed',
83-
'opacity': 0,
84-
'z-index': 11000,
85101
'left': 50 + '%',
86-
'margin-left': -(modal_width/2) + 'px',
102+
'margin-left': - ( modal_width / 2 ) + 'px',
103+
'opacity': 0,
104+
'position': 'fixed',
87105
'top': options.top + 'px'
106+
'z-index': 11000,
88107
});
89108

109+
//// Curtain Up
110+
// Fade in the modal and overlay.
90111
$('.js-leanmodal-overlay').css({ 'display': 'block', opacity: 0 });
91112
$('.js-leanmodal-overlay').fadeTo(300, options.overlayOpacity);
92113
$(modal_id).fadeTo(200, 1);
93114

115+
//// Default Prevention
94116
// Prevent whatever the default was (probably scrolling).
95117
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

Comments
 (0)