Subversion Repositories eFlore/Applications.cel

Rev

Rev 2707 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2707 mathias 1
// Héritage
2
function WidgetSaisieFlorileges() {
3
	this.latLngDebPre = {lat: null, lng: null};
4
	this.latLngFinPre = {lat: null, lng: null};
5
	this.okPourChargementCarte = 2;
6
	this.markerDeb = null;
7
	this.latLngDeb = null;
8
	this.markerFin = null;
9
	this.latLngFin = null;
10
	this.ligneRue = null;
11
	this.googleMapMarqueurDebutUrl = null;
12
	this.googleMapMarqueurFinUrl = null;
13
	this.premierDeplacement = true;
14
}
2708 mathias 15
WidgetSaisieFlorileges.prototype = new WidgetSaisieSauvages();
2707 mathias 16
 
2708 mathias 17
 
18
// @TODO harmoniser avec Sauvages (rue)
2707 mathias 19
WidgetSaisieFlorileges.prototype.initCarto = function() {
20
	var lthis = this;
21
 
22
	this.initialiserGoogleMap();
23
	this.afficherEtapeGeolocalisation(1);
24
 
25
	$('#carte-recherche').autocomplete({
26
		//Cette partie utilise geocoder pour extraire des valeurs d'adresse
27
		source: function(request, response) {
28
 
29
			lthis.geocoder.geocode( {'address': request.term+', France', 'region' : 'fr' }, function(results, status) {
30
				if (status == google.maps.GeocoderStatus.OK) {
31
					response($.map(results, function(item) {
32
						var retour = {
33
							label: item.formatted_address,
34
							value: item.formatted_address,
35
							latitude: item.geometry.location.lat(),
36
							longitude: item.geometry.location.lng()
37
						};
38
						return retour;
39
					}));
40
				} else {
41
					lthis.afficherErreurGoogleMap(status);
42
				}
43
			});
44
		},
45
		// Cette partie est executee a la selection d'une adresse
46
		select: function(event, ui) {
47
			var nouvellePosition = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
48
			lthis.initialiserMarkerDeb();
49
			lthis.deplacerMarkerDeb(nouvellePosition);
50
			lthis.map.setZoom(16);
51
			lthis.afficherEtapeGeolocalisation(2);
52
		}
53
	});
54
}
55
 
56
WidgetSaisieFlorileges.prototype.initForm = function() {
57
	// super() à la main - toute autre manière de faire est über-komplex
58
	WidgetSaisie.prototype.initForm.call(this);
59
 
60
	this.configurerDatePicker('#date-arret-traitement-phyto');
61
	this.surChangementTaxonListe();// Vérif lors du chargement de la page
62
	$('#taxon-liste').on('change', this.surChangementTaxonListe.bind(this));
63
}
64
 
65
WidgetSaisieFlorileges.prototype.initEvts = function() {
66
	var lthis = this;
67
	// super() à la main - toute autre manière de faire est über-komplex
68
	WidgetSaisie.prototype.initEvts.call(this);
69
 
70
	$('.dropdown-menu input, .dropdown-menu label').on('click', function(event) {
71
		event.stopPropagation();
72
	});
73
 
74
	// Afficher/Cacher champs cachés par défaut
75
	this.surChangementPeriodiciteTraitementPhyto();// Vérif lors du chargement de la page
76
	$('#periodicite-traitement-phyto').on('change', this.surChangementPeriodiciteTraitementPhyto.bind(this));
77
 
78
	// Sliders
79
	this.transformerEnSlider('#presence-zone-vegetalise');
80
	this.transformerEnSlider('#hauteur-batiment-avoisinant');
81
	this.transformerEnSlider('#periodicite-traitement-phyto');
82
	this.transformerEnSlider('#resistance-traitement-phyto');
83
	this.transformerEnSlider('#vitesse-croissance');
84
 
85
	// Gestion des obs
86
	$('.cb-milieux').on('click', function(event) {
87
		$(this).valid();
88
		event.stopPropagation();
89
	});
90
	$('input#hauteur-plante').on('blur', function() {
91
		// if there's a bad value
92
		var valeur = $(this).val();
93
		if (! valeur.match(/^[0-9]+$/)) {
94
			// replace it with nothing
95
			var nouvelleValeur = valeur.replace(/[^0-9]/g, '');
96
			$(this).val(nouvelleValeur);
97
		}
98
	});
99
 
100
	// Défilement des photos @TODO harmoniser
101
	$('body').on('click', '.defilement-control-zone', function(event) {
102
		lthis.defilerMiniatures($(this));
103
	});
104
	$('body').on('mouseover', '.defilement-control-zone', function(event) {
105
		$('.defilement-control', this).removeClass('hidden');
106
	});
107
	$('body').on('mouseout', '.defilement-control-zone', function(event) {
108
		$('.defilement-control', this).addClass('hidden');
109
	});
2708 mathias 110
	$('#photo-placeholder').click(function(event) {
111
		$('#fichier').click();
112
	});
2707 mathias 113
};
114
 
