Passed
Push — master ( ab5ba7...172c83 )
by Markus
05:47
created

gymhistory.content.js ➔ loadGymHistory   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 29
rs 8.8571

1 Function

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