Subversion Repositories eFlore/Applications.cel

Rev

Rev 2745 | 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
	});
2745 aurelien 113
 
114
	$('.navigation-rue').on('click', this.surClicRueSuivantPrecedent.bind(this));
2707 mathias 115
};
116
 
117
WidgetSaisieFlorileges.prototype.prechargerForm = function(infos) {
118
	$('input[name="station"]').val(infos.station);
119
 
120
	//console.log(infos.extension);
121
	if (infos.extension) {
122
		var ext = infos.extension;
123
		if (ext.latitudeDebutRue && ext.longitudeDebutRue && ext.latitudeFinRue && ext.longitudeFinRue) {
124
			this.okPourChargementCarte--;
125
			this.latLngDebPre.lat = parseFloat(ext.latitudeDebutRue.valeur);
126
			this.latLngDebPre.lng = parseFloat(ext.longitudeDebutRue.valeur);
127
			this.latLngFinPre.lat = parseFloat(ext.latitudeFinRue.valeur);
128
			this.latLngFinPre.lng = parseFloat(ext.longitudeFinRue.valeur);
129
			this.prechargerRue();
130
		}
131
 
132
		var chpsARemplir = ['adresse', 'typoUrbaine', 'revetementSol', 'presenceZoneVegetalise', 'hauteurBatimentAvoisinant',
133
			'intensiteGestion', 'periodiciteTraitementPhyto', 'dateArretTraitementPhyto', 'itineraireGestion'];
134
		$.each(infos.extension, function(nomDuChp, value) {
135
			if (chpsARemplir.indexOf(nomDuChp) >= 0) {
136
				if ($('[name="' + nomDuChp + '"]').hasClass('slider')) {
137
					$('select[name="' + nomDuChp + '"] option[value="' + value.valeur + '"]')
138
						.attr('selected', 'selected');
139
					var selectedIndexOptions = $('select[name="' + nomDuChp + '"]').prop('selectedIndex') + 1;
140
					$('[name="' + nomDuChp + '"]').parent().find('.horizontal-slider').slider('value', selectedIndexOptions);
141
 
142
					if (nomDuChp == 'periodiciteTraitementPhyto') {
143
						$('[name="periodiciteTraitementPhyto"]').trigger('change');
144
					}
145
				} else {
146
					$('[name="' + nomDuChp + '"]').val(value.valeur);
147
				}
148
			}
149
		});
150
	}
151
}
152
 
153
WidgetSaisieFlorileges.prototype.prechargerRue = function() {
154
	if (this.okPourChargementCarte == 0) {
155
		this.latLngDeb = new google.maps.LatLng(this.latLngDebPre.lat, this.latLngDebPre.lng);
156
		this.markerDeb = undefined;
157
		this.initialiserMarkerDeb();
158
		this.deplacerMarkerDeb(latLngDeb);
159
		this.premierDeplacement = false;
160
		this.markerFin = undefined;
161
		this.latLngFin = new google.maps.LatLng(this.latLngFinPre.lat, this.latLngFinPre.lng);
162
		this.initialiserMarkerFin();
163
		this.deplacerMakerFin(latLngFin)
164
		this.surDeplacementMarkerFin();
165
		this.map.setZoom(16);
166
	}
167
}
168
 
169
WidgetSaisieFlorileges.prototype.focusChampFormulaire = function() {
170
	$('#structure').focus();
171
}
172
 
173
WidgetSaisieFlorileges.prototype.initialiserGoogleMap = function() {
174
	var lthis = this;
175
 
176
	this.latLngDeb = new google.maps.LatLng(46.30871, 2.54395);// Centre de la France
177
	var options = {
178
			zoom: 5,
179
			center: this.latLngDeb,
180
			mapTypeId: google.maps.MapTypeId.HYBRID,
181
			mapTypeControlOptions: {
182
				mapTypeIds: ['OSM',
183
					google.maps.MapTypeId.ROADMAP,
184
					google.maps.MapTypeId.HYBRID,
185
					google.maps.MapTypeId.SATELLITE,
186
					google.maps.MapTypeId.TERRAIN]}
187
		};
188
 
189
	// Ajout de la couche OSM à la carte
190
	var osmMapType = new google.maps.ImageMapType({
191
		getTileUrl: function(coord, zoom) {
192
			return 'http://tile.openstreetmap.org/' + zoom + '/' + coord.x + '/' + coord.y + '.png';
193
		},
194
		tileSize: new google.maps.Size(256, 256),
195
		isPng: true,
196
		alt: 'OpenStreetMap',
197
		name: 'OSM',
198
		maxZoom: 19
199
	});
200
 
201
	// Création de la carte Google
202
	this.map = new google.maps.Map(document.getElementById('map-canvas'), options); //affiche la google map dans la div map_canvas
203
	this.map.mapTypes.set('OSM', osmMapType);
204
 
205
	// Ajout de l'évènment sur click dans Carte
206
	google.maps.event.addListener(this.map, 'click', this.surClickDansCarte.bind(this));
207
 
208
	// Lorsque la carte est chargée, on vérifie si on peut précharger des données
209
	google.maps.event.addListenerOnce(this.map, 'idle', function(){
210
		if (lthis.obsId == '') {
211
			// Initialisation du marker de début de rue
212
			lthis.initialiserMarkerDeb();
213
			// Tentative de geocalisation si aucune obs à précharger
214
			if (lthis.obsId == '') {
215
				lthis.tenterGeolocalisation();
216
			}
217
		} else {
218
			lthis.okPourChargementCarte--;
219
			lthis.prechargerRue();
220
		}
221
	});
222
 
223
	$("#geolocaliser").on('click', this.geolocaliser.bind(this));
224
	// Création du Geocoder
225
	this.geocoder = new google.maps.Geocoder();
226
}
227
 
