treewide: import utility classes explicitly
[project/luci.git] / modules / luci-mod-system / htdocs / luci-static / resources / view / system / startup.js
1 'use strict';
2 'require view';
3 'require rpc';
4 'require fs';
5 'require ui';
6
7 return view.extend({
8 callInitList: rpc.declare({
9 object: 'luci',
10 method: 'getInitList',
11 expect: { '': {} }
12 }),
13
14 callInitAction: rpc.declare({
15 object: 'luci',
16 method: 'setInitAction',
17 params: [ 'name', 'action' ],
18 expect: { result: false }
19 }),
20
21 load: function() {
22 return Promise.all([
23 L.resolveDefault(fs.read('/etc/rc.local'), ''),
24 this.callInitList()
25 ]);
26 },
27
28 handleAction: function(name, action, ev) {
29 return this.callInitAction(name, action).then(function(success) {
30 if (success != true)
31 throw _('Command failed');
32
33 return true;
34 }).catch(function(e) {
35 ui.addNotification(null, E('p', _('Failed to execute "/etc/init.d/%s %s" action: %s').format(name, action, e)));
36 });
37 },
38
39 handleEnableDisable: function(name, isEnabled, ev) {
40 return this.handleAction(name, isEnabled ? 'disable' : 'enable', ev).then(L.bind(function(name, isEnabled, btn) {
41 btn.parentNode.replaceChild(this.renderEnableDisable({
42 name: name,
43 enabled: isEnabled
44 }), btn);
45 }, this, name, !isEnabled, ev.currentTarget));
46 },
47
48 handleRcLocalSave: function(ev) {
49 var value = (document.querySelector('textarea').value || '').trim().replace(/\r\n/g, '\n') + '\n';
50
51 return fs.write('/etc/rc.local', value).then(function() {
52 document.querySelector('textarea').value = value;
53 ui.addNotification(null, E('p', _('Contents have been saved.')), 'info');
54 }).catch(function(e) {
55 ui.addNotification(null, E('p', _('Unable to save contents: %s').format(e.message)));
56 });
57 },
58
59 renderEnableDisable: function(init) {
60 return E('button', {
61 class: 'btn cbi-button-%s'.format(init.enabled ? 'positive' : 'negative'),
62 click: ui.createHandlerFn(this, 'handleEnableDisable', init.name, init.enabled)
63 }, init.enabled ? _('Enabled') : _('Disabled'));
64 },
65
66 render: function(data) {
67 var rcLocal = data[0],
68 initList = data[1],
69 rows = [], list = [];
70
71 var table = E('div', { 'class': 'table' }, [
72 E('div', { 'class': 'tr table-titles' }, [
73 E('div', { 'class': 'th' }, _('Start priority')),
74 E('div', { 'class': 'th' }, _('Initscript')),
75 E('div', { 'class': 'th nowrap cbi-section-actions' })
76 ])
77 ]);
78
79 for (var init in initList)
80 if (initList[init].index < 100)
81 list.push(Object.assign({ name: init }, initList[init]));
82
83 list.sort(function(a, b) {
84 if (a.index != b.index)
85 return a.index - b.index
86
87 return a.name > b.name;
88 });
89
90 for (var i = 0; i < list.length; i++) {
91 rows.push([
92 '%02d'.format(list[i].index),
93 list[i].name,
94 E('div', [
95 this.renderEnableDisable(list[i]),
96 E('button', { 'class': 'btn cbi-button-action', 'click': ui.createHandlerFn(this, 'handleAction', list[i].name, 'start') }, _('Start')),
97 E('button', { 'class': 'btn cbi-button-action', 'click': ui.createHandlerFn(this, 'handleAction', list[i].name, 'restart') }, _('Restart')),
98 E('button', { 'class': 'btn cbi-button-action', 'click': ui.createHandlerFn(this, 'handleAction', list[i].name, 'stop') }, _('Stop'))
99 ])
100 ]);
101 }
102
103 cbi_update_table(table, rows);
104
105 var view = E('div', {}, [
106 E('h2', _('Startup')),
107 E('div', {}, [
108 E('div', { 'data-tab': 'init', 'data-tab-title': _('Initscripts') }, [
109 E('p', {}, _('You can enable or disable installed init scripts here. Changes will applied after a device reboot.<br /><strong>Warning: If you disable essential init scripts like "network", your device might become inaccessible!</strong>')),
110 table
111 ]),
112 E('div', { 'data-tab': 'rc', 'data-tab-title': _('Local Startup') }, [
113 E('p', {}, _('This is the content of /etc/rc.local. Insert your own commands here (in front of \'exit 0\') to execute them at the end of the boot process.')),
114 E('p', {}, E('textarea', { 'style': 'width:100%', 'rows': 20 }, [ (rcLocal != null ? rcLocal : '') ])),
115 E('div', { 'class': 'cbi-page-actions' }, [
116 E('button', {
117 'class': 'btn cbi-button-save',
118 'click': ui.createHandlerFn(this, 'handleRcLocalSave')
119 }, _('Save'))
120 ])
121 ])
122 ])
123 ]);
124
125 ui.tabs.initTabGroup(view.lastElementChild.childNodes);
126
127 return view;
128 },
129
130 handleSaveApply: null,
131 handleSave: null,
132 handleReset: null
133 });