-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelements.js
183 lines (152 loc) · 6.23 KB
/
elements.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
const events = {
click: "click",
input: "input",
change: "change"
};
const elements = {
screenProgressDialog: document.querySelector("#screen-progress-dialog"),
screenProgressSpinnerContainer: document.querySelector("#screen-progress-dialog #screen-progress-spinner-container"),
screenProgressBar: document.querySelector("#screen-progress-dialog #screen-progress-bar"),
screenProgressEvent: document.querySelector("#screen-progress-dialog #screen-progress-event"),
screenProgressInputContainer: document.querySelector("#screen-progress-dialog #screen-progress-input-container"),
screenProgressInput: document.querySelector("#screen-progress-dialog input[name='projectName']"),
screenProgressDialogFooter: document.querySelector("#screen-progress-dialog .modal-footer"),
createProjectTrigger: document.querySelector("#screen-progress-dialog .modal-footer button"),
projectNameDisplay: document.querySelector("#project-name-display"),
fs: document.querySelector("#app #fs"),
fsViewer: document.querySelector("#fs #fs-viewer"),
fsViewerNode: document.querySelectorAll("#fs #fs-viewer li"),
editor: document.querySelector("#app #editor"),
// codeArea: document.querySelector("#app #editor code"),
controlPlane: document.querySelector("#app #control-plane"),
createAppTrigger: document.querySelector("#app #control-plane #create-app-trigger"),
createModelTrigger: document.querySelector("#app #control-plane #create-model-trigger"),
downloadZIPTrigger: document.querySelector("#download-zip-trigger"),
}
const dialogs = {
screenProgressDialog: new bootstrap.Modal(elements.screenProgressDialog, {
backdrop: 'static',
}),
}
function toggleFade(element, isVisible) {
if (isVisible) {
// Fade in: Remove `d-none`, add `fade-in`, and remove `fade-out` if it exists
element.classList.remove('d-none', 'fade-out');
element.classList.add('fade-in');
} else {
// Fade out: Add `fade-out`, wait for the transition to complete, then hide
element.classList.remove('fade-in');
element.classList.add('fade-out');
// Wait for the fade-out transition to complete
element.addEventListener('transitionend', () => {
element.classList.add('d-none'); // Hide the element after fade-out
}, { once: true });
}
}
function toggleButton(element, isDisabled) {
if (isDisabled) {
element.setAttribute("disabled", true)
}
else {
element.removeAttribute("disabled")
}
}
function registerEvenets() {
registerCreateProjectFormEvent();
registerCreateProjectEvent();
registerCreateAppEvent();
registerCreateModelEvent();
registerDownloadZIPEvent();
}
function registerCreateProjectFormEvent() {
elements.screenProgressInput.addEventListener(events.input, async () => {
const projectName = elements.screenProgressInput.value
toggleButton(elements.createProjectTrigger, !projectName)
})
}
function registerCreateProjectEvent() {
elements.createProjectTrigger.addEventListener(events.click, async () => {
toggleButton(elements.createProjectTrigger, true);
const projectName = elements.screenProgressInput.value;
await runDjangoAdminCommand("startproject", projectName);
saveValue(storeKeys.projectName, projectName);
dialogs.screenProgressDialog.hide();
loadApplication();
})
}
function loadApplication() {
const projectName = loadValue(storeKeys.projectName);
chdir(projectName);
const rootLevel = listDir("");
elements.projectNameDisplay.textContent = projectName;
for (entry of rootLevel) {
const node = document.createElement("li")
node.classList.add("list-unstyled")
node.textContent = entry
node.dataset.path = entry;
registerViewFileEvent(node);
elements.fsViewer.appendChild(node)
}
// TODO: more resuable and automatic
elements.fsViewerNode = document.querySelectorAll("#fs #fs-viewer li")
}
function registerViewFileEvent(node) {
node.addEventListener(events.click, async (event) => {
event.stopPropagation()
// TODO: check if ./ is needed
const path = "./" + node.dataset.path;
if (await isFile(path)) {
const content = await loadFile(path);
window.editor.setValue(content);
}
else {
const topNode = document.createElement("ul")
node.appendChild(topNode)
for (entry of listDir(path)) {
const childNode = document.createElement("li")
childNode.classList.add("list-unstyled")
childNode.textContent = entry
childNode.dataset.path = `${path}/${entry}`;
// TODO: seems recursive
registerViewFileEvent(childNode);
topNode.appendChild(childNode)
}
}
})
}
function registerCreateAppEvent() {
elements.createAppTrigger.addEventListener(events.click, async () => {
toggleButton(elements.createAppTrigger, true)
const appName = "myapp";
await runDjangoAdminCommand("startapp", appName)
toggleButton(elements.createAppTrigger, false)
// invalidate fs
// TOOD: better
const node = document.createElement("li")
node.classList.add("list-unstyled")
node.textContent = appName
node.dataset.path = appName;
registerViewFileEvent(node);
elements.fsViewer.appendChild(node)
})
}
function registerCreateModelEvent() {
elements.createModelTrigger.addEventListener(events.click, async () => {
toggleButton(elements.createModelTrigger, true)
// if existed then append or maybe create new file XDDDD
await saveFile(path, `
from django import db
class Model(db.Model):
class Meta:
verbose_name = "model"
`);
toggleButton(elements.createModelTrigger, false)
})
}
async function registerDownloadZIPEvent() {
elements.downloadZIPTrigger.addEventListener(events.click, async () => {
toggleButton(elements.downloadZIPTrigger, true)
await downloadProject()
toggleButton(elements.downloadZIPTrigger, false)
})
}