2b2428f9b70eced96bed3acbdaa1526127de7dd673309c5b9d84bcba49c6db4153de5d6a0c3c962d819c3c2fe8dc268a9baa9c3d30ee9c8a121cd1dd3b844e 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var core = require('@vueuse/core');
  5. var lodashUnified = require('lodash-unified');
  6. var index$3 = require('../../image-viewer/index.js');
  7. var image = require('./image.js');
  8. var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');
  9. var types = require('../../../utils/types.js');
  10. var index = require('../../../hooks/use-locale/index.js');
  11. var index$1 = require('../../../hooks/use-namespace/index.js');
  12. var index$2 = require('../../../hooks/use-attrs/index.js');
  13. var shared = require('@vue/shared');
  14. var scroll = require('../../../utils/dom/scroll.js');
  15. const __default__ = vue.defineComponent({
  16. name: "ElImage",
  17. inheritAttrs: false
  18. });
  19. const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  20. ...__default__,
  21. props: image.imageProps,
  22. emits: image.imageEmits,
  23. setup(__props, { expose, emit }) {
  24. const props = __props;
  25. const { t } = index.useLocale();
  26. const ns = index$1.useNamespace("image");
  27. const rawAttrs = vue.useAttrs();
  28. const containerAttrs = vue.computed(() => {
  29. return lodashUnified.fromPairs(Object.entries(rawAttrs).filter(([key]) => /^(data-|on[A-Z])/i.test(key) || ["id", "style"].includes(key)));
  30. });
  31. const imgAttrs = index$2.useAttrs({
  32. excludeListeners: true,
  33. excludeKeys: vue.computed(() => {
  34. return Object.keys(containerAttrs.value);
  35. })
  36. });
  37. const imageSrc = vue.ref();
  38. const hasLoadError = vue.ref(false);
  39. const isLoading = vue.ref(true);
  40. const showViewer = vue.ref(false);
  41. const container = vue.ref();
  42. const _scrollContainer = vue.ref();
  43. const supportLoading = core.isClient && "loading" in HTMLImageElement.prototype;
  44. let stopScrollListener;
  45. const imageKls = vue.computed(() => [
  46. ns.e("inner"),
  47. preview.value && ns.e("preview"),
  48. isLoading.value && ns.is("loading")
  49. ]);
  50. const imageStyle = vue.computed(() => {
  51. const { fit } = props;
  52. if (core.isClient && fit) {
  53. return { objectFit: fit };
  54. }
  55. return {};
  56. });
  57. const preview = vue.computed(() => {
  58. const { previewSrcList } = props;
  59. return shared.isArray(previewSrcList) && previewSrcList.length > 0;
  60. });
  61. const imageIndex = vue.computed(() => {
  62. const { previewSrcList, initialIndex } = props;
  63. let previewIndex = initialIndex;
  64. if (initialIndex > previewSrcList.length - 1) {
  65. previewIndex = 0;
  66. }
  67. return previewIndex;
  68. });
  69. const isManual = vue.computed(() => {
  70. if (props.loading === "eager")
  71. return false;
  72. return !supportLoading && props.loading === "lazy" || props.lazy;
  73. });
  74. const loadImage = () => {
  75. if (!core.isClient)
  76. return;
  77. isLoading.value = true;
  78. hasLoadError.value = false;
  79. imageSrc.value = props.src;
  80. };
  81. function handleLoad(event) {
  82. isLoading.value = false;
  83. hasLoadError.value = false;
  84. emit("load", event);
  85. }
  86. function handleError(event) {
  87. isLoading.value = false;
  88. hasLoadError.value = true;
  89. emit("error", event);
  90. }
  91. function handleLazyLoad(isIntersecting) {
  92. if (isIntersecting) {
  93. loadImage();
  94. removeLazyLoadListener();
  95. }
  96. }
  97. const lazyLoadHandler = core.useThrottleFn(handleLazyLoad, 200, true);
  98. async function addLazyLoadListener() {
  99. var _a;
  100. if (!core.isClient)
  101. return;
  102. await vue.nextTick();
  103. const { scrollContainer } = props;
  104. if (types.isElement(scrollContainer)) {
  105. _scrollContainer.value = scrollContainer;
  106. } else if (shared.isString(scrollContainer) && scrollContainer !== "") {
  107. _scrollContainer.value = (_a = document.querySelector(scrollContainer)) != null ? _a : void 0;
  108. } else if (container.value) {
  109. const scrollContainer2 = scroll.getScrollContainer(container.value);
  110. _scrollContainer.value = types.isWindow(scrollContainer2) ? void 0 : scrollContainer2;
  111. }
  112. const { stop } = core.useIntersectionObserver(container, ([entry]) => {
  113. lazyLoadHandler(entry.isIntersecting);
  114. }, { root: _scrollContainer });
  115. stopScrollListener = stop;
  116. }
  117. function removeLazyLoadListener() {
  118. if (!core.isClient || !lazyLoadHandler)
  119. return;
  120. stopScrollListener == null ? void 0 : stopScrollListener();
  121. _scrollContainer.value = void 0;
  122. stopScrollListener = void 0;
  123. }
  124. function clickHandler() {
  125. if (!preview.value)
  126. return;
  127. showViewer.value = true;
  128. emit("show");
  129. }
  130. function closeViewer() {
  131. showViewer.value = false;
  132. emit("close");
  133. }
  134. function switchViewer(val) {
  135. emit("switch", val);
  136. }
  137. vue.watch(() => props.src, () => {
  138. if (isManual.value) {
  139. isLoading.value = true;
  140. hasLoadError.value = false;
  141. removeLazyLoadListener();
  142. addLazyLoadListener();
  143. } else {
  144. loadImage();
  145. }
  146. });
  147. vue.onMounted(() => {
  148. if (isManual.value) {
  149. addLazyLoadListener();
  150. } else {
  151. loadImage();
  152. }
  153. });
  154. expose({
  155. showPreview: clickHandler
  156. });
  157. return (_ctx, _cache) => {
  158. return vue.openBlock(), vue.createElementBlock("div", vue.mergeProps({
  159. ref_key: "container",
  160. ref: container
  161. }, vue.unref(containerAttrs), {
  162. class: [vue.unref(ns).b(), _ctx.$attrs.class]
  163. }), [
  164. hasLoadError.value ? vue.renderSlot(_ctx.$slots, "error", { key: 0 }, () => [
  165. vue.createElementVNode("div", {
  166. class: vue.normalizeClass(vue.unref(ns).e("error"))
  167. }, vue.toDisplayString(vue.unref(t)("el.image.error")), 3)
  168. ]) : (vue.openBlock(), vue.createElementBlock(vue.Fragment, { key: 1 }, [
  169. imageSrc.value !== void 0 ? (vue.openBlock(), vue.createElementBlock("img", vue.mergeProps({ key: 0 }, vue.unref(imgAttrs), {
  170. src: imageSrc.value,
  171. loading: _ctx.loading,
  172. style: vue.unref(imageStyle),
  173. class: vue.unref(imageKls),
  174. crossorigin: _ctx.crossorigin,
  175. onClick: clickHandler,
  176. onLoad: handleLoad,
  177. onError: handleError
  178. }), null, 16, ["src", "loading", "crossorigin"])) : vue.createCommentVNode("v-if", true),
  179. isLoading.value ? (vue.openBlock(), vue.createElementBlock("div", {
  180. key: 1,
  181. class: vue.normalizeClass(vue.unref(ns).e("wrapper"))
  182. }, [
  183. vue.renderSlot(_ctx.$slots, "placeholder", {}, () => [
  184. vue.createElementVNode("div", {
  185. class: vue.normalizeClass(vue.unref(ns).e("placeholder"))
  186. }, null, 2)
  187. ])
  188. ], 2)) : vue.createCommentVNode("v-if", true)
  189. ], 64)),
  190. vue.unref(preview) ? (vue.openBlock(), vue.createElementBlock(vue.Fragment, { key: 2 }, [
  191. showViewer.value ? (vue.openBlock(), vue.createBlock(vue.unref(index$3.ElImageViewer), {
  192. key: 0,
  193. "z-index": _ctx.zIndex,
  194. "initial-index": vue.unref(imageIndex),
  195. infinite: _ctx.infinite,
  196. "zoom-rate": _ctx.zoomRate,
  197. "min-scale": _ctx.minScale,
  198. "max-scale": _ctx.maxScale,
  199. "show-progress": _ctx.showProgress,
  200. "url-list": _ctx.previewSrcList,
  201. scale: _ctx.scale,
  202. crossorigin: _ctx.crossorigin,
  203. "hide-on-click-modal": _ctx.hideOnClickModal,
  204. teleported: _ctx.previewTeleported,
  205. "close-on-press-escape": _ctx.closeOnPressEscape,
  206. onClose: closeViewer,
  207. onSwitch: switchViewer
  208. }, vue.createSlots({
  209. toolbar: vue.withCtx((toolbar) => [
  210. vue.renderSlot(_ctx.$slots, "toolbar", vue.normalizeProps(vue.guardReactiveProps(toolbar)))
  211. ]),
  212. default: vue.withCtx(() => [
  213. _ctx.$slots.viewer ? (vue.openBlock(), vue.createElementBlock("div", { key: 0 }, [
  214. vue.renderSlot(_ctx.$slots, "viewer")
  215. ])) : vue.createCommentVNode("v-if", true)
  216. ]),
  217. _: 2
  218. }, [
  219. _ctx.$slots.progress ? {
  220. name: "progress",
  221. fn: vue.withCtx((progress) => [
  222. vue.renderSlot(_ctx.$slots, "progress", vue.normalizeProps(vue.guardReactiveProps(progress)))
  223. ])
  224. } : void 0,
  225. _ctx.$slots["viewer-error"] ? {
  226. name: "viewer-error",
  227. fn: vue.withCtx((viewerError) => [
  228. vue.renderSlot(_ctx.$slots, "viewer-error", vue.normalizeProps(vue.guardReactiveProps(viewerError)))
  229. ])
  230. } : void 0
  231. ]), 1032, ["z-index", "initial-index", "infinite", "zoom-rate", "min-scale", "max-scale", "show-progress", "url-list", "scale", "crossorigin", "hide-on-click-modal", "teleported", "close-on-press-escape"])) : vue.createCommentVNode("v-if", true)
  232. ], 64)) : vue.createCommentVNode("v-if", true)
  233. ], 16);
  234. };
  235. }
  236. });
  237. var Image = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["__file", "image.vue"]]);
  238. exports["default"] = Image;
  239. //# sourceMappingURL=image2.js.map