jquery.modal.js 6.7 KB

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