@@ -17,28 +17,22 @@ import { ref, computed, watch } from "vue";
17
17
18
18
const store = useStore();
19
19
20
+ /* DATA */
20
21
const treeConfig = ref({ nodeWidth: 175, nodeHeight: 100, levelHeight: 200 });
21
22
const treeData = ref<typeof VueTree.treeData>(null);
22
-
23
- //ref to htmlelement
24
- const tree = ref<typeof VueTree>(null);
25
-
26
- // makes tree available through template refs
27
- defineExpose({ tree });
23
+ const tree = ref<typeof VueTree>(null); //ref to htmlelement
24
+ defineExpose({ tree }); // makes tree available through template refs
28
25
29
26
/* COMPUTED */
30
- const activeComponent = computed(() => store.activeComponent);
27
+ const componentMap = computed(() => store.componentMap);
28
+ const activeComponent = computed(() => store.activeComponent); // to collapse child nodes
29
+ const activeTreeNode = computed(() => store.activeTreeNode); // to drag nodes
31
30
32
- // default state of 'activeRoute' is "HomeView"
33
31
const activeRoute = computed(() => store.activeRoute);
34
32
const routes = computed(() => store.routes);
35
33
36
- // App --> HomeView --> ...
37
- const componentMap = computed(() => store.componentMap);
34
+ /* FUNCTIONS */
38
35
39
- const componentData = componentMap;
40
-
41
- // are there options to desensitize this?
42
36
const zoom = (event: WheelEvent) => {
43
37
if (event.deltaY < 0) {
44
38
tree.value.zoomIn();
@@ -101,7 +95,7 @@ const activateNode = (nodeToActivate: string) => {
101
95
};
102
96
103
97
const buildTree = (componentData: typeof VueTree.treeData) => {
104
- // console.log("component data in buildTree", componentData);
98
+ console.log("component data in buildTree", componentData);
105
99
//App is always the root of the tree.
106
100
const treeData: {
107
101
value: string;
@@ -124,7 +118,7 @@ const buildTree = (componentData: typeof VueTree.treeData) => {
124
118
return treeData;
125
119
};
126
120
127
- function buildTreeChildren(array: string[]) {
121
+ const buildTreeChildren = (array: string[]) => {
128
122
// console.log("array in buildTreeChildren", array);
129
123
if (array.length === 0) {
130
124
return [];
@@ -135,11 +129,11 @@ function buildTreeChildren(array: string[]) {
135
129
value: el,
136
130
children: [],
137
131
};
138
- for (const component in componentData .value) {
132
+ for (const component in componentMap .value) {
139
133
if (component === el) {
140
- if (componentData .value[component].children.length > 0) {
134
+ if (componentMap .value[component].children.length > 0) {
141
135
outputObj.children = buildTreeChildren(
142
- componentData .value[component].children
136
+ componentMap .value[component].children
143
137
);
144
138
}
145
139
}
@@ -148,12 +142,11 @@ function buildTreeChildren(array: string[]) {
148
142
});
149
143
return outputArray;
150
144
}
151
- }
145
+ };
152
146
153
- //data
147
+ // invoke + watch buildTree
154
148
treeData.value = buildTree(componentMap.value);
155
149
156
- //watch
157
150
watch(
158
151
componentMap,
159
152
() => {
@@ -163,38 +156,44 @@ watch(
163
156
{ deep: true }
164
157
);
165
158
166
- /*METHODS FOR DRAG-AND-DROP */
167
- const startDrag = (event: Event) => {
168
- // //add a class to make the html element currently being drag transparent
159
+ // store methods (update state)
160
+ const setActiveTreeNode: typeof store.setActiveTreeNode = (payload) =>
161
+ store.setActiveTreeNode(payload);
162
+
163
+ const setPotentialParentNode: typeof store.setPotentialParentNode = (payload) =>
164
+ store.setPotentialParentNode(payload);
165
+
166
+ const moveNode: typeof store.moveNode = (payload) => store.moveNode(payload);
167
+
168
+ /* METHODS FOR DRAG-AND-DROP */
169
+ const startDrag = (event: Event, activeNode: string) => {
170
+ // add a class to make the activeTreeNode currently being dragged transparent
169
171
(event.target as HTMLElement).classList.add("currentlyDragging");
170
- // const dragId = id;
171
- // //store the id of dragged element
172
- // if (activeComponent.value === "") setSelectedIdDrag(dragId );
173
- // else setIdDrag(dragId);
172
+
173
+ //get current activeTreeNode
174
+ setActiveTreeNode(activeNode );
175
+
174
176
console.log("drag event: ", event);
175
177
};
176
178
177
- const dragEnter = (event: Event, id : string) => {
179
+ const dragEnter = (event: Event, nodeName : string) => {
178
180
// event.preventDefault();
179
- // const dropId = id;
180
- // //store the id of the html element whose location the dragged html element could be dropped upon
181
- // if (activeComponent.value === "") setSelectedIdDrop(dropId);
182
- // else setIdDrop(dropId);
181
+
182
+ // Add componentName to potential new parent (store in state)
183
+ setPotentialParentNode(nodeName);
183
184
};
184
185
185
186
const dragOver = (event: Event) => {
186
- // // needed stop the dragend animation so endDrag is invoked automatically
187
+ //needed stop the dragend animation so endDrag is invoked automatically
187
188
// event.preventDefault();
188
189
};
189
190
190
- const endDrag = (event: Event) => {
191
- // //remove the 'currentlyDragging' class after the HTML is dropped to remove transparency
191
+ const endDrag = (event: Event, activeNode: string) => {
192
192
event.preventDefault();
193
- (event.target as HTMLElement).classList.remove("currentlyDragging");
194
- console.log("drag ended: ", event);
195
- // //invoke the action that will use the idDrag and idDrop to sort the HtmlList
196
- // if (activeComponent.value === "") dragDropSortSelectedHtmlElements();
197
- // else dragDropSortHtmlElements();
193
+
194
+ // add component that is being dragged to children of current potential parent
195
+ // remove component from children list of previous parent
196
+ moveNode(activeNode);
198
197
};
199
198
</script>
200
199
@@ -206,15 +205,17 @@ const endDrag = (event: Event) => {
206
205
:config="treeConfig"
207
206
ref="tree"
208
207
@wheel="zoom"
208
+ @dragover.prevent="dragOver($event), false"
209
209
>
210
210
<template v-slot:node="{ node }">
211
211
<span
212
212
v-if="activeComponent === node.value"
213
213
class="tree-node-active"
214
- @dragstart="startDrag($event)"
214
+ @dragstart="startDrag($event, node.value )"
215
215
draggable="true"
216
216
:stop-propagation="true"
217
- @dragend="endDrag($event)"
217
+ @dragend="endDrag($event, node.value)"
218
+ @dragenter.prevent="dragEnter($event, node.value)"
218
219
>
219
220
{{ node.value }}
220
221
</span>
@@ -228,10 +229,11 @@ const endDrag = (event: Event) => {
228
229
v-else
229
230
class="tree-node"
230
231
@click="activateNode(node.value)"
231
- @dragstart="startDrag($event)"
232
+ @dragstart="startDrag($event, node.value )"
232
233
draggable="true"
233
234
:stop-propagation="true"
234
- @dragend="endDrag($event)"
235
+ @dragend="endDrag($event, node.value)"
236
+ @dragenter.prevent="dragEnter($event, node.value)"
235
237
>
236
238
{{ node.value }}
237
239
</span>
0 commit comments