lg-thumbnail.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**!
  2. * lg-thumbnail.js | 0.0.4 | August 9th 2016
  3. * http://sachinchoolur.github.io/lg-thumbnail.js
  4. * Copyright (c) 2016 Sachin N;
  5. * @license Apache 2.0
  6. */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.LgThumbnail = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  7. (function (global, factory) {
  8. if (typeof define === "function" && define.amd) {
  9. define([], factory);
  10. } else if (typeof exports !== "undefined") {
  11. factory();
  12. } else {
  13. var mod = {
  14. exports: {}
  15. };
  16. factory();
  17. global.lgThumbnail = mod.exports;
  18. }
  19. })(this, function () {
  20. 'use strict';
  21. var _extends = Object.assign || function (target) {
  22. for (var i = 1; i < arguments.length; i++) {
  23. var source = arguments[i];
  24. for (var key in source) {
  25. if (Object.prototype.hasOwnProperty.call(source, key)) {
  26. target[key] = source[key];
  27. }
  28. }
  29. }
  30. return target;
  31. };
  32. var thumbnailDefaults = {
  33. thumbnail: true,
  34. animateThumb: true,
  35. currentPagerPosition: 'middle',
  36. thumbWidth: 100,
  37. thumbContHeight: 100,
  38. thumbMargin: 5,
  39. exThumbImage: false,
  40. showThumbByDefault: true,
  41. toggleThumb: true,
  42. pullCaptionUp: true,
  43. enableThumbDrag: true,
  44. enableThumbSwipe: true,
  45. swipeThreshold: 50,
  46. loadYoutubeThumbnail: true,
  47. youtubeThumbSize: 1,
  48. loadVimeoThumbnail: true,
  49. vimeoThumbSize: 'thumbnail_small',
  50. loadDailymotionThumbnail: true
  51. };
  52. var Thumbnail = function Thumbnail(element) {
  53. this.el = element;
  54. this.core = window.lgData[this.el.getAttribute('lg-uid')];
  55. this.core.s = _extends({}, thumbnailDefaults, this.core.s);
  56. this.thumbOuter = null;
  57. this.thumbOuterWidth = 0;
  58. this.thumbTotalWidth = this.core.items.length * (this.core.s.thumbWidth + this.core.s.thumbMargin);
  59. this.thumbIndex = this.core.index;
  60. // Thumbnail animation value
  61. this.left = 0;
  62. this.init();
  63. return this;
  64. };
  65. Thumbnail.prototype.init = function () {
  66. var _this = this;
  67. if (this.core.s.thumbnail && this.core.items.length > 1) {
  68. if (this.core.s.showThumbByDefault) {
  69. setTimeout(function () {
  70. utils.addClass(_this.core.outer, 'lg-thumb-open');
  71. }, 700);
  72. }
  73. if (this.core.s.pullCaptionUp) {
  74. utils.addClass(this.core.outer, 'lg-pull-caption-up');
  75. }
  76. this.build();
  77. if (this.core.s.animateThumb) {
  78. if (this.core.s.enableThumbDrag && !this.core.isTouch && this.core.doCss()) {
  79. this.enableThumbDrag();
  80. }
  81. if (this.core.s.enableThumbSwipe && this.core.isTouch && this.core.doCss()) {
  82. this.enableThumbSwipe();
  83. }
  84. this.thumbClickable = false;
  85. } else {
  86. this.thumbClickable = true;
  87. }
  88. this.toggle();
  89. this.thumbkeyPress();
  90. }
  91. };
  92. Thumbnail.prototype.build = function () {
  93. var _this = this;
  94. var thumbList = '';
  95. var vimeoErrorThumbSize = '';
  96. var $thumb;
  97. var html = '<div class="lg-thumb-outer">' + '<div class="lg-thumb group">' + '</div>' + '</div>';
  98. switch (this.core.s.vimeoThumbSize) {
  99. case 'thumbnail_large':
  100. vimeoErrorThumbSize = '640';
  101. break;
  102. case 'thumbnail_medium':
  103. vimeoErrorThumbSize = '200x150';
  104. break;
  105. case 'thumbnail_small':
  106. vimeoErrorThumbSize = '100x75';
  107. }
  108. utils.addClass(_this.core.outer, 'lg-has-thumb');
  109. _this.core.outer.querySelector('.lg').insertAdjacentHTML('beforeend', html);
  110. _this.thumbOuter = _this.core.outer.querySelector('.lg-thumb-outer');
  111. _this.thumbOuterWidth = _this.thumbOuter.offsetWidth;
  112. if (_this.core.s.animateThumb) {
  113. _this.core.outer.querySelector('.lg-thumb').style.width = _this.thumbTotalWidth + 'px';
  114. _this.core.outer.querySelector('.lg-thumb').style.position = 'relative';
  115. }
  116. if (this.core.s.animateThumb) {
  117. _this.thumbOuter.style.height = _this.core.s.thumbContHeight + 'px';
  118. }
  119. function getThumb(src, thumb, index) {
  120. var isVideo = _this.core.isVideo(src, index) || {};
  121. var thumbImg;
  122. var vimeoId = '';
  123. if (isVideo.youtube || isVideo.vimeo || isVideo.dailymotion) {
  124. if (isVideo.youtube) {
  125. if (_this.core.s.loadYoutubeThumbnail) {
  126. thumbImg = '//img.youtube.com/vi/' + isVideo.youtube[1] + '/' + _this.core.s.youtubeThumbSize + '.jpg';
  127. } else {
  128. thumbImg = thumb;
  129. }
  130. } else if (isVideo.vimeo) {
  131. if (_this.core.s.loadVimeoThumbnail) {
  132. thumbImg = '//i.vimeocdn.com/video/error_' + vimeoErrorThumbSize + '.jpg';
  133. vimeoId = isVideo.vimeo[1];
  134. } else {
  135. thumbImg = thumb;
  136. }
  137. } else if (isVideo.dailymotion) {
  138. if (_this.core.s.loadDailymotionThumbnail) {
  139. thumbImg = '//www.dailymotion.com/thumbnail/video/' + isVideo.dailymotion[1];
  140. } else {
  141. thumbImg = thumb;
  142. }
  143. }
  144. } else {
  145. thumbImg = thumb;
  146. }
  147. thumbList += '<div data-vimeo-id="' + vimeoId + '" class="lg-thumb-item" style="width:' + _this.core.s.thumbWidth + 'px; margin-right: ' + _this.core.s.thumbMargin + 'px"><img src="' + thumbImg + '" /></div>';
  148. vimeoId = '';
  149. }
  150. if (_this.core.s.dynamic) {
  151. for (var j = 0; j < _this.core.s.dynamicEl.length; j++) {
  152. getThumb(_this.core.s.dynamicEl[j].src, _this.core.s.dynamicEl[j].thumb, j);
  153. }
  154. } else {
  155. for (var i = 0; i < _this.core.items.length; i++) {
  156. if (!_this.core.s.exThumbImage) {
  157. getThumb(_this.core.items[i].getAttribute('href') || _this.core.items[i].getAttribute('data-src'), _this.core.items[i].querySelector('img').getAttribute('src'), i);
  158. } else {
  159. getThumb(_this.core.items[i].getAttribute('href') || _this.core.items[i].getAttribute('data-src'), _this.core.items[i].getAttribute(_this.core.s.exThumbImage), i);
  160. }
  161. }
  162. }
  163. _this.core.outer.querySelector('.lg-thumb').innerHTML = thumbList;
  164. $thumb = _this.core.outer.querySelectorAll('.lg-thumb-item');
  165. for (var n = 0; n < $thumb.length; n++) {
  166. /*jshint loopfunc: true */
  167. (function (index) {
  168. var $this = $thumb[index];
  169. var vimeoVideoId = $this.getAttribute('data-vimeo-id');
  170. if (vimeoVideoId) {
  171. window['lgJsonP' + _this.el.getAttribute('lg-uid') + '' + n] = function (content) {
  172. $this.querySelector('img').setAttribute('src', content[0][_this.core.s.vimeoThumbSize]);
  173. };
  174. var script = document.createElement('script');
  175. script.className = 'lg-script';
  176. script.src = '//www.vimeo.com/api/v2/video/' + vimeoVideoId + '.json?callback=lgJsonP' + _this.el.getAttribute('lg-uid') + '' + n;
  177. document.body.appendChild(script);
  178. }
  179. })(n);
  180. }
  181. // manage active class for thumbnail
  182. utils.addClass($thumb[_this.core.index], 'active');
  183. utils.on(_this.core.el, 'onBeforeSlide.lgtm', function () {
  184. for (var j = 0; j < $thumb.length; j++) {
  185. utils.removeClass($thumb[j], 'active');
  186. }
  187. utils.addClass($thumb[_this.core.index], 'active');
  188. });
  189. for (var k = 0; k < $thumb.length; k++) {
  190. /*jshint loopfunc: true */
  191. (function (index) {
  192. utils.on($thumb[index], 'click.lg touchend.lg', function () {
  193. setTimeout(function () {
  194. // In IE9 and bellow touch does not support
  195. // Go to slide if browser does not support css transitions
  196. if (_this.thumbClickable && !_this.core.lgBusy || !_this.core.doCss()) {
  197. _this.core.index = index;
  198. _this.core.slide(_this.core.index, false, true);
  199. }
  200. }, 50);
  201. });
  202. })(k);
  203. }
  204. utils.on(_this.core.el, 'onBeforeSlide.lgtm', function () {
  205. _this.animateThumb(_this.core.index);
  206. });
  207. utils.on(window, 'resize.lgthumb orientationchange.lgthumb', function () {
  208. setTimeout(function () {
  209. _this.animateThumb(_this.core.index);
  210. _this.thumbOuterWidth = _this.thumbOuter.offsetWidth;
  211. }, 200);
  212. });
  213. };
  214. Thumbnail.prototype.setTranslate = function (value) {
  215. utils.setVendor(this.core.outer.querySelector('.lg-thumb'), 'Transform', 'translate3d(-' + value + 'px, 0px, 0px)');
  216. };
  217. Thumbnail.prototype.animateThumb = function (index) {
  218. var $thumb = this.core.outer.querySelector('.lg-thumb');
  219. if (this.core.s.animateThumb) {
  220. var position;
  221. switch (this.core.s.currentPagerPosition) {
  222. case 'left':
  223. position = 0;
  224. break;
  225. case 'middle':
  226. position = this.thumbOuterWidth / 2 - this.core.s.thumbWidth / 2;
  227. break;
  228. case 'right':
  229. position = this.thumbOuterWidth - this.core.s.thumbWidth;
  230. }
  231. this.left = (this.core.s.thumbWidth + this.core.s.thumbMargin) * index - 1 - position;
  232. if (this.left > this.thumbTotalWidth - this.thumbOuterWidth) {
  233. this.left = this.thumbTotalWidth - this.thumbOuterWidth;
  234. }
  235. if (this.left < 0) {
  236. this.left = 0;
  237. }
  238. if (this.core.lGalleryOn) {
  239. if (!utils.hasClass($thumb, 'on')) {
  240. utils.setVendor(this.core.outer.querySelector('.lg-thumb'), 'TransitionDuration', this.core.s.speed + 'ms');
  241. }
  242. if (!this.core.doCss()) {
  243. $thumb.style.left = -this.left + 'px';
  244. }
  245. } else {
  246. if (!this.core.doCss()) {
  247. $thumb.style.left = -this.left + 'px';
  248. }
  249. }
  250. this.setTranslate(this.left);
  251. }
  252. };
  253. // Enable thumbnail dragging and swiping
  254. Thumbnail.prototype.enableThumbDrag = function () {
  255. var _this = this;
  256. var startCoords = 0;
  257. var endCoords = 0;
  258. var isDraging = false;
  259. var isMoved = false;
  260. var tempLeft = 0;
  261. utils.addClass(_this.thumbOuter, 'lg-grab');
  262. utils.on(_this.core.outer.querySelector('.lg-thumb'), 'mousedown.lgthumb', function (e) {
  263. if (_this.thumbTotalWidth > _this.thumbOuterWidth) {
  264. // execute only on .lg-object
  265. e.preventDefault();
  266. startCoords = e.pageX;
  267. isDraging = true;
  268. // ** Fix for webkit cursor issue https://code.google.com/p/chromium/issues/detail?id=26723
  269. _this.core.outer.scrollLeft += 1;
  270. _this.core.outer.scrollLeft -= 1;
  271. // *
  272. _this.thumbClickable = false;
  273. utils.removeClass(_this.thumbOuter, 'lg-grab');
  274. utils.addClass(_this.thumbOuter, 'lg-grabbing');
  275. }
  276. });
  277. utils.on(window, 'mousemove.lgthumb', function (e) {
  278. if (isDraging) {
  279. tempLeft = _this.left;
  280. isMoved = true;
  281. endCoords = e.pageX;
  282. utils.addClass(_this.thumbOuter, 'lg-dragging');
  283. tempLeft = tempLeft - (endCoords - startCoords);
  284. if (tempLeft > _this.thumbTotalWidth - _this.thumbOuterWidth) {
  285. tempLeft = _this.thumbTotalWidth - _this.thumbOuterWidth;
  286. }
  287. if (tempLeft < 0) {
  288. tempLeft = 0;
  289. }
  290. // move current slide
  291. _this.setTranslate(tempLeft);
  292. }
  293. });
  294. utils.on(window, 'mouseup.lgthumb', function () {
  295. if (isMoved) {
  296. isMoved = false;
  297. utils.removeClass(_this.thumbOuter, 'lg-dragging');
  298. _this.left = tempLeft;
  299. if (Math.abs(endCoords - startCoords) < _this.core.s.swipeThreshold) {
  300. _this.thumbClickable = true;
  301. }
  302. } else {
  303. _this.thumbClickable = true;
  304. }
  305. if (isDraging) {
  306. isDraging = false;
  307. utils.removeClass(_this.thumbOuter, 'lg-grabbing');
  308. utils.addClass(_this.thumbOuter, 'lg-grab');
  309. }
  310. });
  311. };
  312. Thumbnail.prototype.enableThumbSwipe = function () {
  313. var _this = this;
  314. var startCoords = 0;
  315. var endCoords = 0;
  316. var isMoved = false;
  317. var tempLeft = 0;
  318. utils.on(_this.core.outer.querySelector('.lg-thumb'), 'touchstart.lg', function (e) {
  319. if (_this.thumbTotalWidth > _this.thumbOuterWidth) {
  320. e.preventDefault();
  321. startCoords = e.targetTouches[0].pageX;
  322. _this.thumbClickable = false;
  323. }
  324. });
  325. utils.on(_this.core.outer.querySelector('.lg-thumb'), 'touchmove.lg', function (e) {
  326. if (_this.thumbTotalWidth > _this.thumbOuterWidth) {
  327. e.preventDefault();
  328. endCoords = e.targetTouches[0].pageX;
  329. isMoved = true;
  330. utils.addClass(_this.thumbOuter, 'lg-dragging');
  331. tempLeft = _this.left;
  332. tempLeft = tempLeft - (endCoords - startCoords);
  333. if (tempLeft > _this.thumbTotalWidth - _this.thumbOuterWidth) {
  334. tempLeft = _this.thumbTotalWidth - _this.thumbOuterWidth;
  335. }
  336. if (tempLeft < 0) {
  337. tempLeft = 0;
  338. }
  339. // move current slide
  340. _this.setTranslate(tempLeft);
  341. }
  342. });
  343. utils.on(_this.core.outer.querySelector('.lg-thumb'), 'touchend.lg', function () {
  344. if (_this.thumbTotalWidth > _this.thumbOuterWidth) {
  345. if (isMoved) {
  346. isMoved = false;
  347. utils.removeClass(_this.thumbOuter, 'lg-dragging');
  348. if (Math.abs(endCoords - startCoords) < _this.core.s.swipeThreshold) {
  349. _this.thumbClickable = true;
  350. }
  351. _this.left = tempLeft;
  352. } else {
  353. _this.thumbClickable = true;
  354. }
  355. } else {
  356. _this.thumbClickable = true;
  357. }
  358. });
  359. };
  360. Thumbnail.prototype.toggle = function () {
  361. var _this = this;
  362. if (_this.core.s.toggleThumb) {
  363. utils.addClass(_this.core.outer, 'lg-can-toggle');
  364. _this.thumbOuter.insertAdjacentHTML('beforeend', '<span class="lg-toggle-thumb lg-icon"></span>');
  365. utils.on(_this.core.outer.querySelector('.lg-toggle-thumb'), 'click.lg', function () {
  366. if (utils.hasClass(_this.core.outer, 'lg-thumb-open')) {
  367. utils.removeClass(_this.core.outer, 'lg-thumb-open');
  368. } else {
  369. utils.addClass(_this.core.outer, 'lg-thumb-open');
  370. }
  371. });
  372. }
  373. };
  374. Thumbnail.prototype.thumbkeyPress = function () {
  375. var _this = this;
  376. utils.on(window, 'keydown.lgthumb', function (e) {
  377. if (e.keyCode === 38) {
  378. e.preventDefault();
  379. utils.addClass(_this.core.outer, 'lg-thumb-open');
  380. } else if (e.keyCode === 40) {
  381. e.preventDefault();
  382. utils.removeClass(_this.core.outer, 'lg-thumb-open');
  383. }
  384. });
  385. };
  386. Thumbnail.prototype.destroy = function () {
  387. if (this.core.s.thumbnail && this.core.items.length > 1) {
  388. utils.off(window, '.lgthumb');
  389. this.thumbOuter.parentNode.removeChild(this.thumbOuter);
  390. utils.removeClass(this.core.outer, 'lg-has-thumb');
  391. var lgScript = document.getElementsByClassName('lg-script');
  392. while (lgScript[0]) {
  393. lgScript[0].parentNode.removeChild(lgScript[0]);
  394. }
  395. }
  396. };
  397. window.lgModules.thumbnail = Thumbnail;
  398. });
  399. },{}]},{},[1])(1)
  400. });