modal.js 6.52 KB
Newer Older
1
/* ========================================================================
2
 * Bootstrap: modal.js v3.0.3
3
 * http://getbootstrap.com/javascript/#modals
4
 * ========================================================================
Zlatan Vasović's avatar
Zlatan Vasović committed
5
 * Copyright 2014 Twitter, Inc.
6
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
7
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
8
9


XhmikosR's avatar
XhmikosR committed
10
+function ($) { 'use strict';
11

fat's avatar
fat committed
12
13
  // MODAL CLASS DEFINITION
  // ======================
Jacob Thornton's avatar
Jacob Thornton committed
14

Jacob Thornton's avatar
Jacob Thornton committed
15
  var Modal = function (element, options) {
fat's avatar
fat committed
16
    this.options   = options
fat's avatar
fat committed
17
    this.$element  = $(element)
fat's avatar
fat committed
18
19
    this.$backdrop =
    this.isShown   = null
Jacob Thornton's avatar
Jacob Thornton committed
20

21
22
23
24
25
26
27
    if (this.options.remote) {
      this.$element
        .find('.modal-content')
        .load(this.options.remote, $.proxy(function () {
          this.$element.trigger('loaded.bs.modal')
        }, this))
    }
fat's avatar
fat committed
28
  }
Jacob Thornton's avatar
Jacob Thornton committed
29

fat's avatar
fat committed
30
  Modal.DEFAULTS = {
Zlatan Vasović's avatar
Zlatan Vasović committed
31
32
33
    backdrop: true,
    keyboard: true,
    show: true
fat's avatar
fat committed
34
  }
35

Jacob Thornton's avatar
Jacob Thornton committed
36
37
  Modal.prototype.toggle = function (_relatedTarget) {
    return this[!this.isShown ? 'show' : 'hide'](_relatedTarget)
fat's avatar
fat committed
38
  }
Jacob Thornton's avatar
Jacob Thornton committed
39

Jacob Thornton's avatar
Jacob Thornton committed
40
  Modal.prototype.show = function (_relatedTarget) {
fat's avatar
fat committed
41
    var that = this
Jacob Thornton's avatar
Jacob Thornton committed
42
    var e    = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })
Jacob Thornton's avatar
Jacob Thornton committed
43

fat's avatar
fat committed
44
    this.$element.trigger(e)
45

fat's avatar
fat committed
46
    if (this.isShown || e.isDefaultPrevented()) return
Jacob Thornton's avatar
Jacob Thornton committed
47

fat's avatar
fat committed
48
    this.isShown = true
49

fat's avatar
fat committed
50
    this.escape()
51

fat's avatar
fat committed
52
53
    this.$element.on('click.dismiss.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))

fat's avatar
fat committed
54
55
    this.backdrop(function () {
      var transition = $.support.transition && that.$element.hasClass('fade')
56

fat's avatar
fat committed
57
      if (!that.$element.parent().length) {
Jacob Thornton's avatar
Jacob Thornton committed
58
        that.$element.appendTo(document.body) // don't move modals dom position
fat's avatar
fat committed
59
      }
60

61
62
63
      that.$element
        .show()
        .scrollTop(0)
64

fat's avatar
fat committed
65
66
67
      if (transition) {
        that.$element[0].offsetWidth // force reflow
      }
68

fat's avatar
fat committed
69
70
71
      that.$element
        .addClass('in')
        .attr('aria-hidden', false)
72

fat's avatar
fat committed
73
      that.enforceFocus()
74

Jacob Thornton's avatar
Jacob Thornton committed
75
76
      var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })

fat's avatar
fat committed
77
      transition ?
fat's avatar
fat committed
78
        that.$element.find('.modal-dialog') // wait for modal to slide in
79
          .one($.support.transition.end, function () {
Jacob Thornton's avatar
Jacob Thornton committed
80
            that.$element.focus().trigger(e)
81
82
          })
          .emulateTransitionEnd(300) :
Jacob Thornton's avatar
Jacob Thornton committed
83
        that.$element.focus().trigger(e)
fat's avatar
fat committed
84
85
    })
  }
86

fat's avatar
fat committed
87
  Modal.prototype.hide = function (e) {
fat's avatar
fat committed
88
    if (e) e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
89

90
    e = $.Event('hide.bs.modal')
91

fat's avatar
fat committed
92
    this.$element.trigger(e)
93

fat's avatar
fat committed
94
    if (!this.isShown || e.isDefaultPrevented()) return
95

fat's avatar
fat committed
96
    this.isShown = false
97

fat's avatar
fat committed
98
    this.escape()
Jacob Thornton's avatar
Jacob Thornton committed
99

100
    $(document).off('focusin.bs.modal')
101

fat's avatar
fat committed
102
103
104
    this.$element
      .removeClass('in')
      .attr('aria-hidden', true)
Jacob Thornton's avatar
Jacob Thornton committed
105
      .off('click.dismiss.modal')
Jacob Thornton's avatar
Jacob Thornton committed
106

fat's avatar
fat committed
107
    $.support.transition && this.$element.hasClass('fade') ?
108
109
110
      this.$element
        .one($.support.transition.end, $.proxy(this.hideModal, this))
        .emulateTransitionEnd(300) :
fat's avatar
fat committed
111
112
      this.hideModal()
  }
Jacob Thornton's avatar
Jacob Thornton committed
113

fat's avatar
fat committed
114
  Modal.prototype.enforceFocus = function () {
Jacob Thornton's avatar
Jacob Thornton committed
115
116
    $(document)
      .off('focusin.bs.modal') // guard against infinite focus loop
fat's avatar
fat committed
117
118
119
120
121
      .on('focusin.bs.modal', $.proxy(function (e) {
        if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
          this.$element.focus()
        }
      }, this))
fat's avatar
fat committed
122
  }
Jacob Thornton's avatar
Jacob Thornton committed
123

fat's avatar
fat committed
124
125
  Modal.prototype.escape = function () {
    if (this.isShown && this.options.keyboard) {
fat's avatar
fat committed
126
      this.$element.on('keyup.dismiss.bs.modal', $.proxy(function (e) {
fat's avatar
fat committed
127
        e.which == 27 && this.hide()
fat's avatar
fat committed
128
      }, this))
fat's avatar
fat committed
129
    } else if (!this.isShown) {
130
      this.$element.off('keyup.dismiss.bs.modal')
fat's avatar
fat committed
131
132
    }
  }
133

fat's avatar
fat committed
134
135
136
137
138
  Modal.prototype.hideModal = function () {
    var that = this
    this.$element.hide()
    this.backdrop(function () {
      that.removeBackdrop()
139
      that.$element.trigger('hidden.bs.modal')
fat's avatar
fat committed
140
141
    })
  }
Jacob Thornton's avatar
Jacob Thornton committed
142

fat's avatar
fat committed
143
  Modal.prototype.removeBackdrop = function () {
fat's avatar
fat committed
144
145
146
    this.$backdrop && this.$backdrop.remove()
    this.$backdrop = null
  }
147

fat's avatar
fat committed
148
  Modal.prototype.backdrop = function (callback) {
fat's avatar
fat committed
149
    var animate = this.$element.hasClass('fade') ? 'fade' : ''
150

fat's avatar
fat committed
151
152
    if (this.isShown && this.options.backdrop) {
      var doAnimate = $.support.transition && animate
153

fat's avatar
fat committed
154
155
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(document.body)
156

Jacob Thornton's avatar
Jacob Thornton committed
157
      this.$element.on('click.dismiss.modal', $.proxy(function (e) {
fat's avatar
fat committed
158
159
160
161
162
        if (e.target !== e.currentTarget) return
        this.options.backdrop == 'static'
          ? this.$element[0].focus.call(this.$element[0])
          : this.hide.call(this)
      }, this))
163

fat's avatar
fat committed
164
      if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
165

fat's avatar
fat committed
166
      this.$backdrop.addClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
167

fat's avatar
fat committed
168
      if (!callback) return
169

fat's avatar
fat committed
170
      doAnimate ?
171
172
173
        this.$backdrop
          .one($.support.transition.end, callback)
          .emulateTransitionEnd(150) :
fat's avatar
fat committed
174
        callback()
175

fat's avatar
fat committed
176
177
    } else if (!this.isShown && this.$backdrop) {
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
178

Chris Rebert's avatar
Chris Rebert committed
179
      $.support.transition && this.$element.hasClass('fade') ?
180
181
182
        this.$backdrop
          .one($.support.transition.end, callback)
          .emulateTransitionEnd(150) :
fat's avatar
fat committed
183
        callback()
184

fat's avatar
fat committed
185
186
187
    } else if (callback) {
      callback()
    }
Jacob Thornton's avatar
Jacob Thornton committed
188
189
190
  }


fat's avatar
fat committed
191
192
  // MODAL PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
193

194
195
  var old = $.fn.modal

Jacob Thornton's avatar
Jacob Thornton committed
196
  $.fn.modal = function (option, _relatedTarget) {
Jacob Thornton's avatar
Jacob Thornton committed
197
    return this.each(function () {
fat's avatar
fat committed
198
      var $this   = $(this)
199
      var data    = $this.data('bs.modal')
fat's avatar
fat committed
200
201
      var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)

202
      if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
Jacob Thornton's avatar
Jacob Thornton committed
203
204
      if (typeof option == 'string') data[option](_relatedTarget)
      else if (options.show) data.show(_relatedTarget)
Jacob Thornton's avatar
Jacob Thornton committed
205
    })
Jacob Thornton's avatar
Jacob Thornton committed
206
207
  }

208
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
209

210

fat's avatar
fat committed
211
212
  // MODAL NO CONFLICT
  // =================
213
214
215
216
217
218
219

  $.fn.modal.noConflict = function () {
    $.fn.modal = old
    return this
  }


fat's avatar
fat committed
220
221
  // MODAL DATA-API
  // ==============
222

223
  $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
fat's avatar
fat committed
224
225
226
    var $this   = $(this)
    var href    = $this.attr('href')
    var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7
fat's avatar
fat committed
227
    var option  = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
228

229
    if ($this.is('a')) e.preventDefault()
230
231

    $target
Jacob Thornton's avatar
Jacob Thornton committed
232
      .modal(option, this)
233
      .one('hide', function () {
234
        $this.is(':visible') && $this.focus()
235
      })
Jacob Thornton's avatar
Jacob Thornton committed
236
  })
237

Jacob Thornton's avatar
Jacob Thornton committed
238
  $(document)
fat's avatar
fat committed
239
    .on('show.bs.modal',  '.modal', function () { $(document.body).addClass('modal-open') })
Jacob Thornton's avatar
Jacob Thornton committed
240
    .on('hidden.bs.modal', '.modal', function () { $(document.body).removeClass('modal-open') })
241

242
}(jQuery);