treewide: import utility classes explicitly
[project/luci.git] / applications / luci-app-wol / htdocs / luci-static / resources / view / wol.js
1 'use strict';
2 'require view';
3 'require dom';
4 'require uci';
5 'require fs';
6 'require ui';
7 'require rpc';
8 'require form';
9 'require tools.widgets as widgets';
10
11 return view.extend({
12 formdata: { wol: {} },
13
14 callHostHints: rpc.declare({
15 object: 'luci-rpc',
16 method: 'getHostHints',
17 expect: { '': {} }
18 }),
19
20 load: function() {
21 return Promise.all([
22 L.resolveDefault(fs.stat('/usr/bin/etherwake')),
23 L.resolveDefault(fs.stat('/usr/bin/wol')),
24 this.callHostHints(),
25 uci.load('etherwake')
26 ]);
27 },
28
29 render: function(data) {
30 var has_ewk = data[0],
31 has_wol = data[1],
32 hosts = data[2],
33 m, s, o;
34
35 this.formdata.has_ewk = has_ewk;
36 this.formdata.has_wol = has_wol;
37
38 m = new form.JSONMap(this.formdata, _('Wake on LAN'),
39 _('Wake on LAN is a mechanism to remotely boot computers in the local network.'));
40
41 s = m.section(form.NamedSection, 'wol');
42
43 if (has_ewk && has_wol) {
44 o = s.option(form.ListValue, 'executable', _('WoL program'),
45 _('Sometimes only one of the two tools works. If one fails, try the other one'));
46
47 o.value('/usr/bin/etherwake', 'Etherwake');
48 o.value('/usr/bin/wol', 'WoL');
49 }
50
51 if (has_ewk) {
52 o = s.option(widgets.DeviceSelect, 'iface', _('Network interface to use'),
53 _('Specifies the interface the WoL packet is sent on'));
54
55 o.default = uci.get('etherwake', 'setup', 'interface');
56 o.rmempty = false;
57 o.noaliases = true;
58 o.noinactive = true;
59
60 if (has_wol)
61 o.depends('executable', '/usr/bin/etherwake');
62 }
63
64 o = s.option(form.Value, 'mac', _('Host to wake up'),
65 _('Choose the host to wake up or enter a custom MAC address to use'));
66
67 o.rmempty = false;
68
69 Object.keys(hosts).sort().forEach(function(mac) {
70 o.value(mac, E([], [ mac, ' (', E('strong', [hosts[mac].name || hosts[mac].ipv4 || hosts[mac].ipv6 || '?']), ')' ]));
71 });
72
73 if (has_ewk) {
74 o = s.option(form.Flag, 'broadcast', ('Send to broadcast address'));
75
76 if (has_wol)
77 o.depends('executable', '/usr/bin/etherwake');
78 }
79
80 return m.render();
81 },
82
83 handleWakeup: function(ev) {
84 var map = document.querySelector('#maincontent .cbi-map'),
85 data = this.formdata;
86
87 return dom.callClassMethod(map, 'save').then(function() {
88 if (!data.wol.mac)
89 return alert(_('No target host specified!'));
90
91 var bin = data.executable || (data.has_ewk ? '/usr/bin/etherwake' : '/usr/bin/wol'),
92 args = [];
93
94 if (bin == '/usr/bin/etherwake') {
95 args.push('-D', '-i', data.wol.iface);
96
97 if (data.wol.broadcast == '1')
98 args.push('-b');
99
100 args.push(data.wol.mac);
101 }
102 else {
103 args.push('-v', data.wol.mac);
104 }
105
106 ui.showModal(_('Waking host'), [
107 E('p', { 'class': 'spinning' }, [ 'Starting WoL utility…' ])
108 ]);
109
110 return fs.exec(bin, args).then(function(res) {
111 ui.showModal(_('Waking host'), [
112 res.stderr ? E('p', [ res.stdout ]) : '',
113 res.stderr ? E('pre', [ res.stderr ]) : '',
114 E('div', { 'class': 'right' }, [
115 E('button', {
116 'class': 'cbi-button cbi-button-primary',
117 'click': ui.hideModal
118 }, [ _('Dismiss') ])
119 ])
120 ]);
121 }).catch(function(err) {
122 ui.hideModal();
123 ui.addNotification(null, [
124 E('p', [ _('Waking host failed: '), err ])
125 ]);
126 });
127 });
128 },
129
130 addFooter: function() {
131 return E('div', { 'class': 'cbi-page-actions' }, [
132 E('button', {
133 'class': 'cbi-button cbi-button-save',
134 'click': L.ui.createHandlerFn(this, 'handleWakeup')
135 }, [ _('Wake up host') ])
136 ]);
137 }
138 });