soselo/views/instance_config.php

106 lines
3.3 KiB
PHP

<div class="flex hmax">
<form class="center card" method="POST">
<h3 id="title"></h3>
<div id="config"></div>
<br>
<div class="flex">
<button onclick="event.preventDefault();
window.history.back()" class="btn"
><i class="fa fa-arrow-left fa-fw"></i>&nbsp;Cancel</button>
<div style="width:1em"></div>
<button class="btn"
><i class="fa fa-save fa-fw"></i>&nbsp;Save configuration</button>
</div>
</form>
<div id="config-item">
<label for="{field}">{importantMark}{placeholder}:</label>
<input id="config_{field}" name="{field}" class="w100"
placeholder="{field}" type="text"/>
</div>
</div>
<script>
window.view.instance_config = {
autofill_fields: {
mastodon: function(hargs, config) {
E.element('#window-instance_config form').setAttribute('action',
`api/v1/config/put/?software=mastodon&instance=${hargs['instance']}`);
const fields = window.consts['instance_config']['mastodon']['fields'];
for (var i = 0; i < fields.length; i++) {
const f = fields[i].startsWith('*') ? fields[i].substr(1) : fields[i];
if (config[f] !== undefined)
E.elemid(`config_${f}`).value = config[f];
}
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 load__instance_config(args) {
const hargs = get_hash_arguments();
const _error400 = function(msg) {
msg = msg || 'Parameters are not correct';
toast.error(msg);
};
const _paint = function() {
E.template('#window-instance_config #title',
capitalize(hargs['software']) + ` (${hargs['instance']})`);
E.template('config', function(TPL) {
var html = '';
const fields = window.consts['instance_config'][hargs['software']]['fields'];
for (var i = 0; i < fields.length; i++) {
const field = fields[i].startsWith('*') ? fields[i].substr(1) : fields[i];
const important = fields[i].startsWith('*');
var tpl = TPL;
tpl = tpl.replaceAll('{field}', field);
tpl = tpl.replaceAll('{placeholder}', human_field_name(field));
tpl = tpl.replaceAll('{importantMark}', important ? '*' : '');
html += tpl;
}
return html;
});
const config = parse_ini_config(window.vars['current_instance']['config']);
window.view.instance_config.autofill_fields[hargs['software']](hargs, config);
};
const _main = function() {
if (hargs['software'] === undefined ||
!window.vars['instance_config']['supported_ap_software']
.includes(hargs['software']))
return _error400();
if (hargs['instance'] === undefined)
return _error400();
var found = false;
for (var i = 0; i < window.vars['instance_config']['hosts'][hargs['software']].length; i++)
{
const item = window.vars['instance_config']['hosts'][hargs['software']][i];
if (item['instance'] === hargs['instance']) {
window.vars['current_instance'] = item;
found = true;
break;
}
}
if (!found)
return _error400();
apcontrol_title(`Config (${hargs['instance']})`);
return _paint();
};
if (window.vars['instance_config'] === undefined) {
http.get('api/v1/config/get',{},function(data) {
window.vars['instance_config'] = JSON.parse(data);
_main();
});
} else { _main() }
}
</script>