| 1 |
(function($){ |
| 2 |
|
| 3 |
var offset; |
| 4 |
|
| 5 |
var methods = { |
| 6 |
|
| 7 |
/** |
| 8 |
* Main fn method |
| 9 |
*/ |
| 10 |
init : function( options ) { |
| 11 |
var defaults = { |
| 12 |
verticalOffset: 100, |
| 13 |
overlay: 0.5, |
| 14 |
closeButton: null |
| 15 |
} |
| 16 |
|
| 17 |
var overlay = $("<div id='lean_overlay'></div>"); |
| 18 |
|
| 19 |
$( "body" ).append(overlay); |
| 20 |
|
| 21 |
options = $.extend( defaults, options ); |
| 22 |
|
| 23 |
this.each( function() { |
| 24 |
|
| 25 |
var o = options, |
| 26 |
window_height = $(window).height(); |
| 27 |
|
| 28 |
offset = parseInt( o.verticalOffset ); |
| 29 |
|
| 30 |
$(this).on( 'click', function(e) { |
| 31 |
|
| 32 |
var modal_id = methods.get_target( $(this) ), |
| 33 |
$modal = $( modal_id ), |
| 34 |
modal_height, |
| 35 |
modal_width; |
| 36 |
|
| 37 |
$( "#lean_overlay" ).on( 'click', function() { |
| 38 |
methods.close( $modal ); |
| 39 |
}); |
| 40 |
|
| 41 |
$( o.closeButton ).on( 'click', function() { |
| 42 |
methods.close( $modal ); |
| 43 |
}); |
| 44 |
|
| 45 |
$modal.outerHeight(); |
| 46 |
$modal.outerWidth(); |
| 47 |
|
| 48 |
$('#lean_overlay').css({ 'display' : 'block', opacity : 0 }); |
| 49 |
|
| 50 |
$('#lean_overlay').fadeTo(200,o.overlay); |
| 51 |
|
| 52 |
$modal.css({ |
| 53 |
'display' : 'block', |
| 54 |
'position' : 'fixed', |
| 55 |
'opacity' : 0, |
| 56 |
'z-index': 100000, |
| 57 |
}); |
| 58 |
|
| 59 |
methods.resize( $modal, window_height, modal_height ); |
| 60 |
|
| 61 |
$modal.fadeTo(200,1); |
| 62 |
|
| 63 |
e.preventDefault(); |
| 64 |
|
| 65 |
}); |
| 66 |
|
| 67 |
}); |
| 68 |
}, |
| 69 |
|
| 70 |
/** |
| 71 |
* Return the ID of the modal to open. |
| 72 |
*/ |
| 73 |
get_target : function( $trigger ) { |
| 74 |
var id = '#' + $trigger.data( 'trigger-modal' ); |
| 75 |
|
| 76 |
if ( 1 === id.length ) { |
| 77 |
id = $trigger.attr( "href" ); |
| 78 |
} |
| 79 |
|
| 80 |
return id; |
| 81 |
}, |
| 82 |
|
| 83 |
/** |
| 84 |
* Close modal |
| 85 |
*/ |
| 86 |
close : function( $modal ) { |
| 87 |
$( "#lean_overlay" ).fadeOut(200); |
| 88 |
$modal.css({ 'display' : 'none' }); |
| 89 |
methods.reset( $modal ); |
| 90 |
}, |
| 91 |
|
| 92 |
/** |
| 93 |
* Reset modal CSS |
| 94 |
*/ |
| 95 |
reset : function( $modal ) { |
| 96 |
$modal.css({ |
| 97 |
'bottom' : 'auto', |
| 98 |
'overflowY' : 'auto' |
| 99 |
}); |
| 100 |
}, |
| 101 |
|
| 102 |
/** |
| 103 |
* Resize modal |
| 104 |
*/ |
| 105 |
resize : function( $modal ) { |
| 106 |
var window_height = $(window).height(), |
| 107 |
modal_width = $modal.outerWidth(), |
| 108 |
modal_height = $modal.outerHeight() |
| 109 |
available_offset = window_height - modal_height, |
| 110 |
modal_is_too_tall = ( function() { |
| 111 |
var modal_calc_height = modal_height + ( 2 * offset ); |
| 112 |
return window_height < modal_calc_height; |
| 113 |
})(), |
| 114 |
modal_css = { |
| 115 |
'left' : 50 + '%', |
| 116 |
'margin-left' : -( modal_width / 2 ) + "px", |
| 117 |
'top' : offset + "px" |
| 118 |
}; |
| 119 |
|
| 120 |
if ( modal_is_too_tall ) { |
| 121 |
if ( available_offset > 0 ) { |
| 122 |
var v_offset = available_offset / 2; |
| 123 |
|
| 124 |
modal_css.top = v_offset + 'px'; |
| 125 |
modal_css.bottom = v_offset + 'px'; |
| 126 |
} |
| 127 |
|
| 128 |
if ( available_offset <= 0 ) { |
| 129 |
modal_css.top = '10px'; |
| 130 |
modal_css.bottom = '10px'; |
| 131 |
modal_css.overflowY = 'scroll'; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
$modal.css( modal_css ); |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
$.fn.extend({ |
| 140 |
leanModal : function( method_or_options ) { |
| 141 |
if ( methods[ method_or_options ] ) { |
| 142 |
return methods[ method_or_options ].apply( this, Array.prototype.slice.call( arguments, 1 )); |
| 143 |
} else if ( typeof method_or_options === 'object' || ! method_or_options ) { |
| 144 |
// Default to "init" |
| 145 |
return methods.init.apply( this, arguments ); |
| 146 |
} else { |
| 147 |
$.error( 'Method ' + method_or_options + ' does not exist on jQuery.leanModal' ); |
| 148 |
} |
| 149 |
} |
| 150 |
}); |
| 151 |
|
| 152 |
})(jQuery); |