viewer.js 102 KB

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