@bimdata/design-system 2.1.0-rc.14 → 2.1.0-rc.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bimdata/design-system",
3
- "version": "2.1.0-rc.14",
3
+ "version": "2.1.0-rc.16",
4
4
  "main": "./src/BIMDataComponents/index.js",
5
5
  "module": "./src/BIMDataComponents/index.js",
6
6
  "files": [
@@ -1,5 +1,4 @@
1
1
  .bimdata-dropdown-list {
2
- height: 42px;
3
2
  position: relative;
4
3
  display: flex;
5
4
  align-items: center;
@@ -1,5 +1,4 @@
1
1
  .bimdata-dropdown {
2
- height: 42px;
3
2
  position: relative;
4
3
  display: flex;
5
4
  align-items: center;
@@ -0,0 +1,123 @@
1
+ <template>
2
+ <div
3
+ class="bimdata-tree"
4
+ @mousedown="onTreeMouseDown"
5
+ @mouseenter="onMouseEnter"
6
+ @mouseleave="onMouseLeave"
7
+ @mouseup.self="onMouseUp"
8
+ >
9
+ <div class="bimdata-tree__nodes" @mouseleave="onNodesMouseLeave">
10
+ <Node v-for="node of data" :key="node.id" :node="node">
11
+ <slot
12
+ name="node"
13
+ :node="node"
14
+ :depth="0"
15
+ :selected="node.id === state.selectedId"
16
+ :hovered="node.id === state.hoveredNode?.id"
17
+ :ancestor-selected="state.hasAncestorSelected(node)"
18
+ />
19
+ </Node>
20
+ </div>
21
+ <div v-if="dropHelperDisplayed" class="bimdata-tree__drop-helper"></div>
22
+ </div>
23
+ </template>
24
+
25
+ <script>
26
+ import { provide, computed, ref } from "vue";
27
+
28
+ import Node from "./Node.vue";
29
+
30
+ import useState from "./state.js";
31
+
32
+ /**
33
+ * @typedef { Object } Node
34
+ * @property { number } id
35
+ * @property { Array } [children=null]
36
+ */
37
+
38
+ export default {
39
+ components: {
40
+ Node,
41
+ },
42
+ provide() {
43
+ return {
44
+ root: this,
45
+ };
46
+ },
47
+ props: {
48
+ /**
49
+ * @type { NodeData }
50
+ */
51
+ data: {
52
+ type: Array,
53
+ required: true,
54
+ validator: value => {
55
+ if (!Array.isArray(value)) return false;
56
+
57
+ const ids = new Set();
58
+
59
+ for (const node of value) {
60
+ if (!Number.isFinite(node?.id)) return false;
61
+ if (node.children && !Array.isArray(node.children)) return false;
62
+ ids.add(node.id);
63
+ }
64
+
65
+ return ids.size === value.length;
66
+ },
67
+ },
68
+ selectedId: {
69
+ type: Number,
70
+ default: null,
71
+ },
72
+ highlightedId: {
73
+ type: Number,
74
+ default: null,
75
+ },
76
+ },
77
+ emits: ["drop", "click", "hover"],
78
+ setup(props, { emit }) {
79
+ const state = useState(props, emit);
80
+
81
+ provide("state", state);
82
+
83
+ const onNodesMouseLeave = () => state.onNodesMouseLeave();
84
+ const onTreeMouseDown = () => state.onTreeMouseDown();
85
+
86
+ const hover = ref(false);
87
+ const onMouseEnter = () => {
88
+ hover.value = true;
89
+ };
90
+ const onMouseLeave = () => {
91
+ hover.value = false;
92
+ };
93
+
94
+ const onMouseUp = () => state.onTreeMouseUp();
95
+
96
+ const dropHelperDisplayed = computed(
97
+ () =>
98
+ hover.value && state.dragPosition !== null && state.hoveredNode === null
99
+ );
100
+
101
+ return {
102
+ state,
103
+ dropHelperDisplayed,
104
+ onMouseEnter,
105
+ onMouseLeave,
106
+ onNodesMouseLeave,
107
+ onTreeMouseDown,
108
+ onMouseUp,
109
+ };
110
+ },
111
+ };
112
+ </script>
113
+
114
+ <style scoped lang="scss">
115
+ .bimdata-tree {
116
+ height: 100%;
117
+
118
+ &__drop-helper {
119
+ height: 2px;
120
+ background-color: var(--color-primary);
121
+ }
122
+ }
123
+ </style>
@@ -0,0 +1,207 @@
1
+ <template>
2
+ <div
3
+ ref="nodeRef"
4
+ class="bimdata-tree__node"
5
+ :class="{
6
+ 'bimdata-tree__node--selected': node.id === state.selectedId,
7
+ 'bimdata-tree__node--ancestor-selected': hasAncestorSelected(node),
8
+ 'bimdata-tree__node--highlighted': node.id === state.highlightedId,
9
+ }"
10
+ @mouseenter="onMouseEnter"
11
+ @mousedown="onMouseDown"
12
+ @mousemove="onMouseMove"
13
+ @mouseup="onMouseUp"
14
+ >
15
+ <div
16
+ v-if="dropHelperPosition === 'top' || dropHelperPosition === 'bottom'"
17
+ class="bimdata-tree__node__drop-helper"
18
+ :style="`--drop-helper-top: ${
19
+ dropHelperPosition === 'top' ? '0px' : '30px'
20
+ };`"
21
+ ></div>
22
+ <div
23
+ class="bimdata-tree__node__content"
24
+ :style="`padding-left: calc(var(--spacing-unit) * 2 * ${depth});`"
25
+ >
26
+ <div class="bimdata-tree__node__content__arrow" @mousedown.stop>
27
+ <div
28
+ v-if="node.children?.length > 0"
29
+ class="bimdata-tree__node__content__arrow__content"
30
+ @click="expanded = !expanded"
31
+ >
32
+ <BIMDataIcon name="chevron" size="xxxs" :rotate="expanded ? 90 : 0" />
33
+ </div>
34
+ </div>
35
+
36
+ <div class="bimdata-tree__node__content__element">
37
+ <slot>
38
+ {{ node.label }}
39
+ </slot>
40
+ </div>
41
+ </div>
42
+ </div>
43
+ <NodeChildren
44
+ v-if="node.children?.length > 0 && expanded"
45
+ :node="node"
46
+ :depth="depth + 1"
47
+ />
48
+ </template>
49
+
50
+ <script>
51
+ import { inject, ref, computed, watch, isRef } from "vue";
52
+
53
+ import NodeChildren from "./NodeChildren.js";
54
+
55
+ export default {
56
+ name: "Node",
57
+ components: {
58
+ NodeChildren,
59
+ },
60
+ inject: ["root"],
61
+ props: {
62
+ node: {
63
+ type: Object,
64
+ required: true,
65
+ },
66
+ depth: {
67
+ type: Number,
68
+ default: 0,
69
+ },
70
+ },
71
+ setup(props) {
72
+ const state = inject("state");
73
+
74
+ let expanded = null;
75
+ if (isRef(props.node.expandedRef)) {
76
+ expanded = props.node.expandedRef;
77
+ } else {
78
+ expanded = ref(false);
79
+ }
80
+ const nodeRef = ref(null);
81
+
82
+ let expandTimer = null;
83
+
84
+ const handleExpandTimer = () => {
85
+ if (
86
+ !props.node.children?.length ||
87
+ !state.dragPosition ||
88
+ state.hoveredNode?.id !== props.node.id ||
89
+ expanded.value
90
+ ) {
91
+ clearTimeout(expandTimer);
92
+ expandTimer = null;
93
+ return;
94
+ }
95
+
96
+ const position = state.getNodeDropPosition(props.node, nodeRef.value);
97
+
98
+ if (position !== "center" && position !== "bottom") {
99
+ clearTimeout(expandTimer);
100
+ expandTimer = null;
101
+ return;
102
+ }
103
+
104
+ if (expandTimer !== null) return;
105
+
106
+ expandTimer = setTimeout(() => {
107
+ expanded.value = true;
108
+ expandTimer = null;
109
+ }, 500);
110
+ };
111
+
112
+ watch(() => state.dragPosition && state.hoveredNode, handleExpandTimer);
113
+
114
+ const onMouseEnter = () => state.onNodeMouseEnter(props.node);
115
+
116
+ const onMouseDown = mouseEvent =>
117
+ state.onNodeMouseDown(props.node, mouseEvent);
118
+
119
+ const onMouseMove = mouseEvent => {
120
+ state.onNodeMouseMove(mouseEvent);
121
+ handleExpandTimer();
122
+ };
123
+
124
+ const dropHelperPosition = computed(() => {
125
+ if (!state.dragPosition || state.hoveredNode?.id !== props.node.id)
126
+ return null;
127
+
128
+ return state.getNodeDropPosition(props.node, nodeRef.value);
129
+ });
130
+
131
+ const onMouseUp = () =>
132
+ state.onNodeMouseUp(props.node, nodeRef.value, expanded.value);
133
+
134
+ return {
135
+ state,
136
+ nodeRef,
137
+ expanded,
138
+ dropHelperPosition,
139
+ hasAncestorSelected: state.hasAncestorSelected,
140
+ onMouseEnter,
141
+ onMouseDown,
142
+ onMouseMove,
143
+ onMouseUp,
144
+ };
145
+ },
146
+ };
147
+ </script>
148
+
149
+ <style scoped lang="scss">
150
+ .bimdata-tree__node {
151
+ height: 32px;
152
+ display: flex;
153
+ align-items: center;
154
+ position: relative;
155
+
156
+ user-select: none;
157
+
158
+ &__drop-helper {
159
+ position: absolute;
160
+ left: 0px;
161
+ right: 0px;
162
+ height: 2px;
163
+ top: var(--drop-helper-top);
164
+ background-color: var(--color-primary);
165
+ }
166
+
167
+ &--selected {
168
+ background-color: var(--color-secondary-light);
169
+ }
170
+ &--ancestor-selected {
171
+ background-color: var(--color-secondary-lighter);
172
+ }
173
+ &--highlighted {
174
+ outline-offset: -1px;
175
+ outline: solid var(--color-primary) 1px;
176
+ }
177
+
178
+ &__content {
179
+ width: 100%;
180
+ height: 100%;
181
+ display: flex;
182
+ border-radius: 6px;
183
+ padding: 0 calc(var(--spacing-unit) / 2);
184
+
185
+ &__arrow {
186
+ width: 18px;
187
+ flex-shrink: 0;
188
+
189
+ &__content {
190
+ cursor: pointer;
191
+ height: 100%;
192
+ display: flex;
193
+ justify-content: center;
194
+ align-items: center;
195
+ }
196
+ }
197
+
198
+ &__element {
199
+ padding-left: calc(var(--spacing-unit) / 2);
200
+ width: calc(100% - 18px);
201
+ height: 100%;
202
+ display: flex;
203
+ align-items: center;
204
+ }
205
+ }
206
+ }
207
+ </style>
@@ -0,0 +1,35 @@
1
+ import { h } from "vue";
2
+
3
+ import Node from "./Node.vue";
4
+
5
+ export default {
6
+ inject: ["root"],
7
+ props: {
8
+ node: {
9
+ type: Object,
10
+ required: true,
11
+ validator: node => Number.isFinite(node?.id),
12
+ },
13
+ depth: {
14
+ type: Number,
15
+ default: 0,
16
+ },
17
+ },
18
+ render() {
19
+ const { node, depth, root } = this;
20
+
21
+ const state = root.state;
22
+
23
+ return node.children.map(child =>
24
+ h(Node, { node: child, depth, key: child.id }, () =>
25
+ root.$slots.node?.({
26
+ node: child,
27
+ depth,
28
+ selected: child.id === state.selectedId,
29
+ hovered: child.id === state.hoveredNode?.id,
30
+ ancestorSelected: state.hasAncestorSelected(child),
31
+ })
32
+ )
33
+ );
34
+ },
35
+ };
@@ -0,0 +1,239 @@
1
+ import { shallowReactive, watch, shallowRef } from "vue";
2
+
3
+ export default function useState(props, emit) {
4
+ const state = shallowReactive({
5
+ selectedId: null,
6
+ highlightedId: null,
7
+ hoveredNode: null,
8
+ trees: props.data,
9
+ dragPosition: null,
10
+ });
11
+
12
+ watch(
13
+ () => props.selectedId,
14
+ () => {
15
+ state.selectedId = props.selectedId ?? null;
16
+ }
17
+ );
18
+
19
+ watch(
20
+ () => props.highlightedId,
21
+ () => {
22
+ state.highlightedId = props.highlightedId ?? null;
23
+ }
24
+ );
25
+
26
+ watch(
27
+ () => state.hoveredNode,
28
+ () => emit("hover", state.hoveredNode)
29
+ );
30
+
31
+ const pressedNode = shallowRef(null);
32
+
33
+ watch(
34
+ () => props.data,
35
+ trees => {
36
+ state.trees = trees;
37
+ }
38
+ );
39
+
40
+ const onMouseUp = () => {
41
+ pressedNode.value = null;
42
+ state.dragPosition = null;
43
+ };
44
+
45
+ const onNodeMouseEnter = node => {
46
+ state.hoveredNode = node;
47
+ };
48
+ const onNodeMouseDown = (node, event) => {
49
+ event.stopPropagation();
50
+
51
+ emit("click", node);
52
+
53
+ pressedNode.value = node;
54
+
55
+ window.addEventListener("mouseup", onMouseUp, { once: true });
56
+ };
57
+ const onNodeMouseMove = event => {
58
+ if (!pressedNode.value) return;
59
+
60
+ const { clientX, clientY } = event;
61
+
62
+ state.dragPosition = { x: clientX, y: clientY };
63
+ };
64
+ const onNodeMouseUp = (node, domElement, expanded) => {
65
+ if (!state.dragPosition) return;
66
+
67
+ if (state.hoveredNode?.id === pressedNode.value?.id) return;
68
+
69
+ if (state.isDescendant(node, pressedNode.value)) return;
70
+
71
+ const position = getNodeDropPosition(node, domElement);
72
+
73
+ let parent = null;
74
+ let index = null;
75
+ const previousParent = getParent(pressedNode.value);
76
+
77
+ switch (position) {
78
+ case "top":
79
+ parent = getParent(node);
80
+ index = (parent?.children ?? state.trees).findIndex(
81
+ child => child.id === node.id
82
+ );
83
+ break;
84
+ case "bottom":
85
+ if (node.children && expanded) {
86
+ parent = node;
87
+ index = 0;
88
+ } else {
89
+ parent = getParent(node);
90
+ index =
91
+ (parent?.children ?? state.trees).findIndex(
92
+ child => child.id === node.id
93
+ ) + 1;
94
+ }
95
+ break;
96
+ case "center":
97
+ parent = node;
98
+ index = 0;
99
+ break;
100
+ }
101
+
102
+ const reorder = parent?.id === previousParent?.id;
103
+ const previousIndex = (previousParent?.children ?? state.trees).findIndex(
104
+ child => child.id === pressedNode.value.id
105
+ );
106
+
107
+ if (
108
+ reorder &&
109
+ (position === "top" || position === "bottom") &&
110
+ previousIndex < index
111
+ ) {
112
+ index--;
113
+ }
114
+
115
+ if (reorder && index === previousIndex) return;
116
+
117
+ emit("drop", {
118
+ node: pressedNode.value,
119
+ previousParent,
120
+ parent,
121
+ index,
122
+ position,
123
+ });
124
+ };
125
+
126
+ const onNodesMouseLeave = () => {
127
+ state.hoveredNode = null;
128
+ };
129
+
130
+ const onTreeMouseDown = () => {
131
+ emit("click", null);
132
+ };
133
+ const onTreeMouseUp = () => {
134
+ if (!state.dragPosition) return;
135
+
136
+ if (state.trees.indexOf(pressedNode.value) === state.trees.length - 1) {
137
+ // to drop the last root node at the end of the list does not fire the drop event
138
+ return;
139
+ }
140
+
141
+ const nodeIsInRootPosition = state.trees.includes(pressedNode.value);
142
+
143
+ emit("drop", {
144
+ node: pressedNode.value,
145
+ parent: null,
146
+ index: nodeIsInRootPosition ? state.trees.length - 1 : state.trees.length,
147
+ previousParent: getParent(pressedNode.value),
148
+ });
149
+ };
150
+
151
+ state.onNodeMouseEnter = onNodeMouseEnter;
152
+ state.onNodeMouseDown = onNodeMouseDown;
153
+ state.onNodeMouseMove = onNodeMouseMove;
154
+ state.onNodeMouseUp = onNodeMouseUp;
155
+
156
+ state.onNodesMouseLeave = onNodesMouseLeave;
157
+
158
+ state.onTreeMouseDown = onTreeMouseDown;
159
+ state.onTreeMouseUp = onTreeMouseUp;
160
+
161
+ // UTILS
162
+
163
+ const getNodeDropPosition = (node, domElement) => {
164
+ const { y } = state.dragPosition;
165
+
166
+ const { top, height } = domElement.getBoundingClientRect();
167
+
168
+ if (node.children) {
169
+ if (y < top + height / 3) return "top";
170
+ if (y > top + (height * 2) / 3) return "bottom";
171
+
172
+ return "center";
173
+ } else {
174
+ if (y < top + height / 2) return "top";
175
+
176
+ return "bottom";
177
+ }
178
+ };
179
+
180
+ const getParent = (node, nodes = state.trees) => {
181
+ for (const childNode of nodes) {
182
+ if (childNode.children?.length) {
183
+ if (childNode.children.some(child => child.id === node.id)) {
184
+ return childNode;
185
+ }
186
+
187
+ const expectedParent = getParent(node, childNode.children);
188
+
189
+ if (expectedParent) return expectedParent;
190
+ }
191
+ }
192
+ };
193
+
194
+ const isAncestor = (node, nodes = state.trees) => {
195
+ for (const childNode of nodes) {
196
+ if (childNode.id === node.id) {
197
+ return true;
198
+ }
199
+ if (childNode.children?.length && isAncestor(node, childNode.children)) {
200
+ return true;
201
+ }
202
+ }
203
+ return false;
204
+ };
205
+
206
+ const getAncestors = (node, nodes = state.trees) => {
207
+ const ancestors = [];
208
+ for (const childNode of nodes) {
209
+ if (childNode.id === node.id) {
210
+ continue;
211
+ }
212
+ if (childNode.children?.length && isAncestor(node, childNode.children)) {
213
+ ancestors.push(childNode, ...getAncestors(node, childNode.children));
214
+ }
215
+ }
216
+ return ancestors;
217
+ };
218
+
219
+ const hasAncestorSelected = node => {
220
+ if (state.selectedId === null) return false;
221
+
222
+ return getAncestors(node).some(
223
+ ancestor => ancestor.id === state.selectedId
224
+ );
225
+ };
226
+
227
+ const isDescendant = (node, ancestor) =>
228
+ ancestor?.children?.some(
229
+ child => child.id === node.id || isDescendant(node, child)
230
+ ) ?? false;
231
+
232
+ state.getNodeDropPosition = getNodeDropPosition;
233
+ state.getParent = getParent;
234
+ state.getAncestors = getAncestors;
235
+ state.hasAncestorSelected = hasAncestorSelected;
236
+ state.isDescendant = isDescendant;
237
+
238
+ return state;
239
+ }
@@ -44,3 +44,4 @@ export { default as BIMDataTextarea } from "./BIMDataTextarea/BIMDataTextarea.vu
44
44
  export { default as BIMDataTextbox } from "./BIMDataTextbox/BIMDataTextbox.vue";
45
45
  export { default as BIMDataToggle } from "./BIMDataToggle/BIMDataToggle.vue";
46
46
  export { default as BIMDataTooltip } from "./BIMDataTooltip/BIMDataTooltip.vue";
47
+ export { default as BIMDataTree } from "./BIMDataTree/BIMDataTree.vue";