viewer.js 103 KB

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