hongluan-ui
Version:
Hongluan Component Library for Vue 3
335 lines (332 loc) • 12.6 kB
JavaScript
import { defineComponent, inject, ref, getCurrentInstance, provide, watch, nextTick, resolveComponent, withDirectives, openBlock, createElementBlock, normalizeClass, withModifiers, createElementVNode, normalizeStyle, renderSlot, createVNode, withCtx, createCommentVNode, createBlock, createTextVNode, toDisplayString, Fragment, renderList, createSlots, normalizeProps, guardReactiveProps, vShow } from 'vue';
import { isFunction, isString } from '@vue/shared';
import { HlCheckbox } from '../../checkbox/index.mjs';
import { HlSpinner } from '../../spinner/index.mjs';
import { HlIcon } from '../../icon/index.mjs';
import '../../system-icon/index.mjs';
import { HlCollapseTransition } from '../../collapse-transition/index.mjs';
import '../../../utils/index.mjs';
import _sfc_main$1 from './tree-node-content.mjs';
import { getNodeKey, handleCurrentChange } from './model/util.mjs';
import { useNodeExpandEventBroadcast } from './model/useNodeExpandEventBroadcast.mjs';
import { dragEventsKey } from './model/useDragNode.mjs';
import Node from './model/node.mjs';
import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
import SystemCaret from '../../system-icon/src/caret.mjs';
import { debugWarn } from '../../../utils/error.mjs';
const _sfc_main = defineComponent({
name: "TreeNode",
components: {
CollapseTransition: HlCollapseTransition,
HlSpinner,
HlCheckbox,
NodeContent: _sfc_main$1,
HlIcon,
SystemCaret
},
props: {
node: {
type: Node,
default: () => ({})
},
props: {
type: Object,
default: () => ({})
},
accordion: Boolean,
renderContent: Function,
renderAfterExpand: Boolean,
showCheckbox: {
type: Boolean,
default: false
},
checkboxDisabled: {
type: Boolean,
default: false
}
},
emits: ["node-expand"],
setup(props, ctx) {
const { broadcastExpanded } = useNodeExpandEventBroadcast(props);
const tree = inject("RootTree");
const expanded = ref(false);
const childNodeRendered = ref(false);
const oldChecked = ref(null);
const oldIndeterminate = ref(null);
const node$ = ref(null);
const dragEvents = inject(dragEventsKey);
const instance = getCurrentInstance();
provide("NodeInstance", instance);
if (!tree) {
debugWarn("Tree", "Can not find node's tree.");
}
if (props.node.expanded) {
expanded.value = true;
childNodeRendered.value = true;
}
const childrenKey = tree.props.props["children"] || "children";
watch(() => {
const children = props.node.data[childrenKey];
return children && [...children];
}, () => {
props.node.updateChildren();
});
watch(() => props.node.indeterminate, (val) => {
handleSelectChange(props.node.checked, val);
});
watch(() => props.node.checked, (val) => {
handleSelectChange(val, props.node.indeterminate);
});
watch(() => props.node.childNodes.length, () => props.node.reInitChecked());
watch(() => props.node.expanded, (val) => {
nextTick(() => expanded.value = val);
if (val) {
childNodeRendered.value = true;
}
});
const getNodeKey$1 = (node) => {
return getNodeKey(tree.props.nodeKey, node.data);
};
const getNodeClass = (node) => {
const nodeClassFunc = props.props.class;
if (!nodeClassFunc) {
return {};
}
let className;
if (isFunction(nodeClassFunc)) {
const { data } = node;
className = nodeClassFunc(data, node);
} else {
className = nodeClassFunc;
}
if (isString(className)) {
return { [className]: true };
} else {
return className;
}
};
const handleSelectChange = (checked, indeterminate) => {
if (oldChecked.value !== checked || oldIndeterminate.value !== indeterminate) {
tree.ctx.emit("check-change", props.node.data, checked, indeterminate);
}
oldChecked.value = checked;
oldIndeterminate.value = indeterminate;
};
const handleClick = (e) => {
handleCurrentChange(tree.store, tree.ctx.emit, () => tree.store.value.setCurrentNode(props.node));
tree.currentNode.value = props.node;
if (tree.props.expandOnClickNode) {
handleExpandIconClick();
}
if (tree.props.checkOnClickNode && !props.node.disabled && !props.checkboxDisabled) {
handleCheckChange(null, {
target: { checked: !props.node.checked }
});
}
tree.ctx.emit("node-click", props.node.data, props.node, instance, e);
};
const handleContextMenu = (event) => {
if (tree.instance.vnode.props["onNodeContextmenu"]) {
event.stopPropagation();
event.preventDefault();
}
tree.ctx.emit("node-contextmenu", event, props.node.data, props.node, instance);
};
const handleExpandIconClick = () => {
if (props.node.isLeaf)
return;
if (expanded.value) {
tree.ctx.emit("node-collapse", props.node.data, props.node, instance);
props.node.collapse();
} else {
props.node.expand();
ctx.emit("node-expand", props.node.data, props.node, instance);
}
};
const handleCheckChange = (value, ev) => {
props.node.setChecked(ev.target.checked, !tree.props.checkStrictly);
nextTick(() => {
const store = tree.store.value;
tree.ctx.emit("check", props.node.data, {
checkedNodes: store.getCheckedNodes(),
checkedKeys: store.getCheckedKeys(),
halfCheckedNodes: store.getHalfCheckedNodes(),
halfCheckedKeys: store.getHalfCheckedKeys()
});
});
};
const handleChildNodeExpand = (nodeData, node, instance2) => {
broadcastExpanded(node);
tree.ctx.emit("node-expand", nodeData, node, instance2);
};
const handleDragStart = (event) => {
if (!tree.props.draggable)
return;
dragEvents.treeNodeDragStart({ event, treeNode: props });
};
const handleDragOver = (event) => {
event.preventDefault();
if (!tree.props.draggable)
return;
dragEvents.treeNodeDragOver({
event,
treeNode: { $el: node$.value, node: props.node }
});
};
const handleDrop = (event) => {
event.preventDefault();
};
const handleDragEnd = (event) => {
if (!tree.props.draggable)
return;
dragEvents.treeNodeDragEnd(event);
};
return {
node$,
tree,
expanded,
childNodeRendered,
oldChecked,
oldIndeterminate,
getNodeKey: getNodeKey$1,
getNodeClass,
handleSelectChange,
handleClick,
handleContextMenu,
handleExpandIconClick,
handleCheckChange,
handleChildNodeExpand,
handleDragStart,
handleDragOver,
handleDrop,
handleDragEnd
};
}
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
const _component_system_caret = resolveComponent("system-caret");
const _component_hl_icon = resolveComponent("hl-icon");
const _component_hl_checkbox = resolveComponent("hl-checkbox");
const _component_hl_spinner = resolveComponent("hl-spinner");
const _component_node_content = resolveComponent("node-content");
const _component_tree_node = resolveComponent("tree-node", true);
const _component_collapse_transition = resolveComponent("collapse-transition");
return withDirectives((openBlock(), createElementBlock("div", {
ref: "node$",
class: normalizeClass(["tree-node", {
"is-expanded": _ctx.expanded,
"is-current": _ctx.node.isCurrent,
"is-hidden": !_ctx.node.visible,
"is-focusable": !_ctx.node.disabled && !_ctx.checkboxDisabled,
"is-checked": !_ctx.node.disabled && !_ctx.checkboxDisabled && _ctx.node.checked,
..._ctx.getNodeClass(_ctx.node)
}]),
role: "treeitem",
tabindex: "-1",
"aria-expanded": _ctx.expanded,
"aria-disabled": _ctx.node.disabled || _ctx.checkboxDisabled,
"aria-checked": _ctx.node.checked,
draggable: _ctx.tree.props.draggable,
"data-key": _ctx.getNodeKey(_ctx.node),
onClick: withModifiers(_ctx.handleClick, ["stop"]),
onContextmenu: _ctx.handleContextMenu,
onDragstart: withModifiers(_ctx.handleDragStart, ["stop"]),
onDragover: withModifiers(_ctx.handleDragOver, ["stop"]),
onDragend: withModifiers(_ctx.handleDragEnd, ["stop"]),
onDrop: withModifiers(_ctx.handleDrop, ["stop"])
}, [
createElementVNode("div", {
class: "tree-node-content",
style: normalizeStyle({ paddingLeft: (_ctx.node.level - 1) * _ctx.tree.props.indent + "em" })
}, [
createElementVNode("span", {
class: normalizeClass([
{
"custom-icon": !!_ctx.$slots.icon,
"is-leaf": _ctx.node.isLeaf,
expanded: !_ctx.node.isLeaf && _ctx.expanded
},
"tree-node-arrow",
_ctx.tree.props.iconClass ? _ctx.tree.props.iconClass : ""
]),
onClick: withModifiers(_ctx.handleExpandIconClick, ["stop"])
}, [
renderSlot(_ctx.$slots, "icon", {
expanded: _ctx.expanded,
isLeaf: _ctx.node.isLeaf
}, () => [
createVNode(_component_hl_icon, null, {
default: withCtx(() => [
createVNode(_component_system_caret)
]),
_: 1
})
])
], 10, ["onClick"]),
createCommentVNode("\u4FEE\u590Dtree\u5D4C\u5165form-item\u7EC4\u4EF6\uFF0C\u5BFC\u81F4label\u6E32\u67D3\u6210span\uFF0C\u70B9\u51FBcheckbox\u4E0D\u80FD\u9009\u4E2D\u95EE\u9898"),
_ctx.showCheckbox ? (openBlock(), createBlock(_component_hl_checkbox, {
key: 0,
style: normalizeStyle(`visibility: ${_ctx.node[_ctx.props.hideCheckbox] ? "hidden" : "visible"}`),
"model-value": _ctx.node.checked,
indeterminate: _ctx.node.indeterminate,
disabled: !!_ctx.node.disabled || _ctx.checkboxDisabled,
"show-label": false,
onClick: withModifiers(() => {
}, ["stop"]),
onChange: _ctx.handleCheckChange
}, {
default: withCtx(() => [
createTextVNode(toDisplayString(""))
]),
_: 1
}, 8, ["style", "model-value", "indeterminate", "disabled", "onClick", "onChange"])) : createCommentVNode("v-if", true),
_ctx.node.loading ? (openBlock(), createBlock(_component_hl_spinner, {
key: 1,
class: "tree-node-loading-icon"
})) : createCommentVNode("v-if", true),
createVNode(_component_node_content, {
node: _ctx.node,
"render-content": _ctx.renderContent
}, null, 8, ["node", "render-content"])
], 4),
createVNode(_component_collapse_transition, null, {
default: withCtx(() => [
!_ctx.renderAfterExpand || _ctx.childNodeRendered ? withDirectives((openBlock(), createElementBlock("div", {
key: 0,
class: "tree-node-children",
role: "group",
"aria-expanded": _ctx.expanded
}, [
(openBlock(true), createElementBlock(Fragment, null, renderList(_ctx.node.childNodes, (child) => {
return openBlock(), createBlock(_component_tree_node, {
key: _ctx.getNodeKey(child),
"render-content": _ctx.renderContent,
"render-after-expand": _ctx.renderAfterExpand,
"show-checkbox": _ctx.showCheckbox,
"checkbox-disabled": _ctx.checkboxDisabled,
node: child,
accordion: _ctx.accordion,
props: _ctx.props,
onNodeExpand: _ctx.handleChildNodeExpand
}, createSlots({ _: 2 }, [
_ctx.$slots.icon ? {
name: "icon",
fn: withCtx((params) => [
renderSlot(_ctx.$slots, "icon", normalizeProps(guardReactiveProps(params)))
])
} : void 0
]), 1032, ["render-content", "render-after-expand", "show-checkbox", "checkbox-disabled", "node", "accordion", "props", "onNodeExpand"]);
}), 128))
], 8, ["aria-expanded"])), [
[vShow, _ctx.expanded]
]) : createCommentVNode("v-if", true)
]),
_: 3
})
], 42, ["aria-expanded", "aria-disabled", "aria-checked", "draggable", "data-key", "onClick", "onContextmenu", "onDragstart", "onDragover", "onDragend", "onDrop"])), [
[vShow, _ctx.node.visible]
]);
}
var TreeNode = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
export { TreeNode as default };
//# sourceMappingURL=tree-node.mjs.map