newtab-imgviewer

为新标签页打开的图片添加图片查看器

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