foundation.clearing.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. ;(function ($, window, document, undefined) {
  2. 'use strict';
  3. Foundation.libs.clearing = {
  4. name : 'clearing',
  5. version: '5.1.1',
  6. settings : {
  7. templates : {
  8. viewing : '<a href="#" class="clearing-close">&times;</a>' +
  9. '<div class="visible-img" style="display: none"><div class="clearing-touch-label"></div><img src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D" alt="" />' +
  10. '<p class="clearing-caption"></p><a href="#" class="clearing-main-prev"><span></span></a>' +
  11. '<a href="#" class="clearing-main-next"><span></span></a></div>'
  12. },
  13. // comma delimited list of selectors that, on click, will close clearing,
  14. // add 'div.clearing-blackout, div.visible-img' to close on background click
  15. close_selectors : '.clearing-close',
  16. touch_label : '&larr;&nbsp;Swipe to Advance&nbsp;&rarr;',
  17. // event initializers and locks
  18. init : false,
  19. locked : false
  20. },
  21. init : function (scope, method, options) {
  22. var self = this;
  23. Foundation.inherit(this, 'throttle image_loaded');
  24. this.bindings(method, options);
  25. if (self.S(this.scope).is('[' + this.attr_name() + ']')) {
  26. this.assemble(self.S('li', this.scope));
  27. } else {
  28. self.S('[' + this.attr_name() + ']', this.scope).each(function () {
  29. self.assemble(self.S('li', this));
  30. });
  31. }
  32. },
  33. events : function (scope) {
  34. var self = this,
  35. S = self.S;
  36. S(this.scope)
  37. .off('.clearing')
  38. .on('click.fndtn.clearing', 'ul[' + this.attr_name() + '] li',
  39. function (e, current, target) {
  40. var current = current || S(this),
  41. target = target || current,
  42. next = current.next('li'),
  43. settings = current.closest('[' + self.attr_name() + ']').data(self.attr_name(true) + '-init'),
  44. image = S(e.target);
  45. e.preventDefault();
  46. if (!settings) {
  47. self.init();
  48. settings = current.closest('[' + self.attr_name() + ']').data(self.attr_name(true) + '-init');
  49. }
  50. // if clearing is open and the current image is
  51. // clicked, go to the next image in sequence
  52. if (target.hasClass('visible') &&
  53. current[0] === target[0] &&
  54. next.length > 0 && self.is_open(current)) {
  55. target = next;
  56. image = S('img', target);
  57. }
  58. // set current and target to the clicked li if not otherwise defined.
  59. self.open(image, current, target);
  60. self.update_paddles(target);
  61. })
  62. .on('click.fndtn.clearing', '.clearing-main-next',
  63. function (e) { self.nav(e, 'next') })
  64. .on('click.fndtn.clearing', '.clearing-main-prev',
  65. function (e) { self.nav(e, 'prev') })
  66. .on('click.fndtn.clearing', this.settings.close_selectors,
  67. function (e) { Foundation.libs.clearing.close(e, this) })
  68. .on('keydown.fndtn.clearing',
  69. function (e) { self.keydown(e) });
  70. S(window).off('.clearing').on('resize.fndtn.clearing',
  71. function () { self.resize() });
  72. this.swipe_events(scope);
  73. },
  74. swipe_events : function (scope) {
  75. var self = this,
  76. S = self.S;
  77. S(this.scope)
  78. .on('touchstart.fndtn.clearing', '.visible-img', function(e) {
  79. if (!e.touches) { e = e.originalEvent; }
  80. var data = {
  81. start_page_x: e.touches[0].pageX,
  82. start_page_y: e.touches[0].pageY,
  83. start_time: (new Date()).getTime(),
  84. delta_x: 0,
  85. is_scrolling: undefined
  86. };
  87. S(this).data('swipe-transition', data);
  88. e.stopPropagation();
  89. })
  90. .on('touchmove.fndtn.clearing', '.visible-img', function(e) {
  91. if (!e.touches) { e = e.originalEvent; }
  92. // Ignore pinch/zoom events
  93. if(e.touches.length > 1 || e.scale && e.scale !== 1) return;
  94. var data = S(this).data('swipe-transition');
  95. if (typeof data === 'undefined') {
  96. data = {};
  97. }
  98. data.delta_x = e.touches[0].pageX - data.start_page_x;
  99. if ( typeof data.is_scrolling === 'undefined') {
  100. data.is_scrolling = !!( data.is_scrolling || Math.abs(data.delta_x) < Math.abs(e.touches[0].pageY - data.start_page_y) );
  101. }
  102. if (!data.is_scrolling && !data.active) {
  103. e.preventDefault();
  104. var direction = (data.delta_x < 0) ? 'next' : 'prev';
  105. data.active = true;
  106. self.nav(e, direction);
  107. }
  108. })
  109. .on('touchend.fndtn.clearing', '.visible-img', function(e) {
  110. S(this).data('swipe-transition', {});
  111. e.stopPropagation();
  112. });
  113. },
  114. assemble : function ($li) {
  115. var $el = $li.parent();
  116. if ($el.parent().hasClass('carousel')) return;
  117. $el.after('<div id="foundationClearingHolder"></div>');
  118. var holder = this.S('#foundationClearingHolder'),
  119. settings = $el.data(this.attr_name(true) + '-init'),
  120. grid = $el.detach(),
  121. data = {
  122. grid: '<div class="carousel">' + grid[0].outerHTML + '</div>',
  123. viewing: settings.templates.viewing
  124. },
  125. wrapper = '<div class="clearing-assembled"><div>' + data.viewing +
  126. data.grid + '</div></div>',
  127. touch_label = this.settings.touch_label;
  128. if (Modernizr.touch) {
  129. wrapper = $(wrapper).find('.clearing-touch-label').html(touch_label).end();
  130. }
  131. holder.after(wrapper).remove();
  132. },
  133. open : function ($image, current, target) {
  134. var self = this,
  135. root = target.closest('.clearing-assembled'),
  136. container = self.S('div', root).first(),
  137. visible_image = self.S('.visible-img', container),
  138. image = self.S('img', visible_image).not($image),
  139. label = self.S('.clearing-touch-label', container);
  140. if (!this.locked()) {
  141. // set the image to the selected thumbnail
  142. image
  143. .attr('src', this.load($image))
  144. .css('visibility', 'hidden');
  145. this.image_loaded(image, function () {
  146. image.css('visibility', 'visible');
  147. // toggle the gallery
  148. root.addClass('clearing-blackout');
  149. container.addClass('clearing-container');
  150. visible_image.show();
  151. this.fix_height(target)
  152. .caption(self.S('.clearing-caption', visible_image), $image)
  153. .center_and_label(image,label)
  154. .shift(current, target, function () {
  155. target.siblings().removeClass('visible');
  156. target.addClass('visible');
  157. });
  158. }.bind(this));
  159. }
  160. },
  161. close : function (e, el) {
  162. e.preventDefault();
  163. var root = (function (target) {
  164. if (/blackout/.test(target.selector)) {
  165. return target;
  166. } else {
  167. return target.closest('.clearing-blackout');
  168. }
  169. }($(el))), container, visible_image;
  170. if (el === e.target && root) {
  171. container = $('div', root).first();
  172. visible_image = $('.visible-img', container);
  173. this.settings.prev_index = 0;
  174. $('ul[' + this.attr_name() + ']', root)
  175. .attr('style', '').closest('.clearing-blackout')
  176. .removeClass('clearing-blackout');
  177. container.removeClass('clearing-container');
  178. visible_image.hide();
  179. }
  180. return false;
  181. },
  182. is_open : function (current) {
  183. return current.parent().prop('style').length > 0;
  184. },
  185. keydown : function (e) {
  186. var clearing = $('ul[' + this.attr_name() + ']', '.clearing-blackout'),
  187. NEXT_KEY = this.rtl ? 37 : 39,
  188. PREV_KEY = this.rtl ? 39 : 37,
  189. ESC_KEY = 27;
  190. if (e.which === NEXT_KEY) this.go(clearing, 'next');
  191. if (e.which === PREV_KEY) this.go(clearing, 'prev');
  192. if (e.which === ESC_KEY) this.S('a.clearing-close').trigger('click');
  193. },
  194. nav : function (e, direction) {
  195. var clearing = $('ul[' + this.attr_name() + ']', '.clearing-blackout');
  196. e.preventDefault();
  197. this.go(clearing, direction);
  198. },
  199. resize : function () {
  200. var image = $('img', '.clearing-blackout .visible-img'),
  201. label = $('.clearing-touch-label', '.clearing-blackout');
  202. if (image.length) {
  203. this.center_and_label(image, label);
  204. }
  205. },
  206. // visual adjustments
  207. fix_height : function (target) {
  208. var lis = target.parent().children(),
  209. self = this;
  210. lis.each(function () {
  211. var li = self.S(this),
  212. image = li.find('img');
  213. if (li.height() > image.outerHeight()) {
  214. li.addClass('fix-height');
  215. }
  216. })
  217. .closest('ul')
  218. .width(lis.length * 100 + '%');
  219. return this;
  220. },
  221. update_paddles : function (target) {
  222. var visible_image = target
  223. .closest('.carousel')
  224. .siblings('.visible-img');
  225. if (target.next().length > 0) {
  226. this.S('.clearing-main-next', visible_image)
  227. .removeClass('disabled');
  228. } else {
  229. this.S('.clearing-main-next', visible_image)
  230. .addClass('disabled');
  231. }
  232. if (target.prev().length > 0) {
  233. this.S('.clearing-main-prev', visible_image)
  234. .removeClass('disabled');
  235. } else {
  236. this.S('.clearing-main-prev', visible_image)
  237. .addClass('disabled');
  238. }
  239. },
  240. center_and_label : function (target, label) {
  241. if (!this.rtl) {
  242. target.css({
  243. marginLeft : -(target.outerWidth() / 2),
  244. marginTop : -(target.outerHeight() / 2)
  245. });
  246. label.css({
  247. marginLeft : -(label.outerWidth() / 2),
  248. marginTop : -(target.outerHeight() / 2)-label.outerHeight()-10
  249. });
  250. } else {
  251. target.css({
  252. marginRight : -(target.outerWidth() / 2),
  253. marginTop : -(target.outerHeight() / 2),
  254. left: 'auto',
  255. right: '50%'
  256. });
  257. label.css({
  258. marginRight : -(label.outerWidth() / 2),
  259. marginTop : -(target.outerHeight() / 2)-label.outerHeight()-10,
  260. left: 'auto',
  261. right: '50%'
  262. });
  263. }
  264. return this;
  265. },
  266. // image loading and preloading
  267. load : function ($image) {
  268. if ($image[0].nodeName === "A") {
  269. var href = $image.attr('href');
  270. } else {
  271. var href = $image.parent().attr('href');
  272. }
  273. this.preload($image);
  274. if (href) return href;
  275. return $image.attr('src');
  276. },
  277. preload : function ($image) {
  278. this
  279. .img($image.closest('li').next())
  280. .img($image.closest('li').prev());
  281. },
  282. img : function (img) {
  283. if (img.length) {
  284. var new_img = new Image(),
  285. new_a = this.S('a', img);
  286. if (new_a.length) {
  287. new_img.src = new_a.attr('href');
  288. } else {
  289. new_img.src = this.S('img', img).attr('src');
  290. }
  291. }
  292. return this;
  293. },
  294. // image caption
  295. caption : function (container, $image) {
  296. var caption = $image.data('caption');
  297. if (caption) {
  298. container
  299. .html(caption)
  300. .show();
  301. } else {
  302. container
  303. .text('')
  304. .hide();
  305. }
  306. return this;
  307. },
  308. // directional methods
  309. go : function ($ul, direction) {
  310. var current = this.S('.visible', $ul),
  311. target = current[direction]();
  312. if (target.length) {
  313. this.S('img', target)
  314. .trigger('click', [current, target]);
  315. }
  316. },
  317. shift : function (current, target, callback) {
  318. var clearing = target.parent(),
  319. old_index = this.settings.prev_index || target.index(),
  320. direction = this.direction(clearing, current, target),
  321. dir = this.rtl ? 'right' : 'left',
  322. left = parseInt(clearing.css('left'), 10),
  323. width = target.outerWidth(),
  324. skip_shift;
  325. var dir_obj = {};
  326. // we use jQuery animate instead of CSS transitions because we
  327. // need a callback to unlock the next animation
  328. // needs support for RTL **
  329. if (target.index() !== old_index && !/skip/.test(direction)){
  330. if (/left/.test(direction)) {
  331. this.lock();
  332. dir_obj[dir] = left + width;
  333. clearing.animate(dir_obj, 300, this.unlock());
  334. } else if (/right/.test(direction)) {
  335. this.lock();
  336. dir_obj[dir] = left - width;
  337. clearing.animate(dir_obj, 300, this.unlock());
  338. }
  339. } else if (/skip/.test(direction)) {
  340. // the target image is not adjacent to the current image, so
  341. // do we scroll right or not
  342. skip_shift = target.index() - this.settings.up_count;
  343. this.lock();
  344. if (skip_shift > 0) {
  345. dir_obj[dir] = -(skip_shift * width);
  346. clearing.animate(dir_obj, 300, this.unlock());
  347. } else {
  348. dir_obj[dir] = 0;
  349. clearing.animate(dir_obj, 300, this.unlock());
  350. }
  351. }
  352. callback();
  353. },
  354. direction : function ($el, current, target) {
  355. var lis = this.S('li', $el),
  356. li_width = lis.outerWidth() + (lis.outerWidth() / 4),
  357. up_count = Math.floor(this.S('.clearing-container').outerWidth() / li_width) - 1,
  358. target_index = lis.index(target),
  359. response;
  360. this.settings.up_count = up_count;
  361. if (this.adjacent(this.settings.prev_index, target_index)) {
  362. if ((target_index > up_count)
  363. && target_index > this.settings.prev_index) {
  364. response = 'right';
  365. } else if ((target_index > up_count - 1)
  366. && target_index <= this.settings.prev_index) {
  367. response = 'left';
  368. } else {
  369. response = false;
  370. }
  371. } else {
  372. response = 'skip';
  373. }
  374. this.settings.prev_index = target_index;
  375. return response;
  376. },
  377. adjacent : function (current_index, target_index) {
  378. for (var i = target_index + 1; i >= target_index - 1; i--) {
  379. if (i === current_index) return true;
  380. }
  381. return false;
  382. },
  383. // lock management
  384. lock : function () {
  385. this.settings.locked = true;
  386. },
  387. unlock : function () {
  388. this.settings.locked = false;
  389. },
  390. locked : function () {
  391. return this.settings.locked;
  392. },
  393. off : function () {
  394. this.S(this.scope).off('.fndtn.clearing');
  395. this.S(window).off('.fndtn.clearing');
  396. },
  397. reflow : function () {
  398. this.init();
  399. }
  400. };
  401. }(jQuery, this, this.document));