viewer.common.js 97 KB

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