tdesign-vue
Version:
156 lines (152 loc) • 4.75 kB
JavaScript
/**
* tdesign v1.14.1
* (c) 2025 tdesign
* @license MIT
*/
import Vue from 'vue';
import raf from 'raf';
import { isFunction } from 'lodash-es';
import { removeDom, getAttach } from '../utils/dom.js';
import '../utils/easing.js';
function isContentRectChanged(rect1, rect2) {
if (!rect1 || !rect2) return;
if (["width", "height", "x", "y"].some(function (k) {
return rect1[k] !== rect2[k];
})) {
return true;
}
return false;
}
function observeResize(elm, cb) {
var _window;
if (!((_window = window) !== null && _window !== void 0 && _window.ResizeObserver) || !elm) return;
var prevContentRect = null;
var ro = new ResizeObserver(function () {
var entries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var _ref = entries[0] || {},
contentRect = _ref.contentRect;
if (isContentRectChanged(contentRect, prevContentRect)) {
prevContentRect = contentRect;
cb(contentRect);
return;
}
if (!prevContentRect) {
prevContentRect = contentRect;
}
});
ro.observe(elm);
return function () {
ro.unobserve(elm);
};
}
var Trigger = Vue.extend({
data: function data() {
return {
contentRect: null
};
},
mounted: function mounted() {
var _process$env,
_this = this;
if (!this.$el || typeof process !== "undefined" && ((_process$env = process.env) === null || _process$env === void 0 ? void 0 : _process$env.NODE_ENV) === "test") return;
this.$on("hook:destroyed", observeResize(this.$el, function (ev) {
_this.$emit("resize", ev);
}));
},
render: function render() {
var _children$;
var h = arguments[0];
var children = this.$slots["default"] || [];
if (children.length > 1 || !((_children$ = children[0]) !== null && _children$ !== void 0 && _children$.tag)) {
return h("span", [children]);
}
return children[0];
}
});
var Container = Vue.extend({
props: {
parent: Object,
visible: Boolean,
attach: [Function]
},
data: function data() {
return {
content: null
};
},
mounted: function mounted() {
var _this2 = this;
if (this.visible) {
raf(this.mountContent);
}
this.$watch("visible", function (visible) {
if (visible) {
_this2.mountContent();
}
});
},
destroyed: function destroyed() {
var _process$env2;
if (typeof process !== "undefined" && ((_process$env2 = process.env) === null || _process$env2 === void 0 ? void 0 : _process$env2.NODE_ENV) === "test") return;
this.unmountContent();
},
methods: {
mountContent: function mountContent() {
var _this$$refs;
if (this.content) return;
var parent = this;
var elm = document.createElement("div");
elm.style.cssText = "position: absolute; top: 0px; left: 0px; width: 100%";
elm.appendChild(document.createElement("div"));
this.content = new this.$root.constructor({
parent: parent,
render: function render() {
return parent.$slots.content;
},
mounted: function mounted() {
parent.$emit("contentMounted");
var content = this.$el.children[0];
if (content) {
this.$on("hook:destroyed", observeResize(content, function () {
parent.$emit("resize");
}));
}
},
destroyed: function destroyed() {
parent.content = null;
removeDom(elm);
}
});
var _this$attach = this.attach(),
attach = _this$attach.attach,
current = _this$attach.current;
var currentAttach = attach === "CURRENT_NODE" ? current : attach;
getAttach(currentAttach, (_this$$refs = this.$refs) === null || _this$$refs === void 0 || (_this$$refs = _this$$refs.triggerRef) === null || _this$$refs === void 0 ? void 0 : _this$$refs.$el).appendChild(elm);
this.content.$mount(elm.children[0]);
},
unmountContent: function unmountContent() {
var _this$content;
if (isFunction((_this$content = this.content) === null || _this$content === void 0 ? void 0 : _this$content.$destroy)) {
this.content.$destroy();
}
},
updateContent: function updateContent() {
var _this$content2;
(_this$content2 = this.content) === null || _this$content2 === void 0 || _this$content2.$forceUpdate();
}
},
render: function render() {
var _this3 = this;
var h = arguments[0];
return h(Trigger, {
"ref": "triggerRef",
"on": {
"resize": function resize() {
return _this3.$emit("resize");
}
}
}, [this.$slots["default"]]);
}
});
export { Container as default };
//# sourceMappingURL=container.js.map