mirror of
https://github.com/remittor/zapret-openwrt.git
synced 2026-01-27 04:40:34 +03:00
luci: service: Rewrite func serviceActionEx
This commit is contained in:
@@ -135,89 +135,51 @@ return view.extend({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
serviceAction: function(action, button) {
|
serviceActionEx: async function(action, button, args = [ ], hide_modal = false, btn_dis = true)
|
||||||
if (button) {
|
{
|
||||||
let elem = document.getElementById(button);
|
let btn = document.getElementById(button);
|
||||||
this.disableButtons(true, elem);
|
this.disableButtons(true, btn);
|
||||||
}
|
|
||||||
poll.stop();
|
poll.stop();
|
||||||
|
let errmsg = null;
|
||||||
let _this = this;
|
try {
|
||||||
|
|
||||||
return tools.handleServiceAction(tools.appName, action)
|
|
||||||
.then(() => {
|
|
||||||
return _this.getAppStatus().then(
|
|
||||||
(status_array) => {
|
|
||||||
_this.setAppStatus(status_array);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
})
|
|
||||||
.catch(e => {
|
|
||||||
ui.addNotification(null, E('p', _('Unable to run service action.') + ' Error: ' + e.message));
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
serviceActionEx: function(action, button, args = [ ], hide_modal = false) {
|
|
||||||
if (button) {
|
|
||||||
let elem = document.getElementById(button);
|
|
||||||
this.disableButtons(true, elem);
|
|
||||||
}
|
|
||||||
poll.stop();
|
|
||||||
|
|
||||||
let _this = this;
|
|
||||||
let exec_cmd = null;
|
let exec_cmd = null;
|
||||||
let exec_arg = [ ];
|
let exec_arg = [ ];
|
||||||
let errmsg = 'ERROR:';
|
|
||||||
if (action == 'start' || action == 'restart') {
|
if (action == 'start' || action == 'restart') {
|
||||||
|
if (tools.checkUnsavedChanges()) {
|
||||||
|
ui.changes.apply(true);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 100));
|
||||||
|
}
|
||||||
exec_cmd = tools.syncCfgPath;
|
exec_cmd = tools.syncCfgPath;
|
||||||
errmsg = _('Unable to run sync_config.sh script.');
|
errmsg = _('Unable to run sync_config.sh script.');
|
||||||
}
|
}
|
||||||
else if (action == 'reset') {
|
if (action == 'reset') {
|
||||||
exec_cmd = tools.defaultCfgPath;
|
exec_cmd = tools.defaultCfgPath;
|
||||||
exec_arg = args; // (reset_ipset)(sync) ==> restore all configs + sync config
|
exec_arg = args; // (reset_ipset)(sync) ==> restore all configs + sync config
|
||||||
errmsg = _('Unable to run restore-def-cfg.sh script.');
|
errmsg = _('Unable to run restore-def-cfg.sh script.');
|
||||||
action = null;
|
action = null;
|
||||||
} else {
|
|
||||||
ui.addNotification(null, E('p', 'ERROR: unknown action'));
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
return fs.exec(exec_cmd, exec_arg)
|
if (exec_cmd) {
|
||||||
.then(function(res) {
|
let res = await fs.exec(exec_cmd, exec_arg);
|
||||||
if (res.code != 0) {
|
if (res.code != 0) {
|
||||||
ui.addNotification(null, E('p', errmsg + ' res.code = ' + res.code));
|
throw Error('res.code = ' + res.code);
|
||||||
action = null; // return with error
|
}
|
||||||
}
|
}
|
||||||
if (hide_modal) {
|
if (hide_modal) {
|
||||||
ui.hideModal();
|
ui.hideModal();
|
||||||
}
|
}
|
||||||
if (!action) {
|
errmsg = null;
|
||||||
return _this.getAppStatus().then(
|
await tools.handleServiceAction(tools.appName, action);
|
||||||
(status_array) => {
|
} catch(e) {
|
||||||
_this.setAppStatus(status_array);
|
let msg = errmsg ? errmsg : _('Unable to run service action') + ' "' + action + '".';
|
||||||
|
ui.addNotification(null, E('p', msg + ' Error: ' + e.message));
|
||||||
|
} finally {
|
||||||
|
if (!poll.active()) {
|
||||||
|
poll.start();
|
||||||
}
|
}
|
||||||
);
|
if (btn && btn_dis) {
|
||||||
|
setTimeout(() => { btn.disabled = true; }, 0);
|
||||||
}
|
}
|
||||||
return _this.serviceAction(action, null);
|
|
||||||
})
|
|
||||||
.catch(e => {
|
|
||||||
ui.addNotification(null, E('p', errmsg + ' Error: ' + e.message));
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
appAction: function(action, button) {
|
|
||||||
if (button) {
|
|
||||||
let elem = document.getElementById(button);
|
|
||||||
this.disableButtons(true, elem);
|
|
||||||
}
|
}
|
||||||
poll.stop();
|
|
||||||
return fs.exec_direct(tools.execPath, [ action ]).then(res => {
|
|
||||||
return this.getAppStatus().then(
|
|
||||||
(status_array) => {
|
|
||||||
this.setAppStatus(status_array);
|
|
||||||
ui.hideModal();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
statusPoll: function() {
|
statusPoll: function() {
|
||||||
@@ -226,7 +188,12 @@ return view.extend({
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
dialogResetCfg: function(ev) {
|
dialogResetCfg: function(ev)
|
||||||
|
{
|
||||||
|
if (tools.checkUnsavedChanges()) {
|
||||||
|
ui.addNotification(null, E('p', _('You have unapplied changes')));
|
||||||
|
return;
|
||||||
|
}
|
||||||
ev.target.blur();
|
ev.target.blur();
|
||||||
|
|
||||||
let reset_base = E('label', [
|
let reset_base = E('label', [
|
||||||
@@ -274,7 +241,7 @@ return view.extend({
|
|||||||
let resetcfg_btn = E('button', {
|
let resetcfg_btn = E('button', {
|
||||||
'class': btn_style_action,
|
'class': btn_style_action,
|
||||||
}, _('Reset settings'));
|
}, _('Reset settings'));
|
||||||
resetcfg_btn.onclick = ui.createHandlerFn(this, () => {
|
resetcfg_btn.onclick = ui.createHandlerFn(this, async () => {
|
||||||
//cancel_button.disabled = true;
|
//cancel_button.disabled = true;
|
||||||
let opt_flags = '';
|
let opt_flags = '';
|
||||||
if (document.getElementById('cfg_reset_base').checked == false) {
|
if (document.getElementById('cfg_reset_base').checked == false) {
|
||||||
@@ -390,9 +357,9 @@ return view.extend({
|
|||||||
};
|
};
|
||||||
|
|
||||||
let btn_enable = create_btn('btn_enable', btn_style_success, _('Enable'));
|
let btn_enable = create_btn('btn_enable', btn_style_success, _('Enable'));
|
||||||
btn_enable.onclick = ui.createHandlerFn(this, this.serviceAction, 'enable', 'btn_enable');
|
btn_enable.onclick = ui.createHandlerFn(this, this.serviceActionEx, 'enable', 'btn_enable');
|
||||||
let btn_disable = create_btn('btn_disable', btn_style_warning, _('Disable'));
|
let btn_disable = create_btn('btn_disable', btn_style_warning, _('Disable'));
|
||||||
btn_disable.onclick = ui.createHandlerFn(this, this.serviceAction, 'disable', 'btn_disable');
|
btn_disable.onclick = ui.createHandlerFn(this, this.serviceActionEx, 'disable', 'btn_disable');
|
||||||
layout_append(_('Service autorun control'), null, [ btn_enable, btn_disable ] );
|
layout_append(_('Service autorun control'), null, [ btn_enable, btn_disable ] );
|
||||||
|
|
||||||
let btn_start = create_btn('btn_start', btn_style_action, _('Start'));
|
let btn_start = create_btn('btn_start', btn_style_action, _('Start'));
|
||||||
@@ -400,7 +367,7 @@ return view.extend({
|
|||||||
let btn_restart = create_btn('btn_restart', btn_style_action, _('Restart'));
|
let btn_restart = create_btn('btn_restart', btn_style_action, _('Restart'));
|
||||||
btn_restart.onclick = ui.createHandlerFn(this, this.serviceActionEx, 'restart', 'btn_restart');
|
btn_restart.onclick = ui.createHandlerFn(this, this.serviceActionEx, 'restart', 'btn_restart');
|
||||||
let btn_stop = create_btn('btn_stop', btn_style_warning, _('Stop'));
|
let btn_stop = create_btn('btn_stop', btn_style_warning, _('Stop'));
|
||||||
btn_stop.onclick = ui.createHandlerFn(this, this.serviceAction, 'stop', 'btn_stop');
|
btn_stop.onclick = ui.createHandlerFn(this, this.serviceActionEx, 'stop', 'btn_stop');
|
||||||
layout_append(_('Service daemons control'), null, [ btn_start, btn_restart, btn_stop ] );
|
layout_append(_('Service daemons control'), null, [ btn_start, btn_restart, btn_stop ] );
|
||||||
|
|
||||||
let btn_reset = create_btn('btn_reset', btn_style_action, _('Reset settings'));
|
let btn_reset = create_btn('btn_reset', btn_style_action, _('Reset settings'));
|
||||||
@@ -428,7 +395,7 @@ return view.extend({
|
|||||||
};
|
};
|
||||||
this.setAppStatus(status_array, elems);
|
this.setAppStatus(status_array, elems);
|
||||||
|
|
||||||
poll.add(L.bind(this.statusPoll, this));
|
poll.add(L.bind(this.statusPoll, this), 2); // interval 2 sec
|
||||||
|
|
||||||
let page_title = tools.AppName;
|
let page_title = tools.AppName;
|
||||||
page_title += '   ';
|
page_title += '   ';
|
||||||
|
|||||||
@@ -149,6 +149,13 @@ return baseclass.extend({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
checkUnsavedChanges: function()
|
||||||
|
{
|
||||||
|
if (!ui.changes) return false;
|
||||||
|
if (!ui.changes.changes) return false;
|
||||||
|
return ui.changes.changes[this.appName] ? true : false;
|
||||||
|
},
|
||||||
|
|
||||||
normalizeValue: function(v) {
|
normalizeValue: function(v) {
|
||||||
return (v && typeof(v) === 'string') ? v.trim().replace(/\r?\n/g, '') : v;
|
return (v && typeof(v) === 'string') ? v.trim().replace(/\r?\n/g, '') : v;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -136,6 +136,10 @@ return baseclass.extend({
|
|||||||
|
|
||||||
openUpdateDialog: function(pkg_arch)
|
openUpdateDialog: function(pkg_arch)
|
||||||
{
|
{
|
||||||
|
if (tools.checkUnsavedChanges()) {
|
||||||
|
ui.addNotification(null, E('p', _('You have unapplied changes')));
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.stage = 0;
|
this.stage = 0;
|
||||||
this.pkg_arch = pkg_arch;
|
this.pkg_arch = pkg_arch;
|
||||||
this.pkg_url = null;
|
this.pkg_url = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user