viewer.js 89 KB

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