tdesign-vue
Version:
534 lines (530 loc) • 21.7 kB
JavaScript
/**
* tdesign v1.14.1
* (c) 2025 tdesign
* @license MIT
*/
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { isNumber, throttle } from 'lodash-es';
import { InfoCircleFilledIcon, CheckCircleFilledIcon, ErrorCircleFilledIcon, CloseIcon } from 'tdesign-icons-vue';
import { Button } from '../button/index.js';
import ActionMixin from './actions.js';
import props from './props.js';
import { renderContent, renderTNodeJSX } from '../utils/render-tnode.js';
import mixins from '../utils/mixins.js';
import getConfigReceiverMixins, { getGlobalIconMixins, getAttachConfigMixins } from '../config-provider/config-receiver.js';
import TransferDom from '../utils/transfer-dom.js';
import { emitEvent } from '../utils/event.js';
import { updateElement } from '../hooks/useDestroyOnClose.js';
import stack from './stack.js';
import { getScrollbarWidth } from '../_common/js/utils/getScrollbarWidth.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/directive.js';
import '../loading/plugin.js';
import 'vue';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/setStyle.js';
import '../_common/js/utils/helper.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '../utils/dom.js';
import 'raf';
import '../utils/easing.js';
import '../loading/props.js';
import '@babel/runtime/helpers/readOnlyError';
import '@babel/runtime/helpers/typeof';
import '@vue/composition-api';
import '../config-provider/context.js';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-d2f11e9e.js';
import '../_chunks/dep-05dc6583.js';
import 'dayjs';
import '../_chunks/dep-62386ed0.js';
import '../config-provider/type.js';
import '../_common/js/global-config/t.js';
import '../config.js';
import '../utils/withInstall.js';
import './style/css.js';
import '../loading/type.js';
import '../button/props.js';
import '../utils/ripple.js';
import '../button/type.js';
import '../_chunks/dep-e77071c7.js';
import '../utils/helper.js';
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 getCSSValue(v) {
return isNaN(Number(v)) ? v : "".concat(Number(v), "px");
}
var mousePosition;
var getClickPosition = function getClickPosition(e) {
mousePosition = {
x: e.clientX,
y: e.clientY
};
setTimeout(function () {
mousePosition = null;
}, 100);
};
if (typeof window !== "undefined" && window.document && window.document.documentElement) {
document.documentElement.addEventListener("click", getClickPosition, true);
}
var key = 1;
var _Dialog = mixins(ActionMixin, getConfigReceiverMixins("dialog"), getGlobalIconMixins(), getAttachConfigMixins("dialog")).extend({
name: "TDialog",
components: {
TButton: Button
},
data: function data() {
return {
uid: 0,
scrollWidth: 0,
disX: 0,
disY: 0,
windowInnerWidth: 0,
windowInnerHeight: 0,
dialogW: 0,
dialogH: 0,
dLeft: 0,
dTop: 0,
styleEl: null,
timer: null,
animationEnd: false
};
},
props: _objectSpread(_objectSpread({}, props), {}, {
instanceGlobal: Object
}),
computed: {
isModal: function isModal() {
return this.mode === "modal";
},
isModeLess: function isModeLess() {
return this.mode === "modeless";
},
isNormal: function isNormal() {
return this.mode === "normal";
},
isFullScreen: function isFullScreen() {
return this.mode === "full-screen";
},
maskClass: function maskClass() {
return ["".concat(this.componentName, "__mask"), !this.showOverlay && "".concat(this.classPrefix, "-is-hidden")];
},
dialogClass: function dialogClass() {
var dialogClass = ["".concat(this.componentName), "".concat(this.componentName, "__modal-").concat(this.theme), this.isModeLess && this.draggable && "".concat(this.componentName, "--draggable")];
if (this.isFullScreen) {
dialogClass.push("".concat(this.componentName, "__fullscreen"));
} else {
dialogClass.push("".concat(this.componentName, "--default"));
}
return dialogClass;
},
positionClass: function positionClass() {
if (this.isNormal) return [];
if (this.isFullScreen) return ["".concat(this.componentName, "__position_fullscreen")];
var dialogClass = ["".concat(this.componentName, "__position"), !!this.top && "".concat(this.componentName, "--top"), "".concat(this.placement && !this.top ? "".concat(this.componentName, "--").concat(this.placement) : "")];
return dialogClass;
},
wrapClass: function wrapClass() {
return [!this.isNormal && "".concat(this.componentName, "__wrap")];
},
ctxClass: function ctxClass() {
return ["".concat(this.componentName, "__ctx"), _defineProperty(_defineProperty(_defineProperty({}, "".concat(this.classPrefix, "-dialog__ctx--fixed"), this.mode === "modal" || this.isFullScreen), "".concat(this.classPrefix, "-dialog__ctx--absolute"), this.isModal && this.showInAttachedElement), "".concat(this.componentName, "__ctx--modeless"), this.isModeLess)];
},
positionStyle: function positionStyle() {
if (this.isFullScreen) return {};
var topStyle = {};
if (this.top !== void 0) {
if (isNumber(this.top) && this.top < 0) {
topStyle.paddingTop = "".concat(this.top, "px");
} else {
topStyle.paddingTop = this.top;
}
}
return topStyle;
},
computedDialogStyle: function computedDialogStyle() {
return !this.isFullScreen ? _objectSpread({
width: getCSSValue(this.width)
}, this.dialogStyle) : _objectSpread({}, this.dialogStyle);
},
computedAttach: function computedAttach() {
return this.isNormal ? void 0 : this.attach || this.globalAttach();
}
},
watch: {
visible: {
handler: function handler(value) {
var _this = this;
if (typeof window === "undefined") return;
if (value) {
this.animationEnd = false;
if (this.isModal && !this.showInAttachedElement || this.isFullScreen) {
if (this.preventScrollThrough) {
this.$nextTick(function () {
document.head.appendChild(_this.styleEl);
});
}
this.$nextTick(function () {
var target = _this.$refs.dialog;
if (mousePosition && target) {
target.style.transformOrigin = "".concat(mousePosition.x - target.offsetLeft, "px ").concat(mousePosition.y - target.offsetTop, "px");
}
});
}
document.activeElement.blur();
} else {
this.clearStyleFunc();
}
this.$nextTick(function () {
_this.storeUid(value);
});
this.addKeyboardEvent(value);
if (this.isModeLess && this.draggable) {
this.$nextTick(function () {
_this.initDragEvent(value);
});
}
if (value && !this.destroyOnClose && requestAnimationFrame) {
requestAnimationFrame(function () {
updateElement(_this);
});
}
},
immediate: true
}
},
mounted: function mounted() {
var hasScrollBar = document.documentElement.scrollHeight > document.documentElement.clientHeight;
var scrollWidth = hasScrollBar ? getScrollbarWidth() : 0;
if (this.draggable) {
window.addEventListener("resize", throttle(this.resizeAdjustPosition, 1e3));
}
this.uid = this._uid;
this.styleEl = document.createElement("style");
this.styleEl.dataset.id = "td_dialog_".concat(+new Date(), "_").concat(key += 1);
this.styleEl.innerHTML = "\n html body {\n overflow-y: hidden;\n width: calc(100% - ".concat(scrollWidth, "px);\n }\n ");
if (this.visible && this.isModal && this.preventScrollThrough && !this.showInAttachedElement) {
document.head.appendChild(this.styleEl);
}
},
beforeDestroy: function beforeDestroy() {
this.addKeyboardEvent(false);
this.destroySelf();
this.destroySelfStyle();
},
directives: {
TransferDom: TransferDom
},
methods: {
clearStyleFunc: function clearStyleFunc() {
var _this2 = this;
clearTimeout(this.timer);
this.timer = setTimeout(function () {
_this2.destroySelfStyle();
}, 150);
},
destroySelfStyle: function destroySelfStyle() {
var _this$styleEl$parentN, _this$styleEl$parentN2;
(_this$styleEl$parentN = this.styleEl.parentNode) === null || _this$styleEl$parentN === void 0 || (_this$styleEl$parentN2 = _this$styleEl$parentN.removeChild) === null || _this$styleEl$parentN2 === void 0 || _this$styleEl$parentN2.call(_this$styleEl$parentN, this.styleEl);
},
destroySelf: function destroySelf() {
var _this$$el$parentNode, _this$$el$parentNode$;
(_this$$el$parentNode = this.$el.parentNode) === null || _this$$el$parentNode === void 0 || (_this$$el$parentNode$ = _this$$el$parentNode.removeChild) === null || _this$$el$parentNode$ === void 0 || _this$$el$parentNode$.call(_this$$el$parentNode, this.$el);
},
storeUid: function storeUid(flag) {
if (flag) {
stack.push(this.uid);
} else {
stack.pop(this.uid);
}
},
addKeyboardEvent: function addKeyboardEvent(status) {
if (status) {
document.addEventListener("keydown", this.keyboardEvent);
this.confirmOnEnter && document.addEventListener("keydown", this.keyboardEnterEvent);
} else {
document.removeEventListener("keydown", this.keyboardEvent);
this.confirmOnEnter && document.removeEventListener("keydown", this.keyboardEnterEvent);
}
},
keyboardEvent: function keyboardEvent(e) {
if (e.code === "Escape" && stack.top === this.uid) {
var _this$closeOnEscKeydo;
emitEvent(this, "esc-keydown", {
e: e
});
if ((_this$closeOnEscKeydo = this.closeOnEscKeydown) !== null && _this$closeOnEscKeydo !== void 0 ? _this$closeOnEscKeydo : this.global.closeOnEscKeydown) {
this.emitCloseEvent({
e: e,
trigger: "esc"
});
}
}
},
keyboardEnterEvent: function keyboardEnterEvent(e) {
var code = e.code;
if ((code === "Enter" || code === "NumpadEnter") && stack.top === this.uid) {
emitEvent(this, "confirm", {
e: e
});
}
},
overlayAction: function overlayAction(e) {
var _this$closeOnOverlayC;
if (e.target !== this.$refs.dialogPosition) {
return;
}
emitEvent(this, "overlay-click", {
e: e
});
if (this.showOverlay && ((_this$closeOnOverlayC = this.closeOnOverlayClick) !== null && _this$closeOnOverlayC !== void 0 ? _this$closeOnOverlayC : this.global.closeOnOverlayClick)) {
this.emitCloseEvent({
e: e,
trigger: "overlay"
});
}
},
closeBtnAction: function closeBtnAction(e) {
emitEvent(this, "close-btn-click", {
e: e
});
this.emitCloseEvent({
trigger: "close-btn",
e: e
});
},
cancelBtnAction: function cancelBtnAction(e) {
emitEvent(this, "cancel", {
e: e
});
this.emitCloseEvent({
trigger: "cancel",
e: e
});
},
confirmBtnAction: function confirmBtnAction(e) {
emitEvent(this, "confirm", {
e: e
});
},
beforeEnter: function beforeEnter() {
emitEvent(this, "before-open");
},
afterEnter: function afterEnter() {
emitEvent(this, "opened");
},
beforeLeave: function beforeLeave() {
emitEvent(this, "before-close");
},
afterLeave: function afterLeave() {
if (this.isModeLess && this.draggable) {
var target = this.$refs.dialog;
if (!target) return;
target.style.position = "relative";
target.style.left = "unset";
target.style.top = "unset";
}
emitEvent(this, "closed");
this.animationEnd = true;
},
emitCloseEvent: function emitCloseEvent(context) {
emitEvent(this, "close", context);
this.$emit("update:visible", false);
},
hasEventOn: function hasEventOn(name) {
var _this$_events;
var eventFuncs = (_this$_events = this["_events"]) === null || _this$_events === void 0 ? void 0 : _this$_events[name];
return !!(eventFuncs !== null && eventFuncs !== void 0 && eventFuncs.length);
},
getIcon: function getIcon() {
var h = this.$createElement;
var _this$useGlobalIcon = this.useGlobalIcon({
InfoCircleFilledIcon: InfoCircleFilledIcon,
CheckCircleFilledIcon: CheckCircleFilledIcon,
ErrorCircleFilledIcon: ErrorCircleFilledIcon
}),
InfoCircleFilledIcon$1 = _this$useGlobalIcon.InfoCircleFilledIcon,
CheckCircleFilledIcon$1 = _this$useGlobalIcon.CheckCircleFilledIcon,
ErrorCircleFilledIcon$1 = _this$useGlobalIcon.ErrorCircleFilledIcon;
var icon = {
info: h(InfoCircleFilledIcon$1, {
"class": "".concat(this.classPrefix, "-is-info")
}),
warning: h(ErrorCircleFilledIcon$1, {
"class": "".concat(this.classPrefix, "-is-warning")
}),
danger: h(ErrorCircleFilledIcon$1, {
"class": "".concat(this.classPrefix, "-is-error")
}),
success: h(CheckCircleFilledIcon$1, {
"class": "".concat(this.classPrefix, "-is-success")
})
};
return icon[this.theme];
},
mousedownHandler: function mousedownHandler(targetEvent) {
var target = this.$refs.dialog;
this.disX = targetEvent.clientX - target.offsetLeft;
this.disY = targetEvent.clientY - target.offsetTop;
this.dialogW = target.offsetWidth;
this.dialogH = target.offsetHeight;
this.windowInnerWidth = window.innerWidth || document.documentElement.clientWidth;
this.windowInnerHeight = window.innerHeight || document.documentElement.clientHeight;
if (this.dialogW > this.windowInnerWidth || this.dialogH > this.windowInnerHeight) return;
document.addEventListener("mousemove", this.mouseMoverHandler);
document.addEventListener("mouseup", this.mouseUpHandler);
document.addEventListener("dragend", this.mouseUpHandler);
},
mouseMoverHandler: function mouseMoverHandler(documentEvent) {
var target = this.$refs.dialog;
var left = documentEvent.clientX - this.disX;
var top = documentEvent.clientY - this.disY;
if (left < 0) left = 0;
if (top < 0) top = 0;
if (this.windowInnerWidth - target.offsetWidth - left < 0) left = this.windowInnerWidth - target.offsetWidth;
if (this.windowInnerHeight - target.offsetHeight - top < 0) top = this.windowInnerHeight - target.offsetHeight;
target.style.position = "absolute";
target.style.left = "".concat(left, "px");
target.style.top = "".concat(top, "px");
},
mouseUpHandler: function mouseUpHandler() {
document.removeEventListener("mousemove", this.mouseMoverHandler);
document.removeEventListener("mouseup", this.mouseUpHandler);
document.removeEventListener("dragend", this.mouseUpHandler);
},
initDragEvent: function initDragEvent(status) {
var target = this.$refs.dialog;
if (status) {
target.addEventListener("mousedown", this.mousedownHandler);
} else {
target.removeEventListener("mousedown", this.mousedownHandler);
}
},
resizeAdjustPosition: function resizeAdjustPosition() {
if (this.visible) {
var target = this.$refs.dialog;
target.style.left = "".concat(this.dLeft * (window.innerWidth / this.windowInnerWidth), "px");
target.style.top = "".concat(this.dTop * (window.innerHeight / this.windowInnerHeight), "px");
}
},
onStopDown: function onStopDown(e) {
if (this.isModeLess && this.draggable) e.stopPropagation();
},
renderDialog: function renderDialog() {
var _this$instanceGlobal, _this$instanceGlobal2, _this$instanceGlobal3;
var h = this.$createElement;
var _this$useGlobalIcon2 = this.useGlobalIcon({
CloseIcon: CloseIcon
}),
CloseIcon$1 = _this$useGlobalIcon2.CloseIcon;
var defaultHeader = h("h5", {
"class": "title"
});
var defaultCloseBtn = h(CloseIcon$1);
var body = renderContent(this, "default", "body");
var defaultFooter = h("div", [this.getCancelBtn({
cancelBtn: this.cancelBtn,
globalCancel: ((_this$instanceGlobal = this.instanceGlobal) === null || _this$instanceGlobal === void 0 ? void 0 : _this$instanceGlobal.cancel) || this.global.cancel,
className: "".concat(this.componentName, "__cancel")
}), this.getConfirmBtn({
theme: this.theme,
confirmBtn: this.confirmBtn,
confirmLoading: this.confirmLoading,
globalConfirm: ((_this$instanceGlobal2 = this.instanceGlobal) === null || _this$instanceGlobal2 === void 0 ? void 0 : _this$instanceGlobal2.confirm) || this.global.confirm,
globalConfirmBtnTheme: ((_this$instanceGlobal3 = this.instanceGlobal) === null || _this$instanceGlobal3 === void 0 ? void 0 : _this$instanceGlobal3.confirmBtnTheme) || this.global.confirmBtnTheme,
className: "".concat(this.componentName, "__confirm")
})]);
var headerClassName = this.isFullScreen ? ["".concat(this.componentName, "__header"), "".concat(this.componentName, "__header--fullscreen")] : "".concat(this.componentName, "__header");
var closeClassName = this.isFullScreen ? ["".concat(this.componentName, "__close"), "".concat(this.componentName, "__close--fullscreen")] : "".concat(this.componentName, "__close");
var bodyClassName = this.theme === "default" ? ["".concat(this.componentName, "__body")] : ["".concat(this.componentName, "__body"), "".concat(this.componentName, "__body__icon")];
var footerContent = renderTNodeJSX(this, "footer", defaultFooter);
if (this.isFullScreen && footerContent) {
bodyClassName.push("".concat(this.componentName, "__body--fullscreen"));
} else if (this.isFullScreen) {
bodyClassName.push("".concat(this.componentName, "__body--fullscreen--without-footer"));
}
var footerClassName = this.isFullScreen ? ["".concat(this.componentName, "__footer"), "".concat(this.componentName, "__footer--fullscreen")] : "".concat(this.componentName, "__footer");
var footer = this.footer ? h("div", {
"class": footerClassName,
"on": {
"mousedown": this.onStopDown
}
}, [footerContent]) : null;
return h("div", {
"class": this.wrapClass
}, [h("div", {
"class": this.positionClass,
"style": this.positionStyle,
"on": {
"click": this.overlayAction
},
"ref": "dialogPosition"
}, [h("div", {
"key": "dialog",
"ref": "dialog",
"class": this.dialogClass,
"style": this.computedDialogStyle
}, [h("div", {
"class": headerClassName,
"on": {
"mousedown": this.onStopDown
}
}, [h("div", {
"class": "".concat(this.componentName, "__header-content")
}, [this.getIcon(), renderTNodeJSX(this, "header", defaultHeader)]), this.closeBtn ? h("span", {
"class": closeClassName,
"on": {
"click": this.closeBtnAction
}
}, [renderTNodeJSX(this, "closeBtn", defaultCloseBtn)]) : null]), h("div", {
"class": bodyClassName,
"on": {
"mousedown": this.onStopDown
}
}, [body]), footer])])]);
}
},
render: function render() {
var h = arguments[0];
var maskView = (this.isModal || this.isFullScreen) && h("div", {
"key": "mask",
"class": this.maskClass
});
var dialogView = this.renderDialog();
var view = [maskView, dialogView];
var ctxStyle = {
zIndex: this.zIndex
};
if (this.destroyOnClose && !this.visible && this.animationEnd) return null;
return h("transition", {
"attrs": {
"duration": 300,
"name": "".concat(this.componentName, "-zoom__vue")
},
"on": {
"beforeEnter": this.beforeEnter,
"afterEnter": this.afterEnter,
"beforeLeave": this.beforeLeave,
"afterLeave": this.afterLeave
}
}, [h("div", {
"directives": [{
name: "show",
value: this.visible
}, {
name: "transfer-dom",
value: this.computedAttach
}],
"class": this.ctxClass,
"style": ctxStyle
}, [view])]);
}
});
export { _Dialog as default };
//# sourceMappingURL=dialog.js.map