modal.js 6.32 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: modal.js v3.1.0
3
 * http://getbootstrap.com/javascript/#modals
4
 * ========================================================================
Zlatan Vasović's avatar
Zlatan Vasović committed
5
 * Copyright 2013 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

Jacob Thornton's avatar
Jacob Thornton committed
21
    if (this.options.remote) this.$element.load(this.options.remote)
fat's avatar
fat committed
22
  }
Jacob Thornton's avatar
Jacob Thornton committed
23

fat's avatar
fat committed
24
  Modal.DEFAULTS = {
Zlatan Vasović's avatar
Zlatan Vasović committed
25
26
27
    backdrop: true,
    keyboard: true,
    show: true
fat's avatar
fat committed
28
  }
29

Jacob Thornton's avatar
Jacob Thornton committed
30
31
  Modal.prototype.toggle = function (_relatedTarget) {
    return this[!this.isShown ? 'show' : 'hide'](_relatedTarget)
fat's avatar
fat committed
32
  }
Jacob Thornton's avatar
Jacob Thornton committed
33

Jacob Thornton's avatar
Jacob Thornton committed
34
  Modal.prototype.show = function (_relatedTarget) {
fat's avatar
fat committed
35
    var that = this
Jacob Thornton's avatar
Jacob Thornton committed
36
    var e    = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })
Jacob Thornton's avatar
Jacob Thornton committed
37

fat's avatar
fat committed
38
    this.$element.trigger(e)
39

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

fat's avatar
fat committed
42
    this.isShown = true
43

fat's avatar
fat committed
44
    this.escape()
45

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

fat's avatar
fat committed
48
49
    this.backdrop(function () {
      var transition = $.support.transition && that.$element.hasClass('fade')
50

fat's avatar
fat committed
51
      if (!that.$element.parent().length) {
Jacob Thornton's avatar
Jacob Thornton committed
52
        that.$element.appendTo(document.body) // don't move modals dom position
fat's avatar
fat committed
53
      }
54

fat's avatar
fat committed
55
      that.$element.show()
56

fat's avatar
fat committed
57
58
59
      if (transition) {
        that.$element[0].offsetWidth // force reflow
      }
60

fat's avatar
fat committed
61
62
63
      that.$element
        .addClass('in')
        .attr('aria-hidden', false)
64

fat's avatar
fat committed
65
      that.enforceFocus()
66

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

fat's avatar
fat committed
69
      transition ?
fat's avatar
fat committed
70
        that.$element.find('.modal-dialog') // wait for modal to slide in
71
          .one($.support.transition.end, function () {
Jacob Thornton's avatar
Jacob Thornton committed
72
            that.$element.focus().trigger(e)
73
74
          })
          .emulateTransitionEnd(300) :
Jacob Thornton's avatar
Jacob Thornton committed
75
        that.$element.focus().trigger(e)
fat's avatar
fat committed
76
77
    })
  }
78

fat's avatar
fat committed
79
  Modal.prototype.hide = function (e) {
fat's avatar
fat committed
80
    if (e) e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
81

82
    e = $.Event('hide.bs.modal')
83

fat's avatar
fat committed
84
    this.$element.trigger(e)
85

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

fat's avatar
fat committed
88
    this.isShown = false
89

fat's avatar
fat committed
90
    this.escape()
Jacob Thornton's avatar
Jacob Thornton committed
91

92
    $(document).off('focusin.bs.modal')
93

fat's avatar
fat committed
94
95
96
    this.$element
      .removeClass('in')
      .attr('aria-hidden', true)
Jacob Thornton's avatar
Jacob Thornton committed
97
      .off('click.dismiss.modal')
Jacob Thornton's avatar
Jacob Thornton committed
98

fat's avatar
fat committed
99
    $.support.transition && this.$element.hasClass('fade') ?
100
101
102
      this.$element
        .one($.support.transition.end, $.proxy(this.hideModal, this))
        .emulateTransitionEnd(300) :
fat's avatar
fat committed
103
104
      this.hideModal()
  }
Jacob Thornton's avatar
Jacob Thornton committed
105

fat's avatar
fat committed
106
  Modal.prototype.enforceFocus = function () {
Jacob Thornton's avatar
Jacob Thornton committed
107
108
    $(document)
      .off('focusin.bs.modal') // guard against infinite focus loop
fat's avatar
fat committed
109
110
111
112
113
      .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
114
  }
Jacob Thornton's avatar
Jacob Thornton committed
115

