-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
282 lines (236 loc) · 10.8 KB
/
app.js
File metadata and controls
282 lines (236 loc) · 10.8 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
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
279
280
281
282
'use strict';
// ══════════════════════════════════════════════════════════════════════════════
// app.js — UI, persistence, and bootstrap
//
// Depends on: parser.js, workloads.js
// Load order in index.html: parser.js → workloads.js → app.js
// ══════════════════════════════════════════════════════════════════════════════
// ── Utilities ─────────────────────────────────────────────────────────────
function esc(s) {
return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
}
function getField(id) {
const el = document.getElementById(id);
return el ? el.value : '';
}
// ── Parse preview ──────────────────────────────────────────────────────────
/**
* Populate the <details id="…-preview"> element with a summary of what
* was parsed from the docker run command.
* Called from workloads.js generate() at render time.
*/
function renderPreview(el, p, jobnameOverride, resolvedFramework, wasAuto) {
if (!p.image) { el.style.display = 'none'; return; }
const jobname = jobnameOverride.trim() || imageToJobName(p.image);
const fwOption = FRAMEWORK_OPTIONS.find(o => o.value === resolvedFramework) || FRAMEWORK_OPTIONS[0];
el.style.display = '';
el.innerHTML = `
<summary>
<code>${esc(jobname)}</code> · port <code>${p.ports[0] || '7860'}</code>
· ${fwOption.label}${wasAuto ? ' <small>(auto-detected)</small>' : ''}
</summary>
<dl>
<dt>Image</dt> <dd><code>${esc(p.image)}</code></dd>
<dt>Job name</dt> <dd>${esc(jobname)}</dd>
<dt>Port</dt> <dd>${p.ports[0] || '7860 (default)'}</dd>
<dt>Framework</dt><dd>${fwOption.label}${wasAuto ? ' — auto-detected' : ''}</dd>
${p.command.length ? `<dt>Command</dt><dd><code>${esc(p.command.join(' '))}</code></dd>` : ''}
</dl>`;
}
// ── Copy button SVGs ───────────────────────────────────────────────────────
const SVG_CLIPBOARD = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path d="M8 8m0 2a2 2 0 0 1 2 -2h8a2 2 0 0 1 2 2v8a2 2 0 0 1 -2 2h-8a2 2 0 0 1 -2 -2z"></path>
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
</svg>`;
const SVG_CHECK = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path d="M5 12l5 5l10 -10"></path>
</svg>`;
// ── Output rendering ───────────────────────────────────────────────────────
function renderOutput() {
const raw = document.getElementById('project-code').value.trim();
const outEl = document.getElementById('cmd-out');
const copyEl = document.getElementById('copy-btn');
const wl = WORKLOADS.find(w => w.id === activeId);
if (!raw) {
outEl.innerHTML = '<span class="comment"># Enter your DaSHR project code above.</span>';
setCopyEnabled(false);
return;
}
let result;
try { result = wl.generate.call(wl, raw, getField); }
catch (e) { result = null; }
const isError = !result || result.startsWith('#');
outEl.innerHTML = isError
? `<span class="comment">${esc(result || '# Fill in the fields above.')}</span>`
: esc(result).replace(/ \\$/gm, ' <span class="dim">\\</span>');
setCopyEnabled(!isError);
}
function setCopyEnabled(enabled) {
const el = document.getElementById('copy-btn');
el.style.pointerEvents = enabled ? '' : 'none';
el.style.opacity = enabled ? '0.6' : '0.15';
}
// ── Nav and panels ─────────────────────────────────────────────────────────
let activeId = WORKLOADS[0].id;
function buildNav() {
const ul = document.getElementById('tab-bar');
WORKLOADS.forEach((wl, idx) => {
const li = document.createElement('li');
const a = document.createElement('a');
a.href = '#';
a.textContent = wl.name;
a.dataset.id = wl.id;
if (idx === 0) a.setAttribute('aria-current', 'page');
a.addEventListener('click', e => {
e.preventDefault();
activeId = wl.id;
document.querySelectorAll('#tab-bar a').forEach(b =>
b.dataset.id === wl.id
? b.setAttribute('aria-current', 'page')
: b.removeAttribute('aria-current')
);
document.querySelectorAll('.tab-panel').forEach(p =>
p.classList.toggle('active', p.id === `panel-${wl.id}`)
);
save();
renderOutput();
});
li.appendChild(a);
ul.appendChild(li);
});
}
function buildPanels() {
const container = document.getElementById('panels');
WORKLOADS.forEach((wl, idx) => {
const panel = document.createElement('div');
panel.id = `panel-${wl.id}`;
panel.className = 'tab-panel' + (idx === 0 ? ' active' : '');
panel.innerHTML = `<p><small>${wl.desc}</small></p>` + wl.fields.call(wl);
container.appendChild(panel);
});
}
// ── Project derived text ───────────────────────────────────────────────────
function updateDerived() {
const raw = document.getElementById('project-code').value.trim();
const el = document.getElementById('project-derived');
if (!raw) { el.textContent = ''; return; }
el.textContent = `-p ${lower(raw)} · ${vclaim(raw)}`;
}
// ── Persistence ────────────────────────────────────────────────────────────
function save() {
try {
localStorage.setItem('runai-gen-project', document.getElementById('project-code').value);
localStorage.setItem('runai-gen-active', activeId);
const state = {};
WORKLOADS.forEach(wl => {
state[wl.id] = {};
document.querySelectorAll(`#panel-${wl.id} input, #panel-${wl.id} select, #panel-${wl.id} textarea`)
.forEach(el => { if (el.id) state[wl.id][el.id] = el.value; });
});
localStorage.setItem('runai-gen-fields', JSON.stringify(state));
} catch (_) {}
}
function restore() {
try {
const proj = localStorage.getItem('runai-gen-project');
if (proj) document.getElementById('project-code').value = proj;
const aid = localStorage.getItem('runai-gen-active');
if (aid && WORKLOADS.find(w => w.id === aid)) {
activeId = aid;
document.querySelectorAll('#tab-bar a').forEach(b =>
b.dataset.id === aid ? b.setAttribute('aria-current', 'page') : b.removeAttribute('aria-current')
);
document.querySelectorAll('.tab-panel').forEach(p =>
p.classList.toggle('active', p.id === `panel-${aid}`)
);
}
const savedFields = localStorage.getItem('runai-gen-fields');
if (savedFields) {
const state = JSON.parse(savedFields);
Object.values(state).forEach(fields =>
Object.entries(fields).forEach(([elId, val]) => {
const el = document.getElementById(elId);
if (el) el.value = val;
})
);
}
} catch (_) {}
}
// ── Top-level section nav ──────────────────────────────────────────────────
function buildSectionNav() {
const SECTION_KEY = 'runai-gen-section';
const links = document.querySelectorAll('nav a[data-section]');
const sections = document.querySelectorAll('.section-panel');
function activateSection(id, persist) {
links.forEach(a => {
a.dataset.section === id
? a.setAttribute('aria-current', 'page')
: a.removeAttribute('aria-current');
});
sections.forEach(s => {
s.classList.toggle('active', s.id === `section-${id}`);
});
if (persist) { try { localStorage.setItem(SECTION_KEY, id); } catch (_) {} }
}
links.forEach(a => {
a.addEventListener('click', e => {
e.preventDefault();
activateSection(a.dataset.section, true);
});
});
// Restore last-viewed section
try {
const saved = localStorage.getItem(SECTION_KEY);
if (saved && document.getElementById(`section-${saved}`)) activateSection(saved, false);
} catch (_) {}
}
// ── Bootstrap ──────────────────────────────────────────────────────────────
document.addEventListener('DOMContentLoaded', () => {
buildSectionNav();
buildNav();
buildPanels();
restore();
updateDerived();
renderOutput();
document.getElementById('project-code').addEventListener('input', () => {
updateDerived();
save();
renderOutput();
});
// Any form change in any panel
document.getElementById('panels').addEventListener('input', e => {
// Auto-populate env vars when the docker command changes
if (e.target.id === 'hf-spaces-docker') {
const parsed = parseDockerRun(e.target.value);
if (parsed.envVars.length) autoPopulateEnvVars('hf-spaces', parsed.envVars);
}
save();
renderOutput();
});
});
// ── Copy command ───────────────────────────────────────────────────────────
function copyCmd() {
const text = document.getElementById('cmd-out').innerText;
if (!text || text.trim().startsWith('#')) return;
const btn = document.getElementById('copy-btn');
navigator.clipboard.writeText(text).then(() => {
btn.innerHTML = SVG_CHECK;
btn.setAttribute('data-tooltip', 'Copied!');
btn.style.opacity = '1';
setTimeout(() => {
btn.innerHTML = SVG_CLIPBOARD;
btn.setAttribute('data-tooltip', 'Copy to clipboard');
btn.style.opacity = '0.6';
}, 2000);
}).catch(() => {
// Fallback for non-secure contexts
const ta = document.createElement('textarea');
ta.value = text;
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
document.body.removeChild(ta);
});
}