228
WidgetSaisieFlorileges.prototype.transformerEnSlider = function(selector) {
229
	$(selector).each(function(index, el) {
230
		// hide the element
231
		$(el).addClass('slider-on');
232
 
233
		// add the slider to each element
234
		var slider = $( '<div class="slider-holder"><div class="horizontal-slider"></div></div>' ).
235
			insertBefore( el ).find('.horizontal-slider').slider({
236
				min: 1,
237
				max: el.options.length,
238
				range: 'min',
239
				value: el.selectedIndex + 1,
240
				slide: function(event, ui) {
241
					el.selectedIndex = ui.value - 1;
242
					slider.find('a').text(el.options[el.selectedIndex].text);
243
 
244
					$(selector + ' option[selected="selected"]').removeAttr('selected');
245
					$(selector + ' :nth-child('+ui.value+')').attr('selected', 'selected')
246
					$(selector).valid();
247
				},
248
				stop: function() {
249
					$(el).change();
250
				}
251
			});
252
 
253
		slider.find('a').text(el.options[el.selectedIndex].text);
254
 
255
		// Create a legend under the slider so we can see the options
256
		var options = [];
257
		for (var option in $(el).children()) {
258
			if (!isNaN(parseInt(option))) {
259
				options.push(el.options[option].text);
260
			}
261
		}
262
		// the width of each legend/option
263
		var width = (slider.width() / (options.length - 1));
264
 
265
		// Add the legend. Half the width of the first and last options for display consistency.
266
		slider.after('<div class="slider-legend"><p style="width:' + (width / 2) + 'px;text-align:left;">' +
267
			options.join('</p><p style="width:' + width + 'px;">') +'</p></div>')
268
			.parent().find('.slider-legend p:last-child').css('width', width / 2)
269
			.css('text-align', 'right');
270
 
271
		// if there are too many options so that the text is wider than the width, then hide the text
272
		var lastChild = slider.parent().find('.slider-legend p:last-child');
273
		if (lastChild[0].clientWidth < lastChild[0].scrollWidth) {
274
			slider.parent().find('.slider-legend p');//.css('text-indent', '200%');
275
		}
276
	});
277
}
278
 
279
WidgetSaisieFlorileges.prototype.surChangementPeriodiciteTraitementPhyto = function() {
280
	if ($('#periodicite-traitement-phyto').val() === 'jamais') {
281
		$('#datp-zone').removeClass('hidden');
282
	} else {
283
		$('#datp-zone').addClass('hidden');
284
	}
285
}
286
 
287
WidgetSaisieFlorileges.prototype.definirReglesFormValidator = function() {
288
	$('#form-observateur').validate({
289
		rules: {
290
			courriel: {
291
				required: true,
292
				email: true},
293
			courriel_confirmation: {
294
				required: true,
295
				equalTo: '#courriel'},
296
			prenom: {
297
				required: true},
298
			nom: {
299
				required: true},
300
			personneStructure: {
301
				required: true},
302
			personneService: {
303
				required: true}
304
		}
305
	});
306
	$('#form-site').validate({
307
		rules: {
308
			station: {
309
				required: true},
310
			latitude : {
311
				required: true,
312
				range: [-90, 90]},
313
			longitude: {
314
				required: true,
315
				range: [-180, 180]},
316
			typoUrbaine: {
317
				required: true},
318
			revetementSol: {
319
				required: true},
320
			intensiteGestion: {
321
				required: true},
322
			periodiciteTraitementPhyto: {
323
				required: true},
324
			itineraireGestion: {
325
				required: true}
326
		}
327
	});
328
	$('#form-date').validate({
329
		rules: {
330
			date: {
331
				required: true,
332
				'dateCel' : true},
333
			dateDerniereIntervention: {
334
				required: true}
335
		},
336
		errorPlacement: function(error, element) {
337
			if (element.attr('name') == 'date') {
338
				element.parent('.input-prepend').after(error);
339
			} else {
340
				error.insertAfter(element);
341
			}
342
		}
343
	});
344
	$('#form-obs').validate({
345
		rules: {
346
			'taxon-liste': {
347
				required: true},
348
			'milieux[]': {
349
				required: true,
350
				minlength: 1},
351
			hauteurPlante: {
352
				required: true,
353
				digits: true},
354
			resistanceTraitementPhyto: {
355
				required: true}
356
		},
357
		errorPlacement: function(error, element) {
358
			if (element.attr('name') == 'milieux[]') {
359
				error.insertAfter('#milieux-controls');
360
			} else {
361
				error.insertAfter(element);
362
			}
363
		}
364
	});
365
}
366
 
