viewer.esm.js 97 KB

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