Passed
Push — master ( 1a2ff5...56b2b9 )
by Markus
02:29
created

core/js/gymhistory.content.js   C

Complexity

Total Complexity 54
Complexity/F 2.45

Size

Lines of Code 217
Function Count 22

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 4
dl 0
loc 217
rs 6.8539
wmc 54
mnd 3
bc 32
fnc 22
bpm 1.4544
cpm 2.4545
noi 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A gymhistory.content.js ➔ hideGymHistoryTables 0 6 1
B gymhistory.content.js ➔ loadGyms 0 34 2
B gymhistory.content.js ➔ printGymHistory 0 11 9
B gymhistory.content.js ➔ loadGymHistory 0 29 1
C gymhistory.content.js ➔ printGym 0 28 8
A gymhistory.content.js ➔ printPokemonList 0 11 1

How to fix   Complexity   

Complexity

Complex classes like core/js/gymhistory.content.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
/** global: gymName */
2
3
$(function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4
	function htmlspecialchars_decode(string) {
5
		var escapeMap = {
6
			"&": "&",
7
			"&lt;": "<",
8
			"&gt;": ">",
9
			"&quot;": "\"",
10
			"&#39;": "'",
11
			"&#039;": "'"
12
		};
13
		return String(string).replace(/&(amp|lt|gt|quot|#0?39);/gi, function (s) {
14
			return escapeMap[s] || s;
15
		});
16
	}
17
18
	$.getJSON("core/json/variables.json", function(variables) {
19
		var pokeimg_suffix = variables['system']['pokeimg_suffix'];
20
		var hide_cp_changes = variables['system']['gymhistory_hide_cp_changes'];
21
22
		$('.gymLoader').hide();
23
24
		var page = 0;
25
		var teamSelector = ''; //''=all; 0=neutral; 1=Blue; 2=Red; 3=Yellow
26
		var rankingFilter = 0; //0=Level & Gyms; 1=Level; 2=Gyms
27
28
		$('input#name').filter(':visible').val(htmlspecialchars_decode(gymName));
29
30
		$('#loadMoreButton').click(function () {
31
			loadGyms(page, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, hide_cp_changes, true);
32
			page++;
33
		}).trigger('click');
34
35
		$('#searchGyms').submit(function ( event ) {
36
			page = 0;
37
			$('#gymsContainer').empty();
38
			$('#loadMoreButton').trigger('click');
39
			event.preventDefault();
40
		});
41
42
		$('.teamSelectorItems').click(function ( event ) {
43
			switch ($(this).attr('id')) {
44
				case 'NeutralTeamsFilter':
45
					teamSelector=0;
46
					break;
47
				case 'BlueTeamFilter':
48
					teamSelector=1;
49
					break;
50
				case 'RedTeamFilter':
51
					teamSelector=2;
52
					break;
53
				case 'YellowFilter':
54
					teamSelector=3;
55
					break;
56
				default:
57
					teamSelector='';
58
			}
59
			$('#teamSelectorText').html($(this).html());
60
			event.preventDefault();
61
			$('#searchGyms').submit();
62
63
		});
64
		$('.rankingOrderItems').click(function ( event ) {
65
			switch ($(this).attr('id')) {
66
				case 'changedFirst':
67
					rankingFilter=0;
68
					break;
69
				case 'nameFirst':
70
					rankingFilter=1;
71
					break;
72
				case 'totalcpFirst':
73
					rankingFilter=2;
74
					break;
75
				default:
76
					rankingFilter=0;
77
			}
78
			$('#rankingOrderText').html($(this).html());
79
			event.preventDefault();
80
			$('#searchGyms').submit();
81
		});
82
		window.onpopstate = function() {
83
			if (window.history.state && 'gymhistory' === window.history.state.page) {
84
				$('input#name').filter(':visible').val(window.history.state.name);
85
				page = 0;
86
				$('#gymsContainer').empty();
87
				loadGyms(page, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, hide_cp_changes, pokeimg_suffix, false);
88
				page++;
89
			} else {
90
				window.history.back();
91
			}
92
		};
93
	});
94
});
95
96
function loadGyms(page, name, teamSelector, rankingFilter, pokeimg_suffix, hide_cp_changes, stayOnPage) {
97
	$('.gymLoader').show();
98
	if (stayOnPage) {
99
		// build a state for this name
100
		var state = {name: name, page: 'gymhistory'};
101
		window.history.pushState(state, 'gymhistory', 'gymhistory?name=' + name);
102
	}
103
	$.ajax({
104
		'async': true,
105
		'type': 'GET',
106
		'global': false,
107
		'dataType': 'json',
108
		'url': 'core/process/aru.php',
109
		'data': {
110
			'type' : 'gyms',
111
			'page' : page,
112
			'name' : name,
113
			'team' : teamSelector,
114
			'ranking' :rankingFilter
115
		}
116
	}).done(function (data) {
117
		var internalIndex = 0;
118
		$.each(data.gyms, function (idx, gym) {
119
			internalIndex++
120
			printGym(gym, pokeimg_suffix, hide_cp_changes);
121
		});
122
		if (internalIndex < 10) {
123
			$('#loadMoreButton').hide();
124
		} else {
125
			$('#loadMoreButton').removeClass('hidden').show();
126
		}
127
		$('.gymLoader').hide();
128
	});
129
}
130
131
function loadGymHistory(page, gym_id, pokeimg_suffix, hide_cp_changes) {
132
	$('#gymHistory_'+gym_id).addClass('active').show();
133
	$('#gymHistory_'+gym_id).find('.gymHistoryLoader').show();
134
	$.ajax({
135
		'async': true,
136
		'type': 'GET',
137
		'global': false,
138
		'dataType': 'json',
139
		'url': 'core/process/aru.php',
140
		'data': {
141
			'type' : 'gymhistory',
142
			'page' : page,
143
			'gym_id' : gym_id
144
		}
145
	}).done(function (data) {
146
		var internalIndex = 0;
147
		$.each(data.entries, function(idx, entry) {
148
			internalIndex++
149
			if (entry.only_cp_changed && hide_cp_changes) return;
150
			printGymHistory(gym_id, entry, pokeimg_suffix);
151
		});
152
		if (internalIndex < 10) {
153
			$('#gymHistory_'+gym_id).find('.loadMoreButtonHistory').hide();
154
		} else {
155
			$('#gymHistory_'+gym_id).find('.loadMoreButtonHistory').removeClass('hidden').data('page', page+1).show();
156
		}
157
		$('#gymHistory_'+gym_id).find('.gymHistoryLoader').hide();
158
	});
159
}
160
161
function printPokemonList(pokemons, pokeimg_suffix) {
162
	var gymPokemon = $('<ul>',{class: 'list-inline'});
163
	$.each(pokemons, function(idx, pokemon) {
164
		var list = $('<li>', {class: pokemon.class});
165
		list.append($('<a>', { class: 'no-link', href : 'pokemon/'+pokemon.pokemon_id }).append($('<img />', { src: 'core/pokemons/'+pokemon.pokemon_id+pokeimg_suffix }).css('height', '2em')));
166
		list.append($('<br><span class="small">'+pokemon.cp+' CP</span>'));
167
		list.append($('<br><span style="font-size:70%"><a href="trainer?name='+pokemon.trainer_name+'" class="no-link">'+pokemon.trainer_name+'</a></span>'));
168
		gymPokemon.append(list);
169
	});
170
	return gymPokemon;
171
}
172
173
function printGymHistory(gym_id, entry, pokeimg_suffix) {
174
	var gymHistory = $('<tr>').css('border-bottom', '2px solid '+(entry.team_id=='3'?'#ffbe08':entry.team_id=='2'?'#ff7676':entry.team_id=='1'?'#00aaff':'#ddd'));
175
	gymHistory.append($('<td>',{text: entry.last_modified}));
176
	gymHistory.append($('<td>',{text: entry.pokemon_count, class: 'level'}).prepend($('<img />', {src:'core/img/map_'+(entry.team_id=='1'?'blue':entry.team_id=='2'?'red':entry.team_id=='3'?'yellow':'white')+'.png'})));
177
	gymHistory.append($('<td>',{text: parseInt(entry.total_cp).toLocaleString('de-DE'), class: entry.class}).append(
178
		entry.total_cp_diff !== 0 ? $('<span class="small"> ('+(entry.total_cp_diff > 0 ? '+' : '')+entry.total_cp_diff+')</span>') : null
179
	));
180
	var gymPokemon = printPokemonList(entry.pokemon, pokeimg_suffix);
181
	gymHistory.append($('<td>').append(gymPokemon));
182
	$('#gymHistory_'+gym_id).find('tbody').append(gymHistory);
183
}
184
185
function hideGymHistoryTables(gymHistoryTables) {
186
	gymHistoryTables.removeClass('active').hide();
187
	gymHistoryTables.find('tbody tr').remove();
188
	gymHistoryTables.find('.loadMoreButtonHistory').hide();
189
	gymHistoryTables.find('.gymHistoryLoader').hide();
190
}
191
192
function printGym(gym, pokeimg_suffix, hide_cp_changes) {
193
	var gymsInfos = $('<tr>',{id: 'gymInfos_'+gym.gym_id}).css('cursor', 'pointer').css('border-bottom', '2px solid '+(gym.team_id=='3'?'#ffbe08':gym.team_id=='2'?'#ff7676':gym.team_id=='1'?'#00aaff':'#ddd')).click(function() {
194
		if (!$('#gymHistory_'+gym.gym_id).hasClass('active')) {
195
			hideGymHistoryTables($('#gymsContainer').find('.gymhistory'));
196
			loadGymHistory(0, gym.gym_id, pokeimg_suffix, hide_cp_changes);
197
		} else {
198
			hideGymHistoryTables($('#gymHistory_'+gym.gym_id));
199
		}
200
	});
201
	gymsInfos.append($('<td>',{text: gym.last_modified}));
202
	if (gym.name.length > 50) { gym.name = gym.name.substr(0, 50) + '…'; }
203
	gymsInfos.append($('<td>',{text: gym.name}));
204
	gymsInfos.append($('<td>',{text: gym.pokemon_count, class: 'level'}).prepend($('<img />', {src:'core/img/map_'+(gym.team_id=='1'?'blue':gym.team_id=='2'?'red':gym.team_id=='3'?'yellow':'white')+'.png'})));
205
	gymsInfos.append($('<td>',{text: parseInt(gym.total_cp).toLocaleString('de-DE')}));
206
	var gymPokemon = printPokemonList(gym.pokemon, pokeimg_suffix);
207
	gymsInfos.append($('<td>').append(gymPokemon));
208
	$('#gymsContainer').append(gymsInfos);
209
	var historyTable = $('<table>',{class: 'table'});
210
	historyTable.append('<thead><tr><th style="min-width:7em">Time</th><th>Level</th><th>Total CP</th><th>Pokémon</th></tr></thead>');
211
	historyTable.append('<tbody></tbody>');
212
	historyTable.append('<tfoot><tr class="loadMore text-center"><td colspan="4"><button class="loadMoreButtonHistory btn btn-default btn-sm hidden">Load more</button></td></tr><tr class="gymHistoryLoader"><td colspan="4"><div class="loader"></div></td></tr></tfoot>');
213
	historyTable.find('.loadMoreButtonHistory').data('page', 0).click(function() {
214
		loadGymHistory($(this).data('page'), gym.gym_id, pokeimg_suffix, hide_cp_changes);
215
	});
216
	var row = $('<td>',{colspan: 6});
217
	row.append(historyTable);
218
	$('#gymsContainer').append($('<tr>', {id: 'gymHistory_'+gym.gym_id, class: 'gymhistory'}).hide().append(row));
219
}
220