cielavenir

twitter-bootstrap modal patched

Oct 11th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* =========================================================
  2.  * bootstrap-modal.js v2.1.1
  3.  * http://twitter.github.com/bootstrap/javascript.html#modals
  4.  * =========================================================
  5.  * Copyright 2012 Twitter, Inc.
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  * http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * ========================================================= */
  19.  
  20.  
  21. !function ($) {
  22.  
  23.   "use strict"; // jshint ;_;
  24.  
  25.  
  26.  /* MODAL CLASS DEFINITION
  27.   * ====================== */
  28.  
  29.   var Modal = function (element, options) {
  30.     this.options = options
  31.     this.$element = $(element)
  32.       .delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this))
  33.     var elem=this.$element;
  34.     this.options.remote && this.$element.find('.modal-body').load(this.options.remote,
  35.       function(responseText, textStatus, xhr){elem.trigger('remoteload');}
  36.     )
  37.   }
  38.  
  39.   Modal.prototype = {
  40.  
  41.       constructor: Modal
  42.  
  43.     , toggle: function () {
  44.         return this[!this.isShown ? 'show' : 'hide']()
  45.       }
  46.  
  47.     , show: function () {
  48.         var that = this
  49.           , e = $.Event('show')
  50.  
  51.         this.$element.trigger(e)
  52.  
  53.         if (this.isShown || e.isDefaultPrevented()) return
  54.  
  55.         $('body').addClass('modal-open')
  56.  
  57.         this.isShown = true
  58.  
  59.         this.escape()
  60.  
  61.         this.backdrop(function () {
  62.           var transition = $.support.transition && that.$element.hasClass('fade')
  63.  
  64.           if (!that.$element.parent().length) {
  65.             that.$element.appendTo(document.body) //don't move modals dom position
  66.           }
  67.  
  68.           that.$element
  69.             .show()
  70.  
  71.           if (transition) {
  72.             that.$element[0].offsetWidth // force reflow
  73.           }
  74.  
  75.           that.$element
  76.             .addClass('in')
  77.             .attr('aria-hidden', false)
  78.             .focus()
  79.  
  80.           that.enforceFocus()
  81.  
  82.           transition ?
  83.             that.$element.one($.support.transition.end, function () { that.$element.trigger('shown') }) :
  84.             that.$element.trigger('shown')
  85.  
  86.         })
  87.       }
  88.  
  89.     , hide: function (e) {
  90.         e && e.preventDefault()
  91.  
  92.         var that = this
  93.  
  94.         e = $.Event('hide')
  95.  
  96.         this.$element.trigger(e)
  97.  
  98.         if (!this.isShown || e.isDefaultPrevented()) return
  99.  
  100.         this.isShown = false
  101.  
  102.         $('body').removeClass('modal-open')
  103.  
  104.         this.escape()
  105.  
  106.         $(document).off('focusin.modal')
  107.  
  108.         this.$element
  109.           .removeClass('in')
  110.           .attr('aria-hidden', true)
  111.  
  112.         $.support.transition && this.$element.hasClass('fade') ?
  113.           this.hideWithTransition() :
  114.           this.hideModal()
  115.       }
  116.  
  117.     , enforceFocus: function () {
  118.         var that = this
  119.         $(document).on('focusin.modal', function (e) {
  120.           if (that.$element[0] !== e.target && !that.$element.has(e.target).length) {
  121.             that.$element.focus()
  122.           }
  123.         })
  124.       }
  125.  
  126.     , escape: function () {
  127.         var that = this
  128.         if (this.isShown && this.options.keyboard) {
  129.           this.$element.on('keyup.dismiss.modal', function ( e ) {
  130.             e.which == 27 && that.hide()
  131.           })
  132.         } else if (!this.isShown) {
  133.           this.$element.off('keyup.dismiss.modal')
  134.         }
  135.       }
  136.  
  137.     , hideWithTransition: function () {
  138.         var that = this
  139.           , timeout = setTimeout(function () {
  140.               that.$element.off($.support.transition.end)
  141.               that.hideModal()
  142.             }, 500)
  143.  
  144.         this.$element.one($.support.transition.end, function () {
  145.           clearTimeout(timeout)
  146.           that.hideModal()
  147.         })
  148.       }
  149.  
  150.     , hideModal: function (that) {
  151.         this.$element
  152.           .hide()
  153.           .trigger('hidden')
  154.  
  155.         this.backdrop()
  156.       }
  157.  
  158.     , removeBackdrop: function () {
  159.         this.$backdrop.remove()
  160.         this.$backdrop = null
  161.       }
  162.  
  163.     , backdrop: function (callback) {
  164.         var that = this
  165.           , animate = this.$element.hasClass('fade') ? 'fade' : ''
  166.  
  167.         if (this.isShown && this.options.backdrop) {
  168.           var doAnimate = $.support.transition && animate
  169.  
  170.           this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
  171.             .appendTo(document.body)
  172.  
  173.           if (this.options.backdrop != 'static') {
  174.             this.$backdrop.click($.proxy(this.hide, this))
  175.           }
  176.  
  177.           if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
  178.  
  179.           this.$backdrop.addClass('in')
  180.  
  181.           doAnimate ?
  182.             this.$backdrop.one($.support.transition.end, callback) :
  183.             callback()
  184.  
  185.         } else if (!this.isShown && this.$backdrop) {
  186.           this.$backdrop.removeClass('in')
  187.  
  188.           $.support.transition && this.$element.hasClass('fade')?
  189.             this.$backdrop.one($.support.transition.end, $.proxy(this.removeBackdrop, this)) :
  190.             this.removeBackdrop()
  191.  
  192.         } else if (callback) {
  193.           callback()
  194.         }
  195.       }
  196.   }
  197.  
  198.  
  199.  /* MODAL PLUGIN DEFINITION
  200.   * ======================= */
  201.  
  202.   $.fn.modal = function (option) {
  203.     return this.each(function () {
  204.       var $this = $(this)
  205.         , data = $this.data('modal')
  206.         , options = $.extend({}, $.fn.modal.defaults, $this.data(), typeof option == 'object' && option)
  207.       if (!data) $this.data('modal', (data = new Modal(this, options)))
  208.       if (typeof option == 'string') data[option]()
  209.       else if (options.show) data.show()
  210.     })
  211.   }
  212.  
  213.   $.fn.modal.defaults = {
  214.       backdrop: true
  215.     , keyboard: true
  216.     , show: true
  217.   }
  218.  
  219.   $.fn.modal.Constructor = Modal
  220.  
  221.  
  222.  /* MODAL DATA-API
  223.   * ============== */
  224.  
  225.   $(function () {
  226.     $('body').on('click.modal.data-api', '[data-toggle="modal"]', function ( e ) {
  227.       var $this = $(this)
  228.         , href = $this.attr('href')
  229.         , $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7
  230.         , option = $target.data('modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
  231.  
  232.       e.preventDefault()
  233.  
  234.       $target
  235.         .modal(option)
  236.         .one('hide', function () {
  237.           $this.focus()
  238.         })
  239.     })
  240.   })
  241.  
  242. }(window.jQuery);
Advertisement
Add Comment
Please, Sign In to add comment