367
WidgetSaisieFlorileges.prototype.validerFormulaire = function() {
368
	var observateur = $('#form-observateur').valid(),
369
		station = $('#form-site').valid(),
370
		date = $('#form-date').valid(),
371
		obs = $('#form-obs').valid(),
372
		debRue = (this.latLngDeb == undefined) ? false : true,
373
		finRue = (this.latLngFin == undefined) ? false : true;
374
	var ok = (observateur && station && obs && date && debRue && finRue) ? true : false;
375
	//console.log(observateur+'-'+station+'-'+obs+'-'+date+'-'+debRue+'-'+finRue);
376
	return ok;
377
}
378
 
2708 mathias 379
/*WidgetSaisieFlorileges.prototype.ajouterObs = function() {
2707 mathias 380
	if (this.validerFormulaire() == true) {
381
		this.obsNbre = this.obsNbre + 1;
382
		$('.obs-nbre').text(this.obsNbre);
383
		$('.obs-nbre').triggerHandler('changement');
384
		this.afficherObs();
385
		this.stockerObsData();
386
		this.supprimerMiniatures();
387
	} else {
388
		var debRue = (this.latLngDeb == undefined) ? false : true,
389
			finRue = (this.latLngFin == undefined) ? false : true;
390
		if (debRue == false || finRue == false) {
391
			this.afficherPanneau('#dialogue-form-invalide-rue');
392
		} else {
393
			this.afficherPanneau('#dialogue-form-invalide');
394
		}
395
	}
2708 mathias 396
}*/
2707 mathias 397
 
398
WidgetSaisieFlorileges.prototype.afficherObs = function() {
399
	var lthis = this;
400
 
401
	var numNomSel = ($('#taxon-liste').val() == '?') ? $('#taxon').data('numNomSel') : $('#taxon-liste').val(),
402
		nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special'),
403
		taxon = ($('#taxon-liste').val() == '?') ? $('#taxon').val() : $('#taxon-liste option:selected').data('nom-a-sauver'),
404
		referentiel = (numNomSel == undefined) ? '' : '['+ this.nomSciReferentiel +']',
405
		commune = $('#commune-nom').text(),
406
		codeInsee = $('#commune-code-insee').text(),
407
		lat = $('input[name="latitude"]').val(),
408
		lng = $('input[name="longitude"]').val(),
409
		date = $('#date').val(),
410
		site = $('#station').val(),
411
		revetement = $('#revetement-sol').val(),
412
		intensiteGestion = $('#intensite-gestion').val(),
413
		resistance = $('#resistance-traitement-phyto').val(),
414
		milieux = this.getMilieux(),
415
		notes = (nomSpecial ? this.taxons[numNomSel]['nom_fr'] + ".<br />" : '') + $('#notes').val();
416
 
417
	$('#liste-obs').prepend(
418
		'<div id="obs'+this.obsNbre+'" class="row-fluid obs obs'+this.obsNbre+'">'+
419
			'<div class="span12">'+
420
				'<div class="well">'+
421
					'<div class="obs-action pull-right has-tooltip" data-placement="bottom" '+
422
						'title="Supprimer cette observation de la liste à transmettre">'+
423
						'<button class="btn btn-danger supprimer-obs" value="'+this.obsNbre+'" title="'+this.obsNbre+'">'+
424
							'<i class="icon-trash icon-white"></i>'+
425
						'</button>'+
426
					'</div> '+
427
					'<div class="row-fluid">'+
428
						'<div class="span2 obs-miniatures">'+
429
						this.ajouterImgMiniatureAuTransfert()+
430
						'</div>'+
431
						'<div class="span8">'+
432
							'<ul class="unstyled">'+
433
								'<li>'+
434
									'<span class="nom-sci">' + taxon + '</span> ' +
435
									this.formaterNumNomSel(numNomSel)+
436
									'<span class="referentiel-obs">' + referentiel + '</span>' +
437
									' observé à ' +
438
									'<span class="commune">' + commune + '</span> ' +
439
									'(' + codeInsee + ') [' + lat +' / ' + lng + ']' +
440
									' le ' +
441
									'<span class="date">' + date + '</span>' +
442
								'</li>' +
443
								'<li>' +
444
									'<span>Site :</span> ' + site + ' ' + ' ; ' +
445
									'<span>Revêtement au sol :</span> ' + revetement + ' ' + ' ; ' +
446
									'<span>Intensité de gestion :</span> ' + intensiteGestion + ' ' + ' ; ' +
447
									'<span>Milieu :</span> ' + milieux + ' ' + ' ; ' +
448
									'<span>Résistance/Résilience :</span> ' + resistance + ' ' +
449
								'</li>' +
450
								'<li>' +
451
									'Commentaires : ' + notes +
452
								'</li>'+
453
							'</ul>'+
454
						'</div>'+
455
					'</div>'+
456
				'</div>'+
457
			'</div>'+
458
		'</div>');
459
	$('#zone-liste-obs').removeClass("hidden").show();
460
}
461
 
