foundation.reveal.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*jslint unparam: true, browser: true, indent: 2 */
  2. ;(function ($, window, document, undefined) {
  3. 'use strict';
  4. Foundation.libs.reveal = {
  5. name: 'reveal',
  6. version : '4.2.0',
  7. locked : false,
  8. settings : {
  9. animation: 'fadeAndPop',
  10. animationSpeed: 250,
  11. closeOnBackgroundClick: true,
  12. closeOnEsc: true,
  13. dismissModalClass: 'close-reveal-modal',
  14. bgClass: 'reveal-modal-bg',
  15. open: function(){},
  16. opened: function(){},
  17. close: function(){},
  18. closed: function(){},
  19. bg : $('.reveal-modal-bg'),
  20. css : {
  21. open : {
  22. 'opacity': 0,
  23. 'visibility': 'visible',
  24. 'display' : 'block'
  25. },
  26. close : {
  27. 'opacity': 1,
  28. 'visibility': 'hidden',
  29. 'display': 'none'
  30. }
  31. }
  32. },
  33. init : function (scope, method, options) {
  34. Foundation.inherit(this, 'data_options delay');
  35. if (typeof method === 'object') {
  36. $.extend(true, this.settings, method);
  37. } else if (typeof options !== 'undefined') {
  38. $.extend(true, this.settings, options);
  39. }
  40. if (typeof method != 'string') {
  41. this.events();
  42. return this.settings.init;
  43. } else {
  44. return this[method].call(this, options);
  45. }
  46. },
  47. events : function () {
  48. var self = this;
  49. $(this.scope)
  50. .off('.fndtn.reveal')
  51. .on('click.fndtn.reveal', '[data-reveal-id]', function (e) {
  52. e.preventDefault();
  53. if (!self.locked) {
  54. var element = $(this),
  55. ajax = element.data('reveal-ajax');
  56. self.locked = true;
  57. if (typeof ajax === 'undefined') {
  58. self.open.call(self, element);
  59. } else {
  60. var url = ajax === true ? element.attr('href') : ajax;
  61. self.open.call(self, element, {url: url});
  62. }
  63. }
  64. })
  65. .on('click.fndtn.reveal touchend.click.fndtn.reveal', this.close_targets(), function (e) {
  66. e.preventDefault();
  67. if (!self.locked) {
  68. var settings = $.extend({}, self.settings, self.data_options($('.reveal-modal.open')));
  69. if ($(e.target)[0] === $('.' + settings.bgClass)[0] && !settings.closeOnBackgroundClick) {
  70. return;
  71. }
  72. self.locked = true;
  73. self.close.call(self, $(this).closest('.reveal-modal'));
  74. }
  75. })
  76. .on('open.fndtn.reveal', '.reveal-modal', this.settings.open)
  77. .on('opened.fndtn.reveal', '.reveal-modal', this.settings.opened)
  78. .on('opened.fndtn.reveal', '.reveal-modal', this.open_video)
  79. .on('close.fndtn.reveal', '.reveal-modal', this.settings.close)
  80. .on('closed.fndtn.reveal', '.reveal-modal', this.settings.closed)
  81. .on('closed.fndtn.reveal', '.reveal-modal', this.close_video);
  82. $( 'body' ).bind( 'keyup.reveal', function ( event ) {
  83. var open_modal = $('.reveal-modal.open'),
  84. settings = $.extend({}, self.settings, self.data_options(open_modal));
  85. if ( event.which === 27 && settings.closeOnEsc) { // 27 is the keycode for the Escape key
  86. open_modal.foundation('reveal', 'close');
  87. }
  88. });
  89. return true;
  90. },
  91. open : function (target, ajax_settings) {
  92. if (target) {
  93. if (typeof target.selector !== 'undefined') {
  94. var modal = $('#' + target.data('reveal-id'));
  95. } else {
  96. var modal = $(this.scope);
  97. ajax_settings = target;
  98. }
  99. } else {
  100. var modal = $(this.scope);
  101. }
  102. if (!modal.hasClass('open')) {
  103. var open_modal = $('.reveal-modal.open');
  104. if (typeof modal.data('css-top') === 'undefined') {
  105. modal.data('css-top', parseInt(modal.css('top'), 10))
  106. .data('offset', this.cache_offset(modal));
  107. }
  108. modal.trigger('open');
  109. if (open_modal.length < 1) {
  110. this.toggle_bg(modal);
  111. }
  112. if (typeof ajax_settings === 'undefined' || !ajax_settings.url) {
  113. this.hide(open_modal, this.settings.css.close);
  114. this.show(modal, this.settings.css.open);
  115. } else {
  116. var self = this,
  117. old_success = typeof ajax_settings.success !== 'undefined' ? ajax_settings.success : null;
  118. $.extend(ajax_settings, {
  119. success: function (data, textStatus, jqXHR) {
  120. if ( $.isFunction(old_success) ) {
  121. old_success(data, textStatus, jqXHR);
  122. }
  123. modal.html(data);
  124. $(modal).foundation('section', 'reflow');
  125. self.hide(open_modal, self.settings.css.close);
  126. self.show(modal, self.settings.css.open);
  127. }
  128. });
  129. $.ajax(ajax_settings);
  130. }
  131. }
  132. },
  133. close : function (modal) {
  134. var modal = modal && modal.length ? modal : $(this.scope),
  135. open_modals = $('.reveal-modal.open');
  136. if (open_modals.length > 0) {
  137. this.locked = true;
  138. modal.trigger('close');
  139. this.toggle_bg(modal);
  140. this.hide(open_modals, this.settings.css.close);
  141. }
  142. },
  143. close_targets : function () {
  144. var base = '.' + this.settings.dismissModalClass;
  145. if (this.settings.closeOnBackgroundClick) {
  146. return base + ', .' + this.settings.bgClass;
  147. }
  148. return base;
  149. },
  150. toggle_bg : function (modal) {
  151. if ($('.reveal-modal-bg').length === 0) {
  152. this.settings.bg = $('<div />', {'class': this.settings.bgClass})
  153. .appendTo('body');
  154. }
  155. if (this.settings.bg.filter(':visible').length > 0) {
  156. this.hide(this.settings.bg);
  157. } else {
  158. this.show(this.settings.bg);
  159. }
  160. },
  161. show : function (el, css) {
  162. // is modal
  163. if (css) {
  164. if (/pop/i.test(this.settings.animation)) {
  165. css.top = $(window).scrollTop() - el.data('offset') + 'px';
  166. var end_css = {
  167. top: $(window).scrollTop() + el.data('css-top') + 'px',
  168. opacity: 1
  169. };
  170. return this.delay(function () {
  171. return el
  172. .css(css)
  173. .animate(end_css, this.settings.animationSpeed, 'linear', function () {
  174. this.locked = false;
  175. el.trigger('opened');
  176. }.bind(this))
  177. .addClass('open');
  178. }.bind(this), this.settings.animationSpeed / 2);
  179. }
  180. if (/fade/i.test(this.settings.animation)) {
  181. var end_css = {opacity: 1};
  182. return this.delay(function () {
  183. return el
  184. .css(css)
  185. .animate(end_css, this.settings.animationSpeed, 'linear', function () {
  186. this.locked = false;
  187. el.trigger('opened');
  188. }.bind(this))
  189. .addClass('open');
  190. }.bind(this), this.settings.animationSpeed / 2);
  191. }
  192. return el.css(css).show().css({opacity: 1}).addClass('open').trigger('opened');
  193. }
  194. // should we animate the background?
  195. if (/fade/i.test(this.settings.animation)) {
  196. return el.fadeIn(this.settings.animationSpeed / 2);
  197. }
  198. return el.show();
  199. },
  200. hide : function (el, css) {
  201. // is modal
  202. if (css) {
  203. if (/pop/i.test(this.settings.animation)) {
  204. var end_css = {
  205. top: - $(window).scrollTop() - el.data('offset') + 'px',
  206. opacity: 0
  207. };
  208. return this.delay(function () {
  209. return el
  210. .animate(end_css, this.settings.animationSpeed, 'linear', function () {
  211. this.locked = false;
  212. el.css(css).trigger('closed');
  213. }.bind(this))
  214. .removeClass('open');
  215. }.bind(this), this.settings.animationSpeed / 2);
  216. }
  217. if (/fade/i.test(this.settings.animation)) {
  218. var end_css = {opacity: 0};
  219. return this.delay(function () {
  220. return el
  221. .animate(end_css, this.settings.animationSpeed, 'linear', function () {
  222. this.locked = false;
  223. el.css(css).trigger('closed');
  224. }.bind(this))
  225. .removeClass('open');
  226. }.bind(this), this.settings.animationSpeed / 2);
  227. }
  228. return el.hide().css(css).removeClass('open').trigger('closed');
  229. }
  230. // should we animate the background?
  231. if (/fade/i.test(this.settings.animation)) {
  232. return el.fadeOut(this.settings.animationSpeed / 2);
  233. }
  234. return el.hide();
  235. },
  236. close_video : function (e) {
  237. var video = $(this).find('.flex-video'),
  238. iframe = video.find('iframe');
  239. if (iframe.length > 0) {
  240. iframe.attr('data-src', iframe[0].src);
  241. iframe.attr('src', 'about:blank');
  242. video.hide();
  243. }
  244. },
  245. open_video : function (e) {
  246. var video = $(this).find('.flex-video'),
  247. iframe = video.find('iframe');
  248. if (iframe.length > 0) {
  249. var data_src = iframe.attr('data-src');
  250. if (typeof data_src === 'string') {
  251. iframe[0].src = iframe.attr('data-src');
  252. } else {
  253. var src = iframe[0].src;
  254. iframe[0].src = undefined;
  255. iframe[0].src = src;
  256. }
  257. video.show();
  258. }
  259. },
  260. cache_offset : function (modal) {
  261. var offset = modal.show().height() + parseInt(modal.css('top'), 10);
  262. modal.hide();
  263. return offset;
  264. },
  265. off : function () {
  266. $(this.scope).off('.fndtn.reveal');
  267. },
  268. reflow : function () {}
  269. };
  270. }(Foundation.zj, this, this.document));