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