| 1 |
( function( $, document ) { |
| 2 |
var $body, |
| 3 |
$overlay, |
| 4 |
options; |
| 5 |
|
| 6 |
var setup = (function() { |
| 7 |
var done = false; |
| 8 |
|
| 9 |
return function( $trigger, params ) { |
| 10 |
if ( done ) { |
| 11 |
return; |
| 12 |
} |
| 13 |
|
| 14 |
options = $.extend({ |
| 15 |
verticalOffset: 100, |
| 16 |
overlay: 0.5, |
| 17 |
closeButton: null |
| 18 |
}, params); |
| 19 |
options.verticalOffset = parseInt(options.verticalOffset); |
| 20 |
|
| 21 |
$body = $( 'body' ); |
| 22 |
$body.append( $( '<div id="lean_overlay"></div>' ) ); |
| 23 |
|
| 24 |
$overlay = $( '#lean_overlay' ); |
| 25 |
|
| 26 |
$trigger.on( 'click', function(e) { |
| 27 |
var modal_id = methods.get_target($(this)), |
| 28 |
$modal = $(modal_id); |
| 29 |
|
| 30 |
methods.open( $modal, e ); |
| 31 |
|
| 32 |
$modal.on( 'click', options.closeButton, function(e){ |
| 33 |
methods.close( $modal ); |
| 34 |
e.preventDefault(); |
| 35 |
}); |
| 36 |
|
| 37 |
$overlay.on( 'click', function() { |
| 38 |
methods.close( $modal ); |
| 39 |
}); |
| 40 |
|
| 41 |
$(document).on( 'keyup', function(e) { |
| 42 |
return 27 === e.keyCode && methods.close( $modal ); |
| 43 |
}); |
| 44 |
|
| 45 |
e.preventDefault(); |
| 46 |
}); |
| 47 |
|
| 48 |
done = true; |
| 49 |
} |
| 50 |
})(); |
| 51 |
|
| 52 |
/** |
| 53 |
* General API methods. |
| 54 |
*/ |
| 55 |
var methods = { |
| 56 |
|
| 57 |
/** |
| 58 |
* Main fn method |
| 59 |
*/ |
| 60 |
init : function( params ) { |
| 61 |
setup(this, params); |
| 62 |
}, |
| 63 |
|
| 64 |
/** |
| 65 |
* Open modal. |
| 66 |
*/ |
| 67 |
open : function( $modal ) { |
| 68 |
var resize = function() { |
| 69 |
methods.resize($modal); |
| 70 |
}; |
| 71 |
|
| 72 |
$overlay.css({ |
| 73 |
'display': 'block', |
| 74 |
'opacity': 0, |
| 75 |
}).fadeTo(200, options.overlay); |
| 76 |
|
| 77 |
$modal.css({ |
| 78 |
'display': 'block', |
| 79 |
'position': 'fixed', |
| 80 |
'opacity': 0, |
| 81 |
'z-index': 100000, |
| 82 |
}).fadeTo(200, 1); |
| 83 |
|
| 84 |
resize(); |
| 85 |
$modal.resize( resize ); |
| 86 |
$(window).resize( resize ); |
| 87 |
|
| 88 |
$modal.trigger( 'focus' ); |
| 89 |
|
| 90 |
/* Provide an event hook for other scripts to use. */ |
| 91 |
$body.trigger( 'charitable:modal:open' ); |
| 92 |
}, |
| 93 |
|
| 94 |
/** |
| 95 |
* Return the ID of the modal to open. |
| 96 |
*/ |
| 97 |
get_target : function( $trigger ) { |
| 98 |
var id = '#' + $trigger.data( 'trigger-modal' ); |
| 99 |
|
| 100 |
if ( 1 === id.length ) { |
| 101 |
id = $trigger.attr( "href" ); |
| 102 |
} |
| 103 |
|
| 104 |
return id; |
| 105 |
}, |
| 106 |
|
| 107 |
/** |
| 108 |
* Close modal |
| 109 |
*/ |
| 110 |
close : function( $modal ) { |
| 111 |
$overlay.fadeOut( 200 ); |
| 112 |
$modal.hide(); |
| 113 |
methods.reset( $modal ); |
| 114 |
|
| 115 |
/* Provide an event hook for other scripts to use. */ |
| 116 |
$body.trigger( 'charitable:modal:close', $modal ); |
| 117 |
}, |
| 118 |
|
| 119 |
/** |
| 120 |
* Reset modal CSS |
| 121 |
*/ |
| 122 |
reset : function( $modal ) { |
| 123 |
$modal.css({ |
| 124 |
'bottom' : 'auto', |
| 125 |
'overflowY' : 'auto' |
| 126 |
}); |
| 127 |
}, |
| 128 |
|
| 129 |
/** |
| 130 |
* Resize modal |
| 131 |
*/ |
| 132 |
resize : function( $modal ) { |
| 133 |
var window_height = $(window).height(), |
| 134 |
modal_width = $modal.outerWidth(), |
| 135 |
modal_height = $modal.outerHeight() |
| 136 |
available_offset = window_height - modal_height, |
| 137 |
modal_is_too_tall = ( function() { |
| 138 |
var modal_calc_height = modal_height + ( 2 * options.verticalOffset ); |
| 139 |
return window_height < modal_calc_height; |
| 140 |
})(), |
| 141 |
modal_css = { |
| 142 |
'left' : 50 + '%', |
| 143 |
'margin-left' : -( modal_width / 2 ) + "px", |
| 144 |
'top' : options.verticalOffset + "px" |
| 145 |
}; |
| 146 |
|
| 147 |
if ( modal_is_too_tall ) { |
| 148 |
if ( available_offset > 0 ) { |
| 149 |
var v_offset = available_offset / 2; |
| 150 |
|
| 151 |
modal_css.top = v_offset + 'px'; |
| 152 |
modal_css.bottom = v_offset + 'px'; |
| 153 |
} |
| 154 |
|
| 155 |
if ( available_offset <= 0 ) { |
| 156 |
modal_css.top = '10px'; |
| 157 |
modal_css.bottom = '10px'; |
| 158 |
modal_css.overflowY = 'scroll'; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
$modal.css( modal_css ); |
| 163 |
|
| 164 |
/* Provide an event hook for other scripts to use. */ |
| 165 |
$body.trigger( 'charitable:modal:resize' ); |
| 166 |
} |
| 167 |
}; |
| 168 |
|
| 169 |
/** |
| 170 |
* Register this as a jQuery function. |
| 171 |
*/ |
| 172 |
$.fn.extend({ |
| 173 |
leanModal : function( method_or_options ) { |
| 174 |
if ( methods[ method_or_options ] ) { |
| 175 |
return methods[ method_or_options ].apply( this, Array.prototype.slice.call( arguments, 1 )); |
| 176 |
} else if ( typeof method_or_options === 'object' || ! method_or_options ) { |
| 177 |
// Default to "init" |
| 178 |
return methods.init.apply( this, arguments ); |
| 179 |
} else { |
| 180 |
$.error( 'Method ' + method_or_options + ' does not exist on jQuery.leanModal' ); |
| 181 |
} |
| 182 |
} |
| 183 |
}); |
| 184 |
|
| 185 |
/** |
| 186 |
* Init function. |
| 187 |
*/ |
| 188 |
$(document).ready( function() { |
| 189 |
$( '[data-trigger-modal]' ).leanModal({ |
| 190 |
closeButton : ".modal-close" |
| 191 |
}); |
| 192 |
}); |
| 193 |
|
| 194 |
})(jQuery, document); |