|
1 | 1 | /**
|
2 | 2 | * @name jQuery Modal
|
3 | 3 | * @author Matt Hinchliffe <https://github.com/i-like-robots/jQuery-Modal>
|
4 |
| - * @modified 06/08/2012 |
5 |
| - * @version 0.9.0 |
| 4 | + * @modified 13/09/2012 |
| 5 | + * @version 1.0.0b |
6 | 6 | * @description A simple modal overlay
|
7 | 7 | *
|
8 | 8 | * @example jQuery plugin setup
|
|
18 | 18 | *
|
19 | 19 | * @example Generated markup
|
20 | 20 | * <div class="modal-wrapper">
|
21 |
| - * <div class="modal-content" /> |
22 | 21 | * <span class="modal-close" data-toggle="modal">Close</span>
|
| 22 | + * <div class="modal-content" /> |
23 | 23 | * </div>
|
24 | 24 | * <div class="modal-overlay" data-toggle="modal" />
|
25 | 25 | */
|
26 |
| - |
27 |
| -/*jshint trailing:true, smarttabs:true */ |
28 | 26 | ; (function($, undefined)
|
29 | 27 | {
|
30 | 28 | "use strict";
|
31 | 29 |
|
32 | 30 | function Modal(target, options)
|
33 | 31 | {
|
34 | 32 | this.opts = $.extend({}, { // Create a new options object for each instance
|
35 |
| - onopen: undefined, |
36 |
| - onclose: undefined, |
37 |
| - onupdate: undefined, |
38 |
| - width: 640, |
39 |
| - maxWidth: '95%', |
40 |
| - height: 480, |
41 |
| - maxHeight: '95%', |
42 |
| - fixed: false, |
43 |
| - overlay: true, |
44 |
| - blur: true, |
45 |
| - escape: true |
| 33 | + onopen: undefined, |
| 34 | + onhide: undefined, |
| 35 | + onupdate: undefined, |
| 36 | + fixed: false, |
| 37 | + overlay: true, |
| 38 | + blur: true, |
| 39 | + escape: true, |
| 40 | + width: 640, |
| 41 | + maxWidth: '95%', |
| 42 | + height: 480, |
| 43 | + maxHeight: '95%' |
46 | 44 | }, options);
|
47 | 45 |
|
48 | 46 | this.target = target;
|
|
62 | 60 | */
|
63 | 61 | _init: function()
|
64 | 62 | {
|
65 |
| - if (this.isInitialized) |
66 |
| - { |
67 |
| - return; |
68 |
| - } |
69 |
| - |
70 | 63 | this.doc = $(document);
|
71 | 64 |
|
72 |
| - // Build modal |
73 |
| - this.wrapper = $('<div class="modal-wrapper"><span class="modal-close" data-toggle="modal">Close</span></div>').css({ |
| 65 | + this.wrapper = $('<div class="modal-wrapper" />').css({ |
74 | 66 | position: this.opts.fixed ? 'fixed' : 'absolute',
|
75 | 67 | width: this.opts.width,
|
76 | 68 | maxWidth: this.opts.maxWidth,
|
77 | 69 | height: this.opts.height,
|
78 | 70 | maxHeight: this.opts.maxHeight,
|
79 | 71 | display: 'none'
|
80 | 72 | });
|
| 73 | + this.close = $('<span class="modal-close" data-toggle="modal">Close</span>').appendTo( this.wrapper ); |
81 | 74 | this.content = $('<div class="modal-content" />').appendTo( this.wrapper );
|
| 75 | + |
82 | 76 | this.wrapper.appendTo( this.target );
|
83 | 77 |
|
84 |
| - // Create overlay |
| 78 | + // Define overlay to prevent errors |
| 79 | + this.overlay = false; |
| 80 | + |
85 | 81 | if (this.opts.overlay)
|
86 | 82 | {
|
87 | 83 | this.overlay = $('<div class="modal-overlay"' + (this.opts.blur ? 'data-toggle="modal"' : '') + ' />')
|
|
108 | 104 | */
|
109 | 105 | align: function()
|
110 | 106 | {
|
111 |
| - var height = this.wrapper.height(), |
112 |
| - width = this.wrapper.width(), |
| 107 | + var height = this.wrapper.outerHeight(), |
| 108 | + width = this.wrapper.outerWidth(), |
113 | 109 | maxHeight = this.context.height(),
|
114 |
| - maxWidth = this.context.width(), |
115 |
| - top = this.opts.fixed ? 0 : this.context.scrollTop(); |
| 110 | + maxWidth = this.context.width(), |
| 111 | + top = this.opts.fixed ? 0 : this.context.scrollTop(); |
116 | 112 |
|
117 | 113 | this.wrapper.css(
|
118 | 114 | {
|
|
138 | 134 | {
|
139 | 135 | this._init();
|
140 | 136 | }
|
141 |
| - else if (this.isOpen) |
142 |
| - { |
143 |
| - return; |
144 |
| - } |
145 | 137 |
|
146 | 138 | var self = this;
|
147 | 139 |
|
|
156 | 148 | {
|
157 | 149 | if ( e.keyCode === 27 )
|
158 | 150 | {
|
159 |
| - self.close(); |
| 151 | + self.hide(); |
160 | 152 | }
|
161 | 153 | });
|
162 | 154 | }
|
163 | 155 |
|
164 | 156 | this.doc.on('click.modal', '[data-toggle="modal"]', function(e)
|
165 | 157 | {
|
166 | 158 | e.preventDefault();
|
167 |
| - self.close(); |
| 159 | + self.hide(); |
168 | 160 | });
|
169 | 161 |
|
170 | 162 | // Fade in
|
|
181 | 173 | this.update(content);
|
182 | 174 | }
|
183 | 175 |
|
| 176 | + |
184 | 177 | // Callbacks
|
185 | 178 | if (this.opts.onopen)
|
186 | 179 | {
|
|
194 | 187 |
|
195 | 188 | /**
|
196 | 189 | * Update
|
197 |
| - * @description Change the modal window contents |
198 |
| - * @param {object} content |
| 190 | + * @description Update the modal window contents |
| 191 | + * @param {object|string} content |
199 | 192 | * @param {function} callback
|
200 | 193 | */
|
201 | 194 | update: function(content, callback)
|
|
221 | 214 | /**
|
222 | 215 | * Resize
|
223 | 216 | * @description Resizes the modal window content area
|
| 217 | + * @param {numeric|string} width |
| 218 | + * @param {numeric|string} height |
224 | 219 | */
|
225 | 220 | resize: function(width, height)
|
226 | 221 | {
|
|
233 | 228 | },
|
234 | 229 |
|
235 | 230 | /**
|
236 |
| - * Close |
237 |
| - * @description Close the modal window and clear contents |
| 231 | + * Hide |
| 232 | + * @description Hide the modal window |
238 | 233 | * @param {function} callback
|
239 | 234 | */
|
240 |
| - close: function(callback) |
| 235 | + hide: function(callback) |
241 | 236 | {
|
242 |
| - if ( ! this.isInitialized || ! this.isOpen) |
243 |
| - { |
244 |
| - return; |
245 |
| - } |
246 |
| - |
247 |
| - var self = this; |
248 |
| - |
249 | 237 | // Unbind events
|
250 | 238 | this.doc.off('.modal');
|
251 | 239 |
|
252 |
| - // Fade out |
253 | 240 | this.wrapper
|
254 | 241 | .add( this.overlay )
|
255 | 242 | .stop()
|
256 |
| - .fadeOut(function() |
257 |
| - { |
258 |
| - self.content[0].innerHTML = ''; |
259 |
| - }); |
| 243 | + .fadeOut(); |
260 | 244 |
|
261 | 245 | this.isOpen = false;
|
262 | 246 |
|
263 | 247 | // Callbacks
|
264 |
| - if (this.opts.onclose) |
| 248 | + if (this.opts.onhide) |
265 | 249 | {
|
266 |
| - this.opts.onclose.call(this); |
| 250 | + this.opts.onhide.call(this); |
267 | 251 | }
|
268 | 252 | if (callback)
|
269 | 253 | {
|
|
0 commit comments