Completed
Push — master ( bc2613...0cc343 )
by Yannick
28:17
created

js/map.common.js   F

Complexity

Total Complexity 73
Complexity/F 1.62

Size

Lines of Code 331
Function Count 45

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 2 Features 0
Metric Value
cc 0
wmc 73
c 9
b 2
f 0
nc 1
mnd 4
bc 77
fnc 45
dl 0
loc 331
rs 3.9761
bpm 1.7111
cpm 1.6222
noi 4

43 Functions

Rating   Name   Duplication   Size   Complexity  
A map.common.js ➔ delCookie 0 3 1
A map.common.js ➔ createCookie 0 11 2
A map.common.js ➔ clickSyncMap2D3D 0 7 2
A map.common.js ➔ mapType 0 19 4
A map.common.js ➔ mapType3D 0 19 4
A map.common.js ➔ sattypes 0 11 3
A map.common.js ➔ airlines 0 10 3
B map.common.js ➔ terrainType 0 17 5
A map.common.js ➔ airlinestype 0 5 1
A map.common.js ➔ mmsifilter 0 4 1
A map.common.js ➔ clickSBS1 0 3 1
A map.common.js ➔ clickIVAO 0 3 1
A map.common.js ➔ clickSingleModel 0 3 1
A map.common.js ➔ clickVATSIM 0 3 1
A map.common.js ➔ clickDisplayISS 0 4 1
A map.common.js ➔ clickDisplayGroundStation 0 4 1
B map.common.js ➔ generateRandomPoint 0 30 1
A map.common.js ➔ show2D 0 11 3
A map.common.js ➔ unitspeed 0 5 1
A map.common.js ➔ clickAPRS 0 3 1
A map.common.js ➔ clickDisplayWeatherStation 0 4 1
A map.common.js ➔ msgbox 0 13 1
A map.common.js ➔ show3D 0 11 3
A map.common.js ➔ clickDisplayMinimap 0 4 1
A map.common.js ➔ clickDisplayFires 0 4 1
A map.common.js ➔ clickUpdateRealtime 0 3 1
A map.common.js ➔ clickPolar 0 4 1
A map.common.js ➔ identfilter 0 4 1
A map.common.js ➔ unitdistance 0 5 1
A map.common.js ➔ addarchive 0 8 1
A map.common.js ➔ clickDisplay2DBuildings 0 4 1
A map.common.js ➔ racefilter 0 9 2
A map.common.js ➔ clickDisplayAirports 0 4 1
A map.common.js ➔ noarchive 0 8 1
A map.common.js ➔ clickphpVMS 0 3 1
A map.common.js ➔ getColor 0 8 1
A map.common.js ➔ clickDisplayLightning 0 4 1
A map.common.js ➔ alliance 0 5 1
A map.common.js ➔ removefilters 0 11 3
A map.common.js ➔ sources 0 10 3
A map.common.js ➔ clickShadows 0 4 1
A map.common.js ➔ clickDisplayWeather 0 4 1
A map.common.js ➔ unitaltitude 0 5 1

How to fix   Complexity   

Complexity

