PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/files/foundation-rtl/3.2/javascripts/jquery.foundation.clearing.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 413 lines | 310 code | 81 blank | 22 comment | 61 complexity | 2bb064074063dd5bbcd6ce1274885f63 MD5 | raw file
  1. /*
  2. * jQuery Foundation Clearing 1.2.1
  3. * http://foundation.zurb.com
  4. * Copyright 2012, ZURB
  5. * Free to use under the MIT license.
  6. * http://www.opensource.org/licenses/mit-license.php
  7. */
  8. /*jslint unparam: true, browser: true, indent: 2 */
  9. ;(function ($, window, document, undefined) {
  10. 'use strict';
  11. var defaults = {
  12. templates : {
  13. viewing : '<a href="#" class="clearing-close">&times;</a>' +
  14. '<div class="visible-img" style="display: none"><img src="#">' +
  15. '<p class="clearing-caption"></p><a href="#" class="clearing-main-left"></a>' +
  16. '<a href="#" class="clearing-main-right"></a></div>'
  17. },
  18. // comma delimited list of selectors that, on click, will close clearing,
  19. // add 'div.clearing-blackout, div.visible-img' to close on background click
  20. close_selectors : 'a.clearing-close',
  21. // event initializers and locks
  22. initialized : false,
  23. locked : false
  24. },
  25. cl = {
  26. init : function (options, extendMethods) {
  27. return this.find('ul[data-clearing]').each(function () {
  28. var doc = $(document),
  29. $el = $(this),
  30. options = options || {},
  31. extendMethods = extendMethods || {},
  32. settings = $el.data('fndtn.clearing.settings');
  33. if (!settings) {
  34. options.$parent = $el.parent();
  35. $el.data('fndtn.clearing.settings', $.extend({}, defaults, options));
  36. cl.assemble($el.find('li'));
  37. if (!defaults.initialized) {
  38. cl.events($el);
  39. if (Modernizr.touch) cl.swipe_events();
  40. }
  41. }
  42. });
  43. },
  44. events : function (el) {
  45. var settings = el.data('fndtn.clearing.settings');
  46. $(document)
  47. .on('click.fndtn.clearing', 'ul[data-clearing] li', function (e, current, target) {
  48. var current = current || $(this),
  49. target = target || current,
  50. settings = current.parent().data('fndtn.clearing.settings');
  51. e.preventDefault();
  52. if (!settings) {
  53. current.parent().foundationClearing();
  54. }
  55. // set current and target to the clicked li if not otherwise defined.
  56. cl.open($(e.target), current, target);
  57. cl.update_paddles(target);
  58. })
  59. .on('click.fndtn.clearing', '.clearing-main-right', function (e) { cl.nav(e, 'next') })
  60. .on('click.fndtn.clearing', '.clearing-main-left', function (e) { cl.nav(e, 'prev') })
  61. .on('click.fndtn.clearing', settings.close_selectors, this.close)
  62. .on('keydown.fndtn.clearing', this.keydown);
  63. $(window).on('resize.fndtn.clearing', this.resize);
  64. defaults.initialized = true;
  65. },
  66. swipe_events : function () {
  67. $(document)
  68. .bind('swipeleft', 'ul[data-clearing]', function (e) { cl.nav(e, 'next') })
  69. .bind('swiperight', 'ul[data-clearing]', function (e) { cl.nav(e, 'prev') })
  70. .bind('movestart', 'ul[data-clearing]', function (e) {
  71. if ((e.distX > e.distY && e.distX < -e.distY) ||
  72. (e.distX < e.distY && e.distX > -e.distY)) {
  73. e.preventDefault();
  74. }
  75. });
  76. },
  77. assemble : function ($li) {
  78. var $el = $li.parent(),
  79. settings = $el.data('fndtn.clearing.settings'),
  80. grid = $el.detach(),
  81. data = {
  82. grid: '<div class="carousel">' + this.outerHTML(grid[0]) + '</div>',
  83. viewing: settings.templates.viewing
  84. },
  85. wrapper = '<div class="clearing-assembled"><div>' + data.viewing + data.grid + '</div></div>';
  86. return settings.$parent.append(wrapper);
  87. },
  88. open : function ($image, current, target) {
  89. var root = target.closest('.clearing-assembled'),
  90. container = root.find('div:first'),
  91. visible_image = container.find('.visible-img'),
  92. image = visible_image.find('img').not($image);
  93. if (!cl.locked()) {
  94. // set the image to the selected thumbnail
  95. image.attr('src', this.load($image));
  96. image.loaded(function () {
  97. // toggle the gallery if not visible
  98. root.addClass('clearing-blackout');
  99. container.addClass('clearing-container');
  100. this.caption(visible_image.find('.clearing-caption'), $image);
  101. visible_image.show();
  102. this.fix_height(target);
  103. this.center(image);
  104. // shift the thumbnails if necessary
  105. this.shift(current, target, function () {
  106. target.siblings().removeClass('visible');
  107. target.addClass('visible');
  108. });
  109. }.bind(this));
  110. }
  111. },
  112. close : function (e) {
  113. e.preventDefault();
  114. var root = (function (target) {
  115. if (/blackout/.test(target.selector)) {
  116. return target;
  117. } else {
  118. return target.closest('.clearing-blackout');
  119. }
  120. }($(this))), container, visible_image;
  121. if (this === e.target && root) {
  122. container = root.find('div:first'),
  123. visible_image = container.find('.visible-img');
  124. defaults.prev_index = 0;
  125. root.find('ul[data-clearing]').attr('style', '')
  126. root.removeClass('clearing-blackout');
  127. container.removeClass('clearing-container');
  128. visible_image.hide();
  129. }
  130. return false;
  131. },
  132. keydown : function (e) {
  133. var clearing = $('.clearing-blackout').find('ul[data-clearing]');
  134. if (e.which === 39) cl.go(clearing, 'next');
  135. if (e.which === 37) cl.go(clearing, 'prev');
  136. if (e.which === 27) $('a.clearing-close').trigger('click');
  137. },
  138. nav : function (e, direction) {
  139. var clearing = $('.clearing-blackout').find('ul[data-clearing]');
  140. e.preventDefault();
  141. this.go(clearing, direction);
  142. },
  143. resize : function () {
  144. var image = $('.clearing-blackout .visible-img').find('img');
  145. if (image.length > 0) {
  146. cl.center(image);
  147. }
  148. },
  149. fix_height : function (target) {
  150. var lis = target.siblings();
  151. lis.each(function () {
  152. var li = $(this),
  153. image = li.find('img');
  154. if (li.height() > image.outerHeight()) {
  155. li.addClass('fix-height');
  156. }
  157. })
  158. .closest('ul').width(lis.length * 100 + '%');
  159. },
  160. update_paddles : function (target) {
  161. var visible_image = target.closest('.carousel').siblings('.visible-img');
  162. if (target.next().length > 0) {
  163. visible_image.find('.clearing-main-right').removeClass('disabled');
  164. } else {
  165. visible_image.find('.clearing-main-right').addClass('disabled');
  166. }
  167. if (target.prev().length > 0) {
  168. visible_image.find('.clearing-main-left').removeClass('disabled');
  169. } else {
  170. visible_image.find('.clearing-main-left').addClass('disabled');
  171. }
  172. },
  173. load : function ($image) {
  174. var href = $image.parent().attr('href');
  175. this.preload($image);
  176. if (href) return href;
  177. return $image.attr('src');
  178. },
  179. preload : function ($image) {
  180. this.img($image.closest('li').next());
  181. this.img($image.closest('li').prev());
  182. },
  183. img : function (img) {
  184. if (img.length > 0) {
  185. var new_img = new Image(),
  186. new_a = img.find('a');
  187. if (new_a.length > 0) {
  188. new_img.src = new_a.attr('href');
  189. } else {
  190. new_img.src = img.find('img').attr('src');
  191. }
  192. }
  193. },
  194. caption : function (container, $image) {
  195. var caption = $image.data('caption');
  196. if (caption) {
  197. container.text(caption).show();
  198. } else {
  199. container.text('').hide();
  200. }
  201. },
  202. go : function ($ul, direction) {
  203. var current = $ul.find('.visible'),
  204. target = current[direction]();
  205. if (target.length > 0) {
  206. target.find('img').trigger('click', [current, target]);
  207. }
  208. },
  209. shift : function (current, target, callback) {
  210. var clearing = target.parent(),
  211. old_index = defaults.prev_index,
  212. direction = this.direction(clearing, current, target),
  213. left = parseInt(clearing.css('left'), 10),
  214. width = target.outerWidth(),
  215. skip_shift;
  216. // we use jQuery animate instead of CSS transitions because we
  217. // need a callback to unlock the next animation
  218. if (target.index() !== old_index && !/skip/.test(direction)){
  219. if (/left/.test(direction)) {
  220. this.lock();
  221. clearing.animate({left : left + width}, 300, this.unlock);
  222. } else if (/right/.test(direction)) {
  223. this.lock();
  224. clearing.animate({left : left - width}, 300, this.unlock);
  225. }
  226. } else if (/skip/.test(direction)) {
  227. // the target image is not adjacent to the current image, so
  228. // do we scroll right or not
  229. skip_shift = target.index() - defaults.up_count;
  230. this.lock();
  231. if (skip_shift > 0) {
  232. clearing.animate({left : -(skip_shift * width)}, 300, this.unlock);
  233. } else {
  234. clearing.animate({left : 0}, 300, this.unlock);
  235. }
  236. }
  237. callback();
  238. },
  239. lock : function () {
  240. defaults.locked = true;
  241. },
  242. unlock : function () {
  243. defaults.locked = false;
  244. },
  245. locked : function () {
  246. return defaults.locked;
  247. },
  248. direction : function ($el, current, target) {
  249. var lis = $el.find('li'),
  250. li_width = lis.outerWidth() + (lis.outerWidth() / 4),
  251. up_count = Math.floor($('.clearing-container').outerWidth() / li_width) - 1,
  252. target_index = lis.index(target),
  253. response;
  254. defaults.up_count = up_count;
  255. if (this.adjacent(defaults.prev_index, target_index)) {
  256. if ((target_index > up_count) && target_index > defaults.prev_index) {
  257. response = 'right';
  258. } else if ((target_index > up_count - 1) && target_index <= defaults.prev_index) {
  259. response = 'left';
  260. } else {
  261. response = false;
  262. }
  263. } else {
  264. response = 'skip';
  265. }
  266. defaults.prev_index = target_index;
  267. return response;
  268. },
  269. adjacent : function (current_index, target_index) {
  270. for (var i = target_index + 1; i >= target_index - 1; i--) {
  271. if (i === current_index) return true;
  272. }
  273. return false;
  274. },
  275. center : function (target) {
  276. target.css({
  277. marginLeft : -(target.outerWidth() / 2),
  278. marginTop : -(target.outerHeight() / 2)
  279. });
  280. },
  281. outerHTML : function (el) {
  282. // support FireFox < 11
  283. return el.outerHTML || new XMLSerializer().serializeToString(el);
  284. }
  285. };
  286. $.fn.foundationClearing = function (method) {
  287. if (cl[method]) {
  288. return cl[method].apply(this, Array.prototype.slice.call(arguments, 1));
  289. } else if (typeof method === 'object' || !method) {
  290. return cl.init.apply(this, arguments);
  291. } else {
  292. $.error('Method ' + method + ' does not exist on jQuery.foundationClearing');
  293. }
  294. };
  295. // jquery.imageready.js
  296. // @weblinc, @jsantell, (c) 2012
  297. (function( $ ) {
  298. $.fn.loaded = function ( callback, userSettings ) {
  299. var
  300. options = $.extend( {}, $.fn.loaded.defaults, userSettings ),
  301. $images = this.find( 'img' ).add( this.filter( 'img' ) ),
  302. unloadedImages = $images.length;
  303. function loaded () {
  304. unloadedImages -= 1;
  305. !unloadedImages && callback();
  306. }
  307. function bindLoad () {
  308. this.one( 'load', loaded );
  309. if ( $.browser.msie ) {
  310. var
  311. src = this.attr( 'src' ),
  312. param = src.match( /\?/ ) ? '&' : '?';
  313. param += options.cachePrefix + '=' + ( new Date() ).getTime();
  314. this.attr( 'src', src + param );
  315. }
  316. }
  317. return $images.each(function () {
  318. var $this = $( this );
  319. if ( !$this.attr( 'src' ) ) {
  320. loaded();
  321. return;
  322. }
  323. this.complete || this.readyState === 4 ?
  324. loaded() :
  325. bindLoad.call( $this );
  326. });
  327. };
  328. $.fn.loaded.defaults = {
  329. cachePrefix: 'random'
  330. };
  331. }(jQuery));
  332. }(jQuery, this, this.document));