lazy-container.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => LazyContainerManager
  21. });
  22. module.exports = __toCommonJS(stdin_exports);
  23. var import_util = require("./util");
  24. const defaultOptions = {
  25. selector: "img"
  26. };
  27. class LazyContainer {
  28. constructor({ el, binding, vnode, lazy }) {
  29. this.el = null;
  30. this.vnode = vnode;
  31. this.binding = binding;
  32. this.options = {};
  33. this.lazy = lazy;
  34. this.queue = [];
  35. this.update({ el, binding });
  36. }
  37. update({ el, binding }) {
  38. this.el = el;
  39. this.options = Object.assign({}, defaultOptions, binding.value);
  40. const imgs = this.getImgs();
  41. imgs.forEach((el2) => {
  42. this.lazy.add(
  43. el2,
  44. Object.assign({}, this.binding, {
  45. value: {
  46. src: "dataset" in el2 ? el2.dataset.src : el2.getAttribute("data-src"),
  47. error: ("dataset" in el2 ? el2.dataset.error : el2.getAttribute("data-error")) || this.options.error,
  48. loading: ("dataset" in el2 ? el2.dataset.loading : el2.getAttribute("data-loading")) || this.options.loading
  49. }
  50. }),
  51. this.vnode
  52. );
  53. });
  54. }
  55. getImgs() {
  56. return Array.from(this.el.querySelectorAll(this.options.selector));
  57. }
  58. clear() {
  59. const imgs = this.getImgs();
  60. imgs.forEach((el) => this.lazy.remove(el));
  61. this.vnode = null;
  62. this.binding = null;
  63. this.lazy = null;
  64. }
  65. }
  66. class LazyContainerManager {
  67. constructor({ lazy }) {
  68. this.lazy = lazy;
  69. this.queue = [];
  70. }
  71. bind(el, binding, vnode) {
  72. const container = new LazyContainer({
  73. el,
  74. binding,
  75. vnode,
  76. lazy: this.lazy
  77. });
  78. this.queue.push(container);
  79. }
  80. update(el, binding, vnode) {
  81. const container = this.queue.find((item) => item.el === el);
  82. if (!container)
  83. return;
  84. container.update({ el, binding, vnode });
  85. }
  86. unbind(el) {
  87. const container = this.queue.find((item) => item.el === el);
  88. if (!container)
  89. return;
  90. container.clear();
  91. (0, import_util.remove)(this.queue, container);
  92. }
  93. }