viewer.js 88 KB

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