{"version":3,"file":"resizable-columns2.mjs","names":[],"sources":["../../../../../../packages/components/resizable-columns/src/resizable-columns.vue"],"sourcesContent":["<template>\n  <div\n    ref=\"columnsContainer\"\n    :class=\"columnsKls\"\n    :widget=\"widget\"\n    @mousedown=\"handleMousedown\"\n  >\n    <slot />\n  </div>\n</template>\n\n<script lang=\"ts\">\nimport {\n  computed,\n  defineComponent,\n  getCurrentInstance,\n  onMounted,\n  provide,\n  reactive,\n  ref,\n} from 'vue'\nimport { useNamespace } from '../../../hooks'\nimport { resizableColumnsRootContextKey } from './constants'\nimport { resizableColumnsProps } from './resizable-columns'\nimport { useGlobalConfig } from '../../config-provider'\nimport { useOrderedChildren } from './ordered-children'\n\nimport type { ResizableColumnContext } from './constants'\n\nexport default defineComponent({\n  name: 'ElResizableColumns',\n  props: resizableColumnsProps,\n  emits: ['collapse', 'expand'],\n  setup(props, { emit }) {\n    const forEditor = useGlobalConfig('forEditor')\n    const ns = useNamespace('resizable-columns')\n    const MIN_WIDTH = props.minWidth > 0 ? props.minWidth : 50\n    const columnsContainer = ref(null)\n    const parentWidth = ref(0)\n\n    const {\n      children: columns,\n      addChild: registerColumn,\n      removeChild: unregisterColumn,\n    } = useOrderedChildren<ResizableColumnContext>(\n      getCurrentInstance()!,\n      'ElResizableColumn'\n    )\n\n    const isVertical = computed(() => props.direction === 'vertical')\n    const columnsKls = computed(() => {\n      return [\n        ns.b(),\n        props.direction === 'vertical' ? 'is-vertical' : 'is-horizontal',\n      ]\n    })\n\n    const handleCollapse = (column: ResizableColumnContext) => {\n      if (!column.collapsed) {\n        for (\n          let rIndex = column.index! + 1;\n          rIndex < columns.value.length;\n          rIndex++\n        ) {\n          const rColumn = columns.value[rIndex]\n          if (!rColumn.collapsed) {\n            rColumn.left = rColumn.left! - column.width!\n            rColumn.width = rColumn.width! + column.width!\n            break\n          } else {\n            rColumn.left = rColumn.left! - column.width!\n          }\n        }\n\n        column.collapsed = true\n        emit('collapse', column.index)\n      }\n    }\n    const handleExpand = (column: ResizableColumnContext) => {\n      if (column.collapsed) {\n        column.collapsed = false\n        const rightColumn = columns.value[column.index! + 1]\n        rightColumn.left = rightColumn.left! + column.width!\n        rightColumn.width =\n          rightColumn.width! > column.width! + MIN_WIDTH\n            ? rightColumn.width! - column.width!\n            : MIN_WIDTH\n\n        const allOtherColumns = columns.value.slice(column.index! + 2)\n        const otherWidth =\n          (parentWidth.value! - rightColumn.left! - rightColumn.width!) /\n          allOtherColumns.length\n        allOtherColumns.forEach((item, index) => {\n          item.left =\n            index * otherWidth + rightColumn.left! + rightColumn.width!\n          item.width = otherWidth\n        })\n\n        emit('expand', column.index)\n      }\n    }\n\n    const initColumnSize = () => {\n      let remainWidth = parentWidth.value\n      const usedWidth = {} as any\n      columns.value.forEach((item, index) => {\n        if (item.props.width && !forEditor.value) {\n          let uWidth: any\n          if (item.props.width.endsWith('px')) {\n            uWidth = Number.parseInt(\n              item.props.width.slice(\n                0,\n                Math.max(0, item.props.width.length - 2)\n              )\n            )\n          } else if (item.props.width.endsWith('%')) {\n            uWidth =\n              (Number.parseInt(\n                item.props.width.slice(\n                  0,\n                  Math.max(0, item.props.width.length - 1)\n                )\n              ) /\n                100) *\n              parentWidth.value\n          }\n          if (uWidth > 0) {\n            usedWidth[String(index)] = uWidth\n            remainWidth -= uWidth as any\n          }\n        }\n      })\n\n      let passedWidth = 0\n      columns.value.forEach((item, index) => {\n        if (item.props.maxWidth) {\n          if (item.props.maxWidth.endsWith('px')) {\n            item.maxWidth = Number.parseInt(\n              item.props.maxWidth.slice(\n                0,\n                Math.max(0, item.props.maxWidth.length - 2)\n              )\n            )\n          } else if (item.props.maxWidth.endsWith('%')) {\n            item.maxWidth =\n              (Number.parseInt(\n                item.props.maxWidth.slice(\n                  0,\n                  Math.max(0, item.props.maxWidth.length - 1)\n                )\n              ) /\n                100) *\n              parentWidth.value\n          }\n        }\n\n        item.left = passedWidth\n        if (columns.value.length === 1) {\n          item.width = parentWidth.value\n        } else {\n          if (usedWidth[String(index)]) {\n            item.width = usedWidth[String(index)]\n          } else {\n            item.width =\n              remainWidth /\n              (columns.value.length - Object.keys(usedWidth).length)\n            usedWidth[String(index)] = item.width\n            remainWidth -= item.width\n          }\n        }\n        passedWidth += item.width!\n        item.index = index\n        item.collapsed = false\n      })\n    }\n\n    provide(\n      resizableColumnsRootContextKey,\n      reactive({\n        props,\n        columns,\n        registerColumn,\n        unregisterColumn,\n        handleCollapse,\n        handleExpand,\n        initColumnSize,\n      })\n    )\n\n    const resizeConfig = reactive({\n      last: {\n        pos: 0, // 上一次鼠标位置\n        index: 0, // 上一个调整宽度的resizer的序号\n      },\n    })\n\n    onMounted(() => {\n      // 初始化layout参数\n      if (columnsContainer.value) {\n        // 监听外层容器 dom resize，更新layout中每个panel的width和left\n        const resizeObserver = new ResizeObserver((entries) => {\n          for (const entry of entries) {\n            const width =\n              props.direction === 'horizontal'\n                ? entry.contentRect.width\n                : entry.contentRect.height\n            parentWidth.value = width\n            initColumnSize()\n          }\n        })\n        resizeObserver.observe(columnsContainer.value)\n      }\n    })\n\n    const handleMousedown = (e: any) => {\n      if (forEditor.value) {\n        return\n      }\n      const index = e.target.dataset.index\n      if (index !== null && index !== undefined) {\n        if (columns.value[index].collapsed) {\n          return\n        }\n\n        // 禁止文字选择\n        document.onselectstart = () => false\n        // 禁止元素拖拽\n        document.ondragstart = () => false\n        resizeConfig.last.pos = isVertical.value ? e.clientY : e.clientX\n        resizeConfig.last.index = Number.parseInt(index)\n        document.addEventListener('mousemove', handleMousemove)\n        document.addEventListener('mouseup', handleMouseup)\n      }\n    }\n\n    const handleMousemove = (e: any) => {\n      // 当前resizer的序号\n      const index = resizeConfig.last.index\n      let offsetX =\n        (isVertical.value ? e.clientY : e.clientX) - resizeConfig.last.pos\n      // 找到左边可以调整宽度的panel\n      let leftIndex = index\n      while (leftIndex >= 0 && columns.value[leftIndex].width! <= MIN_WIDTH) {\n        leftIndex--\n      }\n      let rightIndex = index + 1\n      while (\n        rightIndex < columns.value.length &&\n        columns.value[rightIndex].width! <= MIN_WIDTH\n      ) {\n        rightIndex++\n      }\n      // 当左侧panel全部已经最小化则暂停\n      if (leftIndex < 0 && offsetX < 0) {\n        return\n      }\n      // 当右侧panel全部已经最小化则暂停\n      if (rightIndex >= columns.value.length && offsetX > 0) {\n        return\n      }\n      if (offsetX < 0) {\n        // resizer左侧panel宽度\n        const newLeftWidth = columns.value[leftIndex].width! + offsetX\n        const newRightWidth = columns.value[leftIndex + 1].width! - offsetX\n        const rightMaxWidth = columns.value[leftIndex + 1].maxWidth\n\n        if (newLeftWidth <= MIN_WIDTH) {\n          offsetX = MIN_WIDTH - columns.value[leftIndex].width!\n          columns.value[leftIndex].width = MIN_WIDTH\n        } else if (rightMaxWidth > 0 && newRightWidth > rightMaxWidth) {\n          offsetX = rightMaxWidth - columns.value[leftIndex + 1].width!\n          columns.value[leftIndex].width =\n            columns.value[leftIndex].width! + offsetX\n        } else {\n          columns.value[leftIndex].width = newLeftWidth\n        }\n\n        // 修改被折叠的中间panel（最小宽度）的位置，要放在修改offset的语句后面\n        for (let i = index; i > leftIndex; i--) {\n          columns.value[i].left = columns.value[i].left! + offsetX\n        }\n        columns.value[index + 1].width =\n          columns.value[index + 1].width! - offsetX\n        columns.value[index + 1].left = columns.value[index + 1].left! + offsetX\n        resizeConfig.last.pos = isVertical.value ? e.clientY : e.clientX\n      } else if (offsetX > 0) {\n        // resizer右侧panel宽度\n        const newRightWidth = columns.value[rightIndex].width! - offsetX\n        const newLeftWidth = columns.value[rightIndex - 1].width! + offsetX\n        const leftMaxWidth = columns.value[rightIndex - 1].maxWidth\n\n        if (newRightWidth <= MIN_WIDTH) {\n          // 要和上面反过来，因为这边的offset要是>0的\n          offsetX = columns.value[rightIndex].width! - MIN_WIDTH\n          columns.value[rightIndex].width = MIN_WIDTH\n        } else if (leftMaxWidth > 0 && newLeftWidth > leftMaxWidth) {\n          offsetX = leftMaxWidth - columns.value[rightIndex - 1].width!\n          columns.value[rightIndex].width =\n            columns.value[rightIndex].width! - offsetX\n        } else {\n          columns.value[rightIndex].width = newRightWidth\n        }\n        // 修改被折叠的中间panel（最小宽度）的位置\n        for (let i = index + 1; i <= rightIndex; i++) {\n          columns.value[i].left = columns.value[i].left! + offsetX\n        }\n        columns.value[index].width = columns.value[index].width! + offsetX\n        resizeConfig.last.pos = isVertical.value ? e.clientY : e.clientX\n      }\n    }\n\n    const handleMouseup = () => {\n      // 允许文字选择\n      document.onselectstart = null\n      // 允许元素拖拽\n      document.ondragstart = null\n      document.removeEventListener('mousemove', handleMousemove)\n      document.removeEventListener('mousemove', handleMouseup)\n    }\n\n    const setColumnWidth = (columnIndex: number, width: string) => {\n      setTimeout(() => {\n        let remainWidth = parentWidth.value\n        columns.value.forEach((item, index) => {\n          if (columnIndex === index) {\n            if (width.endsWith('px')) {\n              item.width = Number.parseInt(\n                width.slice(0, Math.max(0, width.length - 2))\n              )\n            } else if (width && width.endsWith('%')) {\n              item.width =\n                (Number.parseInt(\n                  width.slice(0, Math.max(0, width.length - 1))\n                ) /\n                  100) *\n                parentWidth.value\n            } else {\n              item.width = remainWidth / (columns.value.length - index)\n            }\n          } else {\n            item.width = remainWidth / (columns.value.length - index)\n          }\n\n          item.left = parentWidth.value - remainWidth\n          remainWidth -= item.width\n        })\n      })\n    }\n\n    const toggleColumnCollapse = (columnIndex: number, collapse?: boolean) => {\n      setTimeout(() => {\n        if (columnIndex >= 0 && columnIndex < columns.value.length - 1) {\n          const column = columns.value[columnIndex]\n          if (column) {\n            const toCol = collapse !== undefined ? collapse : !column.collapsed\n            if (toCol) {\n              handleCollapse(column)\n            } else {\n              handleExpand(column)\n            }\n          }\n        }\n      })\n    }\n\n    return {\n      ns,\n      columnsContainer,\n      columnsKls,\n\n      handleMousedown,\n      setColumnWidth,\n      toggleColumnCollapse,\n    }\n  },\n})\n</script>\n"],"mappings":";;;;;;qBACE,mBAOM,OAAA;EANJ,KAAI;EACH,OAAK,eAAE,KAAA,UAAU;EACjB,QAAQ,KAAA;EACR,aAAS,OAAA,OAAA,OAAA,MAAA,GAAA,SAAE,KAAA,mBAAA,KAAA,gBAAA,GAAA,IAAA;KAEZ,WAAQ,KAAA,QAAA,SAAA,CAAA,GAAA,IAAA,UAAA"}