Files
ruantiblock_openwrt/luci-app-ruantiblock/htdocs/luci-static/resources/view/ruantiblock/cron.js
T

264 lines
7.5 KiB
JavaScript
Raw Normal View History

2020-06-19 20:43:08 +03:00
'use strict';
'require fs';
'require ui';
2021-12-05 19:18:32 +03:00
'require view';
2020-06-19 20:43:08 +03:00
'require view.ruantiblock.tools as tools';
2021-12-05 19:18:32 +03:00
return view.extend({
2021-11-04 18:57:08 +03:00
crontabRegexp: new RegExp(
2021-12-11 23:39:59 +03:00
`^(\\*?\\/?(\\d){0,2}\\s){5}${tools.execPath} update`),
2021-11-04 18:57:08 +03:00
2021-12-11 23:39:59 +03:00
currentCrontabLines: [],
2021-11-04 18:57:08 +03:00
toDD: function(n){
return String(n).replace(/^(\d)$/, "0$1");
},
cronStatusString: function(s) {
return s || _('No Shedule');
},
2021-12-11 23:39:59 +03:00
stringifyRuabTasks: function(str_array) {
let current_tasks = str_array.filter(s => s.match(this.crontabRegexp));
return current_tasks.join('\n');
2021-11-04 18:57:08 +03:00
},
setCronStatus: function(value) {
document.getElementById('cron_status').value = this.cronStatusString(value);
document.getElementById("btn_cron_del").style.visibility = (value) ?
'visible' : 'hidden';
},
writeCronFile: function() {
2021-12-11 23:39:59 +03:00
let btn_cron_add = document.getElementById('btn_cron_add');
let btn_cron_del = document.getElementById('btn_cron_del');
let crontab_string = this.currentCrontabLines.join('\n');
2021-11-04 18:57:08 +03:00
2021-12-11 23:39:59 +03:00
return fs.write(tools.crontabFile, crontab_string).then(rc => {
2021-11-04 18:57:08 +03:00
ui.addNotification(null, E('p',_('Changes have been saved.')), 'info');
2021-12-11 23:39:59 +03:00
this.setCronStatus(this.stringifyRuabTasks(this.currentCrontabLines));
2021-11-04 18:57:08 +03:00
}).then(() => {
return tools.getInitStatus('cron').then(res => {
if(!res) {
return tools.handleServiceAction('cron', 'enable');
};
});
}).finally(() => {
return tools.handleServiceAction('cron', 'restart');
}).catch(e => {
ui.addNotification(null, E('p', _('Unable to save the changes')
+ ': %s [ %s ]'.format(
e.message, tools.crontabFile
)));
});
},
2021-12-11 23:39:59 +03:00
delRuabShedules: function() {
this.currentCrontabLines = this.currentCrontabLines.filter(
s => s.match(this.crontabRegexp) ? false : true);
},
2021-11-04 18:57:08 +03:00
delCronSchedule: function(ev) {
2021-12-11 23:39:59 +03:00
this.delRuabShedules();
2021-11-04 18:57:08 +03:00
return this.writeCronFile();
},
setCronSchedule: function(ev) {
let hour_interval = document.getElementById('cron_hour_interval').value;
let day_interval = document.getElementById('cron_day_interval').value;
let hour = document.getElementById('cron_hour').value;
let min = document.getElementById('cron_min').value;
let task_string = '%s %s %s * * %s update\n'.format(
min,
2021-12-11 23:39:59 +03:00
(!hour_interval) ? hour :
(hour_interval == "1") ?
'*'
:
'*/' + hour_interval,
(hour_interval || day_interval == "1") ?
'*'
:
'*/' + day_interval,
2021-11-04 18:57:08 +03:00
tools.execPath
);
2021-12-11 23:39:59 +03:00
this.delRuabShedules();
this.currentCrontabLines.push(task_string);
2021-11-04 18:57:08 +03:00
return this.writeCronFile();
},
onchangeHourInterval: function(e) {
let value = e.target.value;
let bool = (value != '');
let cron_hour = document.getElementById('cron_hour');
let cron_day_interval = document.getElementById('cron_day_interval');
cron_hour.disabled = bool;
cron_day_interval.disabled = bool;
// For luci-theme-material
if(bool) {
cron_hour.style.opacity = '50%';
cron_day_interval.style.opacity = '50%';
} else {
cron_hour.style.opacity = '100%';
cron_day_interval.style.opacity = '100%';
};
},
load: function() {
2021-12-11 23:39:59 +03:00
return fs.lines(tools.crontabFile).catch(e => {
2021-11-04 18:57:08 +03:00
ui.addNotification(null, E('p', _('Unable to read the contents')
+ ': %s [ %s ]'.format(
e.message, tools.crontabFile
)));
});
},
render: function(content) {
2021-12-11 23:39:59 +03:00
this.currentCrontabLines = content;
let current_task = this.stringifyRuabTasks(content);
2021-11-04 18:57:08 +03:00
let cron_status = E('textarea', {
2021-12-11 23:39:59 +03:00
'id' : 'cron_status',
'name' : 'cron_status',
'style' : 'width:100% !important; padding:5px 10px 5px 10px !important; resize:none !important;',
'readonly' : 'readonly',
'wrap' : 'off',
'rows' : 2,
'spellcheck': 'false',
2021-11-04 18:57:08 +03:00
}, this.cronStatusString(current_task));
let btn_cron_del = E('button', {
'class': 'cbi-button btn cbi-button-reset',
2021-12-11 23:39:59 +03:00
'id' : 'btn_cron_del',
'name' : 'btn_cron_del',
2021-11-04 18:57:08 +03:00
}, _('Reset'));
btn_cron_del.onclick = ui.createHandlerFn(this, this.delCronSchedule);
btn_cron_del.style.visibility = (current_task) ? 'visible' : 'hidden';
let status_header = E('div', { 'class': 'cbi-section-node' }, [
E('div', { 'class': 'cbi-value' }, [
E('label', { 'class': 'cbi-value-title', 'for': 'cron_status' },
_('Current schedule')),
E('div', { 'class': 'cbi-value-field' },
cron_status),
]),
E('div', { 'class': 'cbi-value' }, [
2022-05-23 22:01:50 +03:00
E('label', { 'class': 'cbi-value-title', 'for': 'btn_cron_del_hidden' }),
E('div', { 'class': 'cbi-value-field' }, [
E('div', {}, btn_cron_del),
E('input', {
'id' : 'btn_cron_del_hidden',
'type': 'hidden',
}),
]),
2021-11-04 18:57:08 +03:00
])
]);
let layout = E('div', { 'class': 'cbi-section-node' });
function layout_append(elem, title, descr) {
descr = (descr) ? E('div', { 'class': 'cbi-value-description' }, descr) : '';
layout.append(
E('div', { 'class': 'cbi-value' }, [
E('label', { 'class': 'cbi-value-title', 'for': elem.id || null },
title),
E('div', { 'class': 'cbi-value-field' },
[ elem, descr ]),
])
)
};
layout_append(E('b', {}, _('Interval')));
2021-12-11 23:39:59 +03:00
let cron_hour_interval = E('select', {
'id' : 'cron_hour_interval',
'style': 'width:60px !important; min-width:60px !important',
}, [
2021-11-04 18:57:08 +03:00
E('option', { 'value': '' }, ''),
E('option', { 'value': '1' }, '∗')
]);
for(let i = 2; i <= 12 ; i += 2) {
cron_hour_interval.append(
E('option', { 'value': String(i) }, '&#8727;/' + i)
);
};
layout_append(cron_hour_interval, _('Hour'));
cron_hour_interval.onchange = this.onchangeHourInterval;
2021-12-11 23:39:59 +03:00
let cron_day_interval = E('select', {
'id' : 'cron_day_interval',
'style': 'width:60px !important; min-width:60px !important',
},
E('option', { 'value': '1' }, '&#8727;')
2021-11-04 18:57:08 +03:00
);
for(let i = 2; i < 8 ; i++) {
cron_day_interval.append(
E('option', { 'value': String(i) }, '&#8727;/' + i)
);
};
cron_day_interval.append(E('option', { 'value': '14' }, '&#8727;/14'));
cron_day_interval.append(E('option', { 'value': '28' }, '&#8727;/28'));
layout_append(cron_day_interval, _('Day'));
layout_append(E('b', {}, _('Time')));
2021-12-11 23:39:59 +03:00
let cron_hour = E('select', {
'id' : 'cron_hour',
'style': 'width:60px !important; min-width:60px !important',
});
2021-11-04 18:57:08 +03:00
for(let i = 0; i < 24 ; i++) {
cron_hour.append(
E('option', { 'value': String(i) }, this.toDD(i))
);
};
layout_append(cron_hour, _('Hour'));
2021-12-11 23:39:59 +03:00
let cron_min = E('select', {
'id' : 'cron_min',
'style': 'width:60px !important; min-width:60px !important',
});
2021-11-04 18:57:08 +03:00
for(let i = 0; i < 60 ; i++) {
cron_min.append(
E('option', { 'value': String(i) }, this.toDD(i))
);
};
layout_append(cron_min, _('Minute'));
2022-05-23 22:01:50 +03:00
let btn_cron_add = E('div', { 'class': 'cbi-value' }, [
E('label', { 'class': 'cbi-value-title', 'for': 'btn_cron_add_hidden' }),
E('div', { 'class': 'cbi-value-field' }, [
E('div', {},
E('button', {
'class': 'btn cbi-button-save',
'id' : 'btn_cron_add',
'name' : 'btn_cron_add',
'click': ui.createHandlerFn(this, this.setCronSchedule),
}, _('Set'))
),
E('input', {
'id' : 'btn_cron_add_hidden',
'type': 'hidden',
}),
]),
]);
layout.append(btn_cron_add);
2021-11-04 18:57:08 +03:00
return E([
E('h2',
{ 'class': 'fade-in' },
_('Ruantiblock') + ' - ' + _('Blacklist updates') + ' (cron)'
),
E('div', { 'class': 'cbi-section-descr fade-in' }),
E('div', { 'class': 'cbi-section fade-in' }, status_header),
E('div', { 'class': 'cbi-section fade-in' }, layout),
]);
},
handleSave : null,
handleSaveApply: null,
handleReset : null,
2020-06-19 20:43:08 +03:00
});