2
0

foundation.reveal.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. ;(function ($, window, document, undefined) {
  2. 'use strict';
  3. var openModals = [];
  4. Foundation.libs.reveal = {
  5. name : 'reveal',
  6. version : '5.5.3',
  7. locked : false,
  8. settings : {
  9. animation : 'fadeAndPop',
  10. animation_speed : 250,
  11. close_on_background_click : true,
  12. close_on_esc : true,
  13. dismiss_modal_class : 'close-reveal-modal',
  14. multiple_opened : false,
  15. bg_class : 'reveal-modal-bg',
  16. root_element : 'body',
  17. open : function(){},
  18. opened : function(){},
  19. close : function(){},
  20. closed : function(){},
  21. on_ajax_error: $.noop,
  22. bg : $('.reveal-modal-bg'),
  23. css : {
  24. open : {
  25. 'opacity' : 0,
  26. 'visibility' : 'visible',
  27. 'display' : 'block'
  28. },
  29. close : {
  30. 'opacity' : 1,
  31. 'visibility' : 'hidden',
  32. 'display' : 'none'
  33. }
  34. }
  35. },
  36. init : function (scope, method, options) {
  37. $.extend(true, this.settings, method, options);
  38. this.bindings(method, options);
  39. },
  40. events : function (scope) {
  41. var self = this,
  42. S = self.S;
  43. S(this.scope)
  44. .off('.reveal')
  45. .on('click.fndtn.reveal', '[' + this.add_namespace('data-reveal-id') + ']:not([disabled])', function (e) {
  46. e.preventDefault();
  47. if (!self.locked) {
  48. var element = S(this),
  49. ajax = element.data(self.data_attr('reveal-ajax')),
  50. replaceContentSel = element.data(self.data_attr('reveal-replace-content'));
  51. self.locked = true;
  52. if (typeof ajax === 'undefined') {
  53. self.open.call(self, element);
  54. } else {
  55. var url = ajax === true ? element.attr('href') : ajax;
  56. self.open.call(self, element, {url : url}, { replaceContentSel : replaceContentSel });
  57. }
  58. }
  59. });
  60. S(document)
  61. .on('click.fndtn.reveal', this.close_targets(), function (e) {
  62. e.preventDefault();
  63. if (!self.locked) {
  64. var settings = S('[' + self.attr_name() + '].open').data(self.attr_name(true) + '-init') || self.settings,
  65. bg_clicked = S(e.target)[0] === S('.' + settings.bg_class)[0];
  66. if (bg_clicked) {
  67. if (settings.close_on_background_click) {
  68. e.stopPropagation();
  69. } else {
  70. return;
  71. }
  72. }
  73. self.locked = true;
  74. self.close.call(self, bg_clicked ? S('[' + self.attr_name() + '].open:not(.toback)') : S(this).closest('[' + self.attr_name() + ']'));
  75. }
  76. });
  77. if (S('[' + self.attr_name() + ']', this.scope).length > 0) {
  78. S(this.scope)
  79. // .off('.reveal')
  80. .on('open.fndtn.reveal', this.settings.open)
  81. .on('opened.fndtn.reveal', this.settings.opened)
  82. .on('opened.fndtn.reveal', this.open_video)
  83. .on('close.fndtn.reveal', this.settings.close)
  84. .on('closed.fndtn.reveal', this.settings.closed)
  85. .on('closed.fndtn.reveal', this.close_video);
  86. } else {
  87. S(this.scope)
  88. // .off('.reveal')
  89. .on('open.fndtn.reveal', '[' + self.attr_name() + ']', this.settings.open)
  90. .on('opened.fndtn.reveal', '[' + self.attr_name() + ']', this.settings.opened)
  91. .on('opened.fndtn.reveal', '[' + self.attr_name() + ']', this.open_video)
  92. .on('close.fndtn.reveal', '[' + self.attr_name() + ']', this.settings.close)
  93. .on('closed.fndtn.reveal', '[' + self.attr_name() + ']', this.settings.closed)
  94. .on('closed.fndtn.reveal', '[' + self.attr_name() + ']', this.close_video);
  95. }
  96. return true;
  97. },
  98. // PATCH #3: turning on key up capture only when a reveal window is open
  99. key_up_on : function (scope) {
  100. var self = this;
  101. // PATCH #1: fixing multiple keyup event trigger from single key press
  102. self.S('body').off('keyup.fndtn.reveal').on('keyup.fndtn.reveal', function ( event ) {
  103. var open_modal = self.S('[' + self.attr_name() + '].open'),
  104. settings = open_modal.data(self.attr_name(true) + '-init') || self.settings ;
  105. // PATCH #2: making sure that the close event can be called only while unlocked,
  106. // so that multiple keyup.fndtn.reveal events don't prevent clean closing of the reveal window.
  107. if ( settings && event.which === 27 && settings.close_on_esc && !self.locked) { // 27 is the keycode for the Escape key
  108. self.close.call(self, open_modal);
  109. }
  110. });
  111. return true;
  112. },
  113. // PATCH #3: turning on key up capture only when a reveal window is open
  114. key_up_off : function (scope) {
  115. this.S('body').off('keyup.fndtn.reveal');
  116. return true;
  117. },
  118. open : function (target, ajax_settings) {
  119. var self = this,
  120. modal;
  121. if (target) {
  122. if (typeof target.selector !== 'undefined') {
  123. // Find the named node; only use the first one found, since the rest of the code assumes there's only one node
  124. modal = self.S('#' + target.data(self.data_attr('reveal-id'))).first();
  125. } else {
  126. modal = self.S(this.scope);
  127. ajax_settings = target;
  128. }
  129. } else {
  130. modal = self.S(this.scope);
  131. }
  132. var settings = modal.data(self.attr_name(true) + '-init');
  133. settings = settings || this.settings;
  134. if (modal.hasClass('open') && target !== undefined && target.attr('data-reveal-id') == modal.attr('id')) {
  135. return self.close(modal);
  136. }
  137. if (!modal.hasClass('open')) {
  138. var open_modal = self.S('[' + self.attr_name() + '].open');
  139. if (typeof modal.data('css-top') === 'undefined') {
  140. modal.data('css-top', parseInt(modal.css('top'), 10))
  141. .data('offset', this.cache_offset(modal));
  142. }
  143. modal.attr('tabindex','0').attr('aria-hidden','false');
  144. this.key_up_on(modal); // PATCH #3: turning on key up capture only when a reveal window is open
  145. // Prevent namespace event from triggering twice
  146. modal.on('open.fndtn.reveal', function(e) {
  147. if (e.namespace !== 'fndtn.reveal') return;
  148. });
  149. modal.on('open.fndtn.reveal').trigger('open.fndtn.reveal');
  150. if (open_modal.length < 1) {
  151. this.toggle_bg(modal, true);
  152. }
  153. if (typeof ajax_settings === 'string') {
  154. ajax_settings = {
  155. url : ajax_settings
  156. };
  157. }
  158. var openModal = function() {
  159. if(open_modal.length > 0) {
  160. if(settings.multiple_opened) {
  161. self.to_back(open_modal);
  162. } else {
  163. self.hide(open_modal, settings.css.close);
  164. }
  165. }
  166. // bl: add the open_modal that isn't already in the background to the openModals array
  167. if(settings.multiple_opened) {
  168. openModals.push(modal);
  169. }
  170. self.show(modal, settings.css.open);
  171. };
  172. if (typeof ajax_settings === 'undefined' || !ajax_settings.url) {
  173. openModal();
  174. } else {
  175. var old_success = typeof ajax_settings.success !== 'undefined' ? ajax_settings.success : null;
  176. $.extend(ajax_settings, {
  177. success : function (data, textStatus, jqXHR) {
  178. if ( $.isFunction(old_success) ) {
  179. var result = old_success(data, textStatus, jqXHR);
  180. if (typeof result == 'string') {
  181. data = result;
  182. }
  183. }
  184. if (typeof options !== 'undefined' && typeof options.replaceContentSel !== 'undefined') {
  185. modal.find(options.replaceContentSel).html(data);
  186. } else {
  187. modal.html(data);
  188. }
  189. self.S(modal).foundation('section', 'reflow');
  190. self.S(modal).children().foundation();
  191. openModal();
  192. }
  193. });
  194. // check for if user initalized with error callback
  195. if (settings.on_ajax_error !== $.noop) {
  196. $.extend(ajax_settings, {
  197. error : settings.on_ajax_error
  198. });
  199. }
  200. $.ajax(ajax_settings);
  201. }
  202. }
  203. self.S(window).trigger('resize');
  204. },
  205. close : function (modal) {
  206. var modal = modal && modal.length ? modal : this.S(this.scope),
  207. open_modals = this.S('[' + this.attr_name() + '].open'),
  208. settings = modal.data(this.attr_name(true) + '-init') || this.settings,
  209. self = this;
  210. if (open_modals.length > 0) {
  211. modal.removeAttr('tabindex','0').attr('aria-hidden','true');
  212. this.locked = true;
  213. this.key_up_off(modal); // PATCH #3: turning on key up capture only when a reveal window is open
  214. modal.trigger('close.fndtn.reveal');
  215. if ((settings.multiple_opened && open_modals.length === 1) || !settings.multiple_opened || modal.length > 1) {
  216. self.toggle_bg(modal, false);
  217. self.to_front(modal);
  218. }
  219. if (settings.multiple_opened) {
  220. var isCurrent = modal.is(':not(.toback)');
  221. self.hide(modal, settings.css.close, settings);
  222. if(isCurrent) {
  223. // remove the last modal since it is now closed
  224. openModals.pop();
  225. } else {
  226. // if this isn't the current modal, then find it in the array and remove it
  227. openModals = $.grep(openModals, function(elt) {
  228. var isThis = elt[0]===modal[0];
  229. if(isThis) {
  230. // since it's not currently in the front, put it in the front now that it is hidden
  231. // so that if it's re-opened, it won't be .toback
  232. self.to_front(modal);
  233. }
  234. return !isThis;
  235. });
  236. }
  237. // finally, show the next modal in the stack, if there is one
  238. if(openModals.length>0) {
  239. self.to_front(openModals[openModals.length - 1]);
  240. }
  241. } else {
  242. self.hide(open_modals, settings.css.close, settings);
  243. }
  244. }
  245. },
  246. close_targets : function () {
  247. var base = '.' + this.settings.dismiss_modal_class;
  248. if (this.settings.close_on_background_click) {
  249. return base + ', .' + this.settings.bg_class;
  250. }
  251. return base;
  252. },
  253. toggle_bg : function (modal, state) {
  254. if (this.S('.' + this.settings.bg_class).length === 0) {
  255. this.settings.bg = $('<div />', {'class': this.settings.bg_class})
  256. .appendTo('body').hide();
  257. }
  258. var visible = this.settings.bg.filter(':visible').length > 0;
  259. if ( state != visible ) {
  260. if ( state == undefined ? visible : !state ) {
  261. this.hide(this.settings.bg);
  262. } else {
  263. this.show(this.settings.bg);
  264. }
  265. }
  266. },
  267. show : function (el, css) {
  268. // is modal
  269. if (css) {
  270. var settings = el.data(this.attr_name(true) + '-init') || this.settings,
  271. root_element = settings.root_element,
  272. context = this;
  273. if (el.parent(root_element).length === 0) {
  274. var placeholder = el.wrap('<div style="display: none;" />').parent();
  275. el.on('closed.fndtn.reveal.wrapped', function () {
  276. el.detach().appendTo(placeholder);
  277. el.unwrap().unbind('closed.fndtn.reveal.wrapped');
  278. });
  279. el.detach().appendTo(root_element);
  280. }
  281. var animData = getAnimationData(settings.animation);
  282. if (!animData.animate) {
  283. this.locked = false;
  284. }
  285. if (animData.pop) {
  286. css.top = $(window).scrollTop() - el.data('offset') + 'px';
  287. var end_css = {
  288. top: $(window).scrollTop() + el.data('css-top') + 'px',
  289. opacity: 1
  290. };
  291. return setTimeout(function () {
  292. return el
  293. .css(css)
  294. .animate(end_css, settings.animation_speed, 'linear', function () {
  295. context.locked = false;
  296. el.trigger('opened.fndtn.reveal');
  297. })
  298. .addClass('open');
  299. }, settings.animation_speed / 2);
  300. }
  301. css.top = $(window).scrollTop() + el.data('css-top') + 'px';
  302. if (animData.fade) {
  303. var end_css = {opacity: 1};
  304. return setTimeout(function () {
  305. return el
  306. .css(css)
  307. .animate(end_css, settings.animation_speed, 'linear', function () {
  308. context.locked = false;
  309. el.trigger('opened.fndtn.reveal');
  310. })
  311. .addClass('open');
  312. }, settings.animation_speed / 2);
  313. }
  314. return el.css(css).show().css({opacity : 1}).addClass('open').trigger('opened.fndtn.reveal');
  315. }
  316. var settings = this.settings;
  317. // should we animate the background?
  318. if (getAnimationData(settings.animation).fade) {
  319. return el.fadeIn(settings.animation_speed / 2);
  320. }
  321. this.locked = false;
  322. return el.show();
  323. },
  324. to_back : function(el) {
  325. el.addClass('toback');
  326. },
  327. to_front : function(el) {
  328. el.removeClass('toback');
  329. },
  330. hide : function (el, css) {
  331. // is modal
  332. if (css) {
  333. var settings = el.data(this.attr_name(true) + '-init'),
  334. context = this;
  335. settings = settings || this.settings;
  336. var animData = getAnimationData(settings.animation);
  337. if (!animData.animate) {
  338. this.locked = false;
  339. }
  340. if (animData.pop) {
  341. var end_css = {
  342. top: - $(window).scrollTop() - el.data('offset') + 'px',
  343. opacity: 0
  344. };
  345. return setTimeout(function () {
  346. return el
  347. .animate(end_css, settings.animation_speed, 'linear', function () {
  348. context.locked = false;
  349. el.css(css).trigger('closed.fndtn.reveal');
  350. })
  351. .removeClass('open');
  352. }, settings.animation_speed / 2);
  353. }
  354. if (animData.fade) {
  355. var end_css = {opacity : 0};
  356. return setTimeout(function () {
  357. return el
  358. .animate(end_css, settings.animation_speed, 'linear', function () {
  359. context.locked = false;
  360. el.css(css).trigger('closed.fndtn.reveal');
  361. })
  362. .removeClass('open');
  363. }, settings.animation_speed / 2);
  364. }
  365. return el.hide().css(css).removeClass('open').trigger('closed.fndtn.reveal');
  366. }
  367. var settings = this.settings;
  368. // should we animate the background?
  369. if (getAnimationData(settings.animation).fade) {
  370. return el.fadeOut(settings.animation_speed / 2);
  371. }
  372. return el.hide();
  373. },
  374. close_video : function (e) {
  375. var video = $('.flex-video', e.target),
  376. iframe = $('iframe', video);
  377. if (iframe.length > 0) {
  378. iframe.attr('data-src', iframe[0].src);
  379. iframe.attr('src', iframe.attr('src'));
  380. video.hide();
  381. }
  382. },
  383. open_video : function (e) {
  384. var video = $('.flex-video', e.target),
  385. iframe = video.find('iframe');
  386. if (iframe.length > 0) {
  387. var data_src = iframe.attr('data-src');
  388. if (typeof data_src === 'string') {
  389. iframe[0].src = iframe.attr('data-src');
  390. } else {
  391. var src = iframe[0].src;
  392. iframe[0].src = undefined;
  393. iframe[0].src = src;
  394. }
  395. video.show();
  396. }
  397. },
  398. data_attr : function (str) {
  399. if (this.namespace.length > 0) {
  400. return this.namespace + '-' + str;
  401. }
  402. return str;
  403. },
  404. cache_offset : function (modal) {
  405. var offset = modal.show().height() + parseInt(modal.css('top'), 10) + modal.scrollY;
  406. modal.hide();
  407. return offset;
  408. },
  409. off : function () {
  410. $(this.scope).off('.fndtn.reveal');
  411. },
  412. reflow : function () {}
  413. };
  414. /*
  415. * getAnimationData('popAndFade') // {animate: true, pop: true, fade: true}
  416. * getAnimationData('fade') // {animate: true, pop: false, fade: true}
  417. * getAnimationData('pop') // {animate: true, pop: true, fade: false}
  418. * getAnimationData('foo') // {animate: false, pop: false, fade: false}
  419. * getAnimationData(null) // {animate: false, pop: false, fade: false}
  420. */
  421. function getAnimationData(str) {
  422. var fade = /fade/i.test(str);
  423. var pop = /pop/i.test(str);
  424. return {
  425. animate : fade || pop,
  426. pop : pop,
  427. fade : fade
  428. };
  429. }
  430. }(jQuery, window, window.document));