-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI_tree.js
279 lines (166 loc) · 6.74 KB
/
UI_tree.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
function is_folder(content){
return Object.keys(content).includes('children')
}
function get_folder_content(path, trees){
if (path === ""){
return trees
}
const search_path = path.split("/")
let content = trees
for (search_folder_name of search_path){
if (content === undefined){
throw `no folders with name ${search_folder_name}, parent doesn't have folders`
}
const located_sub_folders = content.filter(sub_boop => {return sub_boop.name === search_folder_name})
if (located_sub_folders.length > 1){
throw `multiple folders with sanme name: ${search_folder_name}`
}else if (located_sub_folders.length === 0){
throw `no folders with name: ${search_folder_name}`
}
content = located_sub_folders[0].children
}
if (content === undefined){
throw `${path} is a file, not a folder`
}
return content
}
function save_folder(trees, directory,folder_name ){
const new_folder = {name: folder_name, children: ["blank"]} // cant use empty array cause firebase removes it ):
save_content(trees, directory, new_folder)
}
function change_content_order(trees, directory, name, name_to_go_before){
const siblings = get_folder_content(directory, trees)
const indices = [...siblings.keys()]
const from_index = indices.filter(idx => {return siblings[idx].name === name})[0]
if (!from_index){
throw `${name} is not a sheet name`
}
const item = siblings[from_index]
const to_index = indices.filter(idx => {return siblings[idx].name === name_to_go_before})[0]
if (to_index === undefined){
throw `${name_to_go_before} is not a sheet name`
}
siblings.splice(from_index,1)
siblings.splice(to_index,0,item)
}
function arraymove(arr, fromIndex, toIndex) {
var element = arr[fromIndex];
arr.splice(fromIndex, 1);
arr.splice(toIndex, 0, element);
return arr
}
function save_content(trees, directory, content, allow_overwrite = false){
if (typeof content !== "object"){
throw "content must be an object"
}
if (!Object.keys(content).includes("name")){
throw "content must have a name attribute"
}
const siblings = get_folder_content(directory, trees)
const other_names = siblings.map(sibling => {return sibling.name})
const content_idx = other_names.indexOf(content.name)
if (content_idx === -1){
siblings.push(content)
return
}
const competing_content = siblings[content_idx]
const is_conflict = is_folder(content) === is_folder(competing_content)
if (!is_conflict){
siblings.push(content)
return
}
if (!allow_overwrite){
let content_type
if (is_folder(content)){content_type = "folder"}
else{content_type = "sheet"}
throw `${content.name} already a ${content_type} in the directory`
}
siblings[content_idx] = content
}
function delete_content(trees, directory, content_name, should_delete_folder = false){
// could be either a folder or content
const old_siblings = get_folder_content(directory, trees)
const all_contents = old_siblings.filter(sibling => {return sibling.name === content_name})
const contents = all_contents.filter(content => {return is_folder(content) === should_delete_folder})
if (contents.length === 0){
throw `${content_name} not in directory`
}else if (contents.length > 1){
throw "shouldnt happen, shouldnt allow multiple to be saved with same name"
}
const content = contents[0]
const old_idx = old_siblings.indexOf(content)
old_siblings.splice(old_idx, 1)
return content
}
function move_content(trees, old_directory, new_directory, content_name){
// again either a folder or content
const content = delete_content(trees, old_directory, content_name)
save_content(trees, new_directory, content)
}
function replace_UI_tree(trees, container, item_function){
container.innerHTML = ""
const toggle = create_UI_tree_list(trees, item_function)
//return toggle
container.appendChild(toggle)
}
function create_UI_tree_list(trees, item_function, past_names = undefined) {
if (past_names === undefined){
past_names = []
}
const depth = past_names.length
// Create the main container element
const container = document.createElement('div');
container.style.paddingLeft = `${depth*15}px`
for (const tree of trees) {
if (tree === "blank"){
continue
}
subname = tree.name
all_names = [...past_names]
all_names.push(subname)
if (tree.children === undefined){
const item_container = document.createElement("div")
item_function(all_names, item_container)
container.appendChild(item_container)
continue
}
// Create the arrow button
const arrowButton = document.createElement('button');
arrowButton.innerText = '▶';
// Add CSS class to the button
arrowButton.classList.add('library-arrow-button');
// Create the text element
const textElement = document.createElement('span');
textElement.innerText = subname;
const boop = document.createElement("div")
boop.appendChild(arrowButton)
boop.appendChild(textElement)
// Create the toggle div
const toggleDiv = document.createElement('div');
toggleDiv.style.display = 'none';
if (tree.children === undefined){
item_function(all_names, toggleDiv)
}else{
const child_dropdown = create_UI_tree_list(tree.children, item_function, all_names)
toggleDiv.appendChild(child_dropdown)
}
toggleDiv.classList.add('toggle-div'); // Add a CSS class for the toggle div
// Function to toggle the visibility of the toggle div
function toggleDivVisibility() {
if (toggleDiv.style.display === 'none') {
toggleDiv.style.display = 'block';
arrowButton.innerText = '▼';
} else {
toggleDiv.style.display = 'none';
arrowButton.innerText = '▶';
}
}
// Append the arrow button and text element to the container
container.appendChild(boop);
// Add event listener to the arrow button to toggle the div visibility
arrowButton.addEventListener('click', toggleDivVisibility);
// Append the toggle div to the container
container.appendChild(toggleDiv);
}
return container;
}