UNPKG

tdesign-mobile-vue

Version:
570 lines (566 loc) 23 kB
/** * tdesign v1.7.0 * (c) 2024 TDesign Group * @license MIT */ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import { defineComponent, toRefs, ref, computed, watch, onMounted, createVNode, Fragment, Teleport, h, nextTick, mergeProps } from 'vue'; import isFunction from 'lodash/isFunction'; import { Popover } from '../popover/index.js'; import { Popup } from '../popup/index.js'; import { Button } from '../button/index.js'; import config from '../config.js'; import { removeClass, addClass, getWindowScroll } from '../shared/dom.js'; import setStyle from '../_common/js/utils/set-style.js'; import guideProps from './props.js'; import { isFixed, getTargetElm, scrollToParentVisibleArea, scrollToElm, getRelativePosition } from './utils/dom.js'; import { usePrefixClass } from '../hooks/useClass.js'; import { useConfig } from '../config-provider/useConfig.js'; import { useVModel } from '../shared/useVModel/index.js'; import '../popover/popover.js'; import '../popover/props.js'; import '../hooks/tnode.js'; import 'lodash/camelCase'; import 'lodash/kebabCase'; import '../hooks/render-tnode.js'; import 'lodash/isEmpty'; import 'lodash/isString'; import 'lodash/isObject'; import '../shared/useClickAway/index.js'; import '@vueuse/core'; import 'lodash/isArray'; import '../shared/util.js'; import 'lodash/isNumber'; import '../shared/useDefault/index.js'; import '../shared/component.js'; import 'lodash/cloneDeep'; import '../config-provider/context.js'; import 'lodash/mergeWith'; import 'lodash/merge'; import '../_common/js/global-config/mobile/default-config.js'; import '../_common/js/global-config/mobile/locale/zh_CN.js'; import '../_chunks/dep-d5364bc4.js'; import '@babel/runtime/helpers/typeof'; import '../_chunks/dep-eb734424.js'; import 'dayjs'; import '../popup/popup.js'; import 'tdesign-icons-vue-next'; import '../popup/props.js'; import '../overlay/index.js'; import '../overlay/overlay.js'; import '../overlay/props.js'; import '../hooks/useLockScroll.js'; import '../_util/useTouch.js'; import '../_util/getScrollParent.js'; import '../_util/supportsPassive.js'; import '../hooks/useTeleport.js'; import '../shared/render-tnode.js'; import '../button/button.js'; import '../loading/index.js'; import '../loading/loading.js'; import '../loading/icon/gradient.js'; import '../_common/js/loading/circle-adapter.js'; import '../_common/js/utils/helper.js'; import '@babel/runtime/helpers/objectWithoutProperties'; import 'lodash/isNull'; import 'lodash/isUndefined'; import '../loading/icon/spinner.js'; import '../loading/props.js'; import '../loading/plugin.js'; import '../button/props.js'; import '../form/hooks.js'; import 'lodash/isBoolean'; import '../shared/hover.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; } var prefix = config.prefix; var _Guide = defineComponent({ name: "".concat(prefix, "-guide"), components: { TPopover: Popover, TButton: Button, TPopup: Popup }, props: guideProps, setup: function setup(props, context) { var _toRefs = toRefs(props), current = _toRefs.current, modelValue = _toRefs.modelValue, finishButtonProps = _toRefs.finishButtonProps, hideCounter = _toRefs.hideCounter, hideSkip = _toRefs.hideSkip, steps = _toRefs.steps, zIndex = _toRefs.zIndex; var _useVModel = useVModel(current, modelValue, props.defaultCurrent, props.onChange, "current"), _useVModel2 = _slicedToArray(_useVModel, 2), innerCurrent = _useVModel2[0], setInnerCurrent = _useVModel2[1]; var _useConfig = useConfig("guide"), globalConfig = _useConfig.globalConfig; var guideClass = usePrefixClass("guide"); var LOCK_CLASS = "".concat(guideClass.value, "--lock"); var overlayLayerRef = ref(); var highlightLayerRef = ref(); var referenceLayerRef = ref(); var currentHighlightLayerElm = ref(); var popoverWrapperRef = ref(); var actived = ref(false); var stepsTotal = computed(function () { return steps.value.length; }); var currentStepInfo = computed(function () { return steps.value[innerCurrent.value]; }); var isPopover = computed(function () { return getCurrentCrossProps("mode") === "popover"; }); var popoverVisible = ref(false); var isPopoverCenter = computed(function () { return isPopover.value && currentStepInfo.value.placement === "center"; }); var stepProps = computed(function () { if (isPopover.value) { return _objectSpread({ visible: popoverVisible.value, placement: isPopoverCenter.value ? "bottom" : currentStepInfo.value.placement, theme: "light", showArrow: false }, currentStepInfo.value.popoverProps); } return { visible: popoverVisible.value, zIndex: zIndex.value, placement: "center", class: "".concat(guideClass.value, "__dialog"), overlayProps: { zIndex: zIndex.value - 1 } }; }); var currentElmIsFixed = computed(function () { return isFixed(currentHighlightLayerElm.value || document.body); }); var getCurrentCrossProps = function getCurrentCrossProps(propsName) { var _currentStepInfo$valu; return (_currentStepInfo$valu = currentStepInfo.value[propsName]) !== null && _currentStepInfo$valu !== void 0 ? _currentStepInfo$valu : props[propsName]; }; var hWithParams = function hWithParams() { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { currentStepInfo: currentStepInfo.value }; var newH = function newH() { return h.apply(void 0, arguments); }; return Object.assign(newH, params); }; var currentCustomHighlightContent = computed(function () { var highlightContent = currentStepInfo.value.highlightContent; var node = highlightContent; if (isFunction(highlightContent)) { node = highlightContent(hWithParams()); } else if (context.slots.highlightContent) { node = context.slots.highlightContent(hWithParams()); } else if (context.slots["highlight-content"]) { node = context.slots["highlight-content"](hWithParams()); } if (node) { if (!node.props) node.props = {}; node.props.class = node.props.class || ""; } return node; }); var showCustomHighlightContent = computed(function () { return Boolean(currentCustomHighlightContent.value && isPopover.value); }); var showOverlay = computed(function () { return getCurrentCrossProps("showOverlay"); }); var highlightClass = computed(function () { return ["".concat(guideClass.value, "__highlight"), "".concat(guideClass.value, "__highlight--").concat(isPopover.value ? "popover" : "dialog"), "".concat(guideClass.value, "--").concat(currentElmIsFixed.value && isPopover.value ? "fixed" : "absolute")]; }); var maskClass = computed(function () { return ["".concat(guideClass.value, "__highlight--").concat(showOverlay.value ? "mask" : "nomask")]; }); var wrapperClass = computed(function () { return ["".concat(guideClass.value, "__wrapper"), "".concat(guideClass.value, "--").concat(currentElmIsFixed.value ? "fixed" : "absolute"), _defineProperty({}, "".concat(guideClass.value, "__wrapper--content"), !!currentStepInfo.value.content)]; }); var popoverClass = computed(function () { return ["".concat(guideClass.value, "__reference")]; }); var contetnClass = computed(function () { return ["".concat(guideClass.value, "__content--").concat(isPopover.value ? "popover" : "dialog")]; }); var footerClass = computed(function () { return ["".concat(guideClass.value, "__footer"), "".concat(guideClass.value, "__footer--").concat(isPopover.value ? "popover" : "dialog")]; }); var isLast = computed(function () { return innerCurrent.value === stepsTotal.value - 1; }); var buttonSize = computed(function () { return isPopover.value ? "extra-small" : "medium"; }); var setHighlightLayerPosition = function setHighlightLayerPosition(highlightLayer) { var isReference = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var _getRelativePosition = getRelativePosition(currentHighlightLayerElm.value), top = _getRelativePosition.top, left = _getRelativePosition.left; var _currentHighlightLaye = currentHighlightLayerElm.value.getBoundingClientRect(), width = _currentHighlightLaye.width, height = _currentHighlightLaye.height; var highlightPadding = getCurrentCrossProps("highlightPadding"); if (isPopover.value) { width += highlightPadding * 2; height += highlightPadding * 2; top -= highlightPadding; left -= highlightPadding; } else { var _getWindowScroll = getWindowScroll(), scrollTop = _getWindowScroll.scrollTop, scrollLeft = _getWindowScroll.scrollLeft; top += scrollTop; left += scrollLeft; } var style = { top: "".concat(top, "px"), left: "".concat(left, "px") }; if (showCustomHighlightContent.value) { if (isReference) { var _highlightLayerRef$va = highlightLayerRef.value.getBoundingClientRect(), width2 = _highlightLayerRef$va.width, height2 = _highlightLayerRef$va.height; Object.assign(style, { width: "".concat(width2, "px"), height: "".concat(height2, "px") }); } else { Object.assign(style, { width: "auto", height: "auto" }); } } else { Object.assign(style, { width: "".concat(width, "px"), height: "".concat(height, "px") }); } setStyle(highlightLayer, style); }; var setReferenceFullW = function setReferenceFullW(referenceElements) { var style = { left: 0, width: "100vw" }; referenceElements.forEach(function (elem) { return setStyle(elem, style); }); }; var showPopoverGuide = function showPopoverGuide() { nextTick(function () { currentHighlightLayerElm.value = getTargetElm(currentStepInfo.value.element); if (!currentHighlightLayerElm.value) return; scrollToParentVisibleArea(currentHighlightLayerElm.value); setHighlightLayerPosition(highlightLayerRef.value); setHighlightLayerPosition(popoverWrapperRef.value, true); setHighlightLayerPosition(referenceLayerRef.value, true); scrollToElm(currentHighlightLayerElm.value); isPopoverCenter.value && setReferenceFullW([referenceLayerRef.value, popoverWrapperRef.value]); }); }; var showDialogGuide = function showDialogGuide() { nextTick(function () { currentHighlightLayerElm.value = getTargetElm(currentStepInfo.value.element); scrollToParentVisibleArea(currentHighlightLayerElm.value); setHighlightLayerPosition(highlightLayerRef.value); scrollToElm(currentHighlightLayerElm.value); }); }; var showGuide = function showGuide() { if (isPopover.value) { showPopoverGuide(); } else { showDialogGuide(); } nextTick(function () { popoverVisible.value = true; }); }; var destroyGuide = function destroyGuide() { var _highlightLayerRef$va2, _overlayLayerRef$valu; (_highlightLayerRef$va2 = highlightLayerRef.value) === null || _highlightLayerRef$va2 === void 0 || _highlightLayerRef$va2.parentNode.removeChild(highlightLayerRef.value); (_overlayLayerRef$valu = overlayLayerRef.value) === null || _overlayLayerRef$valu === void 0 || _overlayLayerRef$valu.parentNode.removeChild(overlayLayerRef.value); removeClass(document.body, LOCK_CLASS); }; var renderButtonContent = function renderButtonContent(buttonProps, defaultContent) { var _ref2 = buttonProps || {}, content = _ref2.content; return isFunction(content) ? content(h) : content || defaultContent; }; var handleSkip = function handleSkip(e) { var _props$onSkip; var total = stepsTotal.value; actived.value = false; setInnerCurrent(-1, { e: e, total: total }); (_props$onSkip = props.onSkip) === null || _props$onSkip === void 0 || _props$onSkip.call(props, { e: e, current: innerCurrent.value, total: total }); }; var handleNext = function handleNext(e) { var _props$onNextStepClic; var total = stepsTotal.value; setInnerCurrent(innerCurrent.value + 1, { e: e, total: total }); (_props$onNextStepClic = props.onNextStepClick) === null || _props$onNextStepClic === void 0 || _props$onNextStepClic.call(props, { e: e, next: innerCurrent.value + 1, current: innerCurrent.value, total: total }); }; var handleFinish = function handleFinish(e) { var _props$onFinish; var total = stepsTotal.value; actived.value = false; setInnerCurrent(-1, { e: e, total: total }); (_props$onFinish = props.onFinish) === null || _props$onFinish === void 0 || _props$onFinish.call(props, { e: e, current: innerCurrent.value, total: total }); }; var handleBack = function handleBack(e) { var _props$onBack; var total = stepsTotal.value; setInnerCurrent(0, { e: e, total: total }); (_props$onBack = props.onBack) === null || _props$onBack === void 0 || _props$onBack.call(props, { e: e, current: innerCurrent.value, total: total }); }; var contentProps = computed(function () { return { handleSkip: handleSkip, handleNext: handleNext, handleFinish: handleFinish, handleBack: handleBack, current: innerCurrent.value, total: stepsTotal.value }; }); var initGuide = function initGuide() { if (innerCurrent.value >= 0 && innerCurrent.value < steps.value.length) { if (!actived.value) { actived.value = true; addClass(document.body, LOCK_CLASS); } showGuide(); } }; watch(innerCurrent, function (val) { if (val >= 0 && val < stepsTotal.value) { isPopover.value && (popoverVisible.value = false); initGuide(); } else { actived.value = false; destroyGuide(); } }); onMounted(function () { initGuide(); }); return function () { var renderStepContent = function renderStepContent() { var _finishButtonProps$va; var renderTitleNode = function renderTitleNode() { var title = currentStepInfo.value.title; var renderTitle = null; if (isFunction(title)) { renderTitle = title(hWithParams()); } else if (context.slots.title) { renderTitle = context.slots.title(hWithParams()); } else if (typeof title === "string") { renderTitle = title; } else if (title) { renderTitle = h(title); } return renderTitle; }; var renderBodyNode = function renderBodyNode() { var body = currentStepInfo.value.body; var renderBody = null; if (isFunction(body)) { renderBody = body(hWithParams()); } else if (context.slots.body) { renderBody = context.slots.body(hWithParams()); } else if (typeof body === "string") { renderBody = body; } else if (body) { renderBody = h(body); } return renderBody; }; var renderCounterNode = function renderCounterNode() { var params = { total: stepsTotal.value, current: innerCurrent.value }; var renderCounter = null; var counter = props.counter; if (isFunction(counter)) { renderCounter = counter(h, params); } else if (context.slots.counter) { renderCounter = context.slots.counter(hWithParams(params)); } else if (counter) { renderCounter = h(counter, params); } return renderCounter || " (".concat(innerCurrent.value + 1, "/").concat(stepsTotal.value, ")"); }; return createVNode("div", { "class": contetnClass.value }, [createVNode("div", { "class": "".concat(guideClass.value, "__tooltip") }, [createVNode("div", { "class": "".concat(guideClass.value, "__title") }, [renderTitleNode()]), createVNode("div", { "class": "".concat(guideClass.value, "__desc") }, [renderBodyNode()])]), createVNode("div", { "class": footerClass.value }, [!hideSkip.value && !isLast.value && createVNode(Button, mergeProps({ "key": "skip", "class": "".concat(guideClass.value, "__skip"), "theme": "light", "size": buttonSize.value, "variant": "base", "content": globalConfig.value.skip }, getCurrentCrossProps("skipButtonProps"), { "onClick": handleSkip }), null), !isLast.value && createVNode(Button, mergeProps({ "key": "next", "class": "".concat(guideClass.value, "__next"), "theme": "primary", "size": buttonSize.value, "variant": "base" }, getCurrentCrossProps("nextButtonProps"), { "onClick": handleNext }), { content: function content() { return createVNode(Fragment, null, [renderButtonContent(getCurrentCrossProps("nextButtonProps"), globalConfig.value.next), !hideCounter.value && renderCounterNode()]); } }), isLast.value && createVNode(Button, mergeProps({ "key": "back", "class": "".concat(guideClass.value, "__back"), "theme": "light", "size": buttonSize.value, "variant": "base", "content": globalConfig.value.back }, getCurrentCrossProps("backButtonProps"), { "onClick": handleBack }), null), isLast.value && createVNode(Button, mergeProps({ "key": "finish", "class": "".concat(guideClass.value, "__finish"), "theme": "primary", "size": buttonSize.value, "variant": "base" }, (_finishButtonProps$va = finishButtonProps.value) !== null && _finishButtonProps$va !== void 0 ? _finishButtonProps$va : {}, { "onClick": handleFinish }), { content: function content() { return createVNode(Fragment, null, [renderButtonContent(finishButtonProps.value, globalConfig.value.finish), !hideCounter.value && renderCounterNode()]); } })])]); }; var renderContentNode = function renderContentNode() { var content = currentStepInfo.value.content; var renderContent; if (isFunction(content)) { renderContent = content(hWithParams(contentProps.value)); } else if (context.slots.content) { renderContent = context.slots.content(hWithParams(contentProps.value)); } else if (content) { renderContent = h(content, contentProps.value); } return renderContent; }; var renderPopover = function renderPopover() { return createVNode(Popover, stepProps.value, { triggerElement: function triggerElement() { return isPopover.value && createVNode("div", { "ref": referenceLayerRef, "class": _toConsumableArray(popoverClass.value) }, null); }, content: function content() { return isPopover.value && renderContentNode() ? renderContentNode() : renderStepContent(); } }); }; var renderPopup = function renderPopup() { return createVNode(Popup, stepProps.value, { default: function _default() { return isPopover.value && renderContentNode() ? renderContentNode() : renderStepContent(); } }); }; var renderCurrentCustomHighlightContentNode = function renderCurrentCustomHighlightContentNode() { var highlightContent = currentStepInfo.value.highlightContent; var node = highlightContent; if (isFunction(highlightContent)) { node = highlightContent(hWithParams()); } else if (context.slots.highlightContent) { node = context.slots.highlightContent(hWithParams()); } else if (context.slots["highlight-content"]) { node = context.slots["highlight-content"](hWithParams()); } if (node) { if (!node.props) node.props = {}; node.props.class = node.props.class || ""; } return node; }; return createVNode(Fragment, null, [actived.value && createVNode(Teleport, { "to": "body" }, { default: function _default() { return [createVNode("div", { "ref": overlayLayerRef, "class": "".concat(guideClass.value, "__overlay"), "style": { zIndex: "".concat(zIndex.value - 2) } }, null), createVNode("div", { "ref": highlightLayerRef, "class": [].concat(_toConsumableArray(highlightClass.value), _toConsumableArray(maskClass.value)), "style": { zIndex: "".concat(zIndex.value - 1) } }, [showCustomHighlightContent.value && renderCurrentCustomHighlightContentNode()]), createVNode("div", { "ref": popoverWrapperRef, "class": wrapperClass.value, "style": { zIndex: zIndex.value } }, [isPopover.value ? renderPopover() : renderPopup()])]; } })]); }; } }); export { _Guide as default }; //# sourceMappingURL=guide.js.map