Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Failed Conditions
Push — main ( 2c9538...02418a )
by Dan
38s queued 18s
created

ajax.js ➔ updateDisplay   B

Complexity

Conditions 7

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
dl 0
loc 22
rs 8
c 0
b 0
f 0
1
"use strict";
2
3
// We declare and use this function to make clear to the minifier that the eval will not effect the later parts of the script despite being called there.
4
var exec = function(s) {
5
	eval(s);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
6
};
7
8
(function() {
9
	var updateRefreshTimeout, refreshSpeed, refreshReady = true, xmlHttpRefresh, refreshUrl = null;
10
11
	/**
12
	 * Turn off AJAX auto-refresh by default. It can only be enabled by
13
	 * a call to `setupRefresh`.
14
	 */
15
	var refreshEnabled = false;
16
17
	/**
18
	 * Schedule the next AJAX auto-refresh.
19
	 */
20
	function scheduleRefresh() {
21
		// Remove any existing refresh schedule
22
		clearTimeout(updateRefreshTimeout);
23
		// Delay before first AJAX udpate to avoid rapid re-triggers.
24
		updateRefreshTimeout = setTimeout(updateRefreshRequest, refreshSpeed);
25
	}
26
27
	/**
28
	 * Stops AJAX auto-refresh and cancels the current request.
29
	 * Can be restarted with `scheduleRefresh()`.
30
	 */
31
	function cancelRefresh() {
32
		clearTimeout(updateRefreshTimeout);
33
		if (xmlHttpRefresh !== undefined) {
34
			xmlHttpRefresh.abort();
35
		}
36
	}
37
38
	/**
39
	 * Disables AJAX auto-refresh permanently on this page.
40
	 */
41
	function disableRefresh() {
42
		refreshEnabled = false;
43
		cancelRefresh();
44
	}
45
46
	/**
47
	 * Update content displayed on the page given XML data.
48
	 */
49
	function updateDisplay(data) {
50
		$('all > *', data).each(function(i, e) {
51
			if(e.tagName === 'JS') {
52
				$(e.childNodes).each(function(i, e) {
53
					if(e.tagName === 'EVAL') {
54
						exec($(e).text());
55
					}
56
					else {
57
						if(e.tagName === 'ALERT') {
58
							$(JSON.parse($(e).text())).each(function(i, e) {
59
								alert(e);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
60
							});
61
						}
62
						window[e.tagName] = JSON.parse($(e).text());
63
					}
64
				});
65
			}
66
			else {
67
				$('#'+e.tagName).html($(e).text());
68
			}
69
		});
70
	}
71
72
	function updateRefreshRequest() {
73
		if (refreshEnabled === true && refreshReady === true) {
74
			refreshReady = false;
75
			xmlHttpRefresh = ajaxLink(refreshUrl);
76
		}
77
	}
78
79
	/**
80
	 * Set up the AJAX auto-refresh for this page.
81
	 * Specify refreshSpeed in milliseconds.
82
	 */
83
	window.initRefresh = function(_refreshSpeed) {
84
		// If auto-refresh is disabled in preferences, then this function is
85
		// not called, so make sure the refresh is enabled ONLY if this
86
		// function is called.
87
		refreshEnabled = true;
88
89
		// Similarly, we only need event listeners when refresh is enabled.
90
		window.onfocus = scheduleRefresh;
91
		window.onblur = cancelRefresh;
92
		window.onunload = cancelRefresh;
93
94
		if(!_refreshSpeed) {
95
			return;
96
		}
97
		refreshSpeed = _refreshSpeed;
98
		refreshUrl = location.href;
99
		scheduleRefresh();
100
	};
101
102
	// The following section attempts to prevent users from taking multiple
103
	// actions within the same page. Possible actions include:
104
	//
105
	//  1. Pressing a hotkey
106
	//  2. Submitting a form
107
	//  3. Clicking a link
108
	//
109
	// We need to ensure that doing any one of these actions prevents all
110
	// other actions from having any effect. The next three functions
111
	// attempt to accomplish this.
112
	//
113
	var linkFollowed = false;
114
	window.followLink = function(href) {
115
		"use strict";
116
		// Prevent further actions after a hotkey is pressed.
117
		return function() {
118
			if(linkFollowed !== true) {
119
				linkFollowed = true;
120
				location.href = href;
121
				disableRefresh();
122
			}
123
		};
124
	};
125
	$(function() {
126
		// Prevent further actions after a form is submitted.
127
		$('form').submit(function(e) {
128
			if (linkFollowed === true) {
129
				e.preventDefault();
130
			} else {
131
				linkFollowed = true;
132
				disableRefresh();
133
			}
134
		});
135
		// Prevent further actions after a link is clicked.
136
		// This is skipped if the link has a "target" attribute specified.
137
		$('a[href]:not([target])').click(function(e) {
138
			// Did we click the link with the left mouse button?
139
			// We don't want to trigger this on right/middle clicks.
140
			if(e.which !== 1) {
141
				return;
142
			}
143
			// Don't trigger if clicked link has a no-op href attribute.
144
			if (this.href === 'javascript:void(0)') {
145
				return;
146
			}
147
			if(linkFollowed !== true) {
148
				linkFollowed = true;
149
				location.href = this.href;
150
				disableRefresh();
151
			}
152
			e.preventDefault();
153
		});
154
	});
155
156
	/**
157
	 * Perform a generic AJAX update.
158
	 * Auto-refresh is stopped during the request, and started back up
159
	 * (if not disabled) after the request completes.
160
	 */
161
	window.ajaxLink = function(link, {callback=null, params={}, updateRefreshUrl=true} = {}) {
162
		cancelRefresh();
163
		params.ajax = 1;
164
		return $.get(link, params, function(data) {
165
				// Update content on the page based on the XML results. The callback,
166
				// if any, may depend on the updated elements, so run after update.
167
				updateDisplay(data);
168
				if (callback !== null) {
169
					callback();
170
				}
171
172
				// Update refresh URL so that subsequent refreshes use the correct SN.
173
				if (updateRefreshUrl) {
174
					refreshUrl = link;
175
				}
176
				refreshReady = true;
177
				scheduleRefresh();
178
		}, 'xml');
179
	};
180
181
	window.toggleWepD = function(link) {
182
		$('.wep1:visible').slideToggle(600);
183
		$('.wep1:hidden').fadeToggle(600);
184
		// Don't change refreshUrl here, because the toggle link exits without
185
		// returning any content updates.
186
		ajaxLink(link, {updateRefreshUrl: false});
187
	};
188
189
})();
190