115
WidgetSaisieFlorileges.prototype.prechargerForm = function(infos) {
116
	$('input[name="station"]').val(infos.station);
117
 
118
	//console.log(infos.extension);
119
	if (infos.extension) {
120
		var ext = infos.extension;
121
		if (ext.latitudeDebutRue && ext.longitudeDebutRue && ext.latitudeFinRue && ext.longitudeFinRue) {
122
			this.okPourChargementCarte--;
123
			this.latLngDebPre.lat = parseFloat(ext.latitudeDebutRue.valeur);
124
			this.latLngDebPre.lng = parseFloat(ext.longitudeDebutRue.valeur);
125
			this.latLngFinPre.lat = parseFloat(ext.latitudeFinRue.valeur);
126
			this.latLngFinPre.lng = parseFloat(ext.longitudeFinRue.valeur);
127
			this.prechargerRue();
128
		}
129
 
130
		var chpsARemplir = ['adresse', 'typoUrbaine', 'revetementSol', 'presenceZoneVegetalise', 'hauteurBatimentAvoisinant',
131
			'intensiteGestion', 'periodiciteTraitementPhyto', 'dateArretTraitementPhyto', 'itineraireGestion'];
132
		$.each(infos.extension, function(nomDuChp, value) {
133
			if (chpsARemplir.indexOf(nomDuChp) >= 0) {
134
				if ($('[name="' + nomDuChp + '"]').hasClass('slider')) {
135
					$('select[name="' + nomDuChp + '"] option[value="' + value.valeur + '"]')
136
						.attr('selected', 'selected');
137
					var selectedIndexOptions = $('select[name="' + nomDuChp + '"]').prop('selectedIndex') + 1;
138
					$('[name="' + nomDuChp + '"]').parent().find('.horizontal-slider').slider('value', selectedIndexOptions);
139
 
140
					if (nomDuChp == 'periodiciteTraitementPhyto') {
141
						$('[name="periodiciteTraitementPhyto"]').trigger('change');
142
					}
143
				} else {
144
					$('[name="' + nomDuChp + '"]').val(value.valeur);
145
				}
146
			}
147
		});
148
	}
149
}
150
 
151
WidgetSaisieFlorileges.prototype.prechargerRue = function() {
152
	if (this.okPourChargementCarte == 0) {
153
		this.latLngDeb = new google.maps.LatLng(this.latLngDebPre.lat, this.latLngDebPre.lng);
154
		this.markerDeb = undefined;
155
		this.initialiserMarkerDeb();
156
		this.deplacerMarkerDeb(latLngDeb);
157
		this.premierDeplacement = false;
158
		this.markerFin = undefined;
159
		this.latLngFin = new google.maps.LatLng(this.latLngFinPre.lat, this.latLngFinPre.lng);
160
		this.initialiserMarkerFin();
161
		this.deplacerMakerFin(latLngFin)
162
		this.surDeplacementMarkerFin();
163
		this.map.setZoom(16);
164
	}
165
}
166
 
