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

Passed
Push — main ( 6eb332...15a06a )
by Dan
07:01
created

src/htdocs/js/ajax.js   A

Complexity

Total Complexity 31
Complexity/F 1.63

Size

Lines of Code 181
Function Count 19

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 83
c 0
b 0
f 0
dl 0
loc 181
rs 9.92
wmc 31
mnd 12
bc 12
fnc 19
bpm 0.6314
cpm 1.6315
noi 2

5 Functions

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