|
| 1 | +/* |
| 2 | +A simple jQuery modal (http://github.com/kylefox/jquery-modal) |
| 3 | +Version 0.5.2 |
| 4 | + */ |
| 5 | +(function ($) { |
| 6 | + var current = null; |
| 7 | + $.modal = function (el, options) { |
| 8 | + $.modal.close(); |
| 9 | + var remove, |
| 10 | + target; |
| 11 | + this.$body = $('body'); |
| 12 | + this.options = $.extend({}, $.modal.defaults, options); |
| 13 | + if (el.is('a')) { |
| 14 | + target = el.attr('href'); |
| 15 | + if (/^#/.test(target)) { |
| 16 | + this.$elm = $(target); |
| 17 | + if (this.$elm.length !== 1) |
| 18 | + return null; |
| 19 | + this.open() |
| 20 | + } else { |
| 21 | + this.$elm = $('<div>'); |
| 22 | + this.$body.append(this.$elm); |
| 23 | + remove = function (event, modal) { |
| 24 | + modal.elm.remove() |
| 25 | + }; |
| 26 | + this.showSpinner(); |
| 27 | + el.trigger($.modal.AJAX_SEND); |
| 28 | + $.get(target).done(function (html) { |
| 29 | + if (!current) |
| 30 | + return; |
| 31 | + el.trigger($.modal.AJAX_SUCCESS); |
| 32 | + current.$elm.empty().append(html).on($.modal.CLOSE, remove); |
| 33 | + current.hideSpinner(); |
| 34 | + current.open(); |
| 35 | + el.trigger($.modal.AJAX_COMPLETE) |
| 36 | + }).fail(function () { |
| 37 | + el.trigger($.modal.AJAX_FAIL); |
| 38 | + current.hideSpinner(); |
| 39 | + el.trigger($.modal.AJAX_COMPLETE) |
| 40 | + }) |
| 41 | + } |
| 42 | + } else { |
| 43 | + this.$elm = el; |
| 44 | + this.open() |
| 45 | + } |
| 46 | + }; |
| 47 | + $.modal.prototype = { |
| 48 | + constructor : $.modal, |
| 49 | + open : function () { |
| 50 | + this.block(); |
| 51 | + this.show(); |
| 52 | + if (this.options.escapeClose) { |
| 53 | + $(document).on('keydown.modal', function (event) { |
| 54 | + if (event.which == 27) |
| 55 | + $.modal.close() |
| 56 | + }) |
| 57 | + } |
| 58 | + if (this.options.clickClose) |
| 59 | + this.blocker.click($.modal.close) |
| 60 | + }, |
| 61 | + close : function () { |
| 62 | + this.unblock(); |
| 63 | + this.hide(); |
| 64 | + $(document).off('keydown.modal') |
| 65 | + }, |
| 66 | + block : function () { |
| 67 | + this.$elm.trigger($.modal.BEFORE_BLOCK, [this._ctx()]); |
| 68 | + this.blocker = $('<div class="jquery-modal blocker"></div>').css({ |
| 69 | + top : 0, |
| 70 | + right : 0, |
| 71 | + bottom : 0, |
| 72 | + left : 0, |
| 73 | + width : "100%", |
| 74 | + height : $(document.body).width(), |
| 75 | + position : "absolute", |
| 76 | + zIndex : this.options.zIndex, |
| 77 | + background : this.options.overlay, |
| 78 | + opacity : this.options.opacity |
| 79 | + }); |
| 80 | + this.$body.append(this.blocker); |
| 81 | + this.$elm.trigger($.modal.BLOCK, [this._ctx()]) |
| 82 | + }, |
| 83 | + unblock : function () { |
| 84 | + this.blocker.remove() |
| 85 | + }, |
| 86 | + show : function () { |
| 87 | + this.$elm.trigger($.modal.BEFORE_OPEN, [this._ctx()]); |
| 88 | + if (this.options.showClose) { |
| 89 | + this.closeButton = $('<a href="#close-modal" rel="modal:close" class="close-modal">' + this.options.closeText + '</a>'); |
| 90 | + this.$elm.append(this.closeButton) |
| 91 | + } |
| 92 | + this.$elm.addClass(this.options.modalClass + ' current'); |
| 93 | + this.center(); |
| 94 | + this.$elm.show().trigger($.modal.OPEN, [this._ctx()]) |
| 95 | + }, |
| 96 | + hide : function () { |
| 97 | + this.$elm.trigger($.modal.BEFORE_CLOSE, [this._ctx()]); |
| 98 | + if (this.closeButton) |
| 99 | + this.closeButton.remove(); |
| 100 | + this.$elm.removeClass('current').hide(); |
| 101 | + this.$elm.trigger($.modal.CLOSE, [this._ctx()]) |
| 102 | + }, |
| 103 | + showSpinner : function () { |
| 104 | + if (!this.options.showSpinner) |
| 105 | + return; |
| 106 | + this.spinner = this.spinner || $('<div class="' + this.options.modalClass + '-spinner"></div>').append(this.options.spinnerHtml); |
| 107 | + this.$body.append(this.spinner); |
| 108 | + this.spinner.show() |
| 109 | + }, |
| 110 | + hideSpinner : function () { |
| 111 | + if (this.spinner) |
| 112 | + this.spinner.remove() |
| 113 | + }, |
| 114 | + center : function () { |
| 115 | + this.$elm.css({ |
| 116 | + position : 'absolute', |
| 117 | + top : "50%", |
| 118 | + left : "50%", |
| 119 | + marginTop : - (this.$elm.outerHeight() / 2), |
| 120 | + marginLeft : - (this.$elm.outerWidth() / 2), |
| 121 | + zIndex : this.options.zIndex + 1 |
| 122 | + }) |
| 123 | + }, |
| 124 | + _ctx : function () { |
| 125 | + return { |
| 126 | + elm : this.$elm, |
| 127 | + blocker : this.blocker, |
| 128 | + options : this.options |
| 129 | + } |
| 130 | + } |
| 131 | + }; |
| 132 | + $.modal.prototype.resize = $.modal.prototype.center; |
| 133 | + $.modal.close = function (event) { |
| 134 | + if (!current) |
| 135 | + return; |
| 136 | + if (event) |
| 137 | + event.preventDefault(); |
| 138 | + current.close(); |
| 139 | + current = null |
| 140 | + }; |
| 141 | + $.modal.resize = function () { |
| 142 | + if (!current) |
| 143 | + return; |
| 144 | + current.resize() |
| 145 | + }; |
| 146 | + $.modal.defaults = { |
| 147 | + overlay : "#000", |
| 148 | + opacity : 0.75, |
| 149 | + zIndex : 1, |
| 150 | + escapeClose : true, |
| 151 | + clickClose : true, |
| 152 | + closeText : 'Close', |
| 153 | + modalClass : "modal", |
| 154 | + spinnerHtml : null, |
| 155 | + showSpinner : true, |
| 156 | + showClose : true |
| 157 | + }; |
| 158 | + $.modal.BEFORE_BLOCK = 'modal:before-block'; |
| 159 | + $.modal.BLOCK = 'modal:block'; |
| 160 | + $.modal.BEFORE_OPEN = 'modal:before-open'; |
| 161 | + $.modal.OPEN = 'modal:open'; |
| 162 | + $.modal.BEFORE_CLOSE = 'modal:before-close'; |
| 163 | + $.modal.CLOSE = 'modal:close'; |
| 164 | + $.modal.AJAX_SEND = 'modal:ajax:send'; |
| 165 | + $.modal.AJAX_SUCCESS = 'modal:ajax:success'; |
| 166 | + $.modal.AJAX_FAIL = 'modal:ajax:fail'; |
| 167 | + $.modal.AJAX_COMPLETE = 'modal:ajax:complete'; |
| 168 | + $.fn.modal = function (options) { |
| 169 | + if (this.length === 1) { |
| 170 | + current = new $.modal(this, options) |
| 171 | + } |
| 172 | + return this |
| 173 | + }; |
| 174 | + $(document).on('click', 'a[rel="modal:close"]', $.modal.close); |
| 175 | + $(document).on('click', 'a[rel="modal:open"]', function (event) { |
| 176 | + event.preventDefault(); |
| 177 | + $(this).modal() |
| 178 | + }) |
| 179 | +})(jQuery); |
0 commit comments