viewer.esm.js 75 KB

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