167
WidgetSaisieFlorileges.prototype.focusChampFormulaire = function() {
168
	$('#structure').focus();
169
}
170
 
171
WidgetSaisieFlorileges.prototype.initialiserGoogleMap = function() {
172
	var lthis = this;
173
 
174
	this.latLngDeb = new google.maps.LatLng(46.30871, 2.54395);// Centre de la France
175
	var options = {
176
			zoom: 5,
177
			center: this.latLngDeb,
178
			mapTypeId: google.maps.MapTypeId.HYBRID,
179
			mapTypeControlOptions: {
180
				mapTypeIds: ['OSM',
181
					google.maps.MapTypeId.ROADMAP,
182
					google.maps.MapTypeId.HYBRID,
183
					google.maps.MapTypeId.SATELLITE,
184
					google.maps.MapTypeId.TERRAIN]}
185
		};
186
 
187
	// Ajout de la couche OSM à la carte
188
	var osmMapType = new google.maps.ImageMapType({
189
		getTileUrl: function(coord, zoom) {
190
			return 'http://tile.openstreetmap.org/' + zoom + '/' + coord.x + '/' + coord.y + '.png';
191
		},
192
		tileSize: new google.maps.Size(256, 256),
193
		isPng: true,
194
		alt: 'OpenStreetMap',
195
		name: 'OSM',
196
		maxZoom: 19
197
	});
198
 
199
	// Création de la carte Google
200
	this.map = new google.maps.Map(document.getElementById('map-canvas'), options); //affiche la google map dans la div map_canvas
201
	this.map.mapTypes.set('OSM', osmMapType);
202
 
203
	// Ajout de l'évènment sur click dans Carte
204
	google.maps.event.addListener(this.map, 'click', this.surClickDansCarte.bind(this));
205
 
206
	// Lorsque la carte est chargée, on vérifie si on peut précharger des données
207
	google.maps.event.addListenerOnce(this.map, 'idle', function(){
208
		if (lthis.obsId == '') {
209
			// Initialisation du marker de début de rue
210
			lthis.initialiserMarkerDeb();
211
			// Tentative de geocalisation si aucune obs à précharger
212
			if (lthis.obsId == '') {
213
				lthis.tenterGeolocalisation();
214
			}
215
		} else {
216
			lthis.okPourChargementCarte--;
217
			lthis.prechargerRue();
218
		}
219
	});
220
 
221
	$("#geolocaliser").on('click', this.geolocaliser.bind(this));
222
	// Création du Geocoder
223
	this.geocoder = new google.maps.Geocoder();
224
}
225
 
226
WidgetSaisieFlorileges.prototype.transformerEnSlider = function(selector) {
227
	$(selector).each(function(index, el) {
228
		// hide the element
229
		$(el).addClass('slider-on');
230
 
231
		// add the slider to each element
232
		var slider = $( '<div class="slider-holder"><div class="horizontal-slider"></div></div>' ).
233
			insertBefore( el ).find('.horizontal-slider').slider({
234
				min: 1,
235
				max: el.options.length,
236
				range: 'min',
237
				value: el.selectedIndex + 1,
238
				slide: function(event, ui) {
239
					el.selectedIndex = ui.value - 1;
240
					slider.find('a').text(el.options[el.selectedIndex].text);
241
 
242
					$(selector + ' option[selected="selected"]').removeAttr('selected');
243
					$(selector + ' :nth-child('+ui.value+')').attr('selected', 'selected')
244
					$(selector).valid();
245
				},
246
				stop: function() {
247
					$(el).change();
248
				}
249
			});
250
 
251
		slider.find('a').text(el.options[el.selectedIndex].text);
252
 
253
		// Create a legend under the slider so we can see the options
254
		var options = [];
255
		for (var option in $(el).children()) {
256
			if (!isNaN(parseInt(option))) {
257
				options.push(el.options[option].text);
258
			}
259
		}
260
		// the width of each legend/option
261
		var width = (slider.width() / (options.length - 1));
262
 
263
		// Add the legend. Half the width of the first and last options for display consistency.
264
		slider.after('<div class="slider-legend"><p style="width:' + (width / 2) + 'px;text-align:left;">' +
265
			options.join('</p><p style="width:' + width + 'px;">') +'</p></div>')
266
			.parent().find('.slider-legend p:last-child').css('width', width / 2)
267
			.css('text-align', 'right');
268
 
269
		// if there are too many options so that the text is wider than the width, then hide the text
270
		var lastChild = slider.parent().find('.slider-legend p:last-child');
271
		if (lastChild[0].clientWidth < lastChild[0].scrollWidth) {
272
			slider.parent().find('.slider-legend p');//.css('text-indent', '200%');
273
		}
274
	});
275
}
276
 
