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
Pull Request — master (#1101)
by Dan
05:32
created

ajax.js ➔ scheduleRefresh   A

Complexity

Conditions 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
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, sn;
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
	function getURLParameter(paramName, href) {
47
		if ( href.indexOf("?") > -1 ) {
48
			var paramList = href.substr(href.indexOf("?")).split("&");
49
			for (var i = 0; i < paramList.length; i++) {
50
				if (paramList[i].toUpperCase().indexOf(paramName.toUpperCase() + "=") > -1 ) {
51
					return paramList[i].split("=")[1];
52
				}
53
			}
54
		}
55
		return false;
56
	}
57
58
	// This is used as a jQuery.get callback, but we don't use the arguments
59
	// (textStatus, jqXHR), so they are omitted here.
60
	function updateRefresh(data) {
61
		$('all > *', data).each(function(i, e) {
62
			if(e.tagName === 'JS') {
63
				$(e.childNodes).each(function(i, e) {
64
					if(e.tagName === 'EVAL') {
65
						exec($(e).text());
66
					}
67
					else {
68
						if(e.tagName === 'ALERT') {
69
							$(JSON.parse($(e).text())).each(function(i, e) {
70
								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...
71
							});
72
						}
73
						window[e.tagName] = JSON.parse($(e).text());
74
					}
75
				});
76
			}
77
			else {
78
				$('#'+e.tagName).html($(e).text());
79
			}
80
		});
81
		refreshReady = true;
82
		scheduleRefresh();
83
	}
84
85
	function updateRefreshRequest() {
86
		if (refreshEnabled === true && refreshReady === true) {
87
			refreshReady = false;
88
			xmlHttpRefresh = $.get('', {sn:sn, ajax:1}, updateRefresh, 'xml');
89
		}
90
	}
91
92
	/**
93
	 * Set up the AJAX auto-refresh for this page.
94
	 * Specify refreshSpeed in milliseconds.
95
	 */
96
	window.initRefresh = function(_refreshSpeed) {
97
		// If auto-refresh is disabled in preferences, then this function is
98
		// not called, so make sure the refresh is enabled ONLY if this
99
		// function is called.
100
		refreshEnabled = true;
101
102
		// Similarly, we only need event listeners when refresh is enabled.
103
		window.onfocus = scheduleRefresh;
104
		window.onblur = cancelRefresh;
105
		window.onunload = cancelRefresh;
106
107
		if(!_refreshSpeed) {
108
			return;
109
		}
110
		refreshSpeed = _refreshSpeed;
111
		sn = getURLParameter('sn', location.href);
112
		if(sn===false) {
113
			return;
114
		}
115
		scheduleRefresh();
116
	};
117
118
	// The following section attempts to prevent users from taking multiple
119
	// actions within the same page. Possible actions include:
120
	//
121
	//  1. Pressing a hotkey
122
	//  2. Submitting a form
123
	//  3. Clicking a link
124
	//
125
	// We need to ensure that doing any one of these actions prevents all
126
	// other actions from having any effect. The next three functions
127
	// attempt to accomplish this.
128
	//
129
	var linkFollowed = false;
130
	window.followLink = function(href) {
131
		"use strict";
132
		// Prevent further actions after a hotkey is pressed.
133
		return function() {
134
			if(linkFollowed !== true) {
135
				linkFollowed = true;
136
				location.href = href;
137
				disableRefresh();
138
			}
139
		};
140
	};
141
	$(function() {
142
		// Prevent further actions after a form is submitted.
143
		$('form').submit(function(e) {
144
			if (linkFollowed === true) {
145
				e.preventDefault();
146
			} else {
147
				linkFollowed = true;
148
				disableRefresh();
149
			}
150
		});
151
		// Prevent further actions after a link is clicked.
152
		// This is skipped if the link has a "target" attribute specified.
153
		$('a[href]:not([target])').click(function(e) {
154
			// Did we click the link with the left mouse button?
155
			// We don't want to trigger this on right/middle clicks.
156
			if(e.which !== 1) {
157
				return;
158
			}
159
			// Don't trigger if clicked link has a no-op href attribute.
160
			if (this.href === 'javascript:void(0)') {
161
				return;
162
			}
163
			if(linkFollowed !== true) {
164
				linkFollowed = true;
165
				location.href = this.href;
166
				disableRefresh();
167
			}
168
			e.preventDefault();
169
		});
170
	});
171
172
	/**
173
	 * Perform a generic AJAX update.
174
	 * Auto-refresh is stopped during the request, and started back up
175
	 * (if not disabled) after the request completes.
176
	 */
177
	window.ajaxLink = function(link, callback=null, params=null) {
178
		cancelRefresh();
179
		if (params === null) {
180
			params = {ajax: 1};
181
		}
182
		$.get(link, params, function(data) {
183
				sn = getURLParameter('sn', link);
184
				updateRefresh(data);
185
				if (callback !== null) {
186
					callback();
187
				}
188
			}, 'xml');
189
	};
190
191
	window.toggleWepD = function(link) {
192
		$('.wep1:visible').slideToggle(600);
193
		$('.wep1:hidden').fadeToggle(600);
194
		// Omit updateRefresh here so that we can smoothly animate the change
195
		$.get(link, {ajax: 1});
196
	};
197
198
})();
199