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

trainer.content.js ➔ ... ➔ String.replace   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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