Load current configuration on "instance_config" view

This commit is contained in:
Bofh 2022-11-25 01:44:03 +01:00
parent 06097720f1
commit 4a123a550d
5 changed files with 92 additions and 9 deletions

View File

View File

@ -40,6 +40,12 @@ body {
.center { margin: auto } .center { margin: auto }
.wmax { width: 100vw } .wmax { width: 100vw }
.hmax { height: 100vh } .hmax { height: 100vh }
.w100 { width: 100% }
.h100 { height: 100% }
.card {
border-radius: .5em;
box-shadow: 0px 0px .5em #0000004f;
}
textarea { textarea {
max-width: -moz-available; max-width: -moz-available;
@ -74,4 +80,22 @@ a {
height: 2.5em; height: 2.5em;
} }
#window-instance_config {
background: #d7d7d7;
}
#window-instance_config #title {
margin-top: 0;
}
#window-instance_config form {
background: #fff;
padding: 2.5em 2em;
}
#window-instance_config form .item {
margin-bottom: 1em;
}
#window-instance_config form .item input[type="text"] {
padding: .5em;
margin-top: .4em;
}
</style> </style>

View File

@ -14,7 +14,8 @@
window.consts = { window.consts = {
'instance_config': { 'instance_config': {
'mastodon': { 'mastodon': {
'musthave': ['psql_host','psql_port','psql_db','psql_name','psql_pwd','redis_host','redis_port'], 'musthave': ['instance_url','psql_host','psql_port','psql_db',
'psql_username','psql_password','redis_host','redis_port','api_authkey'],
}, },
} }
}; };

View File

@ -42,10 +42,21 @@ var E = {
} }
}, },
template: function(_id, cback) { template: function(_id, cback) {
var tpl = E.elemid(_id+'-item').outerHTML; var tpl = E.elemid(_id+'-item');
tpl = tpl.replace('id="'+_id+'-item"','class="item"'); if (tpl !== null) {
tpl = tpl.replace('data-src=', 'src='); tpl = tpl.outerHTML;
E.elemid(_id).innerHTML = cback(tpl); tpl = tpl.replace('id="'+_id+'-item"','class="item"');
tpl = tpl.replace('data-src=', 'src=');
}
var elem = null;
elem = E.elemid(_id);
if (elem === null)
elem = E.element(_id);
if (elem === null)
return;
if (typeof cback === 'string')
elem.innerText = cback;
else elem.innerHTML = cback(tpl);
}, },
}; };
@ -123,6 +134,17 @@ function parse_hash_arguments(str,by) {
return args; return args;
} }
function human_field_name(str) {
str = ' '+str+' ';
return capitalize(
str.replaceAll('_', ' ')
.replaceAll(' api ',' API ')
.replaceAll(' db ',' DB ')
.replaceAll(' url ',' URL ')
.replaceAll(' psql ',' Postgres ')
.trim()
);
}
function capitalize(str) { return str[0].toUpperCase() + str.substring(1) } function capitalize(str) { return str[0].toUpperCase() + str.substring(1) }
function printstack() { console.error(new Error().stack) } function printstack() { console.error(new Error().stack) }
function uuidv4() { function uuidv4() {

View File

@ -1,7 +1,32 @@
<div class="flex hmax"> <div class="flex hmax">
<form class="center card">
<h3 id="title"></h3>
<div id="config"></div>
</form>
<div id="config-item">
<label for="{field}">{placeholder}:</label>
<input id="config_{field}" name="{field}" class="w100"
placeholder="{field}" type="text"/>
</div>
</div> </div>
<script> <script>
var autofill_fields = {
mastodon: function(hargs, config) {
const fields = window.consts['instance_config']['mastodon']['musthave'];
for (var i = 0; i < fields.length; i++) {
if (config[fields[i]] !== undefined)
E.elemid(`config_${fields[i]}`).value = config[fields[i]];
}
if (config['instance_url'] === undefined) {
http.get('api/v1/network/resolve-instance?hostname='+hargs['instance'],
{}, function(js) {
if (js['instance'] !== undefined)
E.elemid('config_instance_url').value = js['instance'];
});
}
},
};
function title__instance_config() {} function title__instance_config() {}
function load__instance_config(args) { function load__instance_config(args) {
const hargs = parse_hash_arguments(args[1]); const hargs = parse_hash_arguments(args[1]);
@ -12,8 +37,21 @@ function load__instance_config(args) {
}; };
const _paint = function() { const _paint = function() {
console.log(window.vars['current_instance']); E.template('#window-instance_config #title',
console.log('TODO: handle UI and stuff here'); capitalize(hargs['software']) + ` (${hargs['instance']})`);
E.template('config', function(TPL) {
var html = '';
const fields = window.consts['instance_config'][hargs['software']]['musthave'];
for (var i = 0; i < fields.length; i++) {
var tpl = TPL;
tpl = tpl.replaceAll('{field}', fields[i]);
tpl = tpl.replaceAll('{placeholder}', human_field_name(fields[i]));
html += tpl;
}
return html;
});
const config = parse_ini_config(window.vars['current_instance']['config']);
window.autofill_fields[hargs['software']](hargs, config);
}; };
const _main = function() { const _main = function() {
@ -37,8 +75,6 @@ function load__instance_config(args) {
return _error400(); return _error400();
apcontrol_title(`Config (${hargs['instance']})`); apcontrol_title(`Config (${hargs['instance']})`);
const config = parse_ini_config(window.vars['current_instance']['config']);
console.log(config);
return _paint(); return _paint();
}; };