-
-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathPageTree.vue
More file actions
129 lines (113 loc) · 2.53 KB
/
Copy pathPageTree.vue
File metadata and controls
129 lines (113 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<script>
import Tree from "./Tree.vue";
/**
* @displayName PageTree
* @since 4.0.0
*/
export default {
name: "k-page-tree",
extends: Tree,
inheritAttrs: false,
props: {
current: {
type: String
},
move: {
type: String
},
root: {
default: true,
type: Boolean
}
},
data() {
return {
state: []
};
},
async mounted() {
if (this.items) {
this.state = this.items;
} else {
// temporarily add an item to show loading spinner
this.state = [{ icon: "loader" }];
// load top-level items (e.g. only site)
const items = await this.load(null);
await this.open(items[0]);
// if root is disabled, show the first level of children
this.state = this.root ? items : items[0].children;
// open current recursively, but only trigger from top-level PageTree
if (this.current) {
this.preselect(this.current);
}
}
},
methods: {
findItem(id) {
return this.state.find((item) => this.isItem(item, id));
},
isItem(item, target) {
return (
item.value === target || item.uuid === target || item.id === target
);
},
async load(path) {
return await this.$panel.get("site/tree", {
query: {
move: this.move ?? null,
parent: path
}
});
},
async open(item) {
if (!item) {
return;
}
if (item.hasChildren === false) {
return false;
}
this.$set(item, "loading", true);
// children have not been loaded yet
if (typeof item.children === "string") {
item.children = await this.load(item.children);
}
this.$set(item, "open", true);
this.$set(item, "loading", false);
},
async preselect(page) {
// skip the parents API call
// if the page is already in the loaded items
const existing = this.findItem(page);
if (existing) {
this.$emit("select", existing);
return;
}
// get array of parent uuids/ids
const response = await this.$panel.get("site/tree/parents", {
query: {
page,
root: this.root
}
});
const parents = response.data;
let tree = this;
// go through all parents, try to find the matching item,
// open it and pass forward the pointer to that tree component
for (let index = 0; index < parents.length; index++) {
const value = parents[index];
const item = tree.findItem(value);
if (!item) {
return;
}
await this.open(item);
tree = tree.$refs[value][0];
}
// find current page in deepest tree and trigger select listeners
const item = tree.findItem(page);
if (item) {
this.$emit("select", item);
}
}
}
};
</script>