viewer.common.js 75 KB

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