summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-vpnbypass/htdocs/luci-static/resources/vpnbypass/widgets.js
blob: 3085d932d614f86b6675b79d78c9aae081a6d140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Thsis file wouldn't have been possible without help from [@vsviridov](https://github.com/vsviridov)

'require ui';
'require rpc';
'require form';

var pkg = {
	get Name() { return 'vpnbypass'; }
};

var _getInitList = rpc.declare({
	object: 'luci.' + pkg.Name,
	method: 'getInitList',
	params: ['name']
});

var _setInitAction = rpc.declare({
	object: 'luci.' + pkg.Name,
	method: 'setInitAction',
	params: ['name', 'action'],
	expect: { result: false }
});

var _getInitStatus = rpc.declare({
	object: 'luci.' + pkg.Name,
	method: 'getInitStatus',
	params: ['name']
});

var RPC = {
	listeners: [],
	on: function on(event, callback) {
		var pair = { event: event, callback: callback }
		this.listeners.push(pair);
		return function unsubscribe() {
			this.listeners = this.listeners.filter(function (listener) {
				return listener !== pair;
			});
		}.bind(this);
	},
	emit: function emit(event, data) {
		this.listeners.forEach(function (listener) {
			if (listener.event === event) {
				listener.callback(data);
			}
		});
	},
	getInitList: function getInitList(name) {
		_getInitList(name).then(function (result) {
			this.emit('getInitList', result);
		}.bind(this));

	},
	getInitStatus: function getInitStatus(name) {
		_getInitStatus(name).then(function (result) {
			this.emit('getInitStatus', result);
		}.bind(this));
	},
	setInitAction: function setInitAction(name, action) {
		_setInitAction(name, action).then(function (result) {
			this.emit('setInitAction', result);
		}.bind(this));
	}
}

var statusCBI = form.DummyValue.extend({
	renderWidget: function (section) {
		var status = E('span', {}, _("Quering") + "...");
		RPC.on('getInitStatus', function (reply) {
			if (reply[pkg.Name].version) {
				if (reply[pkg.Name].running) {
					status.innerText = _("Running (version: %s)").format(reply[pkg.Name].version);
				}
				else {
					if (reply[pkg.Name].enabled) {
						status.innerText = _("Stopped (version: %s)").format(reply[pkg.Name].version);
					}
					else {
						status.innerText = _("Stopped (Disabled)");
					}
				}
			}
			else {
				status.innerText = _("Not installed or not found")
			}
		});
		return E('div', {}, [status]);
	}
});

var buttonsCBI = form.DummyValue.extend({
	renderWidget: function (section) {

		var btn_gap = E('span', {}, '  ');
		var btn_gap_long = E('span', {}, '        ');

		var btn_start = E('button', {
			'class': 'btn cbi-button cbi-button-apply',
			disabled: true,
			click: function (ev) {
				ui.showModal(null, [
					E('p', { 'class': 'spinning' }, _('Starting %s service').format(pkg.Name))
				]);
				return RPC.setInitAction(pkg.Name, 'start');
			}
		}, _('Start'));

		var btn_action = E('button', {
			'class': 'btn cbi-button cbi-button-apply',
			disabled: true,
			click: function (ev) {
				ui.showModal(null, [
					E('p', { 'class': 'spinning' }, _('Restarting %s service').format(pkg.Name))
				]);
				return RPC.setInitAction(pkg.Name, 'restart');
			}
		}, _('Restart'));

		var btn_stop = E('button', {
			'class': 'btn cbi-button cbi-button-reset',
			disabled: true,
			click: function (ev) {
				ui.showModal(null, [
					E('p', { 'class': 'spinning' }, _('Stopping %s service').format(pkg.Name))
				]);
				return RPC.setInitAction(pkg.Name, 'stop');
			}
		}, _('Stop'));

		var btn_enable = E('button', {
			'class': 'btn cbi-button cbi-button-apply',
			disabled: true,
			click: function (ev) {
				ui.showModal(null, [
					E('p', { 'class': 'spinning' }, _('Enabling %s service').format(pkg.Name))
				]);
				return RPC.setInitAction(pkg.Name, 'enable');
			}
		}, _('Enable'));

		var btn_disable = E('button', {
			'class': 'btn cbi-button cbi-button-reset',
			disabled: true,
			click: function (ev) {
				ui.showModal(null, [
					E('p', { 'class': 'spinning' }, _('Disabling %s service').format(pkg.Name))
				]);
				return RPC.setInitAction(pkg.Name, 'disable');
			}
		}, _('Disable'));

		RPC.on('getInitStatus', function (reply) {
			if (reply[pkg.Name].version) {
				if (reply[pkg.Name].enabled) {
					btn_enable.disabled = true;
					btn_disable.disabled = false;
					if (reply[pkg.Name].running) {
						btn_start.disabled = true;
						btn_action.disabled = false;
						btn_stop.disabled = false;
					}
					else {
						btn_start.disabled = false;
						btn_action.disabled = true;
						btn_stop.disabled = true;
					}
				}
				else {
					btn_start.disabled = true;
					btn_action.disabled = true;
					btn_stop.disabled = true;
					btn_enable.disabled = false;
					btn_disable.disabled = true;
				}
			}
		});

		RPC.getInitStatus(pkg.Name);
		
		return E('div', {}, [btn_start, btn_gap, btn_action, btn_gap, btn_stop, btn_gap_long, btn_enable, btn_gap, btn_disable]);
	}
});

RPC.on('setInitAction', function (reply) {
	ui.hideModal();
	RPC.getInitStatus(pkg.Name);
});

return L.Class.extend({
	Status: statusCBI,
	Buttons: buttonsCBI
});