summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base/luasrc/view/cbi/apply_widget.htm
blob: 0df16e88c85e50869b7a91e6d61c130cd052e110 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<% export("cbi_apply_widget", function(redirect_ok, rollback_token) -%>
<style type="text/css">
	#cbi_apply_overlay {
		position: absolute;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
		background: rgba(0, 0, 0, 0.7);
		display: none;
		z-index: 20000;
	}

	#cbi_apply_overlay .alert-message {
		position: relative;
		top: 10%;
		width: 60%;
		margin: auto;
		display: flex;
		flex-wrap: wrap;
		min-height: 32px;
		align-items: center;
	}

	#cbi_apply_overlay .alert-message > h4,
	#cbi_apply_overlay .alert-message > p,
	#cbi_apply_overlay .alert-message > div {
		flex-basis: 100%;
	}

	#cbi_apply_overlay .alert-message > img {
		margin-right: 1em;
		flex-basis: 32px;
	}

	body.apply-overlay-active {
		overflow: hidden;
		height: 100vh;
	}

	body.apply-overlay-active #cbi_apply_overlay {
		display: block;
	}
</style>

<script type="text/javascript" src="<%=resource%>/cbi.js?v=git-18.138.59467-72fe5dd"></script>
<script type="text/javascript">//<![CDATA[
	var xhr = new XHR(),
	    uci_apply_auth = { sid: '<%=luci.dispatcher.context.authsession%>', token: '<%=token%>' },
	    uci_apply_rollback = <%=math.max(luci.config and luci.config.apply and luci.config.apply.rollback or 30, 30)%>,
	    uci_apply_holdoff = <%=math.max(luci.config and luci.config.apply and luci.config.apply.holdoff or 4, 1)%>,
	    uci_apply_timeout = <%=math.max(luci.config and luci.config.apply and luci.config.apply.timeout or 5, 1)%>,
	    uci_apply_display = <%=math.max(luci.config and luci.config.apply and luci.config.apply.display or 1.5, 1)%>,
	    uci_confirm_auth = <% if rollback_token then %>{ token: '<%=rollback_token%>' }<% else %>null<% end %>,
	    was_xhr_poll_running = false;

	function uci_status_message(type, content) {
		var overlay = document.getElementById('cbi_apply_overlay') || document.body.appendChild(E('<div id="cbi_apply_overlay"><div class="alert-message"></div></div>')),
		    message = overlay.querySelector('.alert-message');

		if (message && type) {
			if (!message.classList.contains(type)) {
				message.classList.remove('notice');
				message.classList.remove('warning');
				message.classList.add(type);
			}

			if (content)
				message.innerHTML = content;

			document.body.classList.add('apply-overlay-active');

			if (!was_xhr_poll_running) {
				was_xhr_poll_running = XHR.running();
				XHR.halt();
			}
		}
		else {
			document.body.classList.remove('apply-overlay-active');

			if (was_xhr_poll_running)
				XHR.run();
		}
	}

	function uci_rollback(checked) {
		if (checked) {
			uci_status_message('warning',
				'<img src="<%=resource%>/icons/loading.gif" alt="" style="vertical-align:middle" /> ' +
				'<%:Failed to confirm apply within %ds, waiting for rollback…%>'.format(uci_apply_rollback));

			var call = function(r, data, duration) {
				if (r.status === 204) {
					uci_status_message('warning',
						'<h4><%:Configuration has been rolled back!%></h4>' +
						'<p><%:The device could not be reached within %d seconds after applying the pending changes, which caused the configuration to be rolled back for safety reasons. If you believe that the configuration changes are correct nonetheless, proceed by applying anyway. Alternatively, you can dismiss this warning and edit changes before attempting to apply again, or revert all pending changes to keep the currently working configuration state.%></p>'.format(uci_apply_rollback) +
						'<div class="right">' +
							'<input type="button" class="btn" onclick="uci_status_message(false)" value="<%:Dismiss%>" /> ' +
							'<input type="button" class="btn cbi-button-action important" onclick="uci_revert()" value="<%:Revert changes%>" /> ' +
							'<input type="button" class="btn cbi-button-negative important" onclick="uci_apply(false)" value="<%:Apply anyway%>" />' +
						'</div>');

					return;
				}

				var delay = isNaN(duration) ? 0 : Math.max(1000 - duration, 0);
				window.setTimeout(function() {
					xhr.post('<%=url("admin/uci/confirm")%>', uci_apply_auth, call, uci_apply_timeout * 1000);
				}, delay);
			};

			call({ status: 0 });
		}
		else {
			uci_status_message('warning',
				'<h4><%:Device unreachable!%></h4>' +
				'<p><%:Could not regain access to the device after applying the configuration changes. You might need to reconnect if you modified network related settings such as the IP address or wireless security credentials.%></p>');
		}
	}

	function uci_confirm(checked, deadline) {
		var tt;
		var ts = Date.now();

		uci_status_message('notice');

		var call = function(r, data, duration) {
			if (Date.now() >= deadline) {
				uci_rollback(checked);
				return;
			}
			else if (r && (r.status === 200 || r.status === 204)) {
				var indicator = document.querySelector('.uci_change_indicator');
				if (indicator) indicator.style.display = 'none';

				uci_status_message('notice', '<%:Configuration has been applied.%>');

				window.clearTimeout(tt);
				window.setTimeout(function() {
					<% if redirect_ok then -%>
						location.href = decodeURIComponent('<%=luci.util.urlencode(redirect_ok)%>');
					<%- else -%>
						window.location = window.location.href.split('#')[0];
					<% end %>
				}, uci_apply_display * 1000);

				return;
			}

			var delay = isNaN(duration) ? 0 : Math.max(1000 - duration, 0);
			window.setTimeout(function() {
				xhr.post('<%=url("admin/uci/confirm")%>', uci_confirm_auth, call, uci_apply_timeout * 1000);
			}, delay);
		};

		var tick = function() {
			var now = Date.now();

			uci_status_message('notice',
				'<img src="<%=resource%>/icons/loading.gif" alt="" style="vertical-align:middle" /> ' +
				'<%:Waiting for configuration to be applied… %ds%>'.format(Math.max(Math.floor((deadline - Date.now()) / 1000), 0)));

			if (now >= deadline)
				return;

			tt = window.setTimeout(tick, 1000 - (now - ts));
			ts = now;
		};

		tick();

		/* wait a few seconds for the settings to become effective */
		window.setTimeout(call, Math.max(uci_apply_holdoff * 1000 - ((ts + uci_apply_rollback * 1000) - deadline), 1));
	}

	function uci_apply(checked) {
		uci_status_message('notice',
			'<img src="<%=resource%>/icons/loading.gif" alt="" style="vertical-align:middle" /> ' +
			'<%:Starting configuration apply…%>');

		xhr.post('<%=url("admin/uci")%>/' + (checked ? 'apply_rollback' : 'apply_unchecked'), uci_apply_auth, function(r, tok) {
			if (r.status === (checked ? 200 : 204)) {
				if (checked && tok !== null && typeof(tok) === 'object' && typeof(tok.token) === 'string')
					uci_confirm_auth = tok;

				uci_confirm(checked, Date.now() + uci_apply_rollback * 1000);
			}
			else if (checked && r.status === 204) {
				uci_status_message('notice', '<%:There are no changes to apply.%>');
				window.setTimeout(function() {
					<% if redirect_ok then -%>
						location.href = decodeURIComponent('<%=luci.util.urlencode(redirect_ok)%>');
					<%- else -%>
						uci_status_message(false);
					<%- end %>
				}, uci_apply_display * 1000);
			}
			else {
				uci_status_message('warning', '<%_Apply request failed with status <code>%h</code>%>'.format(r.responseText || r.statusText || r.status));
				window.setTimeout(function() { uci_status_message(false); }, uci_apply_display * 1000);
			}
		});
	}

	function uci_revert() {
		uci_status_message('notice',
			'<img src="<%=resource%>/icons/loading.gif" alt="" style="vertical-align:middle" /> ' +
			'<%:Reverting configuration…%>');

		xhr.post('<%=url("admin/uci/revert")%>', uci_apply_auth, function(r) {
			if (r.status === 200) {
				uci_status_message('notice', '<%:Changes have been reverted.%>');
				window.setTimeout(function() {
					<% if redirect_ok then -%>
						location.href = decodeURIComponent('<%=luci.util.urlencode(redirect_ok)%>');
					<%- else -%>
						window.location = window.location.href.split('#')[0];
					<%- end %>
				}, uci_apply_display * 1000);
			}
			else {
				uci_status_message('warning', '<%_Revert request failed with status <code>%h</code>%>'.format(r.statusText || r.status));
				window.setTimeout(function() { uci_status_message(false); }, uci_apply_display * 1000);
			}
		});
	}
//]]></script>
<%-	end) %>