| @@ -1,7 +1,6 @@ | ||
| 1 | -(function($, document){ | |
| 2 | - var setup = false, | |
| 3 | - $body = $('body'), | |
| 1 | +( function( $, document ) { | |
| 2 | + var $body, | |
| 4 | 3 | $overlay, |
| 5 | 4 | options; |
| 6 | 5 | |
| 7 | 6 | var setup = (function() { |
| @@ -18,16 +17,23 @@ | ||
| 18 | 17 | closeButton: null |
| 19 | 18 | }, params); |
| 20 | 19 | options.verticalOffset = parseInt(options.verticalOffset); |
| 21 | 20 | |
| 22 | - $body.append($('<div id="lean_overlay"></div>')); | |
| 21 | + $body = $( 'body' ); | |
| 22 | + $body.append( $( '<div id="lean_overlay"></div>' ) ); | |
| 23 | 23 | |
| 24 | - $overlay = $('#lean_overlay'); | |
| 24 | + $overlay = $( '#lean_overlay' ); | |
| 25 | 25 | |
| 26 | 26 | $trigger.on( 'click', function(e) { |
| 27 | - var modal_id = methods.get_target($(this)), | |
| 28 | - $modal = $(modal_id); | |
| 29 | - | |
| 27 | + var modal_id = methods.get_target($(this)); | |
| 28 | + | |
| 29 | + // If no valid modal target, don't proceed | |
| 30 | + if ( ! modal_id ) { | |
| 31 | + return; | |
| 32 | + } | |
| 33 | + | |
| 34 | + var $modal = $(modal_id); | |
| 35 | + | |
| 30 | 36 | methods.open( $modal, e ); |
| 31 | 37 | |
| 32 | 38 | $modal.on( 'click', options.closeButton, function(e){ |
| 33 | 39 | methods.close( $modal ); |
| @@ -69,13 +75,13 @@ | ||
| 69 | 75 | methods.resize($modal); |
| 70 | 76 | }; |
| 71 | 77 | |
| 72 | 78 | $overlay.css({ |
| 73 | - 'display': 'block', | |
| 79 | + 'display': 'block', | |
| 74 | 80 | 'opacity': 0, |
| 75 | 81 | }).fadeTo(200, options.overlay); |
| 76 | 82 | |
| 77 | - $modal.css({ | |
| 83 | + $modal.css({ | |
| 78 | 84 | 'display': 'block', |
| 79 | 85 | 'position': 'fixed', |
| 80 | 86 | 'opacity': 0, |
| 81 | 87 | 'z-index': 100000, |
| @@ -84,9 +90,9 @@ | ||
| 84 | 90 | resize(); |
| 85 | 91 | $modal.resize( resize ); |
| 86 | 92 | $(window).resize( resize ); |
| 87 | 93 | |
| 88 | - $modal.focus(); | |
| 94 | + $modal.trigger( 'focus' ); | |
| 89 | 95 | |
| 90 | 96 | /* Provide an event hook for other scripts to use. */ |
| 91 | 97 | $body.trigger( 'charitable:modal:open' ); |
| 92 | 98 | }, |
| @@ -94,19 +100,25 @@ | ||
| 94 | 100 | /** |
| 95 | 101 | * Return the ID of the modal to open. |
| 96 | 102 | */ |
| 97 | 103 | get_target : function( $trigger ) { |
| 98 | - var id = '#' + $trigger.data( 'trigger-modal' ); | |
| 104 | + var id = $trigger.data( 'trigger-modal' ); | |
| 99 | 105 | |
| 100 | - if ( 1 === id.length ) { | |
| 101 | - id = $trigger.attr( "href" ); | |
| 106 | + // If data-trigger-modal is empty or undefined, don't use href as fallback | |
| 107 | + if ( ! id || id.length === 0 ) { | |
| 108 | + return null; | |
| 102 | 109 | } |
| 103 | 110 | |
| 111 | + // Ensure it starts with # for proper selector | |
| 112 | + if ( id.charAt( 0 ) !== '#' ) { | |
| 113 | + id = '#' + id; | |
| 114 | + } | |
| 115 | + | |
| 104 | 116 | return id; |
| 105 | 117 | }, |
| 106 | 118 | |
| 107 | 119 | /** |
| 108 | - * Close modal | |
| 120 | + * Close modal | |
| 109 | 121 | */ |
| 110 | 122 | close : function( $modal ) { |
| 111 | 123 | $overlay.fadeOut( 200 ); |
| 112 | 124 | $modal.hide(); |
| @@ -118,11 +130,11 @@ | ||
| 118 | 130 | |
| 119 | 131 | /** |
| 120 | 132 | * Reset modal CSS |
| 121 | 133 | */ |
| 122 | - reset : function( $modal ) { | |
| 134 | + reset : function( $modal ) { | |
| 123 | 135 | $modal.css({ |
| 124 | - 'bottom' : 'auto', | |
| 136 | + 'bottom' : 'auto', | |
| 125 | 137 | 'overflowY' : 'auto' |
| 126 | 138 | }); |
| 127 | 139 | }, |
| 128 | 140 | |
| @@ -128,17 +140,17 @@ | ||
| 128 | 140 | |
| 129 | 141 | /** |
| 130 | 142 | * Resize modal |
| 131 | 143 | */ |
| 132 | - resize : function( $modal ) { | |
| 133 | - var window_height = $(window).height(), | |
| 144 | + resize : function( $modal ) { | |
| 145 | + var window_height = $(window).height(), | |
| 134 | 146 | modal_width = $modal.outerWidth(), |
| 135 | 147 | modal_height = $modal.outerHeight() |
| 136 | - available_offset = window_height - modal_height, | |
| 148 | + available_offset = window_height - modal_height, | |
| 137 | 149 | modal_is_too_tall = ( function() { |
| 138 | 150 | var modal_calc_height = modal_height + ( 2 * options.verticalOffset ); |
| 139 | 151 | return window_height < modal_calc_height; |
| 140 | - })(), | |
| 152 | + })(), | |
| 141 | 153 | modal_css = { |
| 142 | 154 | 'left' : 50 + '%', |
| 143 | 155 | 'margin-left' : -( modal_width / 2 ) + "px", |
| 144 | 156 | 'top' : options.verticalOffset + "px" |
| @@ -154,23 +166,23 @@ | ||
| 154 | 166 | |
| 155 | 167 | if ( available_offset <= 0 ) { |
| 156 | 168 | modal_css.top = '10px'; |
| 157 | 169 | modal_css.bottom = '10px'; |
| 158 | - modal_css.overflowY = 'scroll'; | |
| 170 | + modal_css.overflowY = 'scroll'; | |
| 159 | 171 | } |
| 160 | - } | |
| 172 | + } | |
| 161 | 173 | |
| 162 | 174 | $modal.css( modal_css ); |
| 163 | 175 | |
| 164 | 176 | /* Provide an event hook for other scripts to use. */ |
| 165 | 177 | $body.trigger( 'charitable:modal:resize' ); |
| 166 | - } | |
| 167 | - }; | |
| 168 | - | |
| 169 | - /** | |
| 178 | + } | |
| 179 | + }; | |
| 180 | + | |
| 181 | + /** | |
| 170 | 182 | * Register this as a jQuery function. |
| 171 | 183 | */ |
| 172 | - $.fn.extend({ | |
| 184 | + $.fn.extend({ | |
| 173 | 185 | leanModal : function( method_or_options ) { |
| 174 | 186 | if ( methods[ method_or_options ] ) { |
| 175 | 187 | return methods[ method_or_options ].apply( this, Array.prototype.slice.call( arguments, 1 )); |
| 176 | 188 | } else if ( typeof method_or_options === 'object' || ! method_or_options ) { |
| @@ -177,9 +189,9 @@ | ||
| 177 | 189 | // Default to "init" |
| 178 | 190 | return methods.init.apply( this, arguments ); |
| 179 | 191 | } else { |
| 180 | 192 | $.error( 'Method ' + method_or_options + ' does not exist on jQuery.leanModal' ); |
| 181 | - } | |
| 193 | + } | |
| 182 | 194 | } |
| 183 | 195 | }); |
| 184 | 196 | |
| 185 | 197 | /** |
| @@ -188,7 +200,7 @@ | ||
| 188 | 200 | $(document).ready( function() { |
| 189 | 201 | $( '[data-trigger-modal]' ).leanModal({ |
| 190 | 202 | closeButton : ".modal-close" |
| 191 | 203 | }); |
| 192 | - }); | |
| 193 | - | |
| 204 | + }); | |
| 205 | + | |
| 194 | 206 | })(jQuery, document); |