277
WidgetSaisieFlorileges.prototype.surChangementPeriodiciteTraitementPhyto = function() {
278
	if ($('#periodicite-traitement-phyto').val() === 'jamais') {
279
		$('#datp-zone').removeClass('hidden');
280
	} else {
281
		$('#datp-zone').addClass('hidden');
282
	}
283
}
284
 
285
WidgetSaisieFlorileges.prototype.definirReglesFormValidator = function() {
286
	$('#form-observateur').validate({
287
		rules: {
288
			courriel: {
289
				required: true,
290
				email: true},
291
			courriel_confirmation: {
292
				required: true,
293
				equalTo: '#courriel'},
294
			prenom: {
295
				required: true},
296
			nom: {
297
				required: true},
298
			personneStructure: {
299
				required: true},
300
			personneService: {
301
				required: true}
302
		}
303
	});
304
	$('#form-site').validate({
305
		rules: {
306
			station: {
307
				required: true},
308
			latitude : {
309
				required: true,
310
				range: [-90, 90]},
311
			longitude: {
312
				required: true,
313
				range: [-180, 180]},
314
			typoUrbaine: {
315
				required: true},
316
			revetementSol: {
317
				required: true},
318
			intensiteGestion: {
319
				required: true},
320
			periodiciteTraitementPhyto: {
321
				required: true},
322
			itineraireGestion: {
323
				required: true}
324
		}
325
	});
326
	$('#form-date').validate({
327
		rules: {
328
			date: {
329
				required: true,
330
				'dateCel' : true},
331
			dateDerniereIntervention: {
332
				required: true}
333
		},
334
		errorPlacement: function(error, element) {
335
			if (element.attr('name') == 'date') {
336
				element.parent('.input-prepend').after(error);
337
			} else {
338
				error.insertAfter(element);
339
			}
340
		}
341
	});
342
	$('#form-obs').validate({
343
		rules: {
344
			'taxon-liste': {
345
				required: true},
346
			'milieux[]': {
347
				required: true,
348
				minlength: 1},
349
			hauteurPlante: {
350
				required: true,
351
				digits: true},
352
			resistanceTraitementPhyto: {
353
				required: true}
354
		},
355
		errorPlacement: function(error, element) {
356
			if (element.attr('name') == 'milieux[]') {
357
				error.insertAfter('#milieux-controls');
358
			} else {
359
				error.insertAfter(element);
360
			}
361
		}
362
	});
363
}
364
 
365
WidgetSaisieFlorileges.prototype.validerFormulaire = function() {
366
	var observateur = $('#form-observateur').valid(),
367
		station = $('#form-site').valid(),
368
		date = $('#form-date').valid(),
369
		obs = $('#form-obs').valid(),
370
		debRue = (this.latLngDeb == undefined) ? false : true,
371
		finRue = (this.latLngFin == undefined) ? false : true;
372
	var ok = (observateur && station && obs && date && debRue && finRue) ? true : false;
373
	//console.log(observateur+'-'+station+'-'+obs+'-'+date+'-'+debRue+'-'+finRue);
374
	return ok;
375
}
376
 
