viewer.js 104 KB

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