Complex classes like js/map.common.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
function getCookie(cname) {
2
    var name = cname + "=";
3
    var ca = document.cookie.split(';');
4
    for(var i=0; i<ca.length; i++) {
5
	var c = ca[i];
6
	while (c.charAt(0)==' ') c = c.substring(1);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
7
	if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
8
    }
9
    return "";
10
}
11
12
function delCookie(cname) {
13
    document.cookie = cname + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
14
}
15
16
function createCookie(name, value, days) {
17
    var date, expires;
18
    if (days) {
19
	date = new Date();
20
	date.setTime(date.getTime()+(days*24*60*60*1000));
21
	expires = "; expires="+date.toGMTString();
22
    } else {
23
	expires = "";
24
    }
25
    document.cookie = name+"="+value+expires+"; path=/";
26
}
27
28
function mapType(selectObj) {
29
    var idx = selectObj.selectedIndex;
30
    var atype = selectObj.options[idx].value;
31
    var type = atype.split('-');
32
    if (type[0] == 'Mapbox') {
33
	createCookie('MapType',type[0],9999);
34
	createCookie('MapTypeId',type[1],9999);
35
	if (getCookie('Map2D3DSync')) {
36
	    createCookie('MapType3D',type[0],9999);
37
	    createCookie('MapType3DId',type[1],9999);
38
	}
39
    } else {
40
	createCookie('MapType',atype,9999);
41
	if (getCookie('Map2D3DSync')) {
42
	    createCookie('MapType3D',atype,9999);
43
	}
44
    }
45
    window.location.reload();
46
}
47
function mapType3D(selectObj) {
48
    var idx = selectObj.selectedIndex;
49
    var atype = selectObj.options[idx].value;
50
    var type = atype.split('-');
51
    if (type[0] == 'Mapbox') {
52
	createCookie('MapType3D',type[0],9999);
53
	createCookie('MapType3DId',type[1],9999);
54
	if (getCookie('Map2D3DSync')) {
55
	    createCookie('MapType',type[0],9999);
56
	    createCookie('MapTypeId',type[1],9999);
57
	}
58
    } else {
59
	createCookie('MapType3D',atype,9999);
60
	if (getCookie('Map2D3DSync')) {
61
	    createCookie('MapType',atype,9999);
62
	}
63
    }
64
    window.location.reload();
65
}
66
function clickSyncMap2D3D(cb) {
67
    createCookie('Map2D3DSync',cb.checked,9999);
68
    if (cb.checked) {
69
	createCookie('MapType3D',getCookie('MapType'),9999);
70
	createCookie('MapType3DId',getCookie('MapTypeId'),9999);
71
    }
72
}
73
74
function terrainType(selectObj) {
75
    var idx = selectObj.selectedIndex;
76
    var atype = selectObj.options[idx].value;
77
    var type = atype.split('-');
78
    document.cookie =  'MapTerrain='+type+'; expires=Thu, 2 Aug 2100 20:47:11 UTC; path=/'
79
    createCookie('MapTerrain',type,9999);
80
    if (type == 'stk') {
81
	stkterrain();
82
    } else if (type == 'articdem') {
83
	articterrain();
84
    } else if (type == 'ellipsoid') {
85
	ellipsoidterrain();
86
    } else if (type == 'vrterrain') {
87
	vrtheworldterrain();
88
    }
89
    //window.location.reload();
90
}
91
92
function sattypes(selectObj) {
93
    var sattypes = [], sattype;
94
    for (var i=0, len=selectObj.options.length; i< len;i++) {
95
	sattype = selectObj.options[i];
96
	if (sattype.selected) {
97
	    sattypes.push(sattype.value);
98
	}
99
    }
100
    createCookie('sattypes',sattypes.join(),2);
101
    updateSat();
102
}
103
function airlines(selectObj) {
104
    var airs = [], air;
105
    for (var i=0, len=selectObj.options.length; i< len;i++) {
106
	air = selectObj.options[i];
107
	if (air.selected) {
108
	    airs.push(air.value);
109
	}
110
    }
111
    createCookie('filter_Airlines',airs.join(),2);
112
}
113
function airlinestype(selectObj) {
114
    var idx = selectObj.selectedIndex;
115
    var airtype = selectObj.options[idx].value;
116
    createCookie('filter_airlinestype',airtype,2);
117
}
118
function racefilter(selectObj) {
119
    var idx = selectObj.selectedIndex;
120
    var race = selectObj.options[idx].value;
121
    if (race == 'all') {
122
	delCookie('filter_race');
123
    } else {
124
	createCookie('filter_race',race,2);
125
    }
126
}
127
function alliance(selectObj) {
128
    var idx = selectObj.selectedIndex;
129
    var alliance = selectObj.options[idx].value;
130
    createCookie('filter_alliance',alliance,2);
131
}
132
function identfilter() {
133
    var ident = $("#identfilter").value;
134
    createCookie('filter_ident',ident,2);
135
}
136
function mmsifilter() {
137
    var ident = $("#mmsifilter").value;
138
    createCookie('filter_mmsi',ident,2);
139
}
140
function removefilters() {
141
    // Get an array of all cookie names (the regex matches what we don't want)
142
    var cookieNames = document.cookie.split(/=[^;]*(?:;\s*|$)/);
143
    // Remove any that match the pattern
144
    for (var i = 0; i < cookieNames.length; i++) {
145
	if (/^filter_/.test(cookieNames[i])) {
146
	    delCookie(cookieNames[i]);
147
	}
148
    }
149
    window.location.reload();
150
}
151
function sources(selectObj) {
152
    var sources = [], source;
153
    for (var i=0, len=selectObj.options.length; i< len;i++) {
154
	source = selectObj.options[i];
155
	if (source.selected) {
156
	    sources.push(source.value);
157
	}
158
    }
159
    createCookie('filter_Sources',sources.join(),2);
160
}
161
162
163
function show2D() {
164
    createCookie('MapFormat','2d',10);
165
    if (document.getElementById("pointtype").className == 'tracker') {
166
	createCookie('MapTrackTracker',document.getElementById("pointident").className,1);
167
    } else if (document.getElementById("pointtype").className == 'marine') {
168
	createCookie('MapTrackMarine',document.getElementById("pointident").className,1);
169
    } else {
170
	createCookie('MapTrack',document.getElementById("pointident").className,1);
171
    }
172
    window.location.reload();
173
}
174
function show3D() {
175
    createCookie('MapFormat','3d',10);
176
    if (document.getElementById("pointtype").className == 'tracker') {
177
	createCookie('MapTrackTracker',document.getElementById("pointident").className,1);
178
    } else if (document.getElementById("pointtype").className == 'marine') {
179
	createCookie('MapTrackMarine',document.getElementById("pointident").className,1);
180
    } else {
181
	createCookie('MapTrack',document.getElementById("pointident").className,1);
182
    }
183
    window.location.reload();
184
}
185
function clickPolar(cb) {
186
    createCookie('polar',cb.checked,9999);
187
    window.location.reload();
188
}
189
function clickDisplayAirports(cb) {
190
    createCookie('displayairports',cb.checked,9999);
191
    window.location.reload();
192
}
193
function clickDisplayISS(cb) {
194
    createCookie('displayiss',cb.checked,9999);
195
    updateSat();
196
}
197
function clickDisplayMinimap(cb) {
198
    createCookie('displayminimap',cb.checked,9999);
199
    window.location.reload();
200
}
201
function clickShadows(cb) {
202
    createCookie('map3dnoshadows',cb.checked,9999);
203
    window.location.reload();
204
}
205
function clickSingleModel(cb) {
206
    createCookie('singlemodel',cb.checked,9999);
207
}
208
function clickUpdateRealtime(cb) {
209
    createCookie('updaterealtime',cb.checked,9999);
210
}
211
function clickVATSIM(cb) {
212
    createCookie('filter_ShowVATSIM',cb.checked,2);
213
}
214
function clickIVAO(cb) {
215
     createCookie('filter_ShowIVAO',cb.checked,2);
216
}
217
function clickphpVMS(cb) {
218
    createCookie('filter_ShowVMS',cb.checked,2);
219
}
220
function clickSBS1(cb) {
221
    createCookie('filter_ShowSBS1',cb.checked,2);
222
}
223
function clickAPRS(cb) {
224
    createCookie('filter_ShowAPRS',cb.checked,2);
225
}
226
function clickDisplayGroundStation(cb) {
227
    createCookie('show_GroundStation',cb.checked,2);
228
    window.location.reload();
229
}
230
function clickDisplayWeatherStation(cb) {
231
    createCookie('show_WeatherStation',cb.checked,2);
232
    window.location.reload();
233
}
234
function clickDisplayWeather(cb) {
235
    createCookie('show_Weather',cb.checked,2);
236
//    window.location.reload();
237
}
238
function clickDisplayLightning(cb) {
239
    createCookie('show_Lightning',cb.checked,2);
240
    window.location.reload();
241
}
242
function clickDisplayFires(cb) {
243
    createCookie('show_Fires',cb.checked,2);
244
    window.location.reload();
245
}
246
function clickDisplay2DBuildings(cb) {
247
    createCookie('Map2DBuildings',cb.checked,2);
248
    window.location.reload();
249
}
250
function unitdistance(selectObj) {
251
    var idx = selectObj.selectedIndex;
252
    var unit = selectObj.options[idx].value;
253
    createCookie('unitdistance',unit,9999);
254
}
255
function unitspeed(selectObj) {
256
    var idx = selectObj.selectedIndex;
257
    var unit = selectObj.options[idx].value;
258
    createCookie('unitspeed',unit,9999);
259
}
260
function unitaltitude(selectObj) {
261
    var idx = selectObj.selectedIndex;
262
    var unit = selectObj.options[idx].value;
263
    createCookie('unitaltitude',unit,9999);
264
}
265
function addarchive(begindate,enddate) {
266
    console.log('Add archive');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
267
    createCookie('archive',true,2);
268
    createCookie('archive_begin',begindate,2);
269
    createCookie('archive_end',enddate,2);
270
    createCookie('archive_speed',document.getElementById("archivespeed").value,2);
271
    window.location.reload();
272
}
273
function noarchive() {
274
    console.log('Exit archive!');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
275
    delCookie('archive');
276
    delCookie('archive_begin');
277
    delCookie('archive_end');
278
    delCookie('archive_speed');
279
    window.location.reload();
280
}
281
function msgbox(text,buttontext) {
282
	buttontext = buttontext || "OK";
283
	$("<div>" + text + "</div>").dialog({
284
	    dialogClass: "no-close",
285
	    buttons: [{
286
		text: buttontext,
287
		click: function() {
288
		    $( this ).dialog( "close" );
289
		    $(this).remove();
290
		}
291
	    }]
292
	});
293
}
294
function generateRandomPoint (latitude,longitude,height,diff,radius) {
295
296
	//console.log('height: '+height+' - diff: '+diff);
297
	radius = Math.random()*radius;
298
	latitude = latitude*(Math.PI/180.0);
299
	longitude = longitude*(Math.PI/180.0);
300
	
301
	const sinLat = 	Math.sin(latitude)
302
	const cosLat = 	Math.cos(latitude)
303
304
	/* go fixed distance in random direction*/
305
	const bearing = Math.random() * Math.PI*2
306
	const theta = radius/6371000
307
	const sinBearing = Math.sin(bearing)
308
	const cosBearing = Math.cos(bearing)
309
	const sinTheta = Math.sin(theta)
310
	const cosTheta = Math.cos(theta)
311
    
312
	latitude = Math.asin(sinLat*cosTheta+cosLat*sinTheta*cosBearing);
313
	longitude = longitude + Math.atan2( sinBearing*sinTheta*cosLat, cosTheta-sinLat*Math.sin(latitude ));
314
	/* normalize -PI -> +PI radians */
315
	longitude = ((longitude+(Math.PI*3))%(Math.PI*2))-Math.PI
316
	var h = height+(Math.random()*diff)
317
	//console.log('h: '+h);
318
	return {
319
	    latitude: latitude/(Math.PI/180.0),
320
	    longitude: longitude/(Math.PI/180.0),
321
	    height: h
322
	};
323
}
324
function getColor(colorStart,colorEnd,colorCount,step) {
325
	var alpha = (1.0/colorCount)*step;
326
	return {
327
	    r: colorStart[0]*alpha+(1-alpha)*colorEnd[0],
328
	    v: colorStart[1]*alpha+(1-alpha)*colorEnd[1],
329
	    b: colorStart[2]*alpha+(1-alpha)*colorEnd[2]
330
	};
331
}