462
WidgetSaisieFlorileges.prototype.stockerObsData = function() {
2708 mathias 463
 
2707 mathias 464
	var nomHorsListe = $('#taxon-liste').val() == '?' ? true : false;
465
		nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special'),
466
		numNomSel = nomHorsListe ? $('#taxon').data('numNomSel') : $('#taxon-liste').val(),
467
		nomSel = nomHorsListe ? $('#taxon').val() : $('#taxon-liste option:selected').data('nom-a-sauver'),
468
		nomRet = nomHorsListe ? $('#taxon').data('nomRet') : this.taxons[numNomSel]['nom_ret'],
469
		numNomRet = nomHorsListe ? $('#taxon').data('numNomRet') : this.taxons[numNomSel]['num_nom_ret'],
470
		numTaxon = nomHorsListe ? $('#taxon').data('nt') : this.taxons[numNomSel]['num_taxon'],
471
		famille = nomHorsListe ? $('#taxon').data('famille') : this.taxons[numNomSel]['famille'],
472
		referentiel = (numNomSel == undefined) ? '' : this.nomSciReferentiel,
473
		notes = (nomSpecial ? this.taxons[numNomSel]['nom_fr'] + '. ' : '') + $('#notes').val();
474
 
475
	$('#liste-obs').data('obsId'+ this.obsNbre, {
476
		'date': $('#date').val(),
477
		'notes': notes,
478
 
479
		'station': $('#station').val(),
480
		'latitude': $('#latitude').val(),
481
		'longitude': $('#longitude').val(),
482
		'commune_nom': $('#commune-nom').text(),
483
		'commune_code_insee': $('#commune-code-insee').text(),
484
 
485
		'nom_sel': nomSel,
486
		'num_nom_sel': numNomSel,
487
		'nom_ret': nomRet,
488
		'num_nom_ret': numNomRet,
489
		'num_taxon': numTaxon,
490
		'famille': famille,
491
		'referentiel': referentiel,
492
 
493
		'milieu': this.getMilieux(),
494
 
495
		// Ajout des champs images
496
		'image_nom': this.getNomsImgsOriginales(),
497
 
498
		// Ajout des champs étendus de l'obs
499
		'obs_etendue': this.getObsChpEtendus()
500
	});
501
	//console.log($('#liste-obs').data('obsId'+obsNbre));
502
}
503
 
504
WidgetSaisieFlorileges.prototype.getObsChpEtendus = function() {
2708 mathias 505
	// super()
2750 aurelien 506
	var champs = WidgetSaisieSauvages.prototype.getObsChpEtendus.call(this);
2708 mathias 507
 
508
	var perceptionTechnicien = this.getPerceptionTechnicien();
2707 mathias 509
	if (perceptionTechnicien != undefined) {
510
		champs.push(perceptionTechnicien);
511
	}
512
 
513
	return champs;
514
}
515
 
516
WidgetSaisieFlorileges.prototype.getPerceptionTechnicien = function() {
517
	var perceptionTechnicien = undefined,
518
		perceptions = [];
519
	$('.cb-perception-technicien:checked').each(function() {
520
		perceptions.push($(this).val());
521
	});
522
	if (perceptions.length > 0) {
523
		var valeur = Array.prototype.slice.call(perceptions).join(', ');
524
		perceptionTechnicien = {cle: 'perceptionTechnicien', label: "Perceptions par l'équipe", valeur: valeur};
525
	}
526
	return perceptionTechnicien;
527
}