diff options
| author | fat <[email protected]> | 2013-05-16 17:18:15 -0700 |
|---|---|---|
| committer | fat <[email protected]> | 2013-05-16 17:18:15 -0700 |
| commit | 149ecd21bf7c56df897088d09ca309eb43a7dd9d (patch) | |
| tree | 9138ea6d99244c3abf114069482c8c397a0bc2cd /js/modal.js | |
| parent | 53bc93282fbeab3c1b779f1914ab040290668369 (diff) | |
| download | bootstrap-149ecd21bf7c56df897088d09ca309eb43a7dd9d.tar.xz bootstrap-149ecd21bf7c56df897088d09ca309eb43a7dd9d.zip | |
straight trash wang
Diffstat (limited to 'js/modal.js')
| -rw-r--r-- | js/modal.js | 285 |
1 files changed, 138 insertions, 147 deletions
diff --git a/js/modal.js b/js/modal.js index 9c8818812..7acd3930a 100644 --- a/js/modal.js +++ b/js/modal.js @@ -18,207 +18,198 @@ * ========================================================= */ -!function ($) { +!function ($) { "use strict"; - "use strict"; // jshint ;_; - - - /* MODAL CLASS DEFINITION - * ====================== */ + // MODAL CLASS DEFINITION + // ====================== var Modal = function (element, options) { - this.options = options - this.$element = $(element) - .delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this)) - this.options.remote && this.$element.find('.modal-body').load(this.options.remote) - } + this.options = options + this.$element = $(element).delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this)) + this.$backdrop = + this.isShown = null - Modal.prototype = { + if (this.options.remote) this.$element.find('.modal-body').load(this.options.remote) + } - constructor: Modal + Modal.DEFAULTS = { + backdrop: true + , keyboard: true + , show: true + } - , toggle: function () { - return this[!this.isShown ? 'show' : 'hide']() - } + Modal.prototype.toggle = function () { + return this[!this.isShown ? 'show' : 'hide']() + } - , show: function () { - var that = this - , e = $.Event('show') + Modal.prototype.show = function () { + var that = this + var e = $.Event('bs:modal:show') - this.$element.trigger(e) + this.$element.trigger(e) - if (this.isShown || e.isDefaultPrevented()) return + if (this.isShown || e.isDefaultPrevented()) return - this.isShown = true + this.isShown = true - this.escape() + this.escape() - this.backdrop(function () { - var transition = $.support.transition && that.$element.hasClass('fade') + this.backdrop(function () { + var transition = $.support.transition && that.$element.hasClass('fade') - if (!that.$element.parent().length) { - that.$element.appendTo(document.body) //don't move modals dom position - } + if (!that.$element.parent().length) { + that.$element.appendTo(document.body) //don't move modals dom position + } - that.$element.show() + that.$element.show() - if (transition) { - that.$element[0].offsetWidth // force reflow - } + if (transition) { + that.$element[0].offsetWidth // force reflow + } - that.$element - .addClass('in') - .attr('aria-hidden', false) + that.$element + .addClass('in') + .attr('aria-hidden', false) - that.enforceFocus() + that.enforceFocus() - transition ? - that.$element.one($.support.transition.end, function () { that.$element.focus().trigger('shown') }) : - that.$element.focus().trigger('shown') + transition ? + that.$element.one($.support.transition.end, function () { that.$element.focus().trigger('bs:modal:shown') }) : + that.$element.focus().trigger('bs:modal:shown') - }) - } + }) + } - , hide: function (e) { - e && e.preventDefault() + Modal.prototype.show = function (e) { + if (e) e.preventDefault() - var that = this + e = $.Event('bs:modal:hide') - e = $.Event('hide') + this.$element.trigger(e) - this.$element.trigger(e) + if (!this.isShown || e.isDefaultPrevented()) return - if (!this.isShown || e.isDefaultPrevented()) return + this.isShown = false - this.isShown = false + this.escape() - this.escape() + $(document).off('focusin.modal') - $(document).off('focusin.modal') + this.$element + .removeClass('in') + .attr('aria-hidden', true) - this.$element - .removeClass('in') - .attr('aria-hidden', true) + $.support.transition && this.$element.hasClass('fade') ? + this.hideWithTransition() : + this.hideModal() + } - $.support.transition && this.$element.hasClass('fade') ? - this.hideWithTransition() : - this.hideModal() + Modal.prototype.enforceFocus = function () { + $(document).on('focusin.modal', function (e) { + if (this.$element[0] !== e.target && !this.$element.has(e.target).length) { + this.$element.focus() } + }, this) + } - , enforceFocus: function () { - var that = this - $(document).on('focusin.modal', function (e) { - if (that.$element[0] !== e.target && !that.$element.has(e.target).length) { - that.$element.focus() - } - }) - } + Modal.prototype.escape = function () { + if (this.isShown && this.options.keyboard) { + this.$element.on('keyup.dismiss.modal', function ( e ) { + e.which == 27 && this.hide() + }, this) + } else if (!this.isShown) { + this.$element.off('keyup.dismiss.modal') + } + } - , escape: function () { - var that = this - if (this.isShown && this.options.keyboard) { - this.$element.on('keyup.dismiss.modal', function ( e ) { - e.which == 27 && that.hide() - }) - } else if (!this.isShown) { - this.$element.off('keyup.dismiss.modal') - } - } + Modal.prototype.hideWithTransition = function () { + var that = this + var timeout = setTimeout(function () { + that.$element.off($.support.transition.end) + that.hideModal() + }, 500) - , hideWithTransition: function () { - var that = this - , timeout = setTimeout(function () { - that.$element.off($.support.transition.end) - that.hideModal() - }, 500) - - this.$element.one($.support.transition.end, function () { - clearTimeout(timeout) - that.hideModal() - }) - } + this.$element.one($.support.transition.end, function () { + clearTimeout(timeout) + that.hideModal() + }) + } - , hideModal: function () { - var that = this - this.$element.hide() - this.backdrop(function () { - that.removeBackdrop() - that.$element.trigger('hidden') - }) - } + Modal.prototype.hideModal = function () { + var that = this + this.$element.hide() + this.backdrop(function () { + that.removeBackdrop() + that.$element.trigger('bs:modal:hidden') + }) + } - , removeBackdrop: function () { - this.$backdrop && this.$backdrop.remove() - this.$backdrop = null - } + Modal.prototype.removeBackdrop: function () { + this.$backdrop && this.$backdrop.remove() + this.$backdrop = null + } - , backdrop: function (callback) { - var that = this - , animate = this.$element.hasClass('fade') ? 'fade' : '' + Modal.prototype.backdrop: function (callback) { + var that = this + var animate = this.$element.hasClass('fade') ? 'fade' : '' - if (this.isShown && this.options.backdrop) { - var doAnimate = $.support.transition && animate + if (this.isShown && this.options.backdrop) { + var doAnimate = $.support.transition && animate - this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />') - .appendTo(document.body) + this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />') + .appendTo(document.body) - this.$backdrop.click( - this.options.backdrop == 'static' ? - $.proxy(this.$element[0].focus, this.$element[0]) - : $.proxy(this.hide, this) - ) + this.$backdrop.click( + this.options.backdrop == 'static' ? + $.proxy(this.$element[0].focus, this.$element[0]) + : $.proxy(this.hide, this) + ) - if (doAnimate) this.$backdrop[0].offsetWidth // force reflow + if (doAnimate) this.$backdrop[0].offsetWidth // force reflow - this.$backdrop.addClass('in') + this.$backdrop.addClass('in') - if (!callback) return + if (!callback) return - doAnimate ? - this.$backdrop.one($.support.transition.end, callback) : - callback() + doAnimate ? + this.$backdrop.one($.support.transition.end, callback) : + callback() - } else if (!this.isShown && this.$backdrop) { - this.$backdrop.removeClass('in') + } else if (!this.isShown && this.$backdrop) { + this.$backdrop.removeClass('in') - $.support.transition && this.$element.hasClass('fade')? - this.$backdrop.one($.support.transition.end, callback) : - callback() + $.support.transition && this.$element.hasClass('fade')? + this.$backdrop.one($.support.transition.end, callback) : + callback() - } else if (callback) { - callback() - } - } + } else if (callback) { + callback() + } } - /* MODAL PLUGIN DEFINITION - * ======================= */ + // MODAL PLUGIN DEFINITION + // ======================= var old = $.fn.modal $.fn.modal = function (option) { return this.each(function () { - var $this = $(this) - , data = $this.data('modal') - , options = $.extend({}, $.fn.modal.defaults, $this.data(), typeof option == 'object' && option) - if (!data) $this.data('modal', (data = new Modal(this, options))) + var $this = $(this) + var data = $this.data('bs-modal') + var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option) + + if (!data) $this.data('bs-modal', (data = new Modal(this, options))) if (typeof option == 'string') data[option]() else if (options.show) data.show() }) } - $.fn.modal.defaults = { - backdrop: true - , keyboard: true - , show: true - } - $.fn.modal.Constructor = Modal - /* MODAL NO CONFLICT - * ================= */ + // MODAL NO CONFLICT + // ================= $.fn.modal.noConflict = function () { $.fn.modal = old @@ -226,14 +217,14 @@ } - /* MODAL DATA-API - * ============== */ + // MODAL DATA-API + // ============== $(document).on('click.modal.data-api', '[data-toggle="modal"]', function (e) { - var $this = $(this) - , href = $this.attr('href') - , $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7 - , option = $target.data('modal') ? 'toggle' : $.extend({ remote:!/#/.test(href) && href }, $target.data(), $this.data()) + var $this = $(this) + var href = $this.attr('href') + var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7 + var option = $target.data('modal') ? 'toggle' : $.extend({ remote:!/#/.test(href) && href }, $target.data(), $this.data()) e.preventDefault() @@ -245,7 +236,7 @@ }) var $body = $(document.body) - .on('shown', '.modal', function () { $body.addClass('modal-open') }) - .on('hidden', '.modal', function () { $body.removeClass('modal-open') }) + .on('bs:modal:shown', '.modal', function () { $body.addClass('modal-open') }) + .on('bs:modal:hidden', '.modal', function () { $body.removeClass('modal-open') }) }(window.jQuery); |
