Failed Conditions
Pull Request — master (#177)
by PoUpA
05:08 queued 02:35
created

trainer.content.js ➔ ... ➔ $.getJSON(ꞌcore/json/variables.jsonꞌ)   B

Complexity

Conditions 1
Paths 4

Size

Total Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 4
nop 1
dl 0
loc 81
rs 8.8076

5 Functions

Rating   Name   Duplication   Size   Complexity  
B trainer.content.js ➔ ... ➔ $(ꞌ.rankingOrderItemsꞌ).click 0 19 5
A trainer.content.js ➔ ... ➔ $(ꞌ#searchTrainerꞌ).submit 0 8 2
A trainer.content.js ➔ ... ➔ win.scroll 0 7 2
B trainer.content.js ➔ ... ➔ $(ꞌ.teamSelectorItemsꞌ).click 0 22 6
A trainer.content.js ➔ ... ➔ window.onpopstate 0 10 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/** Global: trainerName */
2
3
$(function () {
4
	$.getJSON( "core/json/variables.json", function( jsondata ) {
5
		var pokeimg_suffix=jsondata['system']['pokeimg_suffix'];
6
7
		$('.trainerLoader').hide();
8
		var page = 0;
9
		var teamSelector=0; //0=all;1=Blue;2=Red;3=Yellow
10
		var rankingFilter=0; //0=Level & Gyms; 1=Level; 2=Gyms
11
		
12
		$('input#name').val(trainerName);
13
		loadTrainers(page,$('input#name').val(),null,null,pokeimg_suffix,true);
14
		
15
		page++;
16
		var win = $(window);
17
		win.scroll(function () {
18
			// End of the document reached?
19
			if ($(document).height() - win.height() == win.scrollTop()) {
20
				loadTrainers(page,$('input#name').val(),teamSelector,rankingFilter,pokeimg_suffix,true);
21
				page++;
22
			}
23
		});
24
		$("#searchTrainer").submit(function ( event ) {
25
			page = 0;
26
			$('input#name').val()!=''?$('#trainersGraph').hide():$('#trainersGraph').show();
27
			$('#trainersContainer tr:not(.trainersTemplate)').remove();
28
			loadTrainers(page,$('input#name').val(),teamSelector,rankingFilter,pokeimg_suffix,true);
29
			page++;
30
			event.preventDefault();
31
		});
32
		$(".teamSelectorItems").click(function ( event ) {
33
			switch ($(this).attr("id")) {
34
				case "AllTeamsFilter":
35
					teamSelector=0;
36
					break;
37
				case "BlueTeamFilter":
38
					teamSelector=1;
39
					break;
40
				case "RedTeamFilter":
41
					teamSelector=2;
42
					break;
43
				case "YellowFilter":
44
					teamSelector=3;
45
					break;
46
				default:
47
					teamSelector=0;
48
			}
49
			$("#teamSelectorText").html($(this).html());
50
			event.preventDefault();
51
			$("#searchTrainer").submit();
52
			
53
		});
54
		$(".rankingOrderItems").click(function ( event ) {
55
			switch ($(this).attr("id")) {
56
				case "levelsFirst":
57
					rankingFilter=0;
58
					break;
59
				case "gymsFirst":
60
					rankingFilter=1;
61
					break;
62
				case "maxCpFirst":
63
					rankingFilter=2;
64
					break;
65
				default:
66
					rankingFilter=0;
67
			}
68
			$("#rankingOrderText").html($(this).html());
69
			event.preventDefault();
70
			$("#searchTrainer").submit();
71
			
72
		});
73
		window.onpopstate = function(event) {
0 ignored issues
show
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
74
			if (window.history.state && "Trainer" === window.history.state.page) {
75
				$('#trainersContainer').empty();
76
				$('input#name').val(window.history.state.name);
77
				loadTrainers(0,$('input#name').val(),teamSelector,rankingFilter,pokeimg_suffix, false);
78
			}
79
			else{
80
				history.back();
0 ignored issues
show
Bug introduced by
The variable history seems to be never declared. If this is a global, consider adding a /** global: history */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
81
			}
82
		};
83
		
84
	});
85
});
86
87
function loadTrainers(page,name,teamSelector,rankingFilter,pokeimg_suffix,stayOnPage) {
88
	$('.trainerLoader').show();
89
	if (stayOnPage) {
90
		// build a state for this name
91
		var state = {name: name, page: 'Trainer'};
92
		window.history.pushState(state,'Trainer',"trainer?name="+name);
93
	}
94
	var trainerIndex = 0+(page*10);
95
	$.ajax({
96
		'async': true,
97
		'type': "GET",
98
		'global': false,
99
		'dataType': 'json',
100
		'url': "core/process/aru.php",
101
		'data': {
102
			'request': "",
103
			'target': 'arrange_url',
104
			'method': 'method_target',
105
			'type' : 'trainer',
106
			'page' : page,
107
			'name' : name,
108
			'team' : teamSelector,
109
			'ranking' :rankingFilter
110
		}
111
	}).done(function (data) {
112
		$.each(data, function (trainerName, trainer) {
113
			trainerIndex++;
114
			printTrainer(trainer, trainerIndex,pokeimg_suffix);
115
		});
116
		$('.trainerLoader').hide();
117
	});
118
};
119
120
121
122
function printTrainer(trainer, trainerIndex,pokeimg_suffix) {
123
	var trainersInfos = $('<tr>',{id: 'trainerInfos_'+trainer.name}).css('border-bottom','2px solid '+(trainer.team=="3"?"#ffbe08":trainer.team=="2"?"#ff7676":"#00aaff"));
124
	trainersInfos.append($('<td>',{id : 'trainerIndex_'+trainer.name, text : trainerIndex}));
125
	trainersInfos.append($('<td>',{id : 'trainerRank_'+trainer.name, text : trainer.rank}));
126
	trainersInfos.append($('<td>',{id : 'trainerName_'+trainer.name}).append($('<a>',{href: 'trainer?name='+trainer.name, text: trainer.name})).click(
127
		function (e) {
128
			e.preventDefault();
129
			$('input#name').val(trainer.name);
130
			$("#searchTrainer").submit();
131
			$('#trainerName_'+trainer.name).off('click');
132
		}
133
	));
134
	trainersInfos.append($('<td>',{id : 'trainerLevel_'+trainer.name, text : trainer.level}));
135
	trainersInfos.append($('<td>',{id : 'trainerGyms_'+trainer.name, text : trainer.gyms}));
136
	trainersInfos.append($('<td>',{id : 'trainerLastSeen_'+trainer.name, text : trainer.last_seen}));
137
	$('#trainersContainer').append(trainersInfos);
138
	var trainersPokemonsRow = $('<tr>',{id: 'trainerPokemons_'+trainer.name});
139
	var trainersPokemons = $('<td>',{colspan : 6});
140
	var trainersPokemonsContainer = $('<div>',{class : ""});
141
	for (var pokeIndex = 0; pokeIndex<trainer.pokemons.length; pokeIndex++) {
142
		var pokemon = trainer.pokemons[pokeIndex];
143
		
144
		trainersPokemonsContainer.append(printPokemon(pokemon,pokeimg_suffix));
145
	}
146
147
	trainersPokemons.append(trainersPokemonsContainer);
148
	trainersPokemonsRow.append(trainersPokemons);
149
	$('#trainersContainer').append(trainersPokemonsRow);
150
}
151
152
function printPokemon(pokemon,pokeimg_suffix){
153
	var trainerPokemon = $('<div>',{id : 'trainerPokemon_'+pokemon.pokemon_uid, class: "col-md-1 col-xs-4 pokemon-single", style: "text-align: center" });
154
	var gymClass = "";
155
	if ((pokemon.gym_id===null)) {
156
		gymClass = "unseen";
157
	}
158
	trainerPokemon.append(
159
		$('<a>',
160
			{href : 'pokemon/'+pokemon.pokemon_id}
161
		).append($('<img />',
162
			{	src : 'core/pokemons/'+pokemon.pokemon_id+pokeimg_suffix,
163
				'class' : 'img-responsive '+gymClass
164
			})
165
		)
166
	);
167
	trainerPokemon.append($('<p>',{class : 'pkmn-name'}).append(pokemon.cp));
168
	var progressBar = $('<div>',{class : 'progress'}).css({'height': '6px','margin-bottom': '0'});
169
	progressBar.append(
170
		$('<div>',
171
			{
172
			title: 'IV Attack :'+pokemon.iv_attack, 
173
			class: 'progress-bar progress-bar-danger' ,
174
			role : 'progressbar', 
175
			'aria-valuenow' : pokemon.iv_attack, 
176
			'aria-valuemin' : 0, 
177
			'aria-valuemax' : 45
178
		}).css('width',((100/45)*pokemon.iv_attack ) + '%'))
179
	progressBar.append(
180
		$('<div>',
181
			{
182
			title: 'IV Defense :'+pokemon.iv_defense,
183
			class: 'progress-bar progress-bar-info' ,
184
			role : 'progressbar', 
185
			'aria-valuenow': pokemon.iv_defense, 
186
			'aria-valuemin' : 0, 
187
			'aria-valuemax' : 45
188
		}).css('width',((100/45)*pokemon.iv_defense ) + '%'))
189
	progressBar.append(
190
		$('<div>',
191
			{
192
			title: 'IV Stamina :'+pokemon.iv_stamina, 
193
			class: 'progress-bar progress-bar-success' ,
194
			role : 'progressbar',
195
			'aria-valuenow' :pokemon.iv_stamina,
196
			'aria-valuemin' : 0, 
197
			'aria-valuemax' : 45
198
		}).css('width',((100/45)*pokemon.iv_stamina ) + '%'))
199
	trainerPokemon.append(progressBar);
200
	if (pokemon.last_scanned === '0') {
201
		trainerPokemon.append($('<small>',{text: "Today"}));
202
	}
203
	else if (pokemon.last_scanned === '1') {
204
		trainerPokemon.append($('<small>',{text: pokemon.last_scanned + " Day"}));
205
	}
206
	else {
207
		trainerPokemon.append($('<small>',{text: pokemon.last_scanned + " Days"}));
208
	}
209
	return trainerPokemon;
210
}
211