UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 5.09 kB
{"version":3,"file":"split-panel2.mjs","names":[],"sources":["../../../../../../packages/components/splitter/src/split-panel.vue"],"sourcesContent":["<script lang=\"ts\" setup>\nimport {\n computed,\n getCurrentInstance,\n inject,\n nextTick,\n onBeforeUnmount,\n reactive,\n ref,\n toRefs,\n watch,\n} from 'vue'\nimport { useNamespace } from '@element-plus/hooks'\nimport { throwError } from '@element-plus/utils'\nimport { getCollapsible, isCollapsible } from './hooks/usePanel'\nimport SplitBar from './split-bar.vue'\nimport { splitterPanelEmits } from './split-panel'\nimport { getPct, getPx, isPct, isPx } from './hooks'\nimport { splitterRootContextKey } from './type'\n\nimport type { SplitterPanelProps } from './split-panel'\n\nconst ns = useNamespace('splitter-panel')\n\nconst COMPONENT_NAME = 'ElSplitterPanel'\ndefineOptions({\n name: COMPONENT_NAME,\n})\n\nconst props = withDefaults(defineProps<SplitterPanelProps>(), {\n resizable: true,\n})\n\nconst emits = defineEmits(splitterPanelEmits)\nconst splitterContext = inject(splitterRootContextKey)\nif (!splitterContext)\n throwError(\n COMPONENT_NAME,\n 'usage: <el-splitter><el-splitter-panel /></el-splitter/>'\n )\n\nconst { panels, layout, lazy, containerSize, pxSizes } = toRefs(splitterContext)\n\nconst {\n registerPanel,\n unregisterPanel,\n onCollapse,\n onMoveEnd,\n onMoveStart,\n onMoving,\n} = splitterContext\n\nconst panelEl = ref<HTMLDivElement>()\nconst instance = getCurrentInstance()!\nconst uid = instance.uid\n\nconst index = ref(0)\nconst panel = computed(() => panels.value[index.value])\n\nconst setIndex = (val: number) => {\n index.value = val\n}\n\nconst panelSize = computed(() => {\n if (!panel.value) return 0\n return pxSizes.value[index.value] ?? 0\n})\n\nconst nextSize = computed(() => {\n if (!panel.value) return 0\n return pxSizes.value[index.value + 1] ?? 0\n})\n\nconst nextPanel = computed(() => {\n if (panel.value) {\n return panels.value[index.value + 1]\n }\n return null\n})\n\nconst isResizable = computed(() => {\n if (!nextPanel.value) return false\n return (\n props.resizable &&\n nextPanel.value?.resizable &&\n // If it is 0, it means it is collapsed => check if the minimum value is set\n (panelSize.value !== 0 || !props.min) &&\n (nextSize.value !== 0 || !nextPanel.value.min)\n )\n})\n\n// The last panel doesn't need a drag bar\nconst isShowBar = computed(() => {\n if (!panel.value) return false\n return index.value !== panels.value.length - 1\n})\n\nconst startCollapsible = computed(() =>\n isCollapsible(panel.value, panelSize.value, nextPanel.value, nextSize.value)\n)\n\nconst endCollapsible = computed(() =>\n isCollapsible(nextPanel.value, nextSize.value, panel.value, panelSize.value)\n)\n\nfunction sizeToPx(str: string | number | undefined) {\n if (isPct(str)) {\n return getPct(str) * containerSize.value || 0\n } else if (isPx(str)) {\n return getPx(str)\n }\n return str ?? 0\n}\n\n// Two-way binding for size\nlet isSizeUpdating = false\nwatch(\n () => props.size,\n () => {\n if (!isSizeUpdating && panel.value) {\n if (!containerSize.value) {\n panel.value.size = props.size\n return\n }\n\n const size = sizeToPx(props.size)\n const maxSize = sizeToPx(props.max)\n const minSize = sizeToPx(props.min)\n\n // Ensure it is within the maximum and minimum value range\n const finalSize = Math.min(Math.max(size, minSize || 0), maxSize || size)\n\n if (finalSize !== size) {\n emits('update:size', finalSize)\n }\n\n panel.value.size = finalSize\n }\n }\n)\n\nwatch(\n () => panel.value?.size,\n (val) => {\n if (val !== props.size) {\n isSizeUpdating = true\n emits('update:size', val as number)\n nextTick(() => (isSizeUpdating = false))\n }\n }\n)\n\nwatch(\n () => props.resizable,\n (val) => {\n if (panel.value) {\n panel.value.resizable = val\n }\n }\n)\n\nconst _panel = reactive({\n uid,\n getVnode: () => instance.vnode,\n setIndex,\n ...props,\n collapsible: computed(() => getCollapsible(props.collapsible)),\n})\n\nregisterPanel(_panel)\n\nonBeforeUnmount(() => unregisterPanel(_panel))\n\ndefineExpose({\n /** @description splitter-panel html element */\n splitterPanelRef: panelEl,\n})\n</script>\n\n<template>\n <div\n ref=\"panelEl\"\n :class=\"[ns.b()]\"\n :style=\"{ flexBasis: `${panelSize}px` }\"\n v-bind=\"$attrs\"\n >\n <slot />\n </div>\n <SplitBar\n v-if=\"isShowBar\"\n :index=\"index\"\n :layout=\"layout\"\n :lazy=\"lazy\"\n :resizable=\"isResizable\"\n :start-collapsible=\"startCollapsible\"\n :end-collapsible=\"endCollapsible\"\n @move-start=\"onMoveStart\"\n @moving=\"onMoving\"\n @move-end=\"onMoveEnd\"\n @collapse=\"onCollapse\"\n >\n <template #start-collapsible>\n <slot name=\"start-collapsible\" />\n </template>\n <template #end-collapsible>\n <slot name=\"end-collapsible\" />\n </template>\n </SplitBar>\n</template>\n"],"mappings":""}