UNPKG

@alicloud/console-components

Version:
250 lines (249 loc) 11 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var next_1 = require("@alifd/next"); var react_dom_1 = __importDefault(require("react-dom")); var hoist_non_react_statics_1 = __importDefault(require("hoist-non-react-statics")); var react_1 = __importStar(require("react")); var useCssVar_1 = require("../utils/useCssVar"); var sizeMap = { mini: 400, small: 600, medium: 800, large: 1200 }; var yunxiaoSizeMap = { mini: 440, small: 600, medium: 800, large: 1200 }; // 获取 size 大小 var getCustomWidth = function (size, theme) { var sizeWidth = theme.startsWith('yunxiao') ? yunxiaoSizeMap[size] : sizeMap[size]; if (sizeWidth) { return { style: { width: sizeWidth + "px" } }; } return {}; }; // 设置阴影 var setFooterShadow = function (dom, prefix) { var _a, _b; if (dom) { var dialogBodyDom = (_a = dom === null || dom === void 0 ? void 0 : dom.getElementsByClassName(prefix + "dialog-body")) === null || _a === void 0 ? void 0 : _a[0]; var dialogFooterDom = (_b = dom === null || dom === void 0 ? void 0 : dom.getElementsByClassName(prefix + "dialog-footer")) === null || _b === void 0 ? void 0 : _b[0]; if (dialogFooterDom) { if ((dialogBodyDom === null || dialogBodyDom === void 0 ? void 0 : dialogBodyDom.clientHeight) < (dialogBodyDom === null || dialogBodyDom === void 0 ? void 0 : dialogBodyDom.scrollHeight)) { dialogFooterDom.classList.add(prefix + "dialog-footer-has-shadow"); } else { dialogFooterDom.classList.remove(prefix + "dialog-footer-has-shadow"); } } } }; var Dialog = function (props) { var size = props.size, others = __rest(props, ["size"]); var _a = props.prefix, prefix = _a === void 0 ? 'next-' : _a; var theme = useCssVar_1.useCssVar('--alicloudfe-components-theme').trim(); var customRef = react_1.useRef(null); // 有滚动条时底部显示阴影 var setFooterShadowOfRef = function () { if (theme !== 'wind' && !theme.startsWith('xconsole')) { var dialogDom = react_dom_1.default.findDOMNode(customRef.current); setFooterShadow(dialogDom, prefix); } }; var observer = null; // 绑定监听器 react_1.useEffect(function () { var _a; setFooterShadowOfRef(); var drawerDom = react_dom_1.default.findDOMNode(customRef.current); var drawerBodyDom = (_a = drawerDom === null || drawerDom === void 0 ? void 0 : drawerDom.getElementsByClassName(prefix + "dialog-body")) === null || _a === void 0 ? void 0 : _a[0]; if (drawerBodyDom && !observer) { observer = new MutationObserver(function () { setFooterShadowOfRef(); }); observer.observe(drawerBodyDom, { attributes: true, attributeFilter: ['style'], attributeOldValue: true, childList: true, subtree: true }); } // 销毁 return function () { if (observer) { observer.disconnect(); observer.takeRecords(); observer = null; } }; }); // 云效混合云主题样式主操作在右边 var defaultFooterActions = (function () { if (theme === 'yunxiao' || theme === 'yunxiao-dark' || theme === 'hybridcloud' || theme === 'hybridcloud-dark') return ['cancel', 'ok']; return ['ok', 'cancel']; })(); // 云效主题align在上方 var defaultAlign = (function () { if (theme === 'yunxiao' || theme === 'yunxiao-dark') return 'tc tc'; return 'cc cc'; })(); var defaultMinMargin = (function () { if (theme === 'yunxiao' || theme === 'yunxiao-dark') return 100; return 40; })(); return (react_1.default.createElement(next_1.Dialog, __assign({}, getCustomWidth(size, theme), { footerActions: defaultFooterActions, align: defaultAlign, minMargin: defaultMinMargin, shouldUpdatePosition: true }, others, { ref: customRef }))); }; var showDefaultFooterActions = function (theme) { if (theme === 'yunxiao' || theme === 'yunxiao-dark' || theme === 'hybridcloud' || theme === 'hybridcloud-dark') return ['cancel', 'ok']; return ['ok', 'cancel']; }; var showDefaultAlign = function (theme) { if (theme === 'yunxiao' || theme === 'yunxiao-dark') return 'tc tc'; return 'cc cc'; }; var showDefaultMinMargin = function (theme) { if (theme === 'yunxiao' || theme === 'yunxiao-dark') return 100; return 40; }; // 快捷调用的操作按钮顺序 var show = function (config) { var _a; var size = config.size, others = __rest(config, ["size"]); var _b = config.prefix, prefix = _b === void 0 ? 'next-' : _b; var theme = (_a = window .getComputedStyle) === null || _a === void 0 ? void 0 : _a.call(window, window.document.body).getPropertyValue('--alicloudfe-components-theme').trim(); setTimeout(function () { var _a, _b; var doms = ((_a = document.getElementsByClassName('quick-show')) !== null && _a !== void 0 ? _a : []); var _loop_1 = function (item) { // 初始化判断是否有阴影 setFooterShadow(item, prefix); // 绑定body监听器。监听高度变化 var dialogBodyDom = (_b = item === null || item === void 0 ? void 0 : item.getElementsByClassName(prefix + "dialog-body")) === null || _b === void 0 ? void 0 : _b[0]; var observer = new MutationObserver(function () { setFooterShadow(item, prefix); }); observer.observe(dialogBodyDom, { attributes: true, attributeFilter: ['style'], attributeOldValue: true, childList: true, subtree: true }); // 绑定dialog监听器,用于监听dialog被销毁时销毁所有监听器 var domObserver = new MutationObserver(function () { var _a, _b; if (((_b = (_a = item === null || item === void 0 ? void 0 : item.parentNode) === null || _a === void 0 ? void 0 : _a.parentNode) === null || _b === void 0 ? void 0 : _b.tagName) !== 'BODY') { observer.disconnect(); observer.takeRecords(); observer = null; domObserver.disconnect(); domObserver.takeRecords(); domObserver = null; } }); domObserver.observe(document.body, { attributes: true, attributeFilter: ['style'], attributeOldValue: true, childList: true, subtree: true }); }; for (var _i = 0, doms_1 = doms; _i < doms_1.length; _i++) { var item = doms_1[_i]; _loop_1(item); } }); return next_1.Dialog.show(__assign(__assign(__assign(__assign({}, getCustomWidth(size, theme)), { footerActions: showDefaultFooterActions(theme), align: showDefaultAlign(theme), minMargin: showDefaultMinMargin(theme), shouldUpdatePosition: true }), others), { // 将Dialog.show与其他quick弹窗区分出来,单独做样式覆盖, // 因为它的body是不包含Message的 className: ['quick-show', config.className].filter(Boolean).join(' ') })); }; var confirm = function (config) { var _a; var theme = (_a = window .getComputedStyle) === null || _a === void 0 ? void 0 : _a.call(window, window.document.body).getPropertyValue('--alicloudfe-components-theme').trim(); var size = config.size, others = __rest(config, ["size"]); return next_1.Dialog.confirm(__assign(__assign(__assign({}, getCustomWidth(size, theme)), { footerActions: showDefaultFooterActions(theme), align: showDefaultAlign(theme), minMargin: showDefaultMinMargin(theme), messageProps: { type: 'notice' }, shouldUpdatePosition: true }), others)); }; var alert = function (config) { var _a; var theme = (_a = window .getComputedStyle) === null || _a === void 0 ? void 0 : _a.call(window, window.document.body).getPropertyValue('--alicloudfe-components-theme').trim(); var size = config.size, others = __rest(config, ["size"]); return next_1.Dialog.alert(__assign(__assign(__assign({}, getCustomWidth(size, theme)), { footerActions: showDefaultFooterActions(theme), align: showDefaultAlign(theme), minMargin: showDefaultMinMargin(theme), messageProps: { type: 'warning' }, shouldUpdatePosition: true }), others)); }; hoist_non_react_statics_1.default(Dialog, next_1.Dialog, { show: true, confirm: true }); Dialog.show = show; Dialog.confirm = confirm; Dialog.alert = alert; exports.default = Dialog;