@ohu-mobile/core
Version:
188 lines (187 loc) • 7.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _variables = require("../_config/variables");
var _defineComponent = require("../_utils/defineComponent");
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var animations = {
fade: "".concat(_variables.$prefix, "lazyload-fade"),
none: ''
};
var Lazyload = (0, _defineComponent.defineComponent)('lazyload').create({
props: {
disabled: (0, _defineComponent.props)(Boolean).default(false),
rootElement: _defineComponent.props.ofType().default(null),
threshold: (0, _defineComponent.props)(Number).default(0.01),
rootMargin: (0, _defineComponent.props)(String).default('0px'),
tag: (0, _defineComponent.props)(String).default('div'),
src: (0, _defineComponent.props)(String).default(''),
asyncComponent: _defineComponent.props.ofType().default(null),
imgAttrs: _defineComponent.props.ofType().optional,
imgStyle: _defineComponent.props.ofType().optional,
animation: _defineComponent.props.ofType().default('fade')
},
data: function data() {
return {
hasLoaded: false,
asyncNode: null,
asyncError: null,
hasImageLoaded: false,
observer: null
};
},
watch: {
src: function src() {
this.init();
},
asyncComponent: function asyncComponent() {
this.init();
}
},
methods: {
init: function init() {
this.initState();
if (this.disabled) {
this.reload();
return;
}
this.initObserver();
},
initState: function initState() {
this.asyncError = null;
this.hasLoaded = false;
this.hasImageLoaded = false;
},
reload: function reload() {
var _this = this;
this.initState();
this.$nextTick(function () {
_this.loadAsync();
});
},
loadAsync: function loadAsync() {
var _this2 = this;
if (this.asyncComponent) {
this.asyncComponent().then(function (component) {
var _component$default;
_this2.$emit('loaded');
_this2.asyncNode = _this2.$createElement((_component$default = component.default) !== null && _component$default !== void 0 ? _component$default : component);
}).catch(function (error) {
_this2.$emit('error', error);
_this2.asyncError = error;
}).finally(function () {
_this2.hasLoaded = true;
});
} else if (this.src) {
this.asyncNode = this.$createElement('img', {
attrs: _objectSpread({
src: this.src
}, this.imgAttrs),
style: this.imgStyle,
on: {
error: function error(_error) {
_this2.$emit('error', _error);
_this2.asyncError = _error;
_this2.hasImageLoaded = true;
},
load: function load() {
_this2.$emit('loaded');
_this2.hasImageLoaded = true;
}
}
});
} else {
this.hasLoaded = true;
}
},
initObserver: function initObserver() {
var _this3 = this;
this.destroyObserver();
var observer = new IntersectionObserver(function (entries, observer) {
if (entries[0]) {
if (entries[0].isIntersecting && entries[0].intersectionRatio > 0) {
_this3.reload();
observer.unobserve(_this3.$el);
}
}
}, {
root: this.rootElement,
rootMargin: this.rootMargin,
threshold: this.threshold
});
this.observer = observer;
observer.observe(this.$el);
this.$on('hook:beforeDestroy', function () {
_this3.destroyObserver();
});
},
destroyObserver: function destroyObserver() {
if (this.observer) {
this.observer.disconnect();
this.observer = null;
}
}
},
mounted: function mounted() {
this.init();
},
render: function render() {
var h = arguments[0];
var root = this.$rootCls();
var $slots = this.$slots,
$scopedSlots = this.$scopedSlots,
hasLoaded = this.hasLoaded,
asyncNode = this.asyncNode,
asyncError = this.asyncError,
reload = this.reload,
hasImageLoaded = this.hasImageLoaded,
animation = this.animation;
var content = $scopedSlots.default ? $scopedSlots.default(hasLoaded) : $slots.default && $slots.default[0];
var placeholder = $slots.placeholder && $slots.placeholder[0];
var error = $scopedSlots.error ? $scopedSlots.error({
reload: reload,
error: asyncError || undefined
}) : $slots.error && $slots.error[0];
if (asyncError) {
return h("div", {
"class": root
}, [error || placeholder]);
}
var node;
if (this.src) {
node = [h("div", {
"key": "content"
}, [asyncNode]), hasImageLoaded || h("div", {
"key": "placeholder",
"style": {
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%'
}
}, [placeholder])];
} else {
node = hasLoaded ? h("div", {
"key": "content"
}, [asyncNode || content]) : h("div", {
"key": "placeholder"
}, [placeholder]);
}
return h("transition-group", {
"class": root,
"attrs": {
"name": animations[animation],
"tag": this.tag
}
}, [node]);
}
});
var _default = exports.default = Lazyload;