Passed
Pull Request — master (#366)
by
unknown
11:31
created

trainer.content.js ➔ ... ➔ trainersInfos.click   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
/** global: trainerName */
2
$(function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3
4
	$.getJSON('core/json/variables.json', function(variables) {
5
		var pokeimg_suffix = variables['system']['pokeimg_suffix'];
6
		var iv_numbers = variables['system']['iv_numbers'];
7
8
		$('.trainerLoader').hide();
9
		var page = 0;
10
		var teamSelector = 0; //0=all;1=Blue;2=Red;3=Yellow
11
		var rankingFilter = 0; //0=Level & Gyms; 1=Level; 2=Gyms
12
13
		$('input#name').filter(':visible').val(trainerName);
14
		loadTrainers(page, $('input#name').filter(':visible').val(), null, null, pokeimg_suffix, true, iv_numbers);
15
		if ($('input#name')) {
16
			$('input#name').filter(':visible').val() != '' ? $('#trainersGraph').hide() : $('#trainersGraph').show();
17
			$('#trainersContainer tr:not(.trainersTemplate)').remove();
18
		}
19
20
		page++;
21
		$('#loadMoreButton').click(function() {
22
			loadTrainers(page, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, true, iv_numbers);
23
			page++;
24
		});
25
		$('#searchTrainer').submit(function(event) {
26
			page = 0;
27
			$('input#name').filter(':visible').val() != '' ? $('#trainersGraph').hide() : $('#trainersGraph').show();
28
			$('#trainersContainer tr:not(.trainersTemplate)').remove();
29
			loadTrainers(page, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, true, iv_numbers);
30
			page++;
31
			event.preventDefault();
32
		});
33
		$('.teamSelectorItems').click(function(event) {
34
			switch ($(this).attr('id')) {
35
				case 'AllTeamsFilter':
36
					teamSelector = 0;
37
					break;
38
				case 'BlueTeamFilter':
39
					teamSelector = 1;
40
					break;
41
				case 'RedTeamFilter':
42
					teamSelector = 2;
43
					break;
44
				case 'YellowFilter':
45
					teamSelector = 3;
46
					break;
47
				default:
48
					teamSelector = 0;
49
			}
50
			$('#teamSelectorText').html($(this).html());
51
			event.preventDefault();
52
			$('#searchTrainer').submit();
53
54
		});
55
		$('.rankingOrderItems').click(function(event) {
56
			switch ($(this).attr('id')) {
57
				case 'levelsFirst':
58
					rankingFilter = 0;
59
					break;
60
				case 'gymsFirst':
61
					rankingFilter = 1;
62
					break;
63
				case 'maxCpFirst':
64
					rankingFilter = 2;
65
					break;
66
				default:
67
					rankingFilter = 0;
68
			}
69
			$('#rankingOrderText').html($(this).html());
70
			event.preventDefault();
71
			$('#searchTrainer').submit();
72
73
		});
74
		window.onpopstate = function() {
75
			if (window.history.state && 'Trainer' === window.history.state.page) {
76
				$('#trainersContainer').empty();
77
				$('input#name').filter(':visible').val(window.history.state.name);
78
				loadTrainers(0, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, false, iv_numbers);
79
			} else {
80
				window.history.back();
81
			}
82
		};
83
84
	});
85
});
86
87
function loadTrainers(page, name, teamSelector, rankingFilter, pokeimg_suffix, stayOnPage, iv_numbers) {
88
	$('.trainerLoader').show();
89
90
	if (stayOnPage) {
91
		// build a state for this name
92
		var state = { name: name, page: 'Trainer' };
93
		window.history.pushState(state, 'Trainer', 'trainer?name=' + name);
94
	}
95
	var trainerIndex = 0 + (page * 10);
96
	$.ajax({
97
		'type': 'GET',
98
		'global': false,
99
		'dataType': 'json',
100
		'url': 'core/process/aru.php',
101
		'data': {
102
			'type': 'trainer',
103
			'page': page,
104
			'name': name,
105
			'team': teamSelector,
106
			'ranking': rankingFilter
107
		}
108
	}).done(function(data) {
109
		var internalIndex = 0;
110
		$.each(data.trainers, function(trainerName, trainer) {
111
			trainerIndex++;
112
			internalIndex++
113
			printTrainer(trainer, trainerIndex, pokeimg_suffix, iv_numbers, data.locale);
114
		});
115
		if (internalIndex < 10) {
116
			$('#loadMoreButton').hide();
117
		} else {
118
			$('#loadMoreButton').removeClass('hidden');
119
			$('#loadMoreButton').show();
120
		}
121
		$('.trainerLoader').hide();
122
	});
123
};
124
125
126
127
function printTrainer(trainer, trainerIndex, pokeimg_suffix, iv_numbers, locale) {
128
	var trainersInfos = $('<tr>', { id: 'trainerInfos_' + trainer.name }).css('border-bottom', '2px solid ' + (trainer.team == '3' ? '#ffbe08' : trainer.team == '2' ? '#ff7676' : '#00aaff'));
129
	trainersInfos.append($('<td>', { id: 'trainerIndex_' + trainer.name, text: trainerIndex }));
130
	trainersInfos.append($('<td>', { id: 'trainerRank_' + trainer.name, text: trainer.rank }));
131
	trainersInfos.append($('<td>', { id: 'trainerName_' + trainer.name }).append($('<a>', { href: 'trainer?name=' + trainer.name, text: trainer.name })).click(
132
		function(e) {
133
			e.preventDefault();
134
			$('input#name').filter(':visible').val(trainer.name);
135
			$('#searchTrainer').submit();
136
			$('#trainerName_' + trainer.name).off('click');
137
		}
138
	));
139
	trainersInfos.append($('<td>', { id: 'trainerLevel_' + trainer.name, text: trainer.level }));
140
	trainersInfos.append($('<td>', { id: 'trainerGyms_' + trainer.name, text: trainer.gyms }));
141
	trainersInfos.append($('<td>', { id: 'trainerLastSeen_' + trainer.name, text: trainer.last_seen }));
142
	trainersInfos.append($('<td>', { id: 'trainerShowAll_' + trainer.name }).append('<input type="checkbox" id="showAll">'));
143
	$('#trainersContainer').append(trainersInfos);
144
	var trainersPokemonsRow = $('<tr>', { id: 'trainerPokemons_' + trainer.name });
145
	var trainersPokemons = $('<td>', { colspan: 7 });
146
	var trainersPokemonsContainer = $('<div>', { class: '' });
147
	for (var pokeIndex = 0; pokeIndex < trainer.pokemons.length; pokeIndex++) {
148
		var pokemon = trainer.pokemons[pokeIndex];
149
		trainersPokemonsContainer.append(printPokemon(pokemon, pokeimg_suffix, iv_numbers, locale));
150
	}
151
	trainersInfos.find('#showAll').click(function() {
152
		trainersPokemonsContainer.find('div.pokemon-single.unseen').fadeToggle();
153
	});
154
	trainersPokemons.append(trainersPokemonsContainer);
155
	trainersPokemonsRow.append(trainersPokemons);
156
	$('#trainersContainer').append(trainersPokemonsRow);
157
}
158
159
function printPokemon(pokemon, pokeimg_suffix, iv_numbers, locale) {
160
	var gymClass = pokemon.gym_id === null ? ' unseen' : '';
161
	var trainerPokemon = $('<div>', { id: 'trainerPokemon_' + pokemon.pokemon_uid, class: 'col-md-1 col-xs-4 pokemon-single' + gymClass, style: 'text-align: center' });
162
	if (gymClass) {
163
		trainerPokemon.hide();
164
	}
165
	trainerPokemon.append(
166
		$('<a>', { href: 'pokemon/' + pokemon.pokemon_id }).append($('<img />', {
167
			src: 'core/pokemons/' + pokemon.pokemon_id + pokeimg_suffix,
168
			'class': 'img-responsive' + gymClass
169
		}))
170
	);
171
	trainerPokemon.append($('<p>', { class: 'pkmn-name' }).append(pokemon.cp));
172
	var progressBar
173
	if (iv_numbers) {
174
		progressBar = $('<div>', { class: 'progress' }).css({ 'height': '15px', 'margin-bottom': '0' });
175
		progressBar.append(
176
			$('<div>', {
177
				title: locale.ivAttack + ' :' + pokemon.iv_attack,
178
				class: 'progress-bar progress-bar-danger',
179
				role: 'progressbar',
180
				text: pokemon.iv_attack,
181
				'aria-valuenow': pokemon.iv_attack,
182
				'aria-valuemin': 0,
183
				'aria-valuemax': 45
184
			}).css({ 'width': (100 / 3) + '%', 'line-height': '16px' }))
185
		progressBar.append(
186
			$('<div>', {
187
				title: locale.ivDefense + ' :' + pokemon.iv_defense,
188
				class: 'progress-bar progress-bar-info',
189
				role: 'progressbar',
190
				text: pokemon.iv_defense,
191
				'aria-valuenow': pokemon.iv_defense,
192
				'aria-valuemin': 0,
193
				'aria-valuemax': 45
194
			}).css({ 'width': (100 / 3) + '%', 'line-height': '16px' }))
195
		progressBar.append(
196
			$('<div>', {
197
				title: locale.ivStamina + ' :' + pokemon.iv_stamina,
198
				class: 'progress-bar progress-bar-success',
199
				role: 'progressbar',
200
				text: pokemon.iv_stamina,
201
				'aria-valuenow': pokemon.iv_stamina,
202
				'aria-valuemin': 0,
203
				'aria-valuemax': 45
204
			}).css({ 'width': (100 / 3) + '%', 'line-height': '16px' }))
205
	} else {
206
		progressBar = $('<div>', { class: 'progress' }).css({ 'height': '6px', 'margin-bottom': '0' });
207
		progressBar.append(
208
			$('<div>', {
209
				title: locale.ivAttack + ' :' + pokemon.iv_attack,
210
				class: 'progress-bar progress-bar-danger',
211
				role: 'progressbar',
212
				'aria-valuenow': pokemon.iv_attack,
213
				'aria-valuemin': 0,
214
				'aria-valuemax': 45
215
			}).css('width', ((100 / 45) * pokemon.iv_attack) + '%'))
216
		progressBar.append(
217
			$('<div>', {
218
				title: locale.ivDefense + ' :' + pokemon.iv_defense,
219
				class: 'progress-bar progress-bar-info',
220
				role: 'progressbar',
221
				'aria-valuenow': pokemon.iv_defense,
222
				'aria-valuemin': 0,
223
				'aria-valuemax': 45
224
			}).css('width', ((100 / 45) * pokemon.iv_defense) + '%'))
225
		progressBar.append(
226
			$('<div>', {
227
				title: locale.ivStamina + ' :' + pokemon.iv_stamina,
228
				class: 'progress-bar progress-bar-success',
229
				role: 'progressbar',
230
				'aria-valuenow': pokemon.iv_stamina,
231
				'aria-valuemin': 0,
232
				'aria-valuemax': 45
233
			}).css('width', ((100 / 45) * pokemon.iv_stamina) + '%'))
234
	}
235
	trainerPokemon.append(progressBar);
236
	if (pokemon.deployment_time) {
237
		var diff = (new Date() - new Date(pokemon.deployment_time.replace(/-/g, '/'))) / 1000;
238
		trainerPokemon.append($('<small>', { text: parseInt(diff / 3600) + 'h ' + parseInt((diff / 60) % 60) + 'm' }));
239
	} else if (pokemon.last_scanned === '1') {
240
		trainerPokemon.append($('<small>', { text: pokemon.last_scanned + ' ' + locale.day }));
241
	} else {
242
		trainerPokemon.append($('<small>', { text: pokemon.last_scanned + ' ' + locale.days }));
243
	}
244
	return trainerPokemon;
245
}