fat's avatar
fat committed
116
117
  Modal.prototype.escape = function () {
    if (this.isShown && this.options.keyboard) {
fat's avatar
fat committed
118
      this.$element.on('keyup.dismiss.bs.modal', $.proxy(function (e) {
fat's avatar
fat committed
119
        e.which == 27 && this.hide()
fat's avatar
fat committed
120
      }, this))
fat's avatar
fat committed
121
    } else if (!this.isShown) {
122
      this.$element.off('keyup.dismiss.bs.modal')
fat's avatar
fat committed
123
124
    }
  }
125

fat's avatar
fat committed
126
127
128
129
130
  Modal.prototype.hideModal = function () {
    var that = this
    this.$element.hide()
    this.backdrop(function () {
      that.removeBackdrop()
131
      that.$element.trigger('hidden.bs.modal')
fat's avatar
fat committed
132
133
    })
  }
Jacob Thornton's avatar
Jacob Thornton committed
134

fat's avatar
fat committed
135
  Modal.prototype.removeBackdrop = function () {
fat's avatar
fat committed
136
137
138
    this.$backdrop && this.$backdrop.remove()
    this.$backdrop = null
  }
139

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

fat's avatar
fat committed
143
144
    if (this.isShown && this.options.backdrop) {
      var doAnimate = $.support.transition && animate
145

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

Jacob Thornton's avatar
Jacob Thornton committed
149
      this.$element.on('click.dismiss.modal', $.proxy(function (e) {
fat's avatar
fat committed
150
151
152
153
154
        if (e.target !== e.currentTarget) return
        this.options.backdrop == 'static'
          ? this.$element[0].focus.call(this.$element[0])
          : this.hide.call(this)
      }, this))
155

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

fat's avatar
fat committed
158
      this.$backdrop.addClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
159

fat's avatar
fat committed
160
      if (!callback) return
161

fat's avatar
fat committed
162
      doAnimate ?
163
164
165
        this.$backdrop
          .one($.support.transition.end, callback)
          .emulateTransitionEnd(150) :
fat's avatar
fat committed
166
        callback()
167

fat's avatar
fat committed
168
169
    } else if (!this.isShown && this.$backdrop) {
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
170

Chris Rebert's avatar
Chris Rebert committed
171
      $.support.transition && this.$element.hasClass('fade') ?
172
173
174
        this.$backdrop
          .one($.support.transition.end, callback)
          .emulateTransitionEnd(150) :
fat's avatar
fat committed
175
        callback()
176

fat's avatar
fat committed
177
178
179
    } else if (callback) {
      callback()
    }
Jacob Thornton's avatar
Jacob Thornton committed
180
181
182
  }


fat's avatar
fat committed
183
184
  // MODAL PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
185

186
187
  var old = $.fn.modal

Jacob Thornton's avatar
Jacob Thornton committed
188
  $.fn.modal = function (option, _relatedTarget) {
Jacob Thornton's avatar
Jacob Thornton committed
189
    return this.each(function () {
fat's avatar
fat committed
190
      var $this   = $(this)
191
      var data    = $this.data('bs.modal')
fat's avatar
fat committed
192
193
      var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)

194
      if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
Jacob Thornton's avatar
Jacob Thornton committed
195
196
      if (typeof option == 'string') data[option](_relatedTarget)
      else if (options.show) data.show(_relatedTarget)
Jacob Thornton's avatar
Jacob Thornton committed
197
    })
Jacob Thornton's avatar
Jacob Thornton committed
198
199
  }

200
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
201

202

fat's avatar
fat committed
203
204
  // MODAL NO CONFLICT
  // =================
205
206
207
208
209
210
211

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


fat's avatar
fat committed
212
213
  // MODAL DATA-API
  // ==============
214

215
  $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
fat's avatar
fat committed
216
217
218
    var $this   = $(this)
    var href    = $this.attr('href')
    var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7
Jacob Thornton's avatar
Jacob Thornton committed
219
    var option  = $target.data('modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
220
221
222
223

    e.preventDefault()

    $target
Jacob Thornton's avatar
Jacob Thornton committed
224
      .modal(option, this)
225
      .one('hide', function () {
226
        $this.is(':visible') && $this.focus()
227
      })
Jacob Thornton's avatar
Jacob Thornton committed
228
  })
229

Jacob Thornton's avatar
Jacob Thornton committed
230
  $(document)
fat's avatar
fat committed
231
    .on('show.bs.modal',  '.modal', function () { $(document.body).addClass('modal-open') })
Jacob Thornton's avatar
Jacob Thornton committed
232
    .on('hidden.bs.modal', '.modal', function () { $(document.body).removeClass('modal-open') })
233

234
}(jQuery);