luci-app-advanced-reboot: add missing btn class to buttons
[project/luci.git] / applications / luci-app-advanced-reboot / htdocs / luci-static / resources / view / system / advanced_reboot.js
1 'use strict';
2 'require view';
3 'require rpc';
4 'require ui';
5 'require uci';
6 'require fs';
7
8 return view.extend({
9 translateTable: {
10 NO_BOARD_NAME : function(args) { return _('Unable to find Device Board Name.')},
11 NO_DUAL_FLAG : function(args) {return _('Unable to find Dual Boot Flag Partition.')},
12 ERR_SET_DUAL_FLAG : function(args) { return _('Unable to set Dual Boot Flag Partition entry for partition: %s.').format(args[0])},
13 NO_FIRM_ENV : function(args) { return _('Unable to obtain firmware environment variable: %s.').format(args[0])},
14 ERR_SET_ENV : function(args) { return _('Unable to set firmware environment variable: %s to %s.').format(args[0],args[1])}
15 },
16
17 callReboot: rpc.declare({
18 object: 'system',
19 method: 'reboot',
20 expect: { result: 0 }
21 }),
22
23 callObtainDeviceInfo: rpc.declare({
24 object: 'luci.advanced_reboot',
25 method: 'obtain_device_info',
26 expect: { }
27 }),
28
29 callTogglePartition: rpc.declare({
30 object: 'luci.advanced_reboot',
31 method: 'toggle_boot_partition',
32 expect: { }
33 }),
34
35 callPowerOff: function() {
36 return fs.exec('/sbin/poweroff').then(function() {
37 ui.showModal(_('Shutting down...'), [
38 E('p', { 'class': 'spinning' }, _('The system is shutting down now.<br /> DO NOT POWER OFF THE DEVICE!<br /> It might be necessary to renew the address of your computer to reach the device again, depending on your settings.'))
39 ]);
40 })
41 },
42
43 handlePowerOff: function() {
44
45 ui.showModal(_('Power Off Device'), [
46 E('p', _("WARNING: Power off might result in a reboot on a device which doesn't support power off.<br /><br />\
47 Click \"Proceed\" below to power off your device.")),
48 E('div', { 'class': 'right' }, [
49 E('button', {
50 'class': 'btn',
51 'click': ui.hideModal
52 }, _('Cancel')), ' ',
53 E('button', {
54 'class': 'btn cbi-button cbi-button-positive important',
55 'click': L.bind(this.callPowerOff, this)
56 }, _('Proceed'))
57 ])
58 ]);
59
60 },
61
62 handleReboot: function(ev) {
63 return this.callReboot().then(function(res) {
64 if (res != 0) {
65 ui.addNotification(null, E('p', _('The reboot command failed with code %d').format(res)));
66 L.raise('Error', 'Reboot failed');
67 }
68
69 ui.showModal(_('Rebooting…'), [
70 E('p', { 'class': 'spinning' }, _('Waiting for device...'))
71 ]);
72
73 window.setTimeout(function() {
74 ui.showModal(_('Rebooting…'), [
75 E('p', { 'class': 'spinning alert-message warning' },
76 _('Device unreachable! Still waiting for device...'))
77 ]);
78 }, 150000);
79
80 ui.awaitReconnect();
81 })
82 .catch(function(e) { ui.addNotification(null, E('p', e.message)) });
83 },
84
85 handleTogglePartition: function(ev) {
86 return this.callTogglePartition().then(L.bind(function(res) {
87 if (res.error) {
88 ui.hideModal()
89 return ui.addNotification(null, E('p', this.translateTable[res.error](res.args)));
90 }
91
92 return this.callReboot().then(function(res) {
93 if (res != 0) {
94 ui.addNotification(null, E('p', _('The reboot command failed with code %d').format(res)));
95 L.raise('Error', 'Reboot failed');
96 }
97
98 ui.showModal(_('Rebooting…'), [
99 E('p', { 'class': 'spinning' }, _('The system is rebooting to an alternative partition now.<br /> DO NOT POWER OFF THE DEVICE!<br /> Wait a few minutes before you try to reconnect. It might be necessary to renew the address of your computer to reach the device again, depending on your settings.'))
100 ]);
101
102 window.setTimeout(function() {
103 ui.showModal(_('Rebooting…'), [
104 E('p', { 'class': 'spinning alert-message warning' },
105 _('Device unreachable! Still waiting for device...'))
106 ]);
107 }, 150000);
108
109 ui.awaitReconnect();
110 })
111 .catch(function(e) { ui.addNotification(null, E('p', e.message)) });
112 }, this));
113 },
114
115 handleAlternativeReboot: function(ev) {
116 return Promise.all([
117 L.resolveDefault(fs.stat('/usr/sbin/fw_printenv'), null),
118 L.resolveDefault(fs.stat('/usr/sbin/fw_setenv'), null),
119 ]).then(L.bind(function (data) {
120 if (!data[0] || !data[1]) {
121 return ui.addNotification(null, E('p', _('No access to fw_printenv or fw_printenv!')));
122 }
123
124 ui.showModal(_('Reboot Device to an Alternative Partition') + " - " + _("Confirm"), [
125 E('p', _("WARNING: An alternative partition might have its own settings and completely different firmware.<br /><br />\
126 As your network configuration and WiFi SSID/password on alternative partition might be different,\
127 you might have to adjust your computer settings to be able to access your device once it reboots.<br /><br />\
128 Please also be aware that alternative partition firmware might not provide an easy way to switch active partition\
129 and boot back to the currently active partition.<br /><br />\
130 Click \"Proceed\" below to reboot device to an alternative partition.")),
131 E('div', { 'class': 'right' }, [
132 E('button', {
133 'class': 'btn',
134 'click': ui.hideModal
135 }, _('Cancel')), ' ',
136 E('button', {
137 'class': 'btn cbi-button cbi-button-positive important',
138 'click': L.bind(this.handleTogglePartition, this)
139 }, _('Proceed'))
140 ])
141 ]);
142 }, this))
143 },
144
145 parsePartitions: function(partitions) {
146 var res = [];
147
148 partitions.forEach(L.bind(function(partition) {
149 var func, text;
150
151 if (partition.state == 'Current') {
152 func = 'handleReboot';
153 text = _('Reboot to current partition');
154 } else {
155 func = 'handleAlternativeReboot';
156 text = _('Reboot to alternative partition...');
157 }
158
159 res.push([
160 (partition.number+0x100).toString(16).substr(-2).toUpperCase(),
161 _(partition.state),
162 partition.os.replace("Unknown", _("Unknown")).replace("Compressed", _("Compressed")),
163 E('button', {
164 'class': 'btn cbi-button cbi-button-apply important',
165 'click': ui.createHandlerFn(this, func)
166 }, text)
167 ])
168 }, this));
169
170 return res;
171 },
172
173 load: function() {
174 return Promise.all([
175 uci.changes(),
176 L.resolveDefault(fs.stat('/sbin/poweroff'), null),
177 this.callObtainDeviceInfo()
178 ]);
179 },
180
181 render: function(data) {
182 var changes = data[0],
183 poweroff_supported = data[1] != null ? true : false,
184 device_info = data[2];
185
186 var body = E([
187 E('h2', _('Advanced Reboot'))
188 ]);
189
190 for (var config in (changes || {})) {
191 body.appendChild(E('p', { 'class': 'alert-message warning' },
192 _('Warning: There are unsaved changes that will get lost on reboot!')));
193 break;
194 }
195
196 if (device_info.error)
197 body.appendChild(E('p', { 'class' : 'alert-message warning'}, _("ERROR: ") + this.translateTable[device_info.error]()));
198
199 body.appendChild(E('h3', device_info.device_name + _(' Partitions')));
200 if (device_info.device_name) {
201 var partitions_table = E('table', { 'class': 'table' }, [
202 E('tr', { 'class': 'tr table-titles' }, [
203 E('th', { 'class': 'th' }, [ _('Partition') ]),
204 E('th', { 'class': 'th' }, [ _('Status') ]),
205 E('th', { 'class': 'th' }, [ _('Firmware') ]),
206 E('th', { 'class': 'th' }, [ _('Reboot') ])
207 ])
208 ]);
209
210 cbi_update_table(partitions_table, this.parsePartitions(device_info.partitions));
211
212 body.appendChild(partitions_table);
213 } else {
214 body.appendChild(E('p', { 'class' : 'alert-message warning'},
215 device_info.rom_board_name ? _("Warning: Device (%s) is unknown or isn't a dual-partition device!").format(device_info.rom_board_name)
216 : _('Warning: Unable to obtain device information!')
217 ));
218 }
219
220 body.appendChild(E('hr'));
221 body.appendChild(
222 poweroff_supported ? E('button', {
223 'class': 'btn cbi-button cbi-button-apply important',
224 'click': ui.createHandlerFn(this, 'handlePowerOff')
225 }, _('Perform power off...'))
226
227 : E('p', { 'class' : 'alert-message warning'},
228 _('Warning: This system does not support powering off!'))
229 );
230
231 return body;
232 },
233
234 handleSaveApply: null,
235 handleSave: null,
236 handleReset: null
237 });