Skip to content

Commit 03f7a45

Browse files
author
Jaime de Venecia
committed
saves progress
1 parent 3a54204 commit 03f7a45

File tree

7 files changed

+82
-48
lines changed

7 files changed

+82
-48
lines changed

src/components/right-sidebar/Tree.vue

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,22 @@ import { ref, computed, watch } from "vue";
1717

1818
const store = useStore();
1919

20+
/* DATA */
2021
const treeConfig = ref({ nodeWidth: 175, nodeHeight: 100, levelHeight: 200 });
2122
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
2825

2926
/* 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
3130

32-
// default state of 'activeRoute' is "HomeView"
3331
const activeRoute = computed(() => store.activeRoute);
3432
const routes = computed(() => store.routes);
3533

36-
// App --> HomeView --> ...
37-
const componentMap = computed(() => store.componentMap);
34+
/* FUNCTIONS */
3835

39-
const componentData = componentMap;
40-
41-
// are there options to desensitize this?
4236
const zoom = (event: WheelEvent) => {
4337
if (event.deltaY < 0) {
4438
tree.value.zoomIn();
@@ -101,7 +95,7 @@ const activateNode = (nodeToActivate: string) => {
10195
};
10296

10397
const buildTree = (componentData: typeof VueTree.treeData) => {
104-
// console.log("component data in buildTree", componentData);
98+
console.log("component data in buildTree", componentData);
10599
//App is always the root of the tree.
106100
const treeData: {
107101
value: string;
@@ -124,7 +118,7 @@ const buildTree = (componentData: typeof VueTree.treeData) => {
124118
return treeData;
125119
};
126120

127-
function buildTreeChildren(array: string[]) {
121+
const buildTreeChildren = (array: string[]) => {
128122
// console.log("array in buildTreeChildren", array);
129123
if (array.length === 0) {
130124
return [];
@@ -135,11 +129,11 @@ function buildTreeChildren(array: string[]) {
135129
value: el,
136130
children: [],
137131
};
138-
for (const component in componentData.value) {
132+
for (const component in componentMap.value) {
139133
if (component === el) {
140-
if (componentData.value[component].children.length > 0) {
134+
if (componentMap.value[component].children.length > 0) {
141135
outputObj.children = buildTreeChildren(
142-
componentData.value[component].children
136+
componentMap.value[component].children
143137
);
144138
}
145139
}
@@ -148,12 +142,11 @@ function buildTreeChildren(array: string[]) {
148142
});
149143
return outputArray;
150144
}
151-
}
145+
};
152146

153-
//data
147+
// invoke + watch buildTree
154148
treeData.value = buildTree(componentMap.value);
155149

156-
//watch
157150
watch(
158151
componentMap,
159152
() => {
@@ -163,38 +156,44 @@ watch(
163156
{ deep: true }
164157
);
165158

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
169171
(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+
174176
console.log("drag event: ", event);
175177
};
176178

177-
const dragEnter = (event: Event, id: string) => {
179+
const dragEnter = (event: Event, nodeName: string) => {
178180
// 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);
183184
};
184185

185186
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
187188
// event.preventDefault();
188189
};
189190

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) => {
192192
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);
198197
};
199198
</script>
200199

@@ -206,15 +205,17 @@ const endDrag = (event: Event) => {
206205
:config="treeConfig"
207206
ref="tree"
208207
@wheel="zoom"
208+
@dragover.prevent="dragOver($event), false"
209209
>
210210
<template v-slot:node="{ node }">
211211
<span
212212
v-if="activeComponent === node.value"
213213
class="tree-node-active"
214-
@dragstart="startDrag($event)"
214+
@dragstart="startDrag($event, node.value)"
215215
draggable="true"
216216
:stop-propagation="true"
217-
@dragend="endDrag($event)"
217+
@dragend="endDrag($event, node.value)"
218+
@dragenter.prevent="dragEnter($event, node.value)"
218219
>
219220
{{ node.value }}
220221
</span>
@@ -228,10 +229,11 @@ const endDrag = (event: Event) => {
228229
v-else
229230
class="tree-node"
230231
@click="activateNode(node.value)"
231-
@dragstart="startDrag($event)"
232+
@dragstart="startDrag($event, node.value)"
232233
draggable="true"
233234
:stop-propagation="true"
234-
@dragend="endDrag($event)"
235+
@dragend="endDrag($event, node.value)"
236+
@dragenter.prevent="dragEnter($event, node.value)"
235237
>
236238
{{ node.value }}
237239
</span>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module "@overvue/vue3-tree-chart";

src/layouts/MyLayout.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ q-btn > i {
598598
.switch {
599599
position: relative;
600600
display: block;
601-
vertical-align: top;
601+
// vertical-align: top;
602602
width: 65px;
603603
height: 30px;
604604
padding: 3px;

src/pages/Index2.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import Tree from "../components/right-sidebar/Tree.vue";
1414

1515
<style scoped>
1616
#compDisplay {
17-
/* overflow-x: scroll;
18-
overflow-y: scroll; */
17+
overflow-x: scroll;
18+
overflow-y: scroll;
1919
margin: 0px;
2020
padding: 0px;
2121
height: 100%;

src/store/actions.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,27 @@ const actions: Store<"main", State, {}, Actions> = {
544544
}
545545
this.activeHTML = "";
546546
},
547+
// OverVue v.10.0 –– Drag and Drop for Tree Nodes
548+
setActiveTreeNode(payload) {
549+
this.activeTreeNode = payload;
550+
},
551+
552+
setPotentialParentNode(payload) {
553+
this.potentialParentNode = payload;
554+
},
555+
556+
moveNode(payload) {
557+
558+
console.log('move node: ', payload);
559+
// current node is payload
560+
// potential parent this.potentialParentNode
561+
// update componentMap accordingly
562+
if (this.componentMap[payload]) {
563+
564+
}
565+
566+
// remember to clear potential parent
567+
},
547568

548569
// !Drag-andDrop
549570
//store id of dragged html element in activeComponent

src/store/state/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const newState: types.State = {
3333
componentNameInputValue: "",
3434
projects: [{ filename: "Untitled-1", lastSavedLocation: "" }],
3535

36+
activeTreeNode: "", // OverVue v.10.0 –– Node that is being dragged in tree UI
37+
potentialParentNode: "",
38+
3639
activeRoute: "HomeView",
3740
// need to change to activeComponentName
3841
activeComponent: "",

types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export type State = {
1010
HomeView: RouteComponentMap;
1111
[key: string]: RouteComponentMap | Component;
1212
};
13+
activeTreeNode: string;
14+
potentialParentNode: string;
1315
routes: {
1416
[key: string]: Component[];
1517
};
@@ -121,6 +123,11 @@ export type Actions = {
121123
upOneLayer: (payload: string) => void;
122124
setIdDrag: (payload: string) => void; // idDrag error line 534 of actions.ts
123125
setIdDrop: (payload: string) => void; // idDrag error line 540 of actions.ts
126+
//Overvue v10.0: drag n drop tree
127+
setActiveTreeNode: (payload: string) => void;
128+
moveNode: (payload: string) => void;
129+
setPotentialParentNode: (payload: string) => void;
130+
///////
124131
setSelectedIdDrag: (payload: string) => void;
125132
setSelectedIdDrop: (payload: string) => void;
126133
dragDropSortHtmlElements: () => void;

0 commit comments

Comments
 (0)