2708 mathias 377
/*WidgetSaisieFlorileges.prototype.ajouterObs = function() {
2707 mathias 378
	if (this.validerFormulaire() == true) {
379
		this.obsNbre = this.obsNbre + 1;
380
		$('.obs-nbre').text(this.obsNbre);
381
		$('.obs-nbre').triggerHandler('changement');
382
		this.afficherObs();
383
		this.stockerObsData();
384
		this.supprimerMiniatures();
385
	} else {
386
		var debRue = (this.latLngDeb == undefined) ? false : true,
387
			finRue = (this.latLngFin == undefined) ? false : true;
388
		if (debRue == false || finRue == false) {
389
			this.afficherPanneau('#dialogue-form-invalide-rue');
390
		} else {
391
			this.afficherPanneau('#dialogue-form-invalide');
392
		}
393
	}
2708 mathias 394
}*/
2707 mathias 395
 
396
WidgetSaisieFlorileges.prototype.afficherObs = function() {
397
	var lthis = this;
398
 
399
	var numNomSel = ($('#taxon-liste').val() == '?') ? $('#taxon').data('numNomSel') : $('#taxon-liste').val(),
400
		nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special'),
401
		taxon = ($('#taxon-liste').val() == '?') ? $('#taxon').val() : $('#taxon-liste option:selected').data('nom-a-sauver'),
402
		referentiel = (numNomSel == undefined) ? '' : '['+ this.nomSciReferentiel +']',
403
		commune = $('#commune-nom').text(),
404
		codeInsee = $('#commune-code-insee').text(),
405
		lat = $('input[name="latitude"]').val(),
406
		lng = $('input[name="longitude"]').val(),
407
		date = $('#date').val(),
408
		site = $('#station').val(),
409
		revetement = $('#revetement-sol').val(),
410
		intensiteGestion = $('#intensite-gestion').val(),
411
		resistance = $('#resistance-traitement-phyto').val(),
412
		milieux = this.getMilieux(),
413
		notes = (nomSpecial ? this.taxons[numNomSel]['nom_fr'] + ".<br />" : '') + $('#notes').val();
414
 
415
	$('#liste-obs').prepend(
416
		'<div id="obs'+this.obsNbre+'" class="row-fluid obs obs'+this.obsNbre+'">'+
417
			'<div class="span12">'+
418
				'<div class="well">'+
419
					'<div class="obs-action pull-right has-tooltip" data-placement="bottom" '+
420
						'title="Supprimer cette observation de la liste à transmettre">'+
421
						'<button class="btn btn-danger supprimer-obs" value="'+this.obsNbre+'" title="'+this.obsNbre+'">'+
422
							'<i class="icon-trash icon-white"></i>'+
423
						'</button>'+
424
					'</div> '+
425
					'<div class="row-fluid">'+
426
						'<div class="span2 obs-miniatures">'+
427
						this.ajouterImgMiniatureAuTransfert()+
428
						'</div>'+
429
						'<div class="span8">'+
430
							'<ul class="unstyled">'+
431
								'<li>'+
432
									'<span class="nom-sci">' + taxon + '</span> ' +
433
									this.formaterNumNomSel(numNomSel)+
434
									'<span class="referentiel-obs">' + referentiel + '</span>' +
435
									' observé à ' +
436
									'<span class="commune">' + commune + '</span> ' +
437
									'(' + codeInsee + ') [' + lat +' / ' + lng + ']' +
438
									' le ' +
439
									'<span class="date">' + date + '</span>' +
440
								'</li>' +
441
								'<li>' +
442
									'<span>Site :</span> ' + site + ' ' + ' ; ' +
443
									'<span>Revêtement au sol :</span> ' + revetement + ' ' + ' ; ' +
444
									'<span>Intensité de gestion :</span> ' + intensiteGestion + ' ' + ' ; ' +
445
									'<span>Milieu :</span> ' + milieux + ' ' + ' ; ' +
446
									'<span>Résistance/Résilience :</span> ' + resistance + ' ' +
447
								'</li>' +
448
								'<li>' +
449
									'Commentaires : ' + notes +
450
								'</li>'+
451
							'</ul>'+
452
						'</div>'+
453
					'</div>'+
454
				'</div>'+
455
			'</div>'+
456
		'</div>');
457
	$('#zone-liste-obs').removeClass("hidden").show();
458
}
459
 
