viewer.js 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032
  1. /*!
  2. * Viewer v1.0.0
  3. * https://github.com/fengyuanchen/viewer
  4. *
  5. * Copyright (c) 2015-2018 Chen Fengyuan
  6. * Released under the MIT license
  7. *
  8. * Date: 2018-04-01T06:11:06.751Z
  9. */
  10. (function (global, factory) {
  11. typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('jquery')) :
  12. typeof define === 'function' && define.amd ? define(['jquery'], factory) :
  13. (factory(global.jQuery));
  14. }(this, (function ($) { 'use strict';
  15. $ = $ && $.hasOwnProperty('default') ? $['default'] : $;
  16. var DEFAULTS = {
  17. // Enable inline mode
  18. inline: false,
  19. // Show the button on the top-right of the viewer
  20. button: true,
  21. // Show the navbar
  22. navbar: true,
  23. // Show the title
  24. title: true,
  25. // Show the toolbar
  26. toolbar: true,
  27. // Show the tooltip with image ratio (percentage) when zoom in or zoom out
  28. tooltip: true,
  29. // Enable to move the image
  30. movable: true,
  31. // Enable to zoom the image
  32. zoomable: true,
  33. // Enable to rotate the image
  34. rotatable: true,
  35. // Enable to scale the image
  36. scalable: true,
  37. // Enable CSS3 Transition for some special elements
  38. transition: true,
  39. // Enable to request fullscreen when play
  40. fullscreen: true,
  41. // The amount of time to delay between automatically cycling an image when playing.
  42. interval: 5000,
  43. // Enable keyboard support
  44. keyboard: true,
  45. // Enable a modal backdrop, specify `static` for a backdrop which doesn't close the modal on click
  46. backdrop: true,
  47. // Indicate if show a loading spinner when load image or not.
  48. loading: true,
  49. // Indicate if enable loop viewing or not.
  50. loop: true,
  51. // Min width of the viewer in inline mode
  52. minWidth: 200,
  53. // Min height of the viewer in inline mode
  54. minHeight: 100,
  55. // Define the ratio when zoom the image by wheeling mouse
  56. zoomRatio: 0.1,
  57. // Define the min ratio of the image when zoom out
  58. minZoomRatio: 0.01,
  59. // Define the max ratio of the image when zoom in
  60. maxZoomRatio: 100,
  61. // Define the CSS `z-index` value of viewer in modal mode.
  62. zIndex: 2015,
  63. // Define the CSS `z-index` value of viewer in inline mode.
  64. zIndexInline: 0,
  65. // Define where to get the original image URL for viewing
  66. // Type: String (an image attribute) or Function (should return an image URL)
  67. url: 'src',
  68. // Define where to put the viewer in modal mode.
  69. // Type: String | Element
  70. container: 'body',
  71. // Filter the images for viewing.
  72. // Type: Function (return true if the image is viewable)
  73. filter: null,
  74. // Event shortcuts
  75. ready: null,
  76. show: null,
  77. shown: null,
  78. hide: null,
  79. hidden: null,
  80. view: null,
  81. viewed: null
  82. };
  83. var TEMPLATE = '<div class="viewer-container" touch-action="none">' + '<div class="viewer-canvas"></div>' + '<div class="viewer-footer">' + '<div class="viewer-title"></div>' + '<div class="viewer-toolbar"></div>' + '<div class="viewer-navbar">' + '<ul class="viewer-list"></ul>' + '</div>' + '</div>' + '<div class="viewer-tooltip"></div>' + '<div role="button" class="viewer-button" data-action="mix"></div>' + '<div class="viewer-player"></div>' + '</div>';
  84. var IN_BROWSER = typeof window !== 'undefined';
  85. var WINDOW = IN_BROWSER ? window : {};
  86. var NAMESPACE = 'viewer';
  87. // Actions
  88. var ACTION_MOVE = 'move';
  89. var ACTION_SWITCH = 'switch';
  90. var ACTION_ZOOM = 'zoom';
  91. // Classes
  92. var CLASS_ACTIVE = NAMESPACE + '-active';
  93. var CLASS_CLOSE = NAMESPACE + '-close';
  94. var CLASS_FADE = NAMESPACE + '-fade';
  95. var CLASS_FIXED = NAMESPACE + '-fixed';
  96. var CLASS_FULLSCREEN = NAMESPACE + '-fullscreen';
  97. var CLASS_FULLSCREEN_EXIT = NAMESPACE + '-fullscreen-exit';
  98. var CLASS_HIDE = NAMESPACE + '-hide';
  99. var CLASS_HIDE_MD_DOWN = NAMESPACE + '-hide-md-down';
  100. var CLASS_HIDE_SM_DOWN = NAMESPACE + '-hide-sm-down';
  101. var CLASS_HIDE_XS_DOWN = NAMESPACE + '-hide-xs-down';
  102. var CLASS_IN = NAMESPACE + '-in';
  103. var CLASS_INVISIBLE = NAMESPACE + '-invisible';
  104. var CLASS_LOADING = NAMESPACE + '-loading';
  105. var CLASS_MOVE = NAMESPACE + '-move';
  106. var CLASS_OPEN = NAMESPACE + '-open';
  107. var CLASS_SHOW = NAMESPACE + '-show';
  108. var CLASS_TRANSITION = NAMESPACE + '-transition';
  109. // Events
  110. var EVENT_READY = 'ready';
  111. var EVENT_SHOW = 'show';
  112. var EVENT_SHOWN = 'shown';
  113. var EVENT_HIDE = 'hide';
  114. var EVENT_HIDDEN = 'hidden';
  115. var EVENT_VIEW = 'view';
  116. var EVENT_VIEWED = 'viewed';
  117. var EVENT_CLICK = 'click';
  118. var EVENT_DRAG_START = 'dragstart';
  119. var EVENT_KEY_DOWN = 'keydown';
  120. var EVENT_LOAD = 'load';
  121. var EVENT_POINTER_DOWN = WINDOW.PointerEvent ? 'pointerdown' : 'touchstart mousedown';
  122. var EVENT_POINTER_MOVE = WINDOW.PointerEvent ? 'pointermove' : 'touchmove mousemove';
  123. var EVENT_POINTER_UP = WINDOW.PointerEvent ? 'pointerup pointercancel' : 'touchend touchcancel mouseup';
  124. var EVENT_RESIZE = 'resize';
  125. var EVENT_TRANSITION_END = 'transitionend';
  126. var EVENT_WHEEL = 'wheel mousewheel DOMMouseScroll';
  127. var BUTTONS = ['zoom-in', 'zoom-out', 'one-to-one', 'reset', 'prev', 'play', 'next', 'rotate-left', 'rotate-right', 'flip-horizontal', 'flip-vertical'];
  128. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
  129. return typeof obj;
  130. } : function (obj) {
  131. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  132. };
  133. var classCallCheck = function (instance, Constructor) {
  134. if (!(instance instanceof Constructor)) {
  135. throw new TypeError("Cannot call a class as a function");
  136. }
  137. };
  138. var createClass = function () {
  139. function defineProperties(target, props) {
  140. for (var i = 0; i < props.length; i++) {
  141. var descriptor = props[i];
  142. descriptor.enumerable = descriptor.enumerable || false;
  143. descriptor.configurable = true;
  144. if ("value" in descriptor) descriptor.writable = true;
  145. Object.defineProperty(target, descriptor.key, descriptor);
  146. }
  147. }
  148. return function (Constructor, protoProps, staticProps) {
  149. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  150. if (staticProps) defineProperties(Constructor, staticProps);
  151. return Constructor;
  152. };
  153. }();
  154. /**
  155. * Check if the given value is a string.
  156. * @param {*} value - The value to check.
  157. * @returns {boolean} Returns `true` if the given value is a string, else `false`.
  158. */
  159. function isString(value) {
  160. return typeof value === 'string';
  161. }
  162. /**
  163. * Check if the given value is not a number.
  164. */
  165. var isNaN = Number.isNaN || WINDOW.isNaN;
  166. /**
  167. * Check if the given value is a number.
  168. * @param {*} value - The value to check.
  169. * @returns {boolean} Returns `true` if the given value is a number, else `false`.
  170. */
  171. function isNumber(value) {
  172. return typeof value === 'number' && !isNaN(value);
  173. }
  174. /**
  175. * Check if the given value is undefined.
  176. * @param {*} value - The value to check.
  177. * @returns {boolean} Returns `true` if the given value is undefined, else `false`.
  178. */
  179. function isUndefined(value) {
  180. return typeof value === 'undefined';
  181. }
  182. /**
  183. * Check if the given value is an object.
  184. * @param {*} value - The value to check.
  185. * @returns {boolean} Returns `true` if the given value is an object, else `false`.
  186. */
  187. function isObject(value) {
  188. return (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && value !== null;
  189. }
  190. var hasOwnProperty = Object.prototype.hasOwnProperty;
  191. /**
  192. * Check if the given value is a plain object.
  193. * @param {*} value - The value to check.
  194. * @returns {boolean} Returns `true` if the given value is a plain object, else `false`.
  195. */
  196. function isPlainObject(value) {
  197. if (!isObject(value)) {
  198. return false;
  199. }
  200. try {
  201. var _constructor = value.constructor;
  202. var prototype = _constructor.prototype;
  203. return _constructor && prototype && hasOwnProperty.call(prototype, 'isPrototypeOf');
  204. } catch (e) {
  205. return false;
  206. }
  207. }
  208. /**
  209. * Check if the given value is a function.
  210. * @param {*} value - The value to check.
  211. * @returns {boolean} Returns `true` if the given value is a function, else `false`.
  212. */
  213. function isFunction(value) {
  214. return typeof value === 'function';
  215. }
  216. /**
  217. * Iterate the given data.
  218. * @param {*} data - The data to iterate.
  219. * @param {Function} callback - The process function for each element.
  220. * @returns {*} The original data.
  221. */
  222. function forEach(data, callback) {
  223. if (data && isFunction(callback)) {
  224. if (Array.isArray(data) || isNumber(data.length) /* array-like */) {
  225. var length = data.length;
  226. var i = void 0;
  227. for (i = 0; i < length; i += 1) {
  228. if (callback.call(data, data[i], i, data) === false) {
  229. break;
  230. }
  231. }
  232. } else if (isObject(data)) {
  233. Object.keys(data).forEach(function (key) {
  234. callback.call(data, data[key], key, data);
  235. });
  236. }
  237. }
  238. return data;
  239. }
  240. /**
  241. * Extend the given object.
  242. * @param {*} obj - The object to be extended.
  243. * @param {*} args - The rest objects which will be merged to the first object.
  244. * @returns {Object} The extended object.
  245. */
  246. var assign = Object.assign || function assign(obj) {
  247. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  248. args[_key - 1] = arguments[_key];
  249. }
  250. if (isObject(obj) && args.length > 0) {
  251. args.forEach(function (arg) {
  252. if (isObject(arg)) {
  253. Object.keys(arg).forEach(function (key) {
  254. obj[key] = arg[key];
  255. });
  256. }
  257. });
  258. }
  259. return obj;
  260. };
  261. var REGEXP_SUFFIX = /^(?:width|height|left|top|marginLeft|marginTop)$/;
  262. /**
  263. * Apply styles to the given element.
  264. * @param {Element} element - The target element.
  265. * @param {Object} styles - The styles for applying.
  266. */
  267. function setStyle(element, styles) {
  268. var style = element.style;
  269. forEach(styles, function (value, property) {
  270. if (REGEXP_SUFFIX.test(property) && isNumber(value)) {
  271. value += 'px';
  272. }
  273. style[property] = value;
  274. });
  275. }
  276. /**
  277. * Check if the given element has a special class.
  278. * @param {Element} element - The element to check.
  279. * @param {string} value - The class to search.
  280. * @returns {boolean} Returns `true` if the special class was found.
  281. */
  282. function hasClass(element, value) {
  283. return element.classList ? element.classList.contains(value) : element.className.indexOf(value) > -1;
  284. }
  285. /**
  286. * Add classes to the given element.
  287. * @param {Element} element - The target element.
  288. * @param {string} value - The classes to be added.
  289. */
  290. function addClass(element, value) {
  291. if (!value) {
  292. return;
  293. }
  294. if (isNumber(element.length)) {
  295. forEach(element, function (elem) {
  296. addClass(elem, value);
  297. });
  298. return;
  299. }
  300. if (element.classList) {
  301. element.classList.add(value);
  302. return;
  303. }
  304. var className = element.className.trim();
  305. if (!className) {
  306. element.className = value;
  307. } else if (className.indexOf(value) < 0) {
  308. element.className = className + ' ' + value;
  309. }
  310. }
  311. /**
  312. * Remove classes from the given element.
  313. * @param {Element} element - The target element.
  314. * @param {string} value - The classes to be removed.
  315. */
  316. function removeClass(element, value) {
  317. if (!value) {
  318. return;
  319. }
  320. if (isNumber(element.length)) {
  321. forEach(element, function (elem) {
  322. removeClass(elem, value);
  323. });
  324. return;
  325. }
  326. if (element.classList) {
  327. element.classList.remove(value);
  328. return;
  329. }
  330. if (element.className.indexOf(value) >= 0) {
  331. element.className = element.className.replace(value, '');
  332. }
  333. }
  334. /**
  335. * Add or remove classes from the given element.
  336. * @param {Element} element - The target element.
  337. * @param {string} value - The classes to be toggled.
  338. * @param {boolean} added - Add only.
  339. */
  340. function toggleClass(element, value, added) {
  341. if (!value) {
  342. return;
  343. }
  344. if (isNumber(element.length)) {
  345. forEach(element, function (elem) {
  346. toggleClass(elem, value, added);
  347. });
  348. return;
  349. }
  350. // IE10-11 doesn't support the second parameter of `classList.toggle`
  351. if (added) {
  352. addClass(element, value);
  353. } else {
  354. removeClass(element, value);
  355. }
  356. }
  357. var REGEXP_HYPHENATE = /([a-z\d])([A-Z])/g;
  358. /**
  359. * Transform the given string from camelCase to kebab-case
  360. * @param {string} value - The value to transform.
  361. * @returns {string} The transformed value.
  362. */
  363. function hyphenate(value) {
  364. return value.replace(REGEXP_HYPHENATE, '$1-$2').toLowerCase();
  365. }
  366. /**
  367. * Get data from the given element.
  368. * @param {Element} element - The target element.
  369. * @param {string} name - The data key to get.
  370. * @returns {string} The data value.
  371. */
  372. function getData(element, name) {
  373. if (isObject(element[name])) {
  374. return element[name];
  375. } else if (element.dataset) {
  376. return element.dataset[name];
  377. }
  378. return element.getAttribute('data-' + hyphenate(name));
  379. }
  380. /**
  381. * Set data to the given element.
  382. * @param {Element} element - The target element.
  383. * @param {string} name - The data key to set.
  384. * @param {string} data - The data value.
  385. */
  386. function setData(element, name, data) {
  387. if (isObject(data)) {
  388. element[name] = data;
  389. } else if (element.dataset) {
  390. element.dataset[name] = data;
  391. } else {
  392. element.setAttribute('data-' + hyphenate(name), data);
  393. }
  394. }
  395. /**
  396. * Remove data from the given element.
  397. * @param {Element} element - The target element.
  398. * @param {string} name - The data key to remove.
  399. */
  400. function removeData(element, name) {
  401. if (isObject(element[name])) {
  402. try {
  403. delete element[name];
  404. } catch (e) {
  405. element[name] = undefined;
  406. }
  407. } else if (element.dataset) {
  408. // #128 Safari not allows to delete dataset property
  409. try {
  410. delete element.dataset[name];
  411. } catch (e) {
  412. element.dataset[name] = undefined;
  413. }
  414. } else {
  415. element.removeAttribute('data-' + hyphenate(name));
  416. }
  417. }
  418. var REGEXP_SPACES = /\s\s*/;
  419. var onceSupported = function () {
  420. var supported = false;
  421. if (IN_BROWSER) {
  422. var once = false;
  423. var listener = function listener() {};
  424. var options = Object.defineProperty({}, 'once', {
  425. get: function get$$1() {
  426. supported = true;
  427. return once;
  428. },
  429. /**
  430. * This setter can fix a `TypeError` in strict mode
  431. * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Getter_only}
  432. * @param {boolean} value - The value to set
  433. */
  434. set: function set$$1(value) {
  435. once = value;
  436. }
  437. });
  438. WINDOW.addEventListener('test', listener, options);
  439. WINDOW.removeEventListener('test', listener, options);
  440. }
  441. return supported;
  442. }();
  443. /**
  444. * Remove event listener from the target element.
  445. * @param {Element} element - The event target.
  446. * @param {string} type - The event type(s).
  447. * @param {Function} listener - The event listener.
  448. * @param {Object} options - The event options.
  449. */
  450. function removeListener(element, type, listener) {
  451. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  452. var handler = listener;
  453. type.trim().split(REGEXP_SPACES).forEach(function (event) {
  454. if (!onceSupported) {
  455. var listeners = element.listeners;
  456. if (listeners && listeners[event] && listeners[event][listener]) {
  457. handler = listeners[event][listener];
  458. delete listeners[event][listener];
  459. if (Object.keys(listeners[event]).length === 0) {
  460. delete listeners[event];
  461. }
  462. if (Object.keys(listeners).length === 0) {
  463. delete element.listeners;
  464. }
  465. }
  466. }
  467. element.removeEventListener(event, handler, options);
  468. });
  469. }
  470. /**
  471. * Add event listener to the target element.
  472. * @param {Element} element - The event target.
  473. * @param {string} type - The event type(s).
  474. * @param {Function} listener - The event listener.
  475. * @param {Object} options - The event options.
  476. */
  477. function addListener(element, type, listener) {
  478. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  479. var _handler = listener;
  480. type.trim().split(REGEXP_SPACES).forEach(function (event) {
  481. if (options.once && !onceSupported) {
  482. var _element$listeners = element.listeners,
  483. listeners = _element$listeners === undefined ? {} : _element$listeners;
  484. _handler = function handler() {
  485. for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  486. args[_key2] = arguments[_key2];
  487. }
  488. delete listeners[event][listener];
  489. element.removeEventListener(event, _handler, options);
  490. listener.apply(element, args);
  491. };
  492. if (!listeners[event]) {
  493. listeners[event] = {};
  494. }
  495. if (listeners[event][listener]) {
  496. element.removeEventListener(event, listeners[event][listener], options);
  497. }
  498. listeners[event][listener] = _handler;
  499. element.listeners = listeners;
  500. }
  501. element.addEventListener(event, _handler, options);
  502. });
  503. }
  504. /**
  505. * Dispatch event on the target element.
  506. * @param {Element} element - The event target.
  507. * @param {string} type - The event type(s).
  508. * @param {Object} data - The additional event data.
  509. * @returns {boolean} Indicate if the event is default prevented or not.
  510. */
  511. function dispatchEvent(element, type, data) {
  512. var event = void 0;
  513. // Event and CustomEvent on IE9-11 are global objects, not constructors
  514. if (isFunction(Event) && isFunction(CustomEvent)) {
  515. event = new CustomEvent(type, {
  516. detail: data,
  517. bubbles: true,
  518. cancelable: true
  519. });
  520. } else {
  521. event = document.createEvent('CustomEvent');
  522. event.initCustomEvent(type, true, true, data);
  523. }
  524. return element.dispatchEvent(event);
  525. }
  526. /**
  527. * Get the offset base on the document.
  528. * @param {Element} element - The target element.
  529. * @returns {Object} The offset data.
  530. */
  531. function getOffset(element) {
  532. var box = element.getBoundingClientRect();
  533. return {
  534. left: box.left + (window.pageXOffset - document.documentElement.clientLeft),
  535. top: box.top + (window.pageYOffset - document.documentElement.clientTop)
  536. };
  537. }
  538. /**
  539. * Get transforms base on the given object.
  540. * @param {Object} obj - The target object.
  541. * @returns {string} A string contains transform values.
  542. */
  543. function getTransforms(_ref) {
  544. var rotate = _ref.rotate,
  545. scaleX = _ref.scaleX,
  546. scaleY = _ref.scaleY,
  547. translateX = _ref.translateX,
  548. translateY = _ref.translateY;
  549. var values = [];
  550. if (isNumber(translateX) && translateX !== 0) {
  551. values.push('translateX(' + translateX + 'px)');
  552. }
  553. if (isNumber(translateY) && translateY !== 0) {
  554. values.push('translateY(' + translateY + 'px)');
  555. }
  556. // Rotate should come first before scale to match orientation transform
  557. if (isNumber(rotate) && rotate !== 0) {
  558. values.push('rotate(' + rotate + 'deg)');
  559. }
  560. if (isNumber(scaleX) && scaleX !== 1) {
  561. values.push('scaleX(' + scaleX + ')');
  562. }
  563. if (isNumber(scaleY) && scaleY !== 1) {
  564. values.push('scaleY(' + scaleY + ')');
  565. }
  566. var transform = values.length ? values.join(' ') : 'none';
  567. return {
  568. WebkitTransform: transform,
  569. msTransform: transform,
  570. transform: transform
  571. };
  572. }
  573. /**
  574. * Get an image name from an image url.
  575. * @param {string} url - The target url.
  576. * @example
  577. * // picture.jpg
  578. * getImageNameFromURL('http://domain.com/path/to/picture.jpg?size=1280×960')
  579. * @returns {string} A string contains the image name.
  580. */
  581. function getImageNameFromURL(url) {
  582. return isString(url) ? url.replace(/^.*\//, '').replace(/[?&#].*$/, '') : '';
  583. }
  584. var IS_SAFARI = WINDOW.navigator && /(Macintosh|iPhone|iPod|iPad).*AppleWebKit/i.test(WINDOW.navigator.userAgent);
  585. /**
  586. * Get an image's natural sizes.
  587. * @param {string} image - The target image.
  588. * @param {Function} callback - The callback function.
  589. * @returns {HTMLImageElement} The new image.
  590. */
  591. function getImageNaturalSizes(image, callback) {
  592. var newImage = document.createElement('img');
  593. // Modern browsers (except Safari)
  594. if (image.naturalWidth && !IS_SAFARI) {
  595. callback(image.naturalWidth, image.naturalHeight);
  596. return newImage;
  597. }
  598. var body = document.body || document.documentElement;
  599. newImage.onload = function () {
  600. callback(newImage.width, newImage.height);
  601. if (!IS_SAFARI) {
  602. body.removeChild(newImage);
  603. }
  604. };
  605. newImage.src = image.src;
  606. // iOS Safari will convert the image automatically
  607. // with its orientation once append it into DOM
  608. if (!IS_SAFARI) {
  609. newImage.style.cssText = 'left:0;' + 'max-height:none!important;' + 'max-width:none!important;' + 'min-height:0!important;' + 'min-width:0!important;' + 'opacity:0;' + 'position:absolute;' + 'top:0;' + 'z-index:-1;';
  610. body.appendChild(newImage);
  611. }
  612. return newImage;
  613. }
  614. /**
  615. * Get the related class name of a responsive type number.
  616. * @param {string} type - The responsive type.
  617. * @returns {string} The related class name.
  618. */
  619. function getResponsiveClass(type) {
  620. switch (type) {
  621. case 2:
  622. return CLASS_HIDE_XS_DOWN;
  623. case 3:
  624. return CLASS_HIDE_SM_DOWN;
  625. case 4:
  626. return CLASS_HIDE_MD_DOWN;
  627. default:
  628. return '';
  629. }
  630. }
  631. /**
  632. * Get the max ratio of a group of pointers.
  633. * @param {string} pointers - The target pointers.
  634. * @returns {number} The result ratio.
  635. */
  636. function getMaxZoomRatio(pointers) {
  637. var pointers2 = assign({}, pointers);
  638. var ratios = [];
  639. forEach(pointers, function (pointer, pointerId) {
  640. delete pointers2[pointerId];
  641. forEach(pointers2, function (pointer2) {
  642. var x1 = Math.abs(pointer.startX - pointer2.startX);
  643. var y1 = Math.abs(pointer.startY - pointer2.startY);
  644. var x2 = Math.abs(pointer.endX - pointer2.endX);
  645. var y2 = Math.abs(pointer.endY - pointer2.endY);
  646. var z1 = Math.sqrt(x1 * x1 + y1 * y1);
  647. var z2 = Math.sqrt(x2 * x2 + y2 * y2);
  648. var ratio = (z2 - z1) / z1;
  649. ratios.push(ratio);
  650. });
  651. });
  652. ratios.sort(function (a, b) {
  653. return Math.abs(a) < Math.abs(b);
  654. });
  655. return ratios[0];
  656. }
  657. /**
  658. * Get a pointer from an event object.
  659. * @param {Object} event - The target event object.
  660. * @param {boolean} endOnly - Indicates if only returns the end point coordinate or not.
  661. * @returns {Object} The result pointer contains start and/or end point coordinates.
  662. */
  663. function getPointer(_ref2, endOnly) {
  664. var pageX = _ref2.pageX,
  665. pageY = _ref2.pageY;
  666. var end = {
  667. endX: pageX,
  668. endY: pageY
  669. };
  670. return endOnly ? end : assign({
  671. startX: pageX,
  672. startY: pageY
  673. }, end);
  674. }
  675. /**
  676. * Get the center point coordinate of a group of pointers.
  677. * @param {Object} pointers - The target pointers.
  678. * @returns {Object} The center point coordinate.
  679. */
  680. function getPointersCenter(pointers) {
  681. var pageX = 0;
  682. var pageY = 0;
  683. var count = 0;
  684. forEach(pointers, function (_ref3) {
  685. var startX = _ref3.startX,
  686. startY = _ref3.startY;
  687. pageX += startX;
  688. pageY += startY;
  689. count += 1;
  690. });
  691. pageX /= count;
  692. pageY /= count;
  693. return {
  694. pageX: pageX,
  695. pageY: pageY
  696. };
  697. }
  698. var render = {
  699. render: function render() {
  700. this.initContainer();
  701. this.initViewer();
  702. this.initList();
  703. this.renderViewer();
  704. },
  705. initContainer: function initContainer() {
  706. this.containerData = {
  707. width: window.innerWidth,
  708. height: window.innerHeight
  709. };
  710. },
  711. initViewer: function initViewer() {
  712. var options = this.options,
  713. parent = this.parent;
  714. var viewerData = void 0;
  715. if (options.inline) {
  716. viewerData = {
  717. width: Math.max(parent.offsetWidth, options.minWidth),
  718. height: Math.max(parent.offsetHeight, options.minHeight)
  719. };
  720. this.parentData = viewerData;
  721. }
  722. if (this.fulled || !viewerData) {
  723. viewerData = this.containerData;
  724. }
  725. this.viewerData = assign({}, viewerData);
  726. },
  727. renderViewer: function renderViewer() {
  728. if (this.options.inline && !this.fulled) {
  729. setStyle(this.viewer, this.viewerData);
  730. }
  731. },
  732. initList: function initList() {
  733. var _this = this;
  734. var element = this.element,
  735. options = this.options,
  736. list = this.list;
  737. var items = [];
  738. forEach(this.images, function (image, i) {
  739. var src = image.src;
  740. var alt = image.alt || getImageNameFromURL(src);
  741. var url = options.url;
  742. if (isString(url)) {
  743. url = image.getAttribute(url);
  744. } else if (isFunction(url)) {
  745. url = url.call(_this, image);
  746. }
  747. if (src || url) {
  748. items.push('<li>' + '<img' + (' src="' + (src || url) + '"') + ' role="button"' + ' data-action="view"' + (' data-index="' + i + '"') + (' data-original-url="' + (url || src) + '"') + (' alt="' + alt + '"') + '>' + '</li>');
  749. }
  750. });
  751. list.innerHTML = items.join('');
  752. this.items = list.getElementsByTagName('li');
  753. forEach(this.items, function (item) {
  754. var image = item.firstElementChild;
  755. setData(image, 'filled', true);
  756. if (options.loading) {
  757. addClass(item, CLASS_LOADING);
  758. }
  759. addListener(image, EVENT_LOAD, function (event) {
  760. if (options.loading) {
  761. removeClass(item, CLASS_LOADING);
  762. }
  763. _this.loadImage(event);
  764. }, {
  765. once: true
  766. });
  767. });
  768. if (options.transition) {
  769. addListener(element, EVENT_VIEWED, function () {
  770. addClass(list, CLASS_TRANSITION);
  771. }, {
  772. once: true
  773. });
  774. }
  775. },
  776. renderList: function renderList(index) {
  777. var i = index || this.index;
  778. var width = this.items[i].offsetWidth || 30;
  779. var outerWidth = width + 1; // 1 pixel of `margin-left` width
  780. // Place the active item in the center of the screen
  781. setStyle(this.list, assign({
  782. width: outerWidth * this.length
  783. }, getTransforms({
  784. translateX: (this.viewerData.width - width) / 2 - outerWidth * i
  785. })));
  786. },
  787. resetList: function resetList() {
  788. var list = this.list;
  789. list.innerHTML = '';
  790. removeClass(list, CLASS_TRANSITION);
  791. setStyle(list, getTransforms({
  792. translateX: 0
  793. }));
  794. },
  795. initImage: function initImage(done) {
  796. var _this2 = this;
  797. var options = this.options,
  798. image = this.image,
  799. viewerData = this.viewerData;
  800. var footerHeight = this.footer.offsetHeight;
  801. var viewerWidth = viewerData.width;
  802. var viewerHeight = Math.max(viewerData.height - footerHeight, footerHeight);
  803. var oldImageData = this.imageData || {};
  804. var sizingImage = void 0;
  805. this.imageInitializing = {
  806. abort: function abort() {
  807. sizingImage.onload = null;
  808. }
  809. };
  810. sizingImage = getImageNaturalSizes(image, function (naturalWidth, naturalHeight) {
  811. var aspectRatio = naturalWidth / naturalHeight;
  812. var width = viewerWidth;
  813. var height = viewerHeight;
  814. _this2.imageInitializing = false;
  815. if (viewerHeight * aspectRatio > viewerWidth) {
  816. height = viewerWidth / aspectRatio;
  817. } else {
  818. width = viewerHeight * aspectRatio;
  819. }
  820. width = Math.min(width * 0.9, naturalWidth);
  821. height = Math.min(height * 0.9, naturalHeight);
  822. var imageData = {
  823. naturalWidth: naturalWidth,
  824. naturalHeight: naturalHeight,
  825. aspectRatio: aspectRatio,
  826. ratio: width / naturalWidth,
  827. width: width,
  828. height: height,
  829. left: (viewerWidth - width) / 2,
  830. top: (viewerHeight - height) / 2
  831. };
  832. var initialImageData = assign({}, imageData);
  833. if (options.rotatable) {
  834. imageData.rotate = oldImageData.rotate || 0;
  835. initialImageData.rotate = 0;
  836. }
  837. if (options.scalable) {
  838. imageData.scaleX = oldImageData.scaleX || 1;
  839. imageData.scaleY = oldImageData.scaleY || 1;
  840. initialImageData.scaleX = 1;
  841. initialImageData.scaleY = 1;
  842. }
  843. _this2.imageData = imageData;
  844. _this2.initialImageData = initialImageData;
  845. if (done) {
  846. done();
  847. }
  848. });
  849. },
  850. renderImage: function renderImage(done) {
  851. var _this3 = this;
  852. var image = this.image,
  853. imageData = this.imageData;
  854. setStyle(image, assign({
  855. width: imageData.width,
  856. height: imageData.height,
  857. marginLeft: imageData.left,
  858. marginTop: imageData.top
  859. }, getTransforms(imageData)));
  860. if (done) {
  861. if (this.viewing && this.options.transition) {
  862. var onTransitionEnd = function onTransitionEnd() {
  863. _this3.imageRendering = false;
  864. done();
  865. };
  866. this.imageRendering = {
  867. abort: function abort() {
  868. removeListener(image, EVENT_TRANSITION_END, onTransitionEnd);
  869. }
  870. };
  871. addListener(image, EVENT_TRANSITION_END, onTransitionEnd, {
  872. once: true
  873. });
  874. } else {
  875. done();
  876. }
  877. }
  878. },
  879. resetImage: function resetImage() {
  880. // this.image only defined after viewed
  881. if (this.viewing || this.viewed) {
  882. var image = this.image;
  883. if (this.viewing) {
  884. this.viewing.abort();
  885. }
  886. image.parentNode.removeChild(image);
  887. this.image = null;
  888. }
  889. }
  890. };
  891. var events = {
  892. bind: function bind() {
  893. var element = this.element,
  894. viewer = this.viewer;
  895. addListener(viewer, EVENT_CLICK, this.onClick = this.click.bind(this));
  896. addListener(viewer, EVENT_WHEEL, this.onWheel = this.wheel.bind(this));
  897. addListener(viewer, EVENT_DRAG_START, this.onDragStart = this.dragstart.bind(this));
  898. addListener(this.canvas, EVENT_POINTER_DOWN, this.onPointerDown = this.pointerdown.bind(this));
  899. addListener(element.ownerDocument, EVENT_POINTER_MOVE, this.onPointerMove = this.pointermove.bind(this));
  900. addListener(element.ownerDocument, EVENT_POINTER_UP, this.onPointerUp = this.pointerup.bind(this));
  901. addListener(element.ownerDocument, EVENT_KEY_DOWN, this.onKeyDown = this.keydown.bind(this));
  902. addListener(window, EVENT_RESIZE, this.onResize = this.resize.bind(this));
  903. },
  904. unbind: function unbind() {
  905. var element = this.element,
  906. viewer = this.viewer;
  907. removeListener(viewer, EVENT_CLICK, this.onClick);
  908. removeListener(viewer, EVENT_WHEEL, this.onWheel);
  909. removeListener(viewer, EVENT_DRAG_START, this.onDragStart);
  910. removeListener(this.canvas, EVENT_POINTER_DOWN, this.onPointerDown);
  911. removeListener(element.ownerDocument, EVENT_POINTER_MOVE, this.onPointerMove);
  912. removeListener(element.ownerDocument, EVENT_POINTER_UP, this.onPointerUp);
  913. removeListener(element.ownerDocument, EVENT_KEY_DOWN, this.onKeyDown);
  914. removeListener(window, EVENT_RESIZE, this.onResize);
  915. }
  916. };
  917. var handlers = {
  918. click: function click(_ref) {
  919. var target = _ref.target;
  920. var options = this.options,
  921. imageData = this.imageData;
  922. var action = getData(target, 'action');
  923. switch (action) {
  924. case 'mix':
  925. if (this.played) {
  926. this.stop();
  927. } else if (options.inline) {
  928. if (this.fulled) {
  929. this.exit();
  930. } else {
  931. this.full();
  932. }
  933. } else {
  934. this.hide();
  935. }
  936. break;
  937. case 'hide':
  938. this.hide();
  939. break;
  940. case 'view':
  941. this.view(getData(target, 'index'));
  942. break;
  943. case 'zoom-in':
  944. this.zoom(0.1, true);
  945. break;
  946. case 'zoom-out':
  947. this.zoom(-0.1, true);
  948. break;
  949. case 'one-to-one':
  950. this.toggle();
  951. break;
  952. case 'reset':
  953. this.reset();
  954. break;
  955. case 'prev':
  956. this.prev(options.loop);
  957. break;
  958. case 'play':
  959. this.play(options.fullscreen);
  960. break;
  961. case 'next':
  962. this.next(options.loop);
  963. break;
  964. case 'rotate-left':
  965. this.rotate(-90);
  966. break;
  967. case 'rotate-right':
  968. this.rotate(90);
  969. break;
  970. case 'flip-horizontal':
  971. this.scaleX(-imageData.scaleX || -1);
  972. break;
  973. case 'flip-vertical':
  974. this.scaleY(-imageData.scaleY || -1);
  975. break;
  976. default:
  977. if (this.played) {
  978. this.stop();
  979. }
  980. }
  981. },
  982. load: function load() {
  983. var _this = this;
  984. if (this.timeout) {
  985. clearTimeout(this.timeout);
  986. this.timeout = false;
  987. }
  988. var element = this.element,
  989. options = this.options,
  990. image = this.image,
  991. index = this.index,
  992. viewerData = this.viewerData;
  993. removeClass(image, CLASS_INVISIBLE);
  994. if (options.loading) {
  995. removeClass(this.canvas, CLASS_LOADING);
  996. }
  997. image.style.cssText = 'height:0;' + ('margin-left:' + viewerData.width / 2 + 'px;') + ('margin-top:' + viewerData.height / 2 + 'px;') + 'max-width:none!important;' + 'position:absolute;' + 'width:0;';
  998. this.initImage(function () {
  999. toggleClass(image, CLASS_MOVE, options.movable);
  1000. toggleClass(image, CLASS_TRANSITION, options.transition);
  1001. _this.renderImage(function () {
  1002. _this.viewed = true;
  1003. _this.viewing = false;
  1004. if (isFunction(options.viewed)) {
  1005. addListener(element, EVENT_VIEWED, options.viewed, {
  1006. once: true
  1007. });
  1008. }
  1009. dispatchEvent(element, EVENT_VIEWED, {
  1010. originalImage: _this.images[index],
  1011. index: index,
  1012. image: image
  1013. });
  1014. });
  1015. });
  1016. },
  1017. loadImage: function loadImage(e) {
  1018. var image = e.target;
  1019. var parent = image.parentNode;
  1020. var parentWidth = parent.offsetWidth || 30;
  1021. var parentHeight = parent.offsetHeight || 50;
  1022. var filled = !!getData(image, 'filled');
  1023. getImageNaturalSizes(image, function (naturalWidth, naturalHeight) {
  1024. var aspectRatio = naturalWidth / naturalHeight;
  1025. var width = parentWidth;
  1026. var height = parentHeight;
  1027. if (parentHeight * aspectRatio > parentWidth) {
  1028. if (filled) {
  1029. width = parentHeight * aspectRatio;
  1030. } else {
  1031. height = parentWidth / aspectRatio;
  1032. }
  1033. } else if (filled) {
  1034. height = parentWidth / aspectRatio;
  1035. } else {
  1036. width = parentHeight * aspectRatio;
  1037. }
  1038. setStyle(image, assign({
  1039. width: width,
  1040. height: height
  1041. }, getTransforms({
  1042. translateX: (parentWidth - width) / 2,
  1043. translateY: (parentHeight - height) / 2
  1044. })));
  1045. });
  1046. },
  1047. keydown: function keydown(e) {
  1048. var options = this.options;
  1049. if (!this.fulled || !options.keyboard) {
  1050. return;
  1051. }
  1052. switch (e.keyCode || e.which || e.charCode) {
  1053. // Escape
  1054. case 27:
  1055. if (this.played) {
  1056. this.stop();
  1057. } else if (options.inline) {
  1058. if (this.fulled) {
  1059. this.exit();
  1060. }
  1061. } else {
  1062. this.hide();
  1063. }
  1064. break;
  1065. // Space
  1066. case 32:
  1067. if (this.played) {
  1068. this.stop();
  1069. }
  1070. break;
  1071. // ArrowLeft
  1072. case 37:
  1073. this.prev(options.loop);
  1074. break;
  1075. // ArrowUp
  1076. case 38:
  1077. // Prevent scroll on Firefox
  1078. e.preventDefault();
  1079. // Zoom in
  1080. this.zoom(options.zoomRatio, true);
  1081. break;
  1082. // ArrowRight
  1083. case 39:
  1084. this.next(options.loop);
  1085. break;
  1086. // ArrowDown
  1087. case 40:
  1088. // Prevent scroll on Firefox
  1089. e.preventDefault();
  1090. // Zoom out
  1091. this.zoom(-options.zoomRatio, true);
  1092. break;
  1093. // Ctrl + 0
  1094. case 48:
  1095. // Fall through
  1096. // Ctrl + 1
  1097. // eslint-disable-next-line no-fallthrough
  1098. case 49:
  1099. if (e.ctrlKey) {
  1100. e.preventDefault();
  1101. this.toggle();
  1102. }
  1103. break;
  1104. default:
  1105. }
  1106. },
  1107. dragstart: function dragstart(e) {
  1108. if (e.target.tagName.toLowerCase() === 'img') {
  1109. e.preventDefault();
  1110. }
  1111. },
  1112. pointerdown: function pointerdown(e) {
  1113. var options = this.options,
  1114. pointers = this.pointers;
  1115. if (!this.viewed || this.showing || this.viewing || this.hiding) {
  1116. return;
  1117. }
  1118. if (e.changedTouches) {
  1119. forEach(e.changedTouches, function (touch) {
  1120. pointers[touch.identifier] = getPointer(touch);
  1121. });
  1122. } else {
  1123. pointers[e.pointerId || 0] = getPointer(e);
  1124. }
  1125. var action = options.movable ? ACTION_MOVE : false;
  1126. if (Object.keys(pointers).length > 1) {
  1127. action = ACTION_ZOOM;
  1128. } else if ((e.pointerType === 'touch' || e.type === 'touchstart') && this.isSwitchable()) {
  1129. action = ACTION_SWITCH;
  1130. }
  1131. this.action = action;
  1132. },
  1133. pointermove: function pointermove(e) {
  1134. var options = this.options,
  1135. pointers = this.pointers,
  1136. action = this.action,
  1137. image = this.image;
  1138. if (!this.viewed || !action) {
  1139. return;
  1140. }
  1141. e.preventDefault();
  1142. if (e.changedTouches) {
  1143. forEach(e.changedTouches, function (touch) {
  1144. assign(pointers[touch.identifier], getPointer(touch, true));
  1145. });
  1146. } else {
  1147. assign(pointers[e.pointerId || 0], getPointer(e, true));
  1148. }
  1149. if (action === ACTION_MOVE && options.transition && hasClass(image, CLASS_TRANSITION)) {
  1150. removeClass(image, CLASS_TRANSITION);
  1151. }
  1152. this.change(e);
  1153. },
  1154. pointerup: function pointerup(e) {
  1155. var action = this.action,
  1156. pointers = this.pointers;
  1157. if (e.changedTouches) {
  1158. forEach(e.changedTouches, function (touch) {
  1159. delete pointers[touch.identifier];
  1160. });
  1161. } else {
  1162. delete pointers[e.pointerId || 0];
  1163. }
  1164. if (!action) {
  1165. return;
  1166. }
  1167. if (action === ACTION_MOVE && this.options.transition) {
  1168. addClass(this.image, CLASS_TRANSITION);
  1169. }
  1170. this.action = false;
  1171. },
  1172. resize: function resize() {
  1173. var _this2 = this;
  1174. if (!this.isShown || this.hiding) {
  1175. return;
  1176. }
  1177. this.initContainer();
  1178. this.initViewer();
  1179. this.renderViewer();
  1180. this.renderList();
  1181. if (this.viewed) {
  1182. this.initImage(function () {
  1183. _this2.renderImage();
  1184. });
  1185. }
  1186. if (this.played) {
  1187. if (this.options.fullscreen && this.fulled && !document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) {
  1188. this.stop();
  1189. return;
  1190. }
  1191. forEach(this.player.getElementsByTagName('img'), function (image) {
  1192. addListener(image, EVENT_LOAD, _this2.loadImage.bind(_this2), {
  1193. once: true
  1194. });
  1195. dispatchEvent(image, EVENT_LOAD);
  1196. });
  1197. }
  1198. },
  1199. wheel: function wheel(e) {
  1200. var _this3 = this;
  1201. if (!this.viewed) {
  1202. return;
  1203. }
  1204. e.preventDefault();
  1205. // Limit wheel speed to prevent zoom too fast
  1206. if (this.wheeling) {
  1207. return;
  1208. }
  1209. this.wheeling = true;
  1210. setTimeout(function () {
  1211. _this3.wheeling = false;
  1212. }, 50);
  1213. var ratio = Number(this.options.zoomRatio) || 0.1;
  1214. var delta = 1;
  1215. if (e.deltaY) {
  1216. delta = e.deltaY > 0 ? 1 : -1;
  1217. } else if (e.wheelDelta) {
  1218. delta = -e.wheelDelta / 120;
  1219. } else if (e.detail) {
  1220. delta = e.detail > 0 ? 1 : -1;
  1221. }
  1222. this.zoom(-delta * ratio, true, e);
  1223. }
  1224. };
  1225. var methods = {
  1226. /** Show the viewer (only available in modal mode)
  1227. * @param {boolean} [immediate=false] - Indicates if show the viewer immediately or not.
  1228. * @returns {Viewer} this
  1229. */
  1230. show: function show() {
  1231. var immediate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1232. var element = this.element,
  1233. options = this.options;
  1234. if (options.inline || this.showing || this.isShown || this.showing) {
  1235. return this;
  1236. }
  1237. if (!this.ready) {
  1238. this.build();
  1239. if (this.ready) {
  1240. this.show(immediate);
  1241. }
  1242. return this;
  1243. }
  1244. if (isFunction(options.show)) {
  1245. addListener(element, EVENT_SHOW, options.show, {
  1246. once: true
  1247. });
  1248. }
  1249. if (dispatchEvent(element, EVENT_SHOW) === false || !this.ready) {
  1250. return this;
  1251. }
  1252. if (this.hiding) {
  1253. this.transitioning.abort();
  1254. }
  1255. this.showing = true;
  1256. this.open();
  1257. var viewer = this.viewer;
  1258. removeClass(viewer, CLASS_HIDE);
  1259. if (options.transition && !immediate) {
  1260. var shown = this.shown.bind(this);
  1261. this.transitioning = {
  1262. abort: function abort() {
  1263. removeListener(viewer, EVENT_TRANSITION_END, shown);
  1264. removeClass(viewer, CLASS_IN);
  1265. }
  1266. };
  1267. addClass(viewer, CLASS_TRANSITION);
  1268. // Force reflow to enable CSS3 transition
  1269. // eslint-disable-next-line
  1270. viewer.offsetWidth;
  1271. addListener(viewer, EVENT_TRANSITION_END, shown, {
  1272. once: true
  1273. });
  1274. addClass(viewer, CLASS_IN);
  1275. } else {
  1276. addClass(viewer, CLASS_IN);
  1277. this.shown();
  1278. }
  1279. return this;
  1280. },
  1281. /**
  1282. * Hide the viewer (only available in modal mode)
  1283. * @param {boolean} [immediate=false] - Indicates if hide the viewer immediately or not.
  1284. * @returns {Viewer} this
  1285. */
  1286. hide: function hide() {
  1287. var immediate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1288. var element = this.element,
  1289. options = this.options;
  1290. if (options.inline || this.hiding || !(this.isShown || this.showing)) {
  1291. return this;
  1292. }
  1293. if (isFunction(options.hide)) {
  1294. addListener(element, EVENT_HIDE, options.hide, {
  1295. once: true
  1296. });
  1297. }
  1298. if (dispatchEvent(element, EVENT_HIDE) === false) {
  1299. return this;
  1300. }
  1301. if (this.showing) {
  1302. this.transitioning.abort();
  1303. }
  1304. this.hiding = true;
  1305. if (this.played) {
  1306. this.stop();
  1307. } else if (this.viewing) {
  1308. this.viewing.abort();
  1309. }
  1310. var viewer = this.viewer;
  1311. if (options.transition && !immediate) {
  1312. var hidden = this.hidden.bind(this);
  1313. var hide = function hide() {
  1314. addListener(viewer, EVENT_TRANSITION_END, hidden, {
  1315. once: true
  1316. });
  1317. removeClass(viewer, CLASS_IN);
  1318. };
  1319. this.transitioning = {
  1320. abort: function abort() {
  1321. if (this.viewed) {
  1322. removeListener(this.image, EVENT_TRANSITION_END, hide);
  1323. } else {
  1324. removeListener(viewer, EVENT_TRANSITION_END, hidden);
  1325. }
  1326. }
  1327. };
  1328. if (this.viewed) {
  1329. addListener(this.image, EVENT_TRANSITION_END, hide, {
  1330. once: true
  1331. });
  1332. this.zoomTo(0, false, false, true);
  1333. } else {
  1334. hide();
  1335. }
  1336. } else {
  1337. removeClass(viewer, CLASS_IN);
  1338. this.hidden();
  1339. }
  1340. return this;
  1341. },
  1342. /**
  1343. * View one of the images with image's index
  1344. * @param {number} index - The index of the image to view.
  1345. * @returns {Viewer} this
  1346. */
  1347. view: function view() {
  1348. var _this = this;
  1349. var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  1350. index = Number(index) || 0;
  1351. if (!this.isShown) {
  1352. this.index = index;
  1353. return this.show();
  1354. }
  1355. if (this.hiding || this.played || index < 0 || index >= this.length || this.viewed && index === this.index) {
  1356. return this;
  1357. }
  1358. if (this.viewing) {
  1359. this.viewing.abort();
  1360. }
  1361. var element = this.element,
  1362. options = this.options,
  1363. title = this.title,
  1364. canvas = this.canvas;
  1365. var item = this.items[index];
  1366. var img = item.querySelector('img');
  1367. var url = getData(img, 'originalUrl');
  1368. var alt = img.getAttribute('alt');
  1369. var image = document.createElement('img');
  1370. image.src = url;
  1371. image.alt = alt;
  1372. if (isFunction(options.view)) {
  1373. addListener(element, EVENT_VIEW, options.view, {
  1374. once: true
  1375. });
  1376. }
  1377. if (dispatchEvent(element, EVENT_VIEW, {
  1378. originalImage: this.images[index],
  1379. index: index,
  1380. image: image
  1381. }) === false || !this.isShown || this.hiding || this.played) {
  1382. return this;
  1383. }
  1384. this.image = image;
  1385. removeClass(this.items[this.index], CLASS_ACTIVE);
  1386. addClass(item, CLASS_ACTIVE);
  1387. this.viewed = false;
  1388. this.index = index;
  1389. this.imageData = {};
  1390. addClass(image, CLASS_INVISIBLE);
  1391. if (options.loading) {
  1392. addClass(canvas, CLASS_LOADING);
  1393. }
  1394. canvas.innerHTML = '';
  1395. canvas.appendChild(image);
  1396. // Center current item
  1397. this.renderList();
  1398. // Clear title
  1399. title.innerHTML = '';
  1400. // Generate title after viewed
  1401. var onViewed = function onViewed() {
  1402. var imageData = _this.imageData;
  1403. title.textContent = alt + ' (' + imageData.naturalWidth + ' \xD7 ' + imageData.naturalHeight + ')';
  1404. };
  1405. var onLoad = void 0;
  1406. addListener(element, EVENT_VIEWED, onViewed, {
  1407. once: true
  1408. });
  1409. this.viewing = {
  1410. abort: function abort() {
  1411. removeListener(element, EVENT_VIEWED, onViewed);
  1412. if (image.complete) {
  1413. if (this.imageRendering) {
  1414. this.imageRendering.abort();
  1415. } else if (this.imageInitializing) {
  1416. this.imageInitializing.abort();
  1417. }
  1418. } else {
  1419. removeListener(image, EVENT_LOAD, onLoad);
  1420. if (this.timeout) {
  1421. clearTimeout(this.timeout);
  1422. }
  1423. }
  1424. }
  1425. };
  1426. if (image.complete) {
  1427. this.load();
  1428. } else {
  1429. addListener(image, EVENT_LOAD, onLoad = this.load.bind(this), {
  1430. once: true
  1431. });
  1432. if (this.timeout) {
  1433. clearTimeout(this.timeout);
  1434. }
  1435. // Make the image visible if it fails to load within 1s
  1436. this.timeout = setTimeout(function () {
  1437. removeClass(image, CLASS_INVISIBLE);
  1438. _this.timeout = false;
  1439. }, 1000);
  1440. }
  1441. return this;
  1442. },
  1443. /**
  1444. * View the previous image
  1445. * @param {boolean} [loop=false] - Indicate if view the last one
  1446. * when it is the first one at present.
  1447. * @returns {Viewer} this
  1448. */
  1449. prev: function prev() {
  1450. var loop = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1451. var index = this.index - 1;
  1452. if (index < 0) {
  1453. index = loop ? this.length - 1 : 0;
  1454. }
  1455. this.view(index);
  1456. return this;
  1457. },
  1458. /**
  1459. * View the next image
  1460. * @param {boolean} [loop=false] - Indicate if view the first one
  1461. * when it is the last one at present.
  1462. * @returns {Viewer} this
  1463. */
  1464. next: function next() {
  1465. var loop = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1466. var maxIndex = this.length - 1;
  1467. var index = this.index + 1;
  1468. if (index > maxIndex) {
  1469. index = loop ? 0 : maxIndex;
  1470. }
  1471. this.view(index);
  1472. return this;
  1473. },
  1474. /**
  1475. * Move the image with relative offsets.
  1476. * @param {number} offsetX - The relative offset distance on the x-axis.
  1477. * @param {number} offsetY - The relative offset distance on the y-axis.
  1478. * @returns {Viewer} this
  1479. */
  1480. move: function move(offsetX, offsetY) {
  1481. var imageData = this.imageData;
  1482. this.moveTo(isUndefined(offsetX) ? offsetX : imageData.left + Number(offsetX), isUndefined(offsetY) ? offsetY : imageData.top + Number(offsetY));
  1483. return this;
  1484. },
  1485. /**
  1486. * Move the image to an absolute point.
  1487. * @param {number} x - The x-axis coordinate.
  1488. * @param {number} [y=x] - The y-axis coordinate.
  1489. * @returns {Viewer} this
  1490. */
  1491. moveTo: function moveTo(x) {
  1492. var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : x;
  1493. var imageData = this.imageData;
  1494. x = Number(x);
  1495. y = Number(y);
  1496. if (this.viewed && !this.played && this.options.movable) {
  1497. var changed = false;
  1498. if (isNumber(x)) {
  1499. imageData.left = x;
  1500. changed = true;
  1501. }
  1502. if (isNumber(y)) {
  1503. imageData.top = y;
  1504. changed = true;
  1505. }
  1506. if (changed) {
  1507. this.renderImage();
  1508. }
  1509. }
  1510. return this;
  1511. },
  1512. /**
  1513. * Zoom the image with a relative ratio.
  1514. * @param {number} ratio - The target ratio.
  1515. * @param {boolean} [hasTooltip=false] - Indicates if it has a tooltip or not.
  1516. * @param {Event} [_originalEvent=null] - The original event if any.
  1517. * @returns {Viewer} this
  1518. */
  1519. zoom: function zoom(ratio) {
  1520. var hasTooltip = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  1521. var _originalEvent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  1522. var imageData = this.imageData;
  1523. ratio = Number(ratio);
  1524. if (ratio < 0) {
  1525. ratio = 1 / (1 - ratio);
  1526. } else {
  1527. ratio = 1 + ratio;
  1528. }
  1529. this.zoomTo(imageData.width * ratio / imageData.naturalWidth, hasTooltip, _originalEvent);
  1530. return this;
  1531. },
  1532. /**
  1533. * Zoom the image to an absolute ratio.
  1534. * @param {number} ratio - The target ratio.
  1535. * @param {boolean} [hasTooltip=false] - Indicates if it has a tooltip or not.
  1536. * @param {Event} [_originalEvent=null] - The original event if any.
  1537. * @param {Event} [_zoomable=false] - Indicates if the current zoom is available or not.
  1538. * @returns {Viewer} this
  1539. */
  1540. zoomTo: function zoomTo(ratio) {
  1541. var hasTooltip = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  1542. var _originalEvent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  1543. var _zoomable = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  1544. var options = this.options,
  1545. pointers = this.pointers,
  1546. imageData = this.imageData;
  1547. ratio = Math.max(0, ratio);
  1548. if (isNumber(ratio) && this.viewed && !this.played && (_zoomable || options.zoomable)) {
  1549. if (!_zoomable) {
  1550. var minZoomRatio = Math.max(0.01, options.minZoomRatio);
  1551. var maxZoomRatio = Math.min(100, options.maxZoomRatio);
  1552. ratio = Math.min(Math.max(ratio, minZoomRatio), maxZoomRatio);
  1553. }
  1554. if (_originalEvent && ratio > 0.95 && ratio < 1.05) {
  1555. ratio = 1;
  1556. }
  1557. var newWidth = imageData.naturalWidth * ratio;
  1558. var newHeight = imageData.naturalHeight * ratio;
  1559. if (_originalEvent) {
  1560. var offset = getOffset(this.viewer);
  1561. var center = pointers && Object.keys(pointers).length ? getPointersCenter(pointers) : {
  1562. pageX: _originalEvent.pageX,
  1563. pageY: _originalEvent.pageY
  1564. };
  1565. // Zoom from the triggering point of the event
  1566. imageData.left -= (newWidth - imageData.width) * ((center.pageX - offset.left - imageData.left) / imageData.width);
  1567. imageData.top -= (newHeight - imageData.height) * ((center.pageY - offset.top - imageData.top) / imageData.height);
  1568. } else {
  1569. // Zoom from the center of the image
  1570. imageData.left -= (newWidth - imageData.width) / 2;
  1571. imageData.top -= (newHeight - imageData.height) / 2;
  1572. }
  1573. imageData.width = newWidth;
  1574. imageData.height = newHeight;
  1575. imageData.ratio = ratio;
  1576. this.renderImage();
  1577. if (hasTooltip) {
  1578. this.tooltip();
  1579. }
  1580. }
  1581. return this;
  1582. },
  1583. /**
  1584. * Rotate the image with a relative degree.
  1585. * @param {number} degree - The rotate degree.
  1586. * @returns {Viewer} this
  1587. */
  1588. rotate: function rotate(degree) {
  1589. this.rotateTo((this.imageData.rotate || 0) + Number(degree));
  1590. return this;
  1591. },
  1592. /**
  1593. * Rotate the image to an absolute degree.
  1594. * @param {number} degree - The rotate degree.
  1595. * @returns {Viewer} this
  1596. */
  1597. rotateTo: function rotateTo(degree) {
  1598. var imageData = this.imageData;
  1599. degree = Number(degree);
  1600. if (isNumber(degree) && this.viewed && !this.played && this.options.rotatable) {
  1601. imageData.rotate = degree;
  1602. this.renderImage();
  1603. }
  1604. return this;
  1605. },
  1606. /**
  1607. * Scale the image on the x-axis.
  1608. * @param {number} scaleX - The scale ratio on the x-axis.
  1609. * @returns {Viewer} this
  1610. */
  1611. scaleX: function scaleX(_scaleX) {
  1612. this.scale(_scaleX, this.imageData.scaleY);
  1613. return this;
  1614. },
  1615. /**
  1616. * Scale the image on the y-axis.
  1617. * @param {number} scaleY - The scale ratio on the y-axis.
  1618. * @returns {Viewer} this
  1619. */
  1620. scaleY: function scaleY(_scaleY) {
  1621. this.scale(this.imageData.scaleX, _scaleY);
  1622. return this;
  1623. },
  1624. /**
  1625. * Scale the image.
  1626. * @param {number} scaleX - The scale ratio on the x-axis.
  1627. * @param {number} [scaleY=scaleX] - The scale ratio on the y-axis.
  1628. * @returns {Viewer} this
  1629. */
  1630. scale: function scale(scaleX) {
  1631. var scaleY = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : scaleX;
  1632. var imageData = this.imageData;
  1633. scaleX = Number(scaleX);
  1634. scaleY = Number(scaleY);
  1635. if (this.viewed && !this.played && this.options.scalable) {
  1636. var changed = false;
  1637. if (isNumber(scaleX)) {
  1638. imageData.scaleX = scaleX;
  1639. changed = true;
  1640. }
  1641. if (isNumber(scaleY)) {
  1642. imageData.scaleY = scaleY;
  1643. changed = true;
  1644. }
  1645. if (changed) {
  1646. this.renderImage();
  1647. }
  1648. }
  1649. return this;
  1650. },
  1651. /**
  1652. * Play the images
  1653. * @param {boolean} [fullscreen=false] - Indicate if request fullscreen or not.
  1654. * @returns {Viewer} this
  1655. */
  1656. play: function play() {
  1657. var _this2 = this;
  1658. var fullscreen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1659. if (!this.isShown || this.played) {
  1660. return this;
  1661. }
  1662. var options = this.options,
  1663. player = this.player;
  1664. var onLoad = this.loadImage.bind(this);
  1665. var list = [];
  1666. var total = 0;
  1667. var index = 0;
  1668. this.played = true;
  1669. this.onLoadWhenPlay = onLoad;
  1670. if (fullscreen) {
  1671. this.requestFullscreen();
  1672. }
  1673. addClass(player, CLASS_SHOW);
  1674. forEach(this.items, function (item, i) {
  1675. var img = item.querySelector('img');
  1676. var image = document.createElement('img');
  1677. image.src = getData(img, 'originalUrl');
  1678. image.alt = img.getAttribute('alt');
  1679. total += 1;
  1680. addClass(image, CLASS_FADE);
  1681. toggleClass(image, CLASS_TRANSITION, options.transition);
  1682. if (hasClass(item, CLASS_ACTIVE)) {
  1683. addClass(image, CLASS_IN);
  1684. index = i;
  1685. }
  1686. list.push(image);
  1687. addListener(image, EVENT_LOAD, onLoad, {
  1688. once: true
  1689. });
  1690. player.appendChild(image);
  1691. });
  1692. if (isNumber(options.interval) && options.interval > 0) {
  1693. var play = function play() {
  1694. _this2.playing = setTimeout(function () {
  1695. removeClass(list[index], CLASS_IN);
  1696. index += 1;
  1697. index = index < total ? index : 0;
  1698. addClass(list[index], CLASS_IN);
  1699. play();
  1700. }, options.interval);
  1701. };
  1702. if (total > 1) {
  1703. play();
  1704. }
  1705. }
  1706. return this;
  1707. },
  1708. // Stop play
  1709. stop: function stop() {
  1710. var _this3 = this;
  1711. if (!this.played) {
  1712. return this;
  1713. }
  1714. var player = this.player;
  1715. this.played = false;
  1716. clearTimeout(this.playing);
  1717. forEach(player.getElementsByTagName('img'), function (image) {
  1718. removeListener(image, EVENT_LOAD, _this3.onLoadWhenPlay);
  1719. });
  1720. removeClass(player, CLASS_SHOW);
  1721. player.innerHTML = '';
  1722. this.exitFullscreen();
  1723. return this;
  1724. },
  1725. // Enter modal mode (only available in inline mode)
  1726. full: function full() {
  1727. var _this4 = this;
  1728. var options = this.options,
  1729. viewer = this.viewer,
  1730. image = this.image,
  1731. list = this.list;
  1732. if (!this.isShown || this.played || this.fulled || !options.inline) {
  1733. return this;
  1734. }
  1735. this.fulled = true;
  1736. this.open();
  1737. addClass(this.button, CLASS_FULLSCREEN_EXIT);
  1738. if (options.transition) {
  1739. removeClass(list, CLASS_TRANSITION);
  1740. if (this.viewed) {
  1741. removeClass(image, CLASS_TRANSITION);
  1742. }
  1743. }
  1744. addClass(viewer, CLASS_FIXED);
  1745. viewer.setAttribute('style', '');
  1746. setStyle(viewer, {
  1747. zIndex: options.zIndex
  1748. });
  1749. this.initContainer();
  1750. this.viewerData = assign({}, this.containerData);
  1751. this.renderList();
  1752. if (this.viewed) {
  1753. this.initImage(function () {
  1754. _this4.renderImage(function () {
  1755. if (options.transition) {
  1756. setTimeout(function () {
  1757. addClass(image, CLASS_TRANSITION);
  1758. addClass(list, CLASS_TRANSITION);
  1759. }, 0);
  1760. }
  1761. });
  1762. });
  1763. }
  1764. return this;
  1765. },
  1766. // Exit modal mode (only available in inline mode)
  1767. exit: function exit() {
  1768. var _this5 = this;
  1769. var options = this.options,
  1770. viewer = this.viewer,
  1771. image = this.image,
  1772. list = this.list;
  1773. if (!this.isShown || this.played || !this.fulled || !options.inline) {
  1774. return this;
  1775. }
  1776. this.fulled = false;
  1777. this.close();
  1778. removeClass(this.button, CLASS_FULLSCREEN_EXIT);
  1779. if (options.transition) {
  1780. removeClass(list, CLASS_TRANSITION);
  1781. if (this.viewed) {
  1782. removeClass(image, CLASS_TRANSITION);
  1783. }
  1784. }
  1785. removeClass(viewer, CLASS_FIXED);
  1786. setStyle(viewer, {
  1787. zIndex: options.zIndexInline
  1788. });
  1789. this.viewerData = assign({}, this.parentData);
  1790. this.renderViewer();
  1791. this.renderList();
  1792. if (this.viewed) {
  1793. this.initImage(function () {
  1794. _this5.renderImage(function () {
  1795. if (options.transition) {
  1796. setTimeout(function () {
  1797. addClass(image, CLASS_TRANSITION);
  1798. addClass(list, CLASS_TRANSITION);
  1799. }, 0);
  1800. }
  1801. });
  1802. });
  1803. }
  1804. return this;
  1805. },
  1806. // Show the current ratio of the image with percentage
  1807. tooltip: function tooltip() {
  1808. var _this6 = this;
  1809. var options = this.options,
  1810. tooltipBox = this.tooltipBox,
  1811. imageData = this.imageData;
  1812. if (!this.viewed || this.played || !options.tooltip) {
  1813. return this;
  1814. }
  1815. tooltipBox.textContent = Math.round(imageData.ratio * 100) + '%';
  1816. if (!this.tooltipping) {
  1817. if (options.transition) {
  1818. if (this.fading) {
  1819. dispatchEvent(tooltipBox, EVENT_TRANSITION_END);
  1820. }
  1821. addClass(tooltipBox, CLASS_SHOW);
  1822. addClass(tooltipBox, CLASS_FADE);
  1823. addClass(tooltipBox, CLASS_TRANSITION);
  1824. // Force reflow to enable CSS3 transition
  1825. // eslint-disable-next-line
  1826. tooltipBox.offsetWidth;
  1827. addClass(tooltipBox, CLASS_IN);
  1828. } else {
  1829. addClass(tooltipBox, CLASS_SHOW);
  1830. }
  1831. } else {
  1832. clearTimeout(this.tooltipping);
  1833. }
  1834. this.tooltipping = setTimeout(function () {
  1835. if (options.transition) {
  1836. addListener(tooltipBox, EVENT_TRANSITION_END, function () {
  1837. removeClass(tooltipBox, CLASS_SHOW);
  1838. removeClass(tooltipBox, CLASS_FADE);
  1839. removeClass(tooltipBox, CLASS_TRANSITION);
  1840. _this6.fading = false;
  1841. }, {
  1842. once: true
  1843. });
  1844. removeClass(tooltipBox, CLASS_IN);
  1845. _this6.fading = true;
  1846. } else {
  1847. removeClass(tooltipBox, CLASS_SHOW);
  1848. }
  1849. _this6.tooltipping = false;
  1850. }, 1000);
  1851. return this;
  1852. },
  1853. // Toggle the image size between its natural size and initial size
  1854. toggle: function toggle() {
  1855. if (this.imageData.ratio === 1) {
  1856. this.zoomTo(this.initialImageData.ratio, true);
  1857. } else {
  1858. this.zoomTo(1, true);
  1859. }
  1860. return this;
  1861. },
  1862. // Reset the image to its initial state
  1863. reset: function reset() {
  1864. if (this.viewed && !this.played) {
  1865. this.imageData = assign({}, this.initialImageData);
  1866. this.renderImage();
  1867. }
  1868. return this;
  1869. },
  1870. // Update viewer when images changed
  1871. update: function update() {
  1872. var element = this.element,
  1873. options = this.options,
  1874. isImg = this.isImg;
  1875. // Destroy viewer if the target image was deleted
  1876. if (isImg && !element.parentNode) {
  1877. return this.destroy();
  1878. }
  1879. var images = [];
  1880. forEach(isImg ? [element] : element.querySelectorAll('img'), function (image) {
  1881. if (options.filter) {
  1882. if (options.filter(image)) {
  1883. images.push(image);
  1884. }
  1885. } else {
  1886. images.push(image);
  1887. }
  1888. });
  1889. if (!images.length) {
  1890. return this;
  1891. }
  1892. this.images = images;
  1893. this.length = images.length;
  1894. if (this.ready) {
  1895. var indexes = [];
  1896. forEach(this.items, function (item, i) {
  1897. var img = item.querySelector('img');
  1898. var image = images[i];
  1899. if (image) {
  1900. if (image.src !== img.src) {
  1901. indexes.push(i);
  1902. }
  1903. } else {
  1904. indexes.push(i);
  1905. }
  1906. });
  1907. setStyle(this.list, {
  1908. width: 'auto'
  1909. });
  1910. this.initList();
  1911. if (this.isShown) {
  1912. if (this.length) {
  1913. if (this.viewed) {
  1914. var index = indexes.indexOf(this.index);
  1915. if (index >= 0) {
  1916. this.viewed = false;
  1917. this.view(Math.max(this.index - (index + 1), 0));
  1918. } else {
  1919. addClass(this.items[this.index], CLASS_ACTIVE);
  1920. }
  1921. }
  1922. } else {
  1923. this.image = null;
  1924. this.viewed = false;
  1925. this.index = 0;
  1926. this.imageData = null;
  1927. this.canvas.innerHTML = '';
  1928. this.title.innerHTML = '';
  1929. }
  1930. }
  1931. } else {
  1932. this.build();
  1933. }
  1934. return this;
  1935. },
  1936. // Destroy the viewer
  1937. destroy: function destroy() {
  1938. var element = this.element,
  1939. options = this.options;
  1940. if (!getData(element, NAMESPACE)) {
  1941. return this;
  1942. }
  1943. this.destroyed = true;
  1944. if (this.ready) {
  1945. if (this.played) {
  1946. this.stop();
  1947. }
  1948. if (options.inline) {
  1949. if (this.fulled) {
  1950. this.exit();
  1951. }
  1952. this.unbind();
  1953. } else if (this.isShown) {
  1954. if (this.viewing) {
  1955. if (this.imageRendering) {
  1956. this.imageRendering.abort();
  1957. } else if (this.imageInitializing) {
  1958. this.imageInitializing.abort();
  1959. }
  1960. }
  1961. if (this.hiding) {
  1962. this.transitioning.abort();
  1963. }
  1964. this.hidden();
  1965. } else if (this.showing) {
  1966. this.transitioning.abort();
  1967. this.hidden();
  1968. }
  1969. this.ready = false;
  1970. this.viewer.parentNode.removeChild(this.viewer);
  1971. } else if (options.inline) {
  1972. if (this.delaying) {
  1973. this.delaying.abort();
  1974. } else if (this.initializing) {
  1975. this.initializing.abort();
  1976. }
  1977. }
  1978. if (!options.inline) {
  1979. removeListener(element, EVENT_CLICK, this.onStart);
  1980. }
  1981. removeData(element, NAMESPACE);
  1982. return this;
  1983. }
  1984. };
  1985. var others = {
  1986. open: function open() {
  1987. var body = this.body;
  1988. addClass(body, CLASS_OPEN);
  1989. body.style.paddingRight = this.scrollbarWidth + (parseFloat(this.initialBodyPaddingRight) || 0) + 'px';
  1990. },
  1991. close: function close() {
  1992. var body = this.body;
  1993. removeClass(body, CLASS_OPEN);
  1994. body.style.paddingRight = this.initialBodyPaddingRight;
  1995. },
  1996. shown: function shown() {
  1997. var element = this.element,
  1998. options = this.options;
  1999. this.fulled = true;
  2000. this.isShown = true;
  2001. this.render();
  2002. this.bind();
  2003. this.showing = false;
  2004. if (isFunction(options.shown)) {
  2005. addListener(element, EVENT_SHOWN, options.shown, {
  2006. once: true
  2007. });
  2008. }
  2009. if (dispatchEvent(element, EVENT_SHOWN) === false) {
  2010. return;
  2011. }
  2012. if (this.ready && this.isShown && !this.hiding) {
  2013. this.view(this.index);
  2014. }
  2015. },
  2016. hidden: function hidden() {
  2017. var element = this.element,
  2018. options = this.options;
  2019. this.fulled = false;
  2020. this.viewed = false;
  2021. this.isShown = false;
  2022. this.close();
  2023. this.unbind();
  2024. addClass(this.viewer, CLASS_HIDE);
  2025. this.resetList();
  2026. this.resetImage();
  2027. this.hiding = false;
  2028. if (!this.destroyed) {
  2029. if (isFunction(options.hidden)) {
  2030. addListener(element, EVENT_HIDDEN, options.hidden, {
  2031. once: true
  2032. });
  2033. }
  2034. dispatchEvent(element, EVENT_HIDDEN);
  2035. }
  2036. },
  2037. requestFullscreen: function requestFullscreen() {
  2038. var document = this.element.ownerDocument;
  2039. if (this.fulled && !document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) {
  2040. var documentElement = document.documentElement;
  2041. if (documentElement.requestFullscreen) {
  2042. documentElement.requestFullscreen();
  2043. } else if (documentElement.msRequestFullscreen) {
  2044. documentElement.msRequestFullscreen();
  2045. } else if (documentElement.mozRequestFullScreen) {
  2046. documentElement.mozRequestFullScreen();
  2047. } else if (documentElement.webkitRequestFullscreen) {
  2048. documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  2049. }
  2050. }
  2051. },
  2052. exitFullscreen: function exitFullscreen() {
  2053. if (this.fulled) {
  2054. var document = this.element.ownerDocument;
  2055. if (document.exitFullscreen) {
  2056. document.exitFullscreen();
  2057. } else if (document.msExitFullscreen) {
  2058. document.msExitFullscreen();
  2059. } else if (document.mozCancelFullScreen) {
  2060. document.mozCancelFullScreen();
  2061. } else if (document.webkitExitFullscreen) {
  2062. document.webkitExitFullscreen();
  2063. }
  2064. }
  2065. },
  2066. change: function change(e) {
  2067. var options = this.options,
  2068. pointers = this.pointers;
  2069. var pointer = pointers[Object.keys(pointers)[0]];
  2070. var offsetX = pointer.endX - pointer.startX;
  2071. var offsetY = pointer.endY - pointer.startY;
  2072. switch (this.action) {
  2073. // Move the current image
  2074. case ACTION_MOVE:
  2075. this.move(offsetX, offsetY);
  2076. break;
  2077. // Zoom the current image
  2078. case ACTION_ZOOM:
  2079. this.zoom(getMaxZoomRatio(pointers), false, e);
  2080. break;
  2081. case ACTION_SWITCH:
  2082. this.action = 'switched';
  2083. // Empty `pointers` as `touchend` event will not be fired after swiped in iOS browsers.
  2084. this.pointers = {};
  2085. if (Math.abs(offsetX) > Math.abs(offsetY)) {
  2086. if (offsetX > 1) {
  2087. this.prev(options.loop);
  2088. } else if (offsetX < -1) {
  2089. this.next(options.loop);
  2090. }
  2091. }
  2092. break;
  2093. default:
  2094. }
  2095. // Override
  2096. forEach(pointers, function (p) {
  2097. p.startX = p.endX;
  2098. p.startY = p.endY;
  2099. });
  2100. },
  2101. isSwitchable: function isSwitchable() {
  2102. var imageData = this.imageData,
  2103. viewerData = this.viewerData;
  2104. return this.length > 1 && imageData.left >= 0 && imageData.top >= 0 && imageData.width <= viewerData.width && imageData.height <= viewerData.height;
  2105. }
  2106. };
  2107. var AnotherViewer = WINDOW.Viewer;
  2108. var Viewer = function () {
  2109. /**
  2110. * Create a new Viewer.
  2111. * @param {Element} element - The target element for viewing.
  2112. * @param {Object} [options={}] - The configuration options.
  2113. */
  2114. function Viewer(element) {
  2115. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2116. classCallCheck(this, Viewer);
  2117. if (!element || element.nodeType !== 1) {
  2118. throw new Error('The first argument is required and must be an element.');
  2119. }
  2120. this.element = element;
  2121. this.options = assign({}, DEFAULTS, isPlainObject(options) && options);
  2122. this.action = false;
  2123. this.fading = false;
  2124. this.fulled = false;
  2125. this.hiding = false;
  2126. this.index = 0;
  2127. this.isImg = false;
  2128. this.length = 0;
  2129. this.played = false;
  2130. this.playing = false;
  2131. this.pointers = {};
  2132. this.ready = false;
  2133. this.showing = false;
  2134. this.timeout = false;
  2135. this.tooltipping = false;
  2136. this.viewed = false;
  2137. this.viewing = false;
  2138. this.isShown = false;
  2139. this.wheeling = false;
  2140. this.init();
  2141. }
  2142. createClass(Viewer, [{
  2143. key: 'init',
  2144. value: function init() {
  2145. var _this = this;
  2146. var element = this.element,
  2147. options = this.options;
  2148. if (getData(element, NAMESPACE)) {
  2149. return;
  2150. }
  2151. setData(element, NAMESPACE, this);
  2152. var isImg = element.tagName.toLowerCase() === 'img';
  2153. var images = [];
  2154. forEach(isImg ? [element] : element.querySelectorAll('img'), function (image) {
  2155. if (isFunction(options.filter)) {
  2156. if (options.filter.call(_this, image)) {
  2157. images.push(image);
  2158. }
  2159. } else {
  2160. images.push(image);
  2161. }
  2162. });
  2163. if (!images.length) {
  2164. return;
  2165. }
  2166. this.isImg = isImg;
  2167. this.length = images.length;
  2168. this.images = images;
  2169. var ownerDocument = element.ownerDocument;
  2170. var body = ownerDocument.body || ownerDocument.documentElement;
  2171. this.body = body;
  2172. this.scrollbarWidth = window.innerWidth - ownerDocument.documentElement.clientWidth;
  2173. this.initialBodyPaddingRight = window.getComputedStyle(body).paddingRight;
  2174. // Override `transition` option if it is not supported
  2175. if (isUndefined(document.createElement(NAMESPACE).style.transition)) {
  2176. options.transition = false;
  2177. }
  2178. if (options.inline) {
  2179. var count = 0;
  2180. var progress = function progress() {
  2181. count += 1;
  2182. if (count === _this.length) {
  2183. var timeout = void 0;
  2184. _this.initializing = false;
  2185. _this.delaying = {
  2186. abort: function abort() {
  2187. clearTimeout(timeout);
  2188. }
  2189. };
  2190. // build asynchronously to keep `this.viewer` is accessible in `ready` event handler.
  2191. timeout = setTimeout(function () {
  2192. _this.delaying = false;
  2193. _this.build();
  2194. }, 0);
  2195. }
  2196. };
  2197. this.initializing = {
  2198. abort: function abort() {
  2199. forEach(images, function (image) {
  2200. if (!image.complete) {
  2201. removeListener(image, EVENT_LOAD, progress);
  2202. }
  2203. });
  2204. }
  2205. };
  2206. forEach(images, function (image) {
  2207. if (image.complete) {
  2208. progress();
  2209. } else {
  2210. addListener(image, EVENT_LOAD, progress, {
  2211. once: true
  2212. });
  2213. }
  2214. });
  2215. } else {
  2216. addListener(element, EVENT_CLICK, this.onStart = function (_ref) {
  2217. var target = _ref.target;
  2218. if (target.tagName.toLowerCase() === 'img') {
  2219. _this.view(_this.images.indexOf(target));
  2220. }
  2221. });
  2222. }
  2223. }
  2224. }, {
  2225. key: 'build',
  2226. value: function build() {
  2227. if (this.ready) {
  2228. return;
  2229. }
  2230. var element = this.element,
  2231. options = this.options;
  2232. var parent = element.parentNode;
  2233. var template = document.createElement('div');
  2234. template.innerHTML = TEMPLATE;
  2235. var viewer = template.querySelector('.' + NAMESPACE + '-container');
  2236. var title = viewer.querySelector('.' + NAMESPACE + '-title');
  2237. var toolbar = viewer.querySelector('.' + NAMESPACE + '-toolbar');
  2238. var navbar = viewer.querySelector('.' + NAMESPACE + '-navbar');
  2239. var button = viewer.querySelector('.' + NAMESPACE + '-button');
  2240. var canvas = viewer.querySelector('.' + NAMESPACE + '-canvas');
  2241. this.parent = parent;
  2242. this.viewer = viewer;
  2243. this.title = title;
  2244. this.toolbar = toolbar;
  2245. this.navbar = navbar;
  2246. this.button = button;
  2247. this.canvas = canvas;
  2248. this.footer = viewer.querySelector('.' + NAMESPACE + '-footer');
  2249. this.tooltipBox = viewer.querySelector('.' + NAMESPACE + '-tooltip');
  2250. this.player = viewer.querySelector('.' + NAMESPACE + '-player');
  2251. this.list = viewer.querySelector('.' + NAMESPACE + '-list');
  2252. addClass(title, !options.title ? CLASS_HIDE : getResponsiveClass(options.title));
  2253. addClass(navbar, !options.navbar ? CLASS_HIDE : getResponsiveClass(options.navbar));
  2254. toggleClass(button, CLASS_HIDE, !options.button);
  2255. if (options.backdrop) {
  2256. addClass(viewer, NAMESPACE + '-backdrop');
  2257. if (!options.inline && options.backdrop === true) {
  2258. setData(canvas, 'action', 'hide');
  2259. }
  2260. }
  2261. if (options.toolbar) {
  2262. var list = document.createElement('ul');
  2263. var custom = isPlainObject(options.toolbar);
  2264. var zoomButtons = BUTTONS.slice(0, 3);
  2265. var rotateButtons = BUTTONS.slice(7, 9);
  2266. var scaleButtons = BUTTONS.slice(9);
  2267. if (!custom) {
  2268. addClass(toolbar, getResponsiveClass(options.toolbar));
  2269. }
  2270. forEach(custom ? options.toolbar : BUTTONS, function (value, index) {
  2271. var deep = custom && isPlainObject(value);
  2272. var name = custom ? hyphenate(index) : value;
  2273. var show = deep && !isUndefined(value.show) ? value.show : value;
  2274. if (!show || !options.zoomable && zoomButtons.indexOf(name) !== -1 || !options.rotatable && rotateButtons.indexOf(name) !== -1 || !options.scalable && scaleButtons.indexOf(name) !== -1) {
  2275. return;
  2276. }
  2277. var size = deep && !isUndefined(value.size) ? value.size : value;
  2278. var click = deep && !isUndefined(value.click) ? value.click : value;
  2279. var item = document.createElement('li');
  2280. item.setAttribute('role', 'button');
  2281. addClass(item, NAMESPACE + '-' + name);
  2282. if (!isFunction(click)) {
  2283. setData(item, 'action', name);
  2284. }
  2285. if (isNumber(show)) {
  2286. addClass(item, getResponsiveClass(show));
  2287. }
  2288. if (['small', 'large'].indexOf(size) !== -1) {
  2289. addClass(item, NAMESPACE + '-' + size);
  2290. } else if (name === 'play') {
  2291. addClass(item, NAMESPACE + '-large');
  2292. }
  2293. if (isFunction(click)) {
  2294. addListener(item, EVENT_CLICK, click);
  2295. }
  2296. list.appendChild(item);
  2297. });
  2298. toolbar.appendChild(list);
  2299. } else {
  2300. addClass(toolbar, CLASS_HIDE);
  2301. }
  2302. if (!options.rotatable) {
  2303. var rotates = toolbar.querySelectorAll('li[class*="rotate"]');
  2304. addClass(rotates, CLASS_INVISIBLE);
  2305. forEach(rotates, function (rotate) {
  2306. toolbar.appendChild(rotate);
  2307. });
  2308. }
  2309. if (options.inline) {
  2310. addClass(button, CLASS_FULLSCREEN);
  2311. setStyle(viewer, {
  2312. zIndex: options.zIndexInline
  2313. });
  2314. if (window.getComputedStyle(parent).position === 'static') {
  2315. setStyle(parent, {
  2316. position: 'relative'
  2317. });
  2318. }
  2319. parent.insertBefore(viewer, element.nextSibling);
  2320. } else {
  2321. addClass(button, CLASS_CLOSE);
  2322. addClass(viewer, CLASS_FIXED);
  2323. addClass(viewer, CLASS_FADE);
  2324. addClass(viewer, CLASS_HIDE);
  2325. setStyle(viewer, {
  2326. zIndex: options.zIndex
  2327. });
  2328. var container = options.container;
  2329. if (isString(container)) {
  2330. container = element.ownerDocument.querySelector(container);
  2331. }
  2332. if (!container) {
  2333. container = this.body;
  2334. }
  2335. container.appendChild(viewer);
  2336. }
  2337. if (options.inline) {
  2338. this.render();
  2339. this.bind();
  2340. this.isShown = true;
  2341. }
  2342. this.ready = true;
  2343. if (isFunction(options.ready)) {
  2344. addListener(element, EVENT_READY, options.ready, {
  2345. once: true
  2346. });
  2347. }
  2348. if (dispatchEvent(element, EVENT_READY) === false) {
  2349. this.ready = false;
  2350. return;
  2351. }
  2352. if (this.ready && options.inline) {
  2353. this.view();
  2354. }
  2355. }
  2356. /**
  2357. * Get the no conflict viewer class.
  2358. * @returns {Viewer} The viewer class.
  2359. */
  2360. }], [{
  2361. key: 'noConflict',
  2362. value: function noConflict() {
  2363. window.Viewer = AnotherViewer;
  2364. return Viewer;
  2365. }
  2366. /**
  2367. * Change the default options.
  2368. * @param {Object} options - The new default options.
  2369. */
  2370. }, {
  2371. key: 'setDefaults',
  2372. value: function setDefaults(options) {
  2373. assign(DEFAULTS, isPlainObject(options) && options);
  2374. }
  2375. }]);
  2376. return Viewer;
  2377. }();
  2378. assign(Viewer.prototype, render, events, handlers, methods, others);
  2379. if ($.fn) {
  2380. var AnotherViewer$1 = $.fn.viewer;
  2381. var NAMESPACE$1 = 'viewer';
  2382. $.fn.viewer = function jQueryViewer(option) {
  2383. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  2384. args[_key - 1] = arguments[_key];
  2385. }
  2386. var result = void 0;
  2387. this.each(function (i, element) {
  2388. var $element = $(element);
  2389. var isDestroy = option === 'destroy';
  2390. var viewer = $element.data(NAMESPACE$1);
  2391. if (!viewer) {
  2392. if (isDestroy) {
  2393. return;
  2394. }
  2395. var options = $.extend({}, $element.data(), $.isPlainObject(option) && option);
  2396. viewer = new Viewer(element, options);
  2397. $element.data(NAMESPACE$1, viewer);
  2398. }
  2399. if (typeof option === 'string') {
  2400. var fn = viewer[option];
  2401. if ($.isFunction(fn)) {
  2402. result = fn.apply(viewer, args);
  2403. if (result === viewer) {
  2404. result = undefined;
  2405. }
  2406. if (isDestroy) {
  2407. $element.removeData(NAMESPACE$1);
  2408. }
  2409. }
  2410. }
  2411. });
  2412. return result !== undefined ? result : this;
  2413. };
  2414. $.fn.viewer.Constructor = Viewer;
  2415. $.fn.viewer.setDefaults = Viewer.setDefaults;
  2416. $.fn.viewer.noConflict = function noConflict() {
  2417. $.fn.viewer = AnotherViewer$1;
  2418. return this;
  2419. };
  2420. }
  2421. })));