viewer.esm.js 85 KB

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