460
WidgetSaisieFlorileges.prototype.stockerObsData = function() {
2708 mathias 461
 
2707 mathias 462
	var nomHorsListe = $('#taxon-liste').val() == '?' ? true : false;
463
		nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special'),
464
		numNomSel = nomHorsListe ? $('#taxon').data('numNomSel') : $('#taxon-liste').val(),
465
		nomSel = nomHorsListe ? $('#taxon').val() : $('#taxon-liste option:selected').data('nom-a-sauver'),
466
		nomRet = nomHorsListe ? $('#taxon').data('nomRet') : this.taxons[numNomSel]['nom_ret'],
467
		numNomRet = nomHorsListe ? $('#taxon').data('numNomRet') : this.taxons[numNomSel]['num_nom_ret'],
468
		numTaxon = nomHorsListe ? $('#taxon').data('nt') : this.taxons[numNomSel]['num_taxon'],
469
		famille = nomHorsListe ? $('#taxon').data('famille') : this.taxons[numNomSel]['famille'],
470
		referentiel = (numNomSel == undefined) ? '' : this.nomSciReferentiel,
471
		notes = (nomSpecial ? this.taxons[numNomSel]['nom_fr'] + '. ' : '') + $('#notes').val();
472
 
473
	$('#liste-obs').data('obsId'+ this.obsNbre, {
474
		'date': $('#date').val(),
475
		'notes': notes,
476
 
477
		'station': $('#station').val(),
478
		'latitude': $('#latitude').val(),
479
		'longitude': $('#longitude').val(),
480
		'commune_nom': $('#commune-nom').text(),
481
		'commune_code_insee': $('#commune-code-insee').text(),
482
 
483
		'nom_sel': nomSel,
484
		'num_nom_sel': numNomSel,
485
		'nom_ret': nomRet,
486
		'num_nom_ret': numNomRet,
487
		'num_taxon': numTaxon,
488
		'famille': famille,
489
		'referentiel': referentiel,
490
 
491
		'milieu': this.getMilieux(),
492
 
493
		// Ajout des champs images
494
		'image_nom': this.getNomsImgsOriginales(),
495
 
496
		// Ajout des champs étendus de l'obs
497
		'obs_etendue': this.getObsChpEtendus()
498
	});
499
	//console.log($('#liste-obs').data('obsId'+obsNbre));
500
}
501
 
502
WidgetSaisieFlorileges.prototype.getObsChpEtendus = function() {
2708 mathias 503
	// super()
504
	var champs = WidgetSaisieSauvages.prototype.getObsChampsEtendus.call(this);
505
 
506
	var perceptionTechnicien = this.getPerceptionTechnicien();
2707 mathias 507
	if (perceptionTechnicien != undefined) {
508
		champs.push(perceptionTechnicien);
509
	}
510
 
511
	return champs;
512
}
513
 
514
WidgetSaisieFlorileges.prototype.getPerceptionTechnicien = function() {
515
	var perceptionTechnicien = undefined,
516
		perceptions = [];
517
	$('.cb-perception-technicien:checked').each(function() {
518
		perceptions.push($(this).val());
519
	});
520
	if (perceptions.length > 0) {
521
		var valeur = Array.prototype.slice.call(perceptions).join(', ');
522
		perceptionTechnicien = {cle: 'perceptionTechnicien', label: "Perceptions par l'équipe", valeur: valeur};
523
	}
524
	return perceptionTechnicien;
525
}