viewer.common.js 85 KB

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