jquery.modal.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. A simple jQuery modal
  3. Version 0.5.5
  4. www.198zone.com
  5. */
  6. (function($) {
  7. var current = null;
  8. $.modal = function(el, options) {
  9. $.modal.close(); // Close any open modals.
  10. var remove, target;
  11. this.$body = $('body');
  12. this.options = $.extend({}, $.modal.defaults, options);
  13. this.options.doFade = !isNaN(parseInt(this.options.fadeDuration, 10));
  14. if (el.is('a')) {
  15. target = el.attr('href');
  16. //Select element by id from href
  17. if (/^#/.test(target)) {
  18. this.$elm = $(target);
  19. if (this.$elm.length !== 1) return null;
  20. this.open();
  21. //AJAX
  22. } else {
  23. this.$elm = $('<div>');
  24. this.$body.append(this.$elm);
  25. remove = function(event, modal) { modal.elm.remove(); };
  26. this.showSpinner();
  27. el.trigger($.modal.AJAX_SEND);
  28. $.get(target).done(function(html) {
  29. if (!current) return;
  30. el.trigger($.modal.AJAX_SUCCESS);
  31. current.$elm.empty().append(html).on($.modal.CLOSE, remove);
  32. current.hideSpinner();
  33. current.open();
  34. el.trigger($.modal.AJAX_COMPLETE);
  35. }).fail(function() {
  36. el.trigger($.modal.AJAX_FAIL);
  37. current.hideSpinner();
  38. el.trigger($.modal.AJAX_COMPLETE);
  39. });
  40. }
  41. } else {
  42. this.$elm = el;
  43. this.open();
  44. }
  45. };
  46. $.modal.prototype = {
  47. constructor: $.modal,
  48. open: function() {
  49. var m = this;
  50. if(this.options.doFade) {
  51. this.block();
  52. setTimeout(function() {
  53. m.show();
  54. }, this.options.fadeDuration * this.options.fadeDelay);
  55. } else {
  56. this.block();
  57. this.show();
  58. }
  59. if (this.options.escapeClose) {
  60. $(document).on('keydown.modal', function(event) {
  61. if (event.which == 27) $.modal.close();
  62. });
  63. }
  64. if (this.options.clickClose) this.blocker.click($.modal.close);
  65. },
  66. close: function() {
  67. this.unblock();
  68. this.hide();
  69. $(document).off('keydown.modal');
  70. },
  71. block: function() {
  72. var initialOpacity = this.options.doFade ? 0 : this.options.opacity;
  73. this.$elm.trigger($.modal.BEFORE_BLOCK, [this._ctx()]);
  74. this.blocker = $('<div class="jquery-modal blocker"></div>').css({
  75. top: 0, right: 0, bottom: 0, left: 0,
  76. width: "100%", height: "100%",
  77. position: "fixed",
  78. zIndex: this.options.zIndex,
  79. background: this.options.overlay,
  80. opacity: initialOpacity
  81. });
  82. this.$body.append(this.blocker);
  83. if(this.options.doFade) {
  84. this.blocker.animate({opacity: this.options.opacity}, this.options.fadeDuration);
  85. }
  86. this.$elm.trigger($.modal.BLOCK, [this._ctx()]);
  87. },
  88. unblock: function() {
  89. if(this.options.doFade) {
  90. this.blocker.fadeOut(this.options.fadeDuration, function() {
  91. $(this).remove();
  92. });
  93. } else {
  94. this.blocker.remove();
  95. }
  96. },
  97. show: function() {
  98. this.$elm.trigger($.modal.BEFORE_OPEN, [this._ctx()]);
  99. if (this.options.showClose) {
  100. this.closeButton = $('<a href="#close-modal" rel="modal:close" class="close-modal ' + this.options.closeClass + '">' + this.options.closeText + '</a>');
  101. this.$elm.append(this.closeButton);
  102. }
  103. this.$elm.addClass(this.options.modalClass + ' current');
  104. this.center();
  105. if(this.options.doFade) {
  106. this.$elm.fadeIn(this.options.fadeDuration);
  107. } else {
  108. this.$elm.show();
  109. }
  110. this.$elm.trigger($.modal.OPEN, [this._ctx()]);
  111. },
  112. hide: function() {
  113. this.$elm.trigger($.modal.BEFORE_CLOSE, [this._ctx()]);
  114. if (this.closeButton) this.closeButton.remove();
  115. this.$elm.removeClass('current');
  116. if(this.options.doFade) {
  117. this.$elm.fadeOut(this.options.fadeDuration);
  118. } else {
  119. this.$elm.hide();
  120. }
  121. this.$elm.trigger($.modal.CLOSE, [this._ctx()]);
  122. },
  123. showSpinner: function() {
  124. if (!this.options.showSpinner) return;
  125. this.spinner = this.spinner || $('<div class="' + this.options.modalClass + '-spinner"></div>')
  126. .append(this.options.spinnerHtml);
  127. this.$body.append(this.spinner);
  128. this.spinner.show();
  129. },
  130. hideSpinner: function() {
  131. if (this.spinner) this.spinner.remove();
  132. },
  133. center: function() {
  134. this.$elm.css({
  135. position: 'fixed',
  136. top: "50%",
  137. left: "50%",
  138. marginTop: - (this.$elm.outerHeight() / 2),
  139. marginLeft: - (this.$elm.outerWidth() / 2),
  140. zIndex: this.options.zIndex + 1
  141. });
  142. },
  143. //Return context for custom events
  144. _ctx: function() {
  145. return { elm: this.$elm, blocker: this.blocker, options: this.options };
  146. }
  147. };
  148. //resize is alias for center for now
  149. $.modal.prototype.resize = $.modal.prototype.center;
  150. $.modal.close = function(event) {
  151. if (!current) return;
  152. if (event) event.preventDefault();
  153. current.close();
  154. var that = current.$elm;
  155. current = null;
  156. return that;
  157. };
  158. $.modal.resize = function() {
  159. if (!current) return;
  160. current.resize();
  161. };
  162. // Returns if there currently is an active modal
  163. $.modal.isActive = function () {
  164. return current ? true : false;
  165. }
  166. $.modal.defaults = {
  167. overlay: "#000",
  168. opacity: 0.75,
  169. zIndex: 1,
  170. escapeClose: true,
  171. clickClose: true,
  172. closeText: 'Close',
  173. closeClass: '',
  174. modalClass: "modal",
  175. spinnerHtml: null,
  176. showSpinner: true,
  177. showClose: true,
  178. fadeDuration: null, // Number of milliseconds the fade animation takes.
  179. fadeDelay: 1.0 // Point during the overlay's fade-in that the modal begins to fade in (.5 = 50%, 1.5 = 150%, etc.)
  180. };
  181. // Event constants
  182. $.modal.BEFORE_BLOCK = 'modal:before-block';
  183. $.modal.BLOCK = 'modal:block';
  184. $.modal.BEFORE_OPEN = 'modal:before-open';
  185. $.modal.OPEN = 'modal:open';
  186. $.modal.BEFORE_CLOSE = 'modal:before-close';
  187. $.modal.CLOSE = 'modal:close';
  188. $.modal.AJAX_SEND = 'modal:ajax:send';
  189. $.modal.AJAX_SUCCESS = 'modal:ajax:success';
  190. $.modal.AJAX_FAIL = 'modal:ajax:fail';
  191. $.modal.AJAX_COMPLETE = 'modal:ajax:complete';
  192. $.fn.modal = function(options){
  193. if (this.length === 1) {
  194. current = new $.modal(this, options);
  195. }
  196. return this;
  197. };
  198. // Automatically bind links with rel="modal:close" to, well, close the modal.
  199. $(document).on('click.modal', 'a[rel="modal:close"]', $.modal.close);
  200. $(document).on('click.modal', 'a[rel="modal:open"]', function(event) {
  201. event.preventDefault();
  202. $(this).modal();
  203. });
  204. })(jQuery);