viewer.esm.js 84 KB

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