1/* 2 3 arcticModal â jQuery plugin 4 Version: 0.3 5 Author: Sergey Predvoditelev ([email protected]) 6 Company: Arctic Laboratory (http://arcticlab.ru/) 7 8 Docs & Examples: http://arcticlab.ru/arcticmodal/ 9 10 */ 11(function($) { 12 13 14 var default_options = { 15 16 type: 'html', // ajax или html 17 content: '', 18 url: '', 19 ajax: {}, 20 ajax_request: null, 21 22 closeOnEsc: true, 23 closeOnOverlayClick: true, 24 25 clone: false, 26 27 overlay: { 28 block: undefined, 29 tpl: '<div class="arcticmodal-overlay"></div>', 30 css: { 31 backgroundColor: '#000', 32 opacity: .6 33 } 34 }, 35 36 container: { 37 block: undefined, 38 tpl: '<div class="arcticmodal-container"><table class="arcticmodal-container_i"><tr><td class="arcticmodal-container_i2"></td></tr></table></div>' 39 }, 40 41 wrap: undefined, 42 body: undefined, 43 44 errors: { 45 tpl: '<div class="arcticmodal-error arcticmodal-close"></div>', 46 autoclose_delay: 2000, 47 ajax_unsuccessful_load: 'Error' 48 }, 49 50 openEffect: { 51 type: 'fade', 52 speed: 400 53 }, 54 closeEffect: { 55 type: 'fade', 56 speed: 400 57 }, 58 59 beforeOpen: $.noop, 60 afterOpen: $.noop, 61 beforeClose: $.noop, 62 afterClose: $.noop, 63 afterLoading: $.noop, 64 afterLoadingOnShow: $.noop, 65 errorLoading: $.noop 66 67 }; 68 69 70 var modalID = 0; 71 var modals = $([]); 72 73 74 var utils = { 75 76 77 // ÐпÑеделÑÐµÑ Ð¿ÑоизоÑло ли ÑобÑÑие e вне блока block 78 isEventOut: function(blocks, e) { 79 var r = true; 80 $(blocks).each(function() { 81 if ($(e.target).get(0)==$(this).get(0)) r = false;
82 if ($(e.target).closest('HTML', $(this).get(0)).length==0) r = false; 83 }); 84 return r; 85 } 86 87 88 }; 89 90 91 var modal = { 92 93 94 // ÐозвÑаÑÐ°ÐµÑ ÑлеменÑ, коÑоÑÑм бÑл вÑзван плагин 95 getParentEl: function(el) { 96 var r = $(el); 97 if (r.data('arcticmodal')) return r; 98 r = $(el).closest('.arcticmodal-container').data('arcticmodalParentEl'); 99 if (r) return r; 100 return false; 101 }, 102 103 104 // ÐеÑÐµÑ Ð¾Ð´ 105 transition: function(el, action, options, callback) { 106 callback = callback==undefined ? $.noop : callback; 107 switch (options.type) { 108 case 'fade': 109 action=='show' ? el.fadeIn(options.speed, callback) : el.fadeOut(options.speed, callback); 110 break; 111 case 'none': 112 action=='show' ? el.show() : el.hide(); 113 callback(); 114 break; 115 } 116 }, 117 118 119 // ÐодгоÑвка ÑодеÑжимого окна 120 prepare_body: function(D, $this) { 121 122 // ÐбÑабоÑÑик закÑÑÑÐ¸Ñ 123 $('.arcticmodal-close', D.body).unbind('click.arcticmodal').bind('click.arcticmodal', function() { 124 $this.arcticmodal('close'); 125 return false; 126 }); 127 128 }, 129 130 131 // ÐниÑиализаÑÐ¸Ñ ÑлеменÑа 132 init_el: function($this, options) { 133 var D = $this.data('arcticmodal'); 134 if (D) return; 135 136 D = options; 137 modalID++; 138 D.modalID = modalID; 139 140 // Overlay 141 D.overlay.block = $(D.overlay.tpl); 142 D.overlay.block.css(D.overlay.css); 143 144 // Container 145 D.container.block = $(D.container.tpl); 146 147 // BODY 148 D.body = $('.arcticmodal-container_i2', D.container.block); 149 if (options.clone) { 150 D.body.html($this.clone(true)); 151 } else { 152 $this.before('<div id="arcticmodalReserve' + D.modalID + '" style="display: none" />'); 153 D.body.html($this); 154 } 155 156 // ÐодгоÑовка ÑодеÑжимого 157 modal.prepare_body(D, $this); 158 159 // ÐакÑÑÑие пÑи клике на overlay 160 if (D.closeOnOverlayClick) 161 D.overlay.block.add(D.container.block).click(function(e) { 162 if (utils.isEventOut($('>*', D.body), e)) 163 $this.arcticmodal('close'); 164 }); 165 166 // Ðапомним наÑÑÑойки 167 D.container.block.data('arcticmodalParentEl', $this); 168 $this.data('arcticmodal', D); 169 modals = $.merge(modals, $this); 170 171 // ÐоказаÑÑ 172 $.proxy(actions.show, $this)(); 173 if (D.type=='html') return $this; 174 175 // Ajax-загÑÑзка 176 if (D.ajax.beforeSend!=undefined) { 177 var fn_beforeSend = D.ajax.beforeSend; 178 delete D.ajax.beforeSend; 179 } 180 if (D.ajax.success!=undefined) { 181 var fn_success = D.ajax.success; 182 delete D.ajax.success; 183 } 184 if (D.ajax.error!=undefined) { 185 var fn_error = D.ajax.error; 186 delete D.ajax.error; 187 } 188 var o = $.extend(true, { 189 url: D.url, 190 beforeSend: function() { 191 if (fn_beforeSend==undefined) { 192 D.body.html('<div class="arcticmodal-loading" />'); 193 } else { 194 fn_beforeSend(D, $this); 195 } 196 }, 197 success: function(responce) { 198 199 // СобÑÑие поÑле загÑÑзки до показа ÑодеÑжимого 200 $this.trigger('afterLoading'); 201 D.afterLoading(D, $this, responce); 202 203 if (fn_success==undefined) { 204 D.body.html(responce); 205 } else { 206 fn_success(D, $this, responce); 207 } 208 modal.prepare_body(D, $this); 209 210 // СобÑÑие поÑле загÑÑзки поÑле оÑобÑÐ°Ð¶ÐµÐ½Ð¸Ñ ÑодеÑжимого 211 $this.trigger('afterLoadingOnShow'); 212 D.afterLoadingOnShow(D, $this, responce); 213 214 }, 215 error: function() { 216 217 // СобÑÑие пÑи оÑибке загÑÑзки 218 $this.trigger('errorLoading'); 219 D.errorLoading(D, $this); 220 221 if (fn_error==undefined) { 222 D.body.html(D.errors.tpl); 223 $('.arcticmodal-error', D.body).html(D.errors.ajax_unsuccessful_load); 224 $('.arcticmodal-close', D.body).click(function() { 225 $this.arcticmodal('close'); 226 return false; 227 }); 228 if (D.errors.autoclose_delay) 229 setTimeout(function() { 230 $this.arcticmodal('close'); 231 }, D.errors.autoclose_delay); 232 } else { 233 fn_error(D, $this); 234 } 235 } 236 }, D.ajax); 237 D.ajax_request = $.ajax(o); 238 239 // ÐапомниÑÑ Ð½Ð°ÑÑÑойки 240 $this.data('arcticmodal', D); 241 242 }, 243 244 245 // ÐниÑиализаÑÐ¸Ñ 246 init: function(options) { 247 options = $.extend(true, {}, default_options, options); 248 if ($.isFunction(this)) { 249 if (options==undefined) { 250 $.error('jquery.arcticmodal: Uncorrect parameters'); 251 return; 252 } 253 if (options.type=='') { 254 $.error('jquery.arcticmodal: Don\'t set parameter "type"'); 255 return; 256 } 257 switch (options.type) { 258 case 'html': 259 if (options.content=='') { 260 $.error('jquery.arcticmodal: Don\'t set parameter "content"'); 261 return 262 } 263 var c = options.content; 264 options.content = ''; 265 266 return modal.init_el($(c), options); 267 break; 268 case 'ajax': 269 if (options.url=='') { 270 $.error('jquery.arcticmodal: Don\'t set parameter "url"'); 271 return; 272 } 273 return modal.init_el($('<div />'), options); 274 break; 275 } 276 } else { 277 return this.each(function() { 278 modal.init_el($(this), $.extend(true, {}, options)); 279 }); 280 } 281 } 282 283 284 }; 285 286 287 var actions = { 288 289 290 // ÐоказаÑÑ 291 show: function() { 292 var $this = modal.getParentEl(this); 293 if ($this===false) { 294 $.error('jquery.arcticmodal: Uncorrect call'); 295 return; 296 } 297 var D = $this.data('arcticmodal'); 298
299 // ÐобавиÑÑ overlay и container 300 D.overlay.block.hide(); 301 D.container.block.hide(); 302 $('BODY').append(D.overlay.block); 303 $('BODY').append(D.container.block); 304 305 // СобÑÑие 306 D.beforeOpen(D, $this); 307 $this.trigger('beforeOpen'); 308 309 // Wrap 310 if (D.wrap.css('overflow')!='hidden') { 311 D.wrap.data('arcticmodalOverflow', D.wrap.css('overflow')); 312 var w1 = D.wrap.outerWidth(true); 313 D.wrap.css('overflow', 'hidden'); 314 var w2 = D.wrap.outerWidth(true); 315 if (w2!=w1) 316 D.wrap.css('marginRight', (w2 - w1) + 'px'); 317 } 318 319 // СкÑÑÑÑ Ð¿ÑедÑдÑÑие овеÑлеи 320 modals.not($this).each(function() { 321 var d = $(this).data('arcticmodal'); 322 d.overlay.block.hide(); 323 }); 324 325 // ÐоказаÑÑ 326 modal.transition(D.overlay.block, 'show', modals.length>1 ? {type: 'none'} : D.openEffect); 327 modal.transition(D.container.block, 'show', modals.length>1 ? {type: 'none'} : D.openEffect, function() { 328 D.afterOpen(D, $this); 329 $this.trigger('afterOpen'); 330 }); 331 332 return $this; 333 }, 334 335 336 // ÐакÑÑÑÑ 337 close: function() { 338 if ($.isFunction(this)) { 339 modals.each(function() { 340 $(this).arcticmodal('close'); 341 }); 342 } else { 343 return this.each(function() { 344 var $this = modal.getParentEl(this); 345 if ($this===false) { 346 $.error('jquery.arcticmodal: Uncorrect call'); 347 return; 348 } 349 var D = $this.data('arcticmodal'); 350 351 // СобÑÑие пеÑед закÑÑÑием 352 if (D.beforeClose(D, $this)===false) return; 353 $this.trigger('beforeClose'); 354 355 // ÐоказаÑÑ Ð¿ÑедÑдÑÑие овеÑлеи 356 modals.not($this).last().each(function() { 357 var d = $(this).data('arcticmodal'); 358 d.overlay.block.show(); 359 }); 360 361 modal.transition(D.overlay.block, 'hide', modals.length>1 ? {type: 'none'} : D.closeEffect); 362 modal.transition(D.container.block, 'hide', modals.length>1 ? {type: 'none'} : D.closeEffect, function() { 363 364 // СобÑÑие поÑле закÑÑÑÐ¸Ñ 365 D.afterClose(D, $this); 366 $this.trigger('afterClose'); 367 368 // ÐÑли не клониÑовали - веÑнÑм на меÑÑо 369 if (!D.clone) 370 $('#arcticmodalReserve' + D.modalID).replaceWith(D.body.find('>*')); 371 372 D.overlay.block.remove(); 373 D.container.block.remove(); 374 $this.data('arcticmodal', null); 375 if (!$('.arcticmodal-container').length) { 376 if (D.wrap.data('arcticmodalOverflow')) 377 D.wrap.css('overflow', D.wrap.data('arcticmodalOverflow')); 378 D.wrap.css('marginRight', 0); 379 } 380 381 }); 382 383 if (D.type=='ajax') 384 D.ajax_request.abort(); 385 386 modals = modals.not($this); 387 }); 388 } 389 }, 390 391 392 // УÑÑановиÑÑ Ð¾Ð¿Ñии по-ÑмолÑÐ°Ð½Ð¸Ñ 393 setDefault: function(options) { 394 $.extend(true, default_options, options); 395 } 396 397 398 }; 399 400 401 $(function() { 402 default_options.wrap = $((document.all && !document.querySelector) ? 'html' : 'body'); 403 }); 404 405 406 // ÐакÑÑÑие пÑи нажаÑии Escape 407 $(document).bind('keyup.arcticmodal', function(e) { 408 var m = modals.last(); 409 if (!m.length) return; 410 var D = m.data('arcticmodal'); 411 if (D.closeOnEsc && (e.keyCode===27)) 412 m.arcticmodal('close'); 413 }); 414 415 416 $.arcticmodal = $.fn.arcticmodal = function(method) { 417 418 if (actions[method]) { 419 return actions[method].apply(this, Array.prototype.slice.call(arguments, 1)); 420 } else if (typeof method==='object' || !method) { 421 return modal.init.apply(this, arguments); 422 } else { 423 $.error('jquery.arcticmodal: Method ' + method + ' does not exist'); 424 } 425 426 }; 427 428 429})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.