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