soselo/views/instance.php

201 lines
5.9 KiB
PHP

<div class="hmax">
<div id="tabs"></div>
<div id="tabs-item" data-id="{id}">
<a href="javascript:window.view.instance.load_content('{id}',true)">
<div class="tab-content">
<i class="fa fa-{icon} fa-fw"></i>
<span>{title}</span>
</div>
</a>
</div>
<div id="content">
<div id="container"></div>
</div>
</div>
<script>
window.view.instance = {
show_config: function() {
window.location.hash = window.location.hash.replace('#instance/','#instance_config/');
},
do: {
new_accounts: {
approve: function(_id) {
window.view.instance.do.new_accounts._perform('approve', _id);
},
reject: function(_id) {
window.view.instance.do.new_accounts._perform('reject', _id);
},
_perform: function(action, _id) {
const hargs = get_hash_arguments();
http.get(`api/v1/http/${hargs.software}/accounts/${action}/?instance=${hargs.instance}&id=${_id}`,
{}, function(js) {
window.view.instance.load_content('new-accounts');
});
},
},
},
load: {
new_accounts: function() {
const hargs = get_hash_arguments();
E.http_template('instance/new-account', function(html) {
E.element('#window-instance #container').innerHTML = html;
http.get(`api/v1/database/${hargs.software}/get-pending-users/?instance=${hargs.instance}`,
{}, function(js) {
E.template('new-accounts', function(TPL) {
if ((js.count === undefined || js.account === null)
|| js.count === 0)
return 'There is not any pending accounts';
var tpl = TPL;
tpl = tpl.replaceAll('{count}', js.count);
tpl = tpl.replaceAll('{id}', js.account.id);
tpl = tpl.replaceAll('{text}', html2text(js.account.text));
tpl = tpl.replaceAll('{email}', html2text(js.account.email));
tpl = tpl.replaceAll('{username}', html2text(js.account.username));
tpl = tpl.replaceAll('{ip_address}', html2text(js.account.sign_up_ip));
return tpl;
});
console.log(js);
});
});
},
filter_users: function() {
E.http_template('instance/filter-users', function(html) {
E.element('#window-instance #container').innerHTML = html;
E.elemid('profile-search-type-simple').onchange = function(e) {
if (e.target.checked) {
E.element('textarea[name="profile"]').setAttribute('placeholder', 'Input your text here');
E.element('textarea[name="profile"]').removeAttribute('disabled');
}
};
E.elemid('profile-search-type-expr').onchange = function(e) {
if (e.target.checked) {
E.element('textarea[name="profile"]').setAttribute('placeholder', '(words "apple banana" AND contains "i like apples") OR words "pineapple strawberry"');
E.element('textarea[name="profile"]').removeAttribute('disabled');
}
};
E.elemid('profile-search-type-empty').onchange = function(e) {
if (e.target.checked) {
E.element('textarea[name="profile"]').setAttribute('placeholder', '<Profile content is empty>');
E.element('textarea[name="profile"]').setAttribute('disabled', 'disabled');
}
}
E.custom.btncollapse_render();
});
},
},
load_content: function(content, hashload) {
hashload = hashload === undefined ? false : hashload;
const _empty_content = function() {
E.element('#window-instance #container').innerHTML = '';
};
switch (content) {
case 'home':
_empty_content();
window.location.hash = '#home';
return;
case 'settings':
_empty_content();
return window.view.instance.show_config();
default:
if (hashload === true)
return set_hash_argument('content', content);
_empty_content();
return window.view.instance
.load[content.replaceAll('-','_')]();
}
},
};
function title__instance() {}
function load__instance(args) {
const hargs = get_hash_arguments();
const _error400 = function(msg) {
msg = msg || 'Parameters are not correct';
console.log('TODO: handle error correctly');
};
const _paint = function() {
const menu = [
{
id: 'home',
title: '',
icon: 'home',
},
{
id: 'filter-users',
title: 'Users filter',
icon: 'user',
},
{
id: 'filter-posts',
title: 'Posts filter',
icon: 'file-text',
},
{
id: 'new-accounts',
title: 'New accounts',
icon: 'user-plus',
},
{
id: 'settings',
title: hargs['instance'],
icon: 'gear',
},
];
E.template('tabs', function(TPL) {
var html = '';
for (var i = 0; i < menu.length; i++) {
var tpl = TPL;
tpl = tpl.replaceAll('{id}', menu[i]['id']);
tpl = tpl.replaceAll('{title}', menu[i]['title']);
tpl = tpl.replaceAll('{icon}', menu[i]['icon']);
html += tpl;
}
return html;
});
if (hargs['content'] !== undefined)
window.view.instance.load_content(hargs['content']);
};
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(`Instance (${hargs['instance']})`);
const config = parse_ini_config(window.vars['current_instance']['config']);
const musthave = window.consts['instance_config'][hargs['software']]['musthave'];
for (var i = 0; i < musthave.length; i++) {
if (config[musthave[i]] === undefined) {
window.view.instance.show_config();
return false;
}
}
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>