Merge pull request #1735 from sumpfralle/olsr-jsoninfo-parser-handle-empty-result
[oweals/luci.git] / modules / luci-mod-system / htdocs / luci-static / resources / view / system / startup.js
1 'use strict';
2 'require rpc';
3
4 return L.view.extend({
5         callInitList: rpc.declare({
6                 object: 'luci',
7                 method: 'getInitList',
8                 expect: { '': {} }
9         }),
10
11         callInitAction: rpc.declare({
12                 object: 'luci',
13                 method: 'setInitAction',
14                 params: [ 'name', 'action' ],
15                 expect: { result: false }
16         }),
17
18         callFileRead: rpc.declare({
19                 object: 'file',
20                 method: 'read',
21                 params: [ 'path' ],
22                 expect: { data: '' }
23         }),
24
25         callFileWrite: rpc.declare({
26                 object: 'file',
27                 method: 'write',
28                 params: [ 'path', 'data' ]
29         }),
30
31         load: function() {
32                 return Promise.all([
33                         this.callFileRead('/etc/rc.local'),
34                         this.callInitList()
35                 ]);
36         },
37
38         handleAction: function(name, action, ev) {
39                 return this.callInitAction(name, action).then(function(success) {
40                         if (success != true)
41                                 throw _('Command failed');
42
43                         return true;
44                 }).catch(function(e) {
45                         L.ui.addNotification(null, E('p', _('Failed to execute "/etc/init.d/%s %s" action: %s').format(name, action, e)));
46                 });
47         },
48
49         handleEnableDisable: function(name, isEnabled, ev) {
50                 return this.handleAction(name, isEnabled ? 'disable' : 'enable', ev).then(L.bind(function(name, isEnabled, cell) {
51                         L.dom.content(cell, this.renderEnableDisable({
52                                 name: name,
53                                 enabled: isEnabled
54                         }));
55                 }, this, name, !isEnabled, ev.currentTarget.parentNode));
56         },
57
58         handleRcLocalSave: function(ev) {
59                 var value = (document.querySelector('textarea').value || '').trim().replace(/\r\n/g, '\n') + '\n';
60
61                 return this.callFileWrite('/etc/rc.local', value).then(function(rc) {
62                         if (rc != 0)
63                                 throw rpc.getStatusText(rc);
64
65                         document.querySelector('textarea').value = value;
66                         L.ui.addNotification(null, E('p', _('Contents have been saved.')), 'info');
67                 }).catch(function(e) {
68                         L.ui.addNotification(null, E('p', _('Unable to save contents: %s').format(e)));
69                 });
70         },
71
72         renderEnableDisable: function(init) {
73                 return E('button', {
74                         class: 'btn cbi-button-%s'.format(init.enabled ? 'positive' : 'negative'),
75                         click: L.ui.createHandlerFn(this, 'handleEnableDisable', init.name, init.enabled)
76                 }, init.enabled ? _('Enabled') : _('Disabled'));
77         },
78
79         render: function(data) {
80                 var rcLocal = data[0],
81                     initList = data[1],
82                     rows = [], list = [];
83
84                 var table = E('div', { 'class': 'table' }, [
85                         E('div', { 'class': 'tr table-titles' }, [
86                                 E('div', { 'class': 'th' }, _('Start priority')),
87                                 E('div', { 'class': 'th' }, _('Initscript')),
88                                 E('div', { 'class': 'th' }, _('Enable/Disable')),
89                                 E('div', { 'class': 'th' }, _('Start')),
90                                 E('div', { 'class': 'th' }, _('Restart')),
91                                 E('div', { 'class': 'th' }, _('Stop'))
92                         ])
93                 ]);
94
95                 for (var init in initList)
96                         if (initList[init].index < 100)
97                                 list.push(Object.assign({ name: init }, initList[init]));
98
99                 list.sort(function(a, b) {
100                         if (a.index != b.index)
101                                 return a.index - b.index
102
103                         return a.name > b.name;
104                 });
105
106                 for (var i = 0; i < list.length; i++) {
107                         rows.push([
108                                 '%02d'.format(list[i].index),
109                                 list[i].name,
110                                 this.renderEnableDisable(list[i]),
111                                 E('button', { 'class': 'btn cbi-button-action', 'click': L.ui.createHandlerFn(this, 'handleAction', list[i].name, 'start') }, _('Start')),
112                                 E('button', { 'class': 'btn cbi-button-action', 'click': L.ui.createHandlerFn(this, 'handleAction', list[i].name, 'restart') }, _('Restart')),
113                                 E('button', { 'class': 'btn cbi-button-action', 'click': L.ui.createHandlerFn(this, 'handleAction', list[i].name, 'stop') }, _('Stop'))
114                         ]);
115                 }
116
117                 cbi_update_table(table, rows);
118
119                 var view = E('div', {}, [
120                         E('h2', _('Startup')),
121                         E('div', {}, [
122                                 E('div', { 'data-tab': 'init', 'data-tab-title': _('Initscripts') }, [
123                                         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>')),
124                                         table
125                                 ]),
126                                 E('div', { 'data-tab': 'rc', 'data-tab-title': _('Local Startup') }, [
127                                         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.')),
128                                         E('p', {}, E('textarea', { 'style': 'width:100%', 'rows': 20 }, rcLocal != null ? rcLocal : '')),
129                                         E('div', { 'class': 'cbi-page-actions' }, [
130                                                 E('button', {
131                                                         'class': 'btn cbi-button-save',
132                                                         'click': L.ui.createHandlerFn(this, 'handleRcLocalSave')
133                                                 }, _('Save'))
134                                         ])
135                                 ])
136                         ])
137                 ]);
138
139                 L.ui.tabs.initTabGroup(view.lastElementChild.childNodes);
140
141                 return view;
142         },
143
144         handleSaveApply: null,
145         handleSave: null,
146         handleReset: null
147 });