viewer.js 90 KB

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