modalBox.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. A simple jQuery modal (http://github.com/kylefox/jquery-modal)
  3. Version 0.9.1
  4. */
  5. !(function (o) {
  6. "object" == typeof module && "object" == typeof module.exports
  7. ? o(require("jquery"), window, document)
  8. : o(jQuery, window, document);
  9. })(function (o, t, i, e) {
  10. var s = [],
  11. l = function () {
  12. return s.length ? s[s.length - 1] : null;
  13. },
  14. n = function () {
  15. var o,
  16. t = !1;
  17. for (o = s.length - 1; o >= 0; o--)
  18. s[o].$blocker &&
  19. (s[o].$blocker.toggleClass("current", !t).toggleClass("behind", t),
  20. (t = !0));
  21. };
  22. (o.modal = function (t, i) {
  23. var e, n;
  24. if (
  25. ((this.$body = o("body")),
  26. (this.options = o.extend({}, o.modal.defaults, i)),
  27. (this.options.doFade = !isNaN(parseInt(this.options.fadeDuration, 10))),
  28. (this.$blocker = null),
  29. this.options.closeExisting)
  30. )
  31. for (; o.modal.isActive(); ) o.modal.close();
  32. if ((s.push(this), t.is("a")))
  33. if (((n = t.attr("href")), (this.anchor = t), /^#/.test(n))) {
  34. if (((this.$elm = o(n)), 1 !== this.$elm.length)) return null;
  35. this.$body.append(this.$elm), this.open();
  36. } else
  37. (this.$elm = o("<div>")),
  38. this.$body.append(this.$elm),
  39. (e = function (o, t) {
  40. t.elm.remove();
  41. }),
  42. this.showSpinner(),
  43. t.trigger(o.modal.AJAX_SEND),
  44. o
  45. .get(n)
  46. .done(function (i) {
  47. if (o.modal.isActive()) {
  48. t.trigger(o.modal.AJAX_SUCCESS);
  49. var s = l();
  50. s.$elm.empty().append(i).on(o.modal.CLOSE, e),
  51. s.hideSpinner(),
  52. s.open(),
  53. t.trigger(o.modal.AJAX_COMPLETE);
  54. }
  55. })
  56. .fail(function () {
  57. t.trigger(o.modal.AJAX_FAIL);
  58. var i = l();
  59. i.hideSpinner(), s.pop(), t.trigger(o.modal.AJAX_COMPLETE);
  60. });
  61. else
  62. (this.$elm = t),
  63. (this.anchor = t),
  64. this.$body.append(this.$elm),
  65. this.open();
  66. }),
  67. (o.modal.prototype = {
  68. constructor: o.modal,
  69. open: function () {
  70. var t = this;
  71. this.block(),
  72. this.anchor.blur(),
  73. this.options.doFade
  74. ? setTimeout(function () {
  75. t.show();
  76. }, this.options.fadeDuration * this.options.fadeDelay)
  77. : this.show(),
  78. o(i)
  79. .off("keydown.modal")
  80. .on("keydown.modal", function (o) {
  81. var t = l();
  82. 27 === o.which && t.options.escapeClose && t.close();
  83. }),
  84. this.options.clickClose &&
  85. this.$blocker.click(function (t) {
  86. t.target === this && o.modal.close();
  87. });
  88. },
  89. close: function () {
  90. s.pop(),
  91. this.unblock(),
  92. this.hide(),
  93. o.modal.isActive() || o(i).off("keydown.modal");
  94. },
  95. block: function () {
  96. this.$elm.trigger(o.modal.BEFORE_BLOCK, [this._ctx()]),
  97. this.$body.css("overflow", "hidden"),
  98. (this.$blocker = o(
  99. '<div class="' +
  100. this.options.blockerClass +
  101. ' blocker current"></div>'
  102. ).appendTo(this.$body)),
  103. n(),
  104. this.options.doFade &&
  105. this.$blocker
  106. .css("opacity", 0)
  107. .animate({ opacity: 1 }, this.options.fadeDuration),
  108. this.$elm.trigger(o.modal.BLOCK, [this._ctx()]);
  109. },
  110. unblock: function (t) {
  111. !t && this.options.doFade
  112. ? this.$blocker.fadeOut(
  113. this.options.fadeDuration,
  114. this.unblock.bind(this, !0)
  115. )
  116. : (this.$blocker.children().appendTo(this.$body),
  117. this.$blocker.remove(),
  118. (this.$blocker = null),
  119. n(),
  120. o.modal.isActive() || this.$body.css("overflow", ""));
  121. },
  122. show: function () {
  123. this.$elm.trigger(o.modal.BEFORE_OPEN, [this._ctx()]),
  124. this.options.showClose &&
  125. ((this.closeButton = o(
  126. '<a href="#close-modal" rel="modal:close" class="close-modal ' +
  127. this.options.closeClass +
  128. '">' +
  129. this.options.closeText +
  130. "</a>"
  131. )),
  132. this.$elm.append(this.closeButton)),
  133. this.$elm.addClass(this.options.modalClass).appendTo(this.$blocker),
  134. this.options.doFade
  135. ? this.$elm
  136. .css({ opacity: 0, display: "inline-block" })
  137. .animate({ opacity: 1 }, this.options.fadeDuration)
  138. : this.$elm.css("display", "inline-block"),
  139. this.$elm.trigger(o.modal.OPEN, [this._ctx()]);
  140. },
  141. hide: function () {
  142. this.$elm.trigger(o.modal.BEFORE_CLOSE, [this._ctx()]),
  143. this.closeButton && this.closeButton.remove();
  144. var t = this;
  145. this.options.doFade
  146. ? this.$elm.fadeOut(this.options.fadeDuration, function () {
  147. t.$elm.trigger(o.modal.AFTER_CLOSE, [t._ctx()]);
  148. })
  149. : this.$elm.hide(0, function () {
  150. t.$elm.trigger(o.modal.AFTER_CLOSE, [t._ctx()]);
  151. }),
  152. this.$elm.trigger(o.modal.CLOSE, [this._ctx()]);
  153. },
  154. showSpinner: function () {
  155. this.options.showSpinner &&
  156. ((this.spinner =
  157. this.spinner ||
  158. o(
  159. '<div class="' + this.options.modalClass + '-spinner"></div>'
  160. ).append(this.options.spinnerHtml)),
  161. this.$body.append(this.spinner),
  162. this.spinner.show());
  163. },
  164. hideSpinner: function () {
  165. this.spinner && this.spinner.remove();
  166. },
  167. _ctx: function () {
  168. return {
  169. elm: this.$elm,
  170. $elm: this.$elm,
  171. $blocker: this.$blocker,
  172. options: this.options,
  173. };
  174. },
  175. }),
  176. (o.modal.close = function (t) {
  177. if (o.modal.isActive()) {
  178. t && t.preventDefault();
  179. var i = l();
  180. return i.close(), i.$elm;
  181. }
  182. }),
  183. (o.modal.isActive = function () {
  184. return s.length > 0;
  185. }),
  186. (o.modal.getCurrent = l),
  187. (o.modal.defaults = {
  188. closeExisting: !0,
  189. escapeClose: !0,
  190. clickClose: !0,
  191. closeText: "Close",
  192. closeClass: "",
  193. modalClass: "modal",
  194. blockerClass: "jquery-modal",
  195. spinnerHtml:
  196. '<div class="rect1"></div><div class="rect2"></div><div class="rect3"></div><div class="rect4"></div>',
  197. showSpinner: !0,
  198. showClose: !0,
  199. fadeDuration: null,
  200. fadeDelay: 1,
  201. }),
  202. (o.modal.BEFORE_BLOCK = "modal:before-block"),
  203. (o.modal.BLOCK = "modal:block"),
  204. (o.modal.BEFORE_OPEN = "modal:before-open"),
  205. (o.modal.OPEN = "modal:open"),
  206. (o.modal.BEFORE_CLOSE = "modal:before-close"),
  207. (o.modal.CLOSE = "modal:close"),
  208. (o.modal.AFTER_CLOSE = "modal:after-close"),
  209. (o.modal.AJAX_SEND = "modal:ajax:send"),
  210. (o.modal.AJAX_SUCCESS = "modal:ajax:success"),
  211. (o.modal.AJAX_FAIL = "modal:ajax:fail"),
  212. (o.modal.AJAX_COMPLETE = "modal:ajax:complete"),
  213. (o.fn.modal = function (t) {
  214. return 1 === this.length && new o.modal(this, t), this;
  215. }),
  216. o(i).on("click.modal", 'a[rel~="modal:close"]', o.modal.close),
  217. o(i).on("click.modal", 'a[rel~="modal:open"]', function (t) {
  218. t.preventDefault(), o(this).modal();
  219. });
  220. });