Skip to content

Commit

Permalink
Move config handlers to config.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Oct 12, 2024
1 parent 9214bfb commit c0671da
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
44 changes: 44 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { WebAccess, WebStorage, IndexedDB } from '@zenfs/dom';
import { Iso } from '@zenfs/iso';
import { Zip } from '@zenfs/zip';
import $ from 'jquery';
import { instantiateTemplate } from './templates.js';
import { randomHex, type Entries } from 'utilium';

export type HTMLAttributeName = 'id' | 'class' | 'style' | 'href' | 'src' | 'alt' | 'title' | 'placeholder';

Expand Down Expand Up @@ -146,3 +148,45 @@ export const backends = [
},
},
] satisfies BackendOption<Backend>[];

$('#config .add').on('click', () => {
const li = instantiateTemplate('#mount').find('li');
const id = randomHex(16);
li.find('input[name=id]').val(id);
const select = li.find('select');

select.on('change', () => {
li.find('[backend_specific]').remove();
const backend = backends.find(({ backend }) => backend.name == select.val());
if (!backend) {
return;
}
for (const [name, data] of Object.entries(backend.inputs) as Entries<typeof backend.inputs>) {
if (!data) {
throw new Error();
}

const d = data as BackendInput;

const input = $(d.select ? '<select></select>' : '<input />').attr({ ...d, select: null, parse: null, name, backend_specific: true });
if (d.select) {
for (const [value, text] of Object.entries(d.select)) {
const opt = $('<option />').text(text).val(value);
if (value == '') {
opt.attr({ disabled: true, selected: true });
}
opt.appendTo(input);
}
}
input.appendTo(li);
d.ready?.(input[0] as BackendInputElement);
}
});

for (const { backend } of backends) {
$('<option />').text(backend.name).val(backend.name).appendTo(select);
}
li.appendTo('#config');
});

$('#config .update').on('click', () => {});
46 changes: 1 addition & 45 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import '@xterm/xterm/css/xterm.css';
import './styles.css';

import $ from 'jquery';
import { randomHex, type Entries } from 'utilium';
import { backends, type BackendInput, type BackendInputElement } from './config.js';
import './config.js';
import './shell.js';
import { instantiateTemplate } from './templates.js';

// Switching tabs
$<HTMLButtonElement>('#nav button').on('click', e => {
Expand All @@ -14,45 +12,3 @@ $<HTMLButtonElement>('#nav button').on('click', e => {
.filter('.tab')
.show();
});

$('#config .add').on('click', () => {
const li = instantiateTemplate('#mount').find('li');
const id = randomHex(16);
li.find('input[name=id]').val(id);
const select = li.find('select');

select.on('change', () => {
li.find('[backend_specific]').remove();
const backend = backends.find(({ backend }) => backend.name == select.val());
if (!backend) {
return;
}
for (const [name, data] of Object.entries(backend.inputs) as Entries<typeof backend.inputs>) {
if (!data) {
throw new Error();
}

const d = data as BackendInput;

const input = $(d.select ? '<select></select>' : '<input />').attr({ ...d, select: null, parse: null, name, backend_specific: true });
if (d.select) {
for (const [value, text] of Object.entries(d.select)) {
const opt = $('<option />').text(text).val(value);
if (value == '') {
opt.attr({ disabled: true, selected: true });
}
opt.appendTo(input);
}
}
input.appendTo(li);
d.ready?.(input[0] as BackendInputElement);
}
});

for (const { backend } of backends) {
$('<option />').text(backend.name).val(backend.name).appendTo(select);
}
li.appendTo('#config');
});

$('#config .update').on('click', () => {});

0 comments on commit c0671da

Please sign in to comment.