viewer.common.js 84 KB

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