Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
2697 mathias 1
// Héritage !!
2
function WidgetSaisieSauvages() {
3
	this.markerDeb = undefined;
4
	this.latLngDeb = undefined;
5
	this.markerFin = undefined;
6
	this.latLngCentre = undefined;
7
	this.latLngFin = undefined;
8
	this.ligneRue = undefined;
9
	this.premierDeplacement = true;
10
	this.valeurChamp = "";
11
	this.avertissementDeuxPhotosAffiche = false;
2700 mathias 12
	this.googleMapMarqueurDebutUrl = null;
13
	this.googleMapMarqueurFinUrl = null;
14
	this.ville = null;
15
	this.supprimerIconeUrl = null;
2744 aurelien 16
	this.serviceTraceRueUrl = null;
2709 mathias 17
	this.taxons = {};
2744 aurelien 18
	this.listeRues = {};
19
	this.indexRueSelectionnee = 0;
2701 mathias 20
}
2697 mathias 21
WidgetSaisieSauvages.prototype = new WidgetSaisie();
1346 aurelien 22
 
2697 mathias 23
// surcharge
24
WidgetSaisieSauvages.prototype.initCarto = function() {
25
	this.initialiserGoogleMap();
26
	this.afficherEtapeGeolocalisation(1);
2736 mathias 27
	this.latLng = true; // patch cracra pour éviter de surcharger trouvercommune()
2707 mathias 28
 
2708 mathias 29
	var lthis = this;
2707 mathias 30
	$('#carte-recherche').autocomplete({
31
		//Cette partie utilise geocoder pour extraire des valeurs d'adresse
32
		source: function(request, response) {
33
			lthis.geocoder.geocode( {'address': request.term+', France', 'region' : 'fr' }, function(results, status) {
34
				if (status == google.maps.GeocoderStatus.OK) {
35
					response($.map(results, function(item) {
36
						var rue = "";
37
						$.each(item.address_components, function(){
38
							if (this.types[0] == "route" || this.types[0] == "street_address" ) {
39
								rue = this.short_name;
40
							}
41
						});
42
						var retour = {
43
							label: item.formatted_address,
44
							value: rue,
45
							latitude: item.geometry.location.lat(),
46
							longitude: item.geometry.location.lng()
47
						};
48
						return retour;
49
					}));
50
				} else {
51
					lthis.afficherErreurGoogleMap(status);
52
				}
53
			});
54
		},
55
		// Cette partie est executee a la selection d'une adresse
56
		select: function(event, ui) {
57
			var nouvellePosition = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
58
			lthis.initialiserMarkerDeb();
59
			lthis.deplacerMarkerDeb(nouvellePosition);
60
			lthis.map.setZoom(16);
61
			lthis.afficherEtapeGeolocalisation(2);
62
		}
63
	});
2701 mathias 64
};
1054 jpm 65
 
2697 mathias 66
//surcharge
67
WidgetSaisieSauvages.prototype.initForm = function() {
2708 mathias 68
	// super() à la main - toute autre manière de faire est über-komplex
69
	WidgetSaisie.prototype.initForm.call(this);
1916 jpm 70
 
2697 mathias 71
	this.surChangementTaxonListe();
72
	$('#taxon-liste').on('change', this.surChangementTaxonListe);
2700 mathias 73
	if (this.debug) {
2697 mathias 74
		console.log('Selected taxon:'+$('#taxon-liste option:selected').val());
1916 jpm 75
	}
2697 mathias 76
	$('#taxon-liste').on('blur', this.surChangementValeurTaxon);
77
	$('#taxon').on('blur', this.surChangementValeurTaxon);
2701 mathias 78
};
1916 jpm 79
 
2697 mathias 80
//surcharge
81
WidgetSaisieSauvages.prototype.initEvts = function() {
82
	var lthis = this;
2700 mathias 83
	// super() à la main - toute autre manière de faire est über-komplex
84
	WidgetSaisie.prototype.initEvts.call(this);
85
 
86
	$('.dropdown-menu input, .dropdown-menu label').on('click', function(event) {
87
		event.stopPropagation();
2697 mathias 88
	});
2700 mathias 89
	$('#fichier').off(); // elever l'écouteur d'événements de base
2697 mathias 90
	$('#fichier').on('click change', function(event) {
91
 
92
		if($("#photos-conteneur #miniatures .miniature").length == 1 && ! lthis.avertissementDeuxPhotosAffiche) {
93
			messageAvertissement = "Attention: \n"+
94
			"Sélectionnez uniquement les photos correspondantes à une seule plante \n"+
95
			"(c'est à dire correspondant à un seul individu d'une espèce donnée) \n"+
96
			"vue dans le tronçon de rue inventoriée ";
97
			if(window.confirm(messageAvertissement)) {
98
				lthis.avertissementDeuxPhotosAffiche = true;
99
				return true;
100
			} else {
101
				return false;
1916 jpm 102
			}
2697 mathias 103
		}
104
		if ($(this).val().length > 0) {
105
			arreter(event);
106
			var options = {
107
				success: lthis.afficherMiniature.bind(lthis), // post-submit callback
108
				dataType: 'xml', // 'xml', 'script', or 'json' (expected server response type)
109
				resetForm: true // reset the form after successful submit
110
			};
111
			$('#miniature').append(
2700 mathias 112
				'<img id="miniature-chargement" class="miniature" alt="chargement" src="'+ this.chargementImageIconeUrl +'"/>');
2697 mathias 113
			$('#ajouter-obs').attr('disabled', 'disabled');
114
			if (lthis.verifierFormat($(this).val())) {
115
				$('#form-upload').ajaxSubmit(options);
116
			} else {
117
				$('#form-upload')[0].reset();
118
				window.alert("Le format de fichier n'est pas supporté, les formats acceptés sont "+	$('#fichier').attr('accept'));
119
			}
120
			return false;
121
		}
1916 jpm 122
	});
123
 
2697 mathias 124
	$('.cb-milieux').on('click', function(event) {
125
		$(this).valid();
126
		event.stopPropagation();
127
	});
1956 jpm 128
 
2697 mathias 129
	// Défilement des photos
130
	$('body').on('click', '.defilement-control-zone', function(event) {
131
		lthis.defilerMiniatures($(this));
132
	});
133
	$('body').on('mouseover', '.defilement-control-zone', function(event) {
134
		$('.defilement-control', this).removeClass('hidden');
135
	});
136
	$('body').on('mouseout', '.defilement-control-zone', function(event) {
137
		$('.defilement-control', this).addClass('hidden');
138
	});
1916 jpm 139
 
2697 mathias 140
	$('#photo-placeholder').click(function(event) {
141
		$('#fichier').click();
142
	});
143
	$('#geolocaliser').on('click', this.geolocaliser.bind(this));
2744 aurelien 144
 
145
	$('.navigation-rue').on('click', this.surClicRueSuivantPrecedent.bind(this))
2701 mathias 146
};
2408 jpm 147
 
2697 mathias 148
WidgetSaisieSauvages.prototype.montrerFormIdentite = function() {
149
	$('#zone-courriel-confirmation, #zone-prenom-nom').css('display', 'block');
2701 mathias 150
};
2697 mathias 151
 
152
// surcharge
153
WidgetSaisieSauvages.prototype.initialiserGoogleMap = function() {
154
	var lthis = this;
155
	this.latLngDeb = new google.maps.LatLng(48.8543, 2.3483);// Paris
2700 mathias 156
	if (this.ville == 'Marseille') {
2697 mathias 157
		this.latLngDeb = new google.maps.LatLng(43.29545, 5.37458);
2700 mathias 158
	} else if (this.ville == 'Montpellier') {
2697 mathias 159
		this.latLngDeb = new google.maps.LatLng(43.61077, 3.87672);
1946 jpm 160
	}
1922 jpm 161
	var options = {
1946 jpm 162
			zoom: 16,
2697 mathias 163
			center: this.latLngDeb,
1922 jpm 164
			mapTypeId: google.maps.MapTypeId.HYBRID,
165
			mapTypeControlOptions: {
2408 jpm 166
				mapTypeIds: ['OSM',
167
					google.maps.MapTypeId.ROADMAP,
168
					google.maps.MapTypeId.HYBRID,
169
					google.maps.MapTypeId.SATELLITE,
1922 jpm 170
					google.maps.MapTypeId.TERRAIN]}
171
		};
2408 jpm 172
 
1922 jpm 173
	// Ajout de la couche OSM à la carte
174
	osmMapType = new google.maps.ImageMapType({
175
		getTileUrl: function(coord, zoom) {
176
			return 'http://tile.openstreetmap.org/' + zoom + '/' + coord.x + '/' + coord.y + '.png';
177
		},
178
		tileSize: new google.maps.Size(256, 256),
179
		isPng: true,
180
		alt: 'OpenStreetMap',
181
		name: 'OSM',
182
		maxZoom: 19
1346 aurelien 183
	});
2408 jpm 184
 
1922 jpm 185
	// Création de la carte Google
2697 mathias 186
	this.map = new google.maps.Map(document.getElementById('map-canvas'), options); //affiche la google map dans la div map_canvas
187
	this.map.mapTypes.set('OSM', osmMapType);
2408 jpm 188
 
1922 jpm 189
	// Ajout de l'évènment sur click dans Carte
2697 mathias 190
	google.maps.event.addListener(this.map, 'click', this.surClickDansCarte.bind(this));
2408 jpm 191
 
1922 jpm 192
	// Lorsque la carte est chargée, on vérifie si on peut précharger des données
2697 mathias 193
	google.maps.event.addListenerOnce(this.map, 'idle', function(){
1922 jpm 194
		// Initialisation du marker de début de rue
2697 mathias 195
		lthis.initialiserMarkerDeb();
2708 mathias 196
		if (this.obsId == '') {
2256 aurelien 197
			// Tentative de geocalisation si aucune obs à précharger
2697 mathias 198
			lthis.tenterGeolocalisation();
2256 aurelien 199
		}
1346 aurelien 200
	});
2408 jpm 201
 
1922 jpm 202
	// Création du Geocoder
2697 mathias 203
	this.geocoder = new google.maps.Geocoder();
2701 mathias 204
};
1054 jpm 205
 
2708 mathias 206
WidgetSaisieSauvages.prototype.surClickDansCarte = function(event) {
2736 mathias 207
	this.latLngDeb = event.latLng;
208
	this.deplacerMarkerDeb(this.latLngDeb);
2708 mathias 209
};
210
 
2697 mathias 211
// surcharge
212
WidgetSaisieSauvages.prototype.prechargerForm = function(data) {
2256 aurelien 213
 
214
    $('#carte-recherche').val(data.zoneGeo);
215
	$('#commune-nom').text(data.zoneGeo);
2408 jpm 216
 
2257 aurelien 217
	if(data.hasOwnProperty("codeZoneGeo")) {
218
		$('#commune-code-insee').text(data.codeZoneGeo.replace('INSEE-C:', ''));
219
	}
2408 jpm 220
 
2697 mathias 221
    var pos = new google.maps.LatLng(data.latitude, data.longitude);
2408 jpm 222
 
2256 aurelien 223
    if(data.hasOwnProperty("extension")) {
2697 mathias 224
    	this.initialiserMarkerFin();
2408 jpm 225
 
2256 aurelien 226
    	// cas des obs florilèges qui apparaissent aussi comme des obs sauvages
227
    	// mais qui n'ont pas de coté de rue
228
    	if(data.extension.hasOwnProperty("coteRue")) {
229
    		$('#rue_cote option[value='+data.extension.coteRue.valeur+']').attr("selected", "selected");
230
    	}
2408 jpm 231
 
2697 mathias 232
    	var deb = new google.maps.LatLng(data.extension.latitudeDebutRue.valeur, data.extension.longitudeDebutRue.valeur);
233
	    var fin = new google.maps.LatLng(data.extension.latitudeFinRue.valeur, data.extension.longitudeFinRue.valeur);
2408 jpm 234
 
2708 mathias 235
	    this.mettreAJourMarkerPosition(pos);
2408 jpm 236
 
2697 mathias 237
	    this.deplacerMarkerDeb(deb);
238
	    this.deplacerMarkerFin(fin);
239
	    this.afficherEtapeGeolocalisation(4);
2408 jpm 240
 
2256 aurelien 241
	    var latlngbounds = new google.maps.LatLngBounds();
242
	    latlngbounds.extend(deb);
243
	    latlngbounds.extend(fin);
2697 mathias 244
	    this.map.setCenter(latlngbounds.getCenter());
245
	    this.map.fitBounds(latlngbounds);
2256 aurelien 246
    } else if(data.hasOwnProperty("latitude") && data.hasOwnProperty("longitude")) {
2697 mathias 247
    	this.deplacerMarkerDeb(pos);
2256 aurelien 248
    }
2701 mathias 249
};
2256 aurelien 250
 
2697 mathias 251
WidgetSaisieSauvages.prototype.initialiserMarkerDeb = function() {
252
	this.premierDeplacement = true;
253
	if (this.markerDeb == undefined) {
1922 jpm 254
		// Marqueur de début de Rue
2697 mathias 255
		this.markerDeb = new google.maps.Marker({
256
			map: this.map,
1922 jpm 257
			draggable: true,
258
			title: 'Début de la portion de rue étudiée',
2700 mathias 259
			icon: this.googleMapMarqueurDebutUrl,
2697 mathias 260
			position: this.latLngDeb
1922 jpm 261
		});
2697 mathias 262
		google.maps.event.addListener(this.markerDeb, 'dragend', this.surDeplacementMarkerDeb.bind(this));
1922 jpm 263
	}
2408 jpm 264
 
2697 mathias 265
	this.latLngFin = this.latLngDeb;
266
	if (this.markerFin != undefined) {
267
		this.markerFin.setMap(null);
1922 jpm 268
	}
2697 mathias 269
	this.latLngCentre = this.latLngDeb;
270
	if (this.ligneRue != undefined) {
271
		this.ligneRue.setMap(null);
1922 jpm 272
	}
2701 mathias 273
};
1346 aurelien 274
 
2697 mathias 275
WidgetSaisieSauvages.prototype.surDeplacementMarkerDeb = function() {
2736 mathias 276
	this.latLngDeb = this.markerDeb.getPosition();
277
	this.deplacerMarkerDeb(this.latLngDeb);
2701 mathias 278
};
1346 aurelien 279
 
2697 mathias 280
WidgetSaisieSauvages.prototype.deplacerMarkerDeb = function(nouvellePosition) {
281
	this.latLngDeb = nouvellePosition;
282
	this.markerDeb.setPosition(this.latLngDeb);
283
	this.map.setCenter(this.latLngDeb);
2708 mathias 284
	this.mettreAJourMarkerPosition(this.latLngDeb);
2697 mathias 285
	this.trouverCommune(this.latLngDeb);
1922 jpm 286
 
2744 aurelien 287
	var nouvellePositionFin = new google.maps.LatLng(this.latLngDeb.lat(), this.latLngDeb.lng() + 0.0010);
288
	this.initialiserMarkerFin();
289
	this.deplacerMarkerFin(nouvellePositionFin)
290
	this.afficherEtapeGeolocalisation(3);
2701 mathias 291
};
1346 aurelien 292
 
2697 mathias 293
WidgetSaisieSauvages.prototype.initialiserMarkerFin = function() {
294
	if (this.markerFin == undefined) {
295
		this.markerFin = new google.maps.Marker({
296
			map: this.map,
1922 jpm 297
			draggable: true,
298
			title: 'Fin de la portion de rue étudiée',
2700 mathias 299
			icon: this.googleMapMarqueurFinUrl,
2697 mathias 300
			position: this.latLngFin
1922 jpm 301
		});
2697 mathias 302
		google.maps.event.addListener(this.markerFin, 'dragend', this.surDeplacementMarkerFin.bind(this));
1922 jpm 303
	} else {
2697 mathias 304
		this.markerFin.setMap(null);
1346 aurelien 305
	}
2701 mathias 306
};
1346 aurelien 307
 
2697 mathias 308
WidgetSaisieSauvages.prototype.deplacerMarkerFin = function(nouvellePosition) {
309
	this.latLngFin = nouvellePosition;
310
	this.markerFin.setMap(this.map);
311
	this.markerFin.setPosition(this.latLngFin);
312
	this.dessinerLigneRue(this.latLngDeb, this.latLngFin);
2701 mathias 313
};
1922 jpm 314
 
2697 mathias 315
WidgetSaisieSauvages.prototype.surDeplacementMarkerFin = function() {
2744 aurelien 316
	this.rechercherRue();
317
};
318
 
319
WidgetSaisieSauvages.prototype.surClicRueSuivantPrecedent = function(event) {
320
	event.preventDefault();
321
	if($(event.target).hasClass("navigation-rue-suivant")) {
322
		this.indexRueSelectionnee = this.indexRueSelectionnee < this.listeRues.length - 1 ? this.indexRueSelectionnee + 1 : 0;
323
	} else {
324
		this.indexRueSelectionnee = this.indexRueSelectionnee > 0 ? this.indexRueSelectionnee - 1 : this.listeRues.length - 1;
325
	}
326
	this.mettreRueEnValeur(this.listeRues[this.indexRueSelectionnee]);
327
}
328
 
329
WidgetSaisieSauvages.prototype.afficherChargementRechercheRue = function() {
330
	this.ligneRue.setMap(null);
331
	var img = '<img src="'+this.chargementImageIconeUrl+'" />';
332
	$('#indication-nom-rue-nom').html(img+" Recherche de la rue en cours");
333
	$('#indication-nom-rue').css('visibility','visible');
334
	$('#indication-nom-rue').effect("highlight", {}, 500);
335
	$(".navigation-rue").toggle(false);
336
}
337
 
338
WidgetSaisieSauvages.prototype.rechercherRue = function() {
339
	this.afficherChargementRechercheRue();
340
 
341
	var params = "latitude_debut="+this.markerDeb.getPosition().lat()+"&longitude_debut="+this.markerDeb.getPosition().lng()+
342
	"&latitude_fin="+this.markerFin.getPosition().lat()+"&longitude_fin="+this.markerFin.getPosition().lng();
343
	var lthis = this;
344
 
345
	$.getJSON(this.serviceTraceRueUrl+'?'+params, function(data) {
346
		$('#indication-nom-rue-nom').html("");
347
		lthis.listeRues = data.elements;
348
		var coordDeb = {"lat" : lthis.markerDeb.getPosition().lat(), "lon" : lthis.markerDeb.getPosition().lng()};
349
		var coordFin = {"lat" : lthis.markerFin.getPosition().lat(), "lon" : lthis.markerFin.getPosition().lng()};
350
		var tags = {"name" : "Aucune rue sélectionnée" };
351
		var pasDeRue = {"geometry" : [coordDeb, coordFin], "tags" : tags};
352
		lthis.listeRues.push(pasDeRue);
353
		lthis.indexRueSelectionnee = 0;
354
		lthis.mettreRueEnValeur(lthis.listeRues[0]);
355
	});
356
}
357
 
358
WidgetSaisieSauvages.prototype.mettreRueEnValeur = function(rue) {
359
	this.afficherIndicationRue(rue['tags']['name']);
360
	this.afficherTraceRue(rue['geometry']);
2697 mathias 361
	this.afficherEtapeGeolocalisation(4);
2744 aurelien 362
}
363
 
364
WidgetSaisieSauvages.prototype.afficherIndicationRue = function(indication) {
365
	indication = !!indication ? indication : "Nom de rue inconnu";
366
	$('#indication-nom-rue-nom').html(indication);
367
	$('#indication-nom-rue').css('visibility','visible');
368
	$('#indication-nom-rue').effect("highlight", {}, 500);
369
	$(".navigation-rue").toggle(this.listeRues.length > 1);
370
}
371
 
372
WidgetSaisieSauvages.prototype.afficherTraceRue = function(rue) {
373
 
374
	if (this.ligneRue != undefined) {
375
		this.ligneRue.setMap(null);
376
	}
377
 
378
	var cheminRue = new Array();
379
	if(rue.length > 0) {
380
		$.each(rue, function(index, value) {
381
			cheminRue.push(new google.maps.LatLng(value['lat'], value['lon']));
382
		});
383
 
384
		this.ligneRue = new google.maps.Polyline({
385
			path: cheminRue,
386
			strokeColor: "#FF0000",
387
			strokeOpacity: 1.0,
388
			strokeWeight: 2
389
		});
390
 
391
		this.ligneRue.setMap(this.map);
392
	}
2701 mathias 393
};
1922 jpm 394
 
2697 mathias 395
WidgetSaisieSauvages.prototype.dessinerLigneRue = function(pointDebut, pointFin) {
396
	if (this.ligneRue != undefined) {
397
		this.ligneRue.setMap(null);
1346 aurelien 398
	}
2408 jpm 399
 
2697 mathias 400
	this.ligneRue = new google.maps.Polyline({
1922 jpm 401
		path: [pointDebut, pointFin],
402
		strokeColor: "#FF0000",
403
		strokeOpacity: 1.0,
404
		strokeWeight: 2
405
	});
1346 aurelien 406
 
2697 mathias 407
	this.ligneRue.setMap(this.map);
2701 mathias 408
};
1346 aurelien 409
 
2697 mathias 410
WidgetSaisieSauvages.prototype.afficherCentreRue = function() {
411
	this.latLngDeb = this.markerDeb.getPosition();
412
	this.latLngFin = this.markerFin.getPosition();
413
	this.latLngCentre = new google.maps.LatLng((this.latLngFin.lat() + this.latLngDeb.lat())/2, (this.latLngFin.lng() + this.latLngDeb.lng())/2);
2708 mathias 414
	this.mettreAJourMarkerPosition(this.latLngCentre);
2701 mathias 415
};
1922 jpm 416
 
2697 mathias 417
WidgetSaisieSauvages.prototype.afficherEtapeGeolocalisation = function(numEtape) {
1922 jpm 418
	$('.liste_indication_geolocalisation').children().hide();
419
	$('.liste_indication_geolocalisation :nth-child('+numEtape+')').show();
2701 mathias 420
};
1922 jpm 421
 
2697 mathias 422
// surcharge
423
WidgetSaisieSauvages.prototype.geolocaliser = function(event) {
1922 jpm 424
	var latitude = $('#latitude').val(),
425
		longitude = $('#longitude').val(),
426
		nouvellePosition = new google.maps.LatLng(latitude, longitude);
2697 mathias 427
	this.initialiserMarkerDeb();
428
	this.deplacerMarkerDeb(nouvellePosition);
429
	this.afficherEtapeGeolocalisation(2);
430
	this.map.setZoom(16);
1922 jpm 431
	arreter(event);
2701 mathias 432
};
1922 jpm 433
 
2697 mathias 434
WidgetSaisieSauvages.prototype.tenterGeolocalisation = function() {
2715 mathias 435
	var lthis = this;
1922 jpm 436
	if (navigator.geolocation) {
437
		navigator.geolocation.getCurrentPosition(function(position) {
438
			var latitude = position.coords.latitude,
439
				longitude = position.coords.longitude,
440
				nouvellePosition = new google.maps.LatLng(latitude, longitude);
2715 mathias 441
			lthis.initialiserMarkerDeb();
442
			lthis.deplacerMarkerDeb(nouvellePosition);
443
			lthis.map.setZoom(16);
1922 jpm 444
		});
445
	}
2701 mathias 446
};
1922 jpm 447
 
2697 mathias 448
/**
449
 * AUTO-COMPLÉTION Noms Scientifiques => OK
450
 * sélectionne un nom et puis qu'on le remplacer par un nom non valide
451
 * Garder la trace de la valeur permet de vider le nn lorsqu'on
452
 */
453
WidgetSaisieSauvages.prototype.ajouterAutocompletionNoms = function() {
454
	var lthis = this;
1922 jpm 455
	$('#taxon').autocomplete({
2408 jpm 456
		source: function(requete, add){
1922 jpm 457
			// la variable de requête doit être vidée car sinon le parametre "term" est ajouté
2408 jpm 458
 
2697 mathias 459
			var url = lthis.getUrlAutocompletionNomsSci();
1922 jpm 460
			$.getJSON(url, function(data) {
2697 mathias 461
				var suggestions = lthis.traiterRetourNomsSci(data);
2408 jpm 462
				add(suggestions);
1922 jpm 463
			});
464
		},
465
		html: true
466
	});
2408 jpm 467
 
1922 jpm 468
	$('#taxon').bind('autocompleteselect', function(event, ui) {
469
		$('#taxon').data(ui.item);
2697 mathias 470
		lthis.valeurChamp = $('#taxon').val();
1922 jpm 471
		if (ui.item.retenu == true) {
472
			$('#taxon').addClass('ns-retenu');
473
		} else {
474
			$('#taxon').removeClass('ns-retenu');
475
		}
476
	});
2529 aurelien 477
 
478
	$('#taxon').bind('keypress', function() {
2697 mathias 479
		if(lthis.valeurChamp != $('#taxon').val()) {
2529 aurelien 480
			$('#taxon').data('numNomSel', '');
481
		}
2697 mathias 482
		lthis.valeurChamp = $('#taxon').val();
2529 aurelien 483
	});
2701 mathias 484
};
1922 jpm 485
 
2697 mathias 486
WidgetSaisieSauvages.prototype.getUrlAutocompletionNomsSci = function() {
1922 jpm 487
	var mots = $('#taxon').val(),
2700 mathias 488
		url = this.serviceAutocompletionNomSciUrlTpl.replace('{referentiel}', this.nomSciReferentiel);
1922 jpm 489
	url = url.replace('{masque}', mots);
490
	return url;
2701 mathias 491
};
1922 jpm 492
 
2697 mathias 493
WidgetSaisieSauvages.prototype.traiterRetourNomsSci = function(data) {
1922 jpm 494
	var suggestions = [];
495
	if (data.resultat != undefined) {
496
		$.each(data.resultat, function(i, val) {
497
			val.nn = i;
498
			var nom = {label: '', value: '', nt: '', nomSel: '', nomSelComplet: '', numNomSel: '',
499
				nomRet: '', numNomRet: '', famille: '', retenu: false
500
			};
2700 mathias 501
			if (suggestions.length >= this.autocompletionElementsNbre) {
1922 jpm 502
				nom.label = '...';
503
				nom.value = $('#taxon').val();
504
				suggestions.push(nom);
505
				return false;
506
			} else {
507
				nom.label = val.nom_sci_complet;
508
				nom.value = val.nom_sci_complet;
509
				nom.nt = val.num_taxonomique;
510
				nom.nomSel = val.nom_sci;
511
				nom.nomSelComplet = val.nom_sci_complet;
512
				nom.numNomSel = val.nn;
513
				nom.nomRet = val.nom_retenu_complet;
514
				nom.numNomRet = val['nom_retenu.id'];
515
				nom.famille = val.famille;
516
				nom.retenu = (val.retenu == 'false') ? false : true;
2408 jpm 517
 
1922 jpm 518
				suggestions.push(nom);
519
			}
520
		});
521
	}
522
	return suggestions;
2701 mathias 523
};
1922 jpm 524
 
2697 mathias 525
// surcharge
526
WidgetSaisieSauvages.prototype.configurerFormValidator = function() {
1916 jpm 527
	$.validator.addMethod(
2408 jpm 528
		'dateCel',
529
		function (value, element) {
530
			return value == '' || (/^[0-9]{2}[-\/][0-9]{2}[-\/][0-9]{4}$/.test(value));
531
		},
1916 jpm 532
		'Format : jj/mm/aaaa. Date incomplète, utiliser 0, exemple : 00/12/2011.');
2408 jpm 533
 
1916 jpm 534
	$.extend($.validator.defaults, {
535
		ignore: [],// Forcer Jquery Validate à examiner les éléments avec en display:none;
536
		highlight: function(element) {
537
			$(element).closest('.control-group').removeClass('success').addClass('error');
538
		},
539
		success: function(element) {
540
			element.text('OK!').addClass('valid');
541
			element.closest('.control-group').removeClass('error').addClass('success');
2408 jpm 542
 
1916 jpm 543
			if (element.attr('id') == 'taxon' && $('#taxon').val() != '') {
544
				// Si le taxon n'est pas lié au référentiel, on vide le data associé
545
				if ($('#taxon').data('value') != $('#taxon').val()) {
546
					$('#taxon').data('numNomSel', '');
547
					$('#taxon').data('nomRet', '');
548
					$('#taxon').data('numNomRet', '');
549
					$('#taxon').data('nt', '');
550
					$('#taxon').data('famille', '');
551
				}
552
			}
553
		}
554
	});
2701 mathias 555
};
1916 jpm 556
 
2697 mathias 557
// surcharge
558
WidgetSaisieSauvages.prototype.definirReglesFormValidator = function() {
1916 jpm 559
	$('#form-observateur').validate({
560
		rules: {
561
			courriel: {
562
				required: true,
563
				email: true},
564
			courriel_confirmation: {
565
				required: true,
566
				equalTo: '#courriel'},
567
			prenom: {
568
				required: true},
569
			nom: {
570
				required: true}
571
		}
572
	});
573
	$('#form-obs').validate({
574
		rules: {
575
			station: {
576
				required: true},
577
			latitude : {
578
				required: true,
579
				range: [-90, 90]},
580
			longitude: {
581
				required: true,
582
				range: [-180, 180]},
583
			date: {
584
				required: true,
585
				'dateCel' : true},
1956 jpm 586
			coteRue: {
1916 jpm 587
				required: true},
588
			'taxon-liste': {
589
				required: true},
590
			'milieux[]': {
591
				required: true,
592
				minlength: 1}
593
		},
594
		errorPlacement: function(error, element) {
595
			if (element.attr('name') == 'date') {
596
				element.parent('.input-prepend').after(error);
597
			} else if (element.attr('name') == 'milieux[]') {
598
				error.insertAfter('#milieux-controls');
599
			} else {
600
				error.insertAfter(element);
601
			}
602
		},
603
		messages: {
604
			'milieu[]': 'Vous devez sélectionner au moins un milieu'
605
		}
606
	});
2701 mathias 607
};
1916 jpm 608
 
2697 mathias 609
WidgetSaisieSauvages.prototype.validerFormulaire = function() {
1946 jpm 610
	var observateur = $('#form-observateur').valid(),
611
		obs = $('#form-obs').valid(),
2697 mathias 612
		debRue = (this.latLngDeb == undefined || this.latLngDeb == this.latLngFin) ? false : true,
613
		finRue = (this.latLngFin == undefined || this.latLngDeb == this.latLngFin) ? false : true;
1946 jpm 614
	var ok = (observateur && obs && debRue && finRue) ? true : false;
1964 jpm 615
	//console.log('observateur:'+observateur+'-obs:'+obs+'-debRue:'+debRue+'('+latLngDeb+')-finRue:'+finRue+'('+latLngDeb+')');
1946 jpm 616
	return ok;
2701 mathias 617
};
1946 jpm 618
 
2697 mathias 619
WidgetSaisieSauvages.prototype.surChangementTaxonListe = function() {
1916 jpm 620
	if ($('#taxon-liste').val() === '?') {
621
		$('#taxon-input-groupe').removeClass('hidden');
622
	} else {
623
		$('#taxon-input-groupe').addClass('hidden');
624
	}
2701 mathias 625
};
1916 jpm 626
 
2697 mathias 627
WidgetSaisieSauvages.prototype.surChangementValeurTaxon = function() {
2529 aurelien 628
	var nomHorsListe = $('#taxon-liste').val() == '?' ? true : false;
2697 mathias 629
	var nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special');
630
	var numNomSel = nomHorsListe ? $('#taxon').data('numNomSel') : $('#taxon-liste').val();
2529 aurelien 631
 
632
	// Un nom non valide entraine automatiquement une certitude "à déterminer"
633
	if(nomSpecial || !numNomSel) {
634
		$('#certitude-adeterminer').attr('checked', 'checked');
635
	} else {
636
		$('#certitude-adeterminer').removeAttr('checked');
637
	}
2701 mathias 638
};
2529 aurelien 639
 
2697 mathias 640
// surcharge
641
WidgetSaisieSauvages.prototype.ajouterObs = function() {
642
	if (this.validerFormulaire() == true) {
643
		this.obsNbre = this.obsNbre + 1;
644
		$('.obs-nbre').text(this.obsNbre);
1922 jpm 645
		$('.obs-nbre').triggerHandler('changement');
2697 mathias 646
		this.afficherObs();
647
		this.stockerObsData();
648
		this.supprimerMiniatures();
1922 jpm 649
	} else {
1956 jpm 650
		// Affichage de tous les panneau cachés avec champ obligatoire
2697 mathias 651
		var debRue = (this.latLngDeb == undefined || this.latLngDeb == this.latLngFin) ? false : true,
652
			finRue = (this.latLngFin == undefined || this.latLngDeb == this.latLngFin) ? false : true;
1922 jpm 653
		if (debRue == false || finRue == false) {
2700 mathias 654
			this.afficherPanneau('#dialogue-form-invalide-rue');
1922 jpm 655
		} else {
2700 mathias 656
			this.afficherPanneau('#dialogue-form-invalide');
1922 jpm 657
		}
2697 mathias 658
		this.montrerFormIdentite();
1922 jpm 659
	}
2697 mathias 660
};
1922 jpm 661
 
2697 mathias 662
// surcharge
663
WidgetSaisieSauvages.prototype.afficherObs = function() {
1922 jpm 664
	var numNomSel = ($('#taxon-liste').val() == '?') ? $('#taxon').data('numNomSel') : $('#taxon-liste').val(),
665
		nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special'),
666
		taxon = ($('#taxon-liste').val() == '?') ? $('#taxon').val() : $('#taxon-liste option:selected').data('nom-a-sauver'),
2700 mathias 667
		referentiel = (numNomSel == undefined) ? '' : '['+ this.nomSciReferentiel +']',
1922 jpm 668
		commune = $('#commune-nom').text(),
669
		codeInsee = $('#commune-code-insee').text(),
1946 jpm 670
		station = $('input[name="adresse"]').val(),
1922 jpm 671
		lat = $('input[name="latitude"]').val(),
672
		lng = $('input[name="longitude"]').val(),
673
		date = $('#date').val(),
2697 mathias 674
		milieux = this.getMilieux(),
675
		notes = (nomSpecial ? this.taxons[numNomSel]['nom_fr'] + ".<br />" : '') + $('#notes').val();
2408 jpm 676
 
1922 jpm 677
	$('#liste-obs').prepend(
2697 mathias 678
		'<div id="obs'+this.obsNbre+'" class="row-fluid obs obs'+this.obsNbre+'">' +
1946 jpm 679
			'<div class="span12">' +
680
				'<div class="well">' +
681
					'<div class="obs-action pull-right has-tooltip" data-placement="bottom" ' +
682
						'title="Supprimer cette observation de la liste à transmettre">' +
2697 mathias 683
						'<button class="btn btn-danger supprimer-obs" value="'+this.obsNbre+'" title="'+this.obsNbre+'">' +
1946 jpm 684
							'<i class="icon-trash icon-white"></i>' +
685
						'</button>' +
2408 jpm 686
					'</div> ' +
687
					'<div class="row-fluid">' +
1946 jpm 688
						'<div class="span2 obs-miniatures">' +
2697 mathias 689
						this.ajouterImgMiniatureAuTransfert() +
1922 jpm 690
						'</div>'+
1946 jpm 691
						'<div class="span7">' +
692
							'<ul class="unstyled">' +
1922 jpm 693
								'<li>'+
694
									'<span class="nom-sci">' + taxon + '</span> ' +
2697 mathias 695
									this.formaterNumNomSel(numNomSel) +
1946 jpm 696
									' observé à <br />' +
1993 jpm 697
									'<span class="commune">' + commune + '</span> ' +
698
									'(' + codeInsee + '), ' +
1946 jpm 699
									'<span class="station">' + station + '</span><br /> ' +
1922 jpm 700
									' le ' +
701
									'<span class="date">' + date + '</span>' +
702
								'</li>' +
703
								'<li>' +
1946 jpm 704
									'Milieux : ' + milieux + ' ' + ' ; ' +
1922 jpm 705
								'</li>' +
706
								'<li>' +
2408 jpm 707
									'Notes : ' + notes +
1946 jpm 708
								'</li>' +
709
							'</ul>' +
710
						'</div>' +
711
					'</div>' +
712
				'</div>' +
1922 jpm 713
			'</div>'+
714
		'</div>');
2697 mathias 715
	$('#zone-liste-obs').removeClass("hidden");
716
};
1922 jpm 717
 
2697 mathias 718
WidgetSaisieSauvages.prototype.getMilieux = function() {
1922 jpm 719
	var milieuxStr = '',
720
		milieux = [];
721
	$('.cb-milieux:checked').each(function() {
722
		milieux.push($(this).val());
723
	});
2408 jpm 724
 
1922 jpm 725
	milieuxStr = Array.prototype.slice.call(milieux).join(', ');
726
	return milieuxStr;
2697 mathias 727
};
1922 jpm 728
 
2697 mathias 729
WidgetSaisieSauvages.prototype.ajouterImgMiniatureAuTransfert = function() {
1922 jpm 730
	var html = '',
731
		miniatures = '',
732
		indicateurs = '',
733
		premiere = true,
734
		numero = 1;
735
	if ($('#miniatures img').length == 0) {
2700 mathias 736
		html = '<img class="miniature" alt="Aucune photo"src="'+ this.pasDePhotoIconeUrl +'" />';
1922 jpm 737
	} else if ($('#miniatures img').length >= 1) {
738
		$('#miniatures img').each(function() {
739
			var visible = premiere ? 'miniature-selectionnee' : 'miniature-cachee',
740
				css = $(this).hasClass('b64') ? 'miniature b64' : 'miniature',
741
				src = $(this).attr('src'),
742
				alt = $(this).attr('alt');
2408 jpm 743
 
1922 jpm 744
			var miniature = '<img class="'+css+' '+visible+'"  alt="'+alt+'"src="'+src+'" />';
745
			miniatures += miniature;
2408 jpm 746
 
1922 jpm 747
			var indicateurActif = premiere ? 'active' : '';
748
			var indicateur = '<li class="' + indicateurActif + '" data-numero="' + numero++ + '"></li>';
749
			indicateurs += indicateur;
2408 jpm 750
 
1922 jpm 751
			premiere = false;
752
		});
2408 jpm 753
 
1922 jpm 754
		if ($('#miniatures img').length == 1) {
755
			html = miniatures;
756
		} else {
2408 jpm 757
			html =
1922 jpm 758
				'<div class="defilement">' +
759
					miniatures +
760
					'<a class="defilement-control-zone gauche">' +
761
					'	<span class="defilement-control gauche hidden">&#60;</span>' +
762
					'</a>' +
763
					'<a class="defilement-control-zone droite">' +
764
					'	<span class="defilement-control droite hidden">&#62;</span>' +
765
					'</a>' +
766
					'<ol class="defilement-indicateurs">' + indicateurs + '</ol>' +
767
				'</div>';
768
		}
769
	}
770
	return html;
2697 mathias 771
};
1922 jpm 772
 
2697 mathias 773
WidgetSaisieSauvages.prototype.defilerMiniatures = function(element) {
1922 jpm 774
	var miniatureSelectionne = element.siblings('img.miniature-selectionnee');
775
	miniatureSelectionne.removeClass('miniature-selectionnee').addClass('miniature-cachee');
776
	var miniatureAffichee = miniatureSelectionne;
2408 jpm 777
 
1922 jpm 778
	var indicateurActif = element.parent().find('.defilement-indicateurs .active');
779
	indicateurActif.removeClass('active');
2408 jpm 780
 
1922 jpm 781
	if (element.hasClass('defilement-control-zone') && element.hasClass('gauche')) {
782
		if (miniatureSelectionne.prev('.miniature').length != 0) {
783
			miniatureAffichee = miniatureSelectionne.prev('.miniature');
784
			indicateurActif.prev().addClass('active');
785
		} else {
786
			miniatureAffichee = miniatureSelectionne.siblings('.miniature').last();
787
			indicateurActif.siblings().last().addClass('active');
788
		}
789
	} else {
790
		if (miniatureSelectionne.next('.miniature').length != 0) {
791
			miniatureAffichee = miniatureSelectionne.next('.miniature');
792
			indicateurActif.next().addClass('active');
793
		} else {
794
			miniatureAffichee = miniatureSelectionne.siblings('.miniature').first();
795
			indicateurActif.siblings().first().addClass('active');
796
		}
797
	}
798
	miniatureAffichee.addClass('miniature-selectionnee').removeClass('miniature-cachee');
2701 mathias 799
};
1922 jpm 800
 
2697 mathias 801
WidgetSaisieSauvages.prototype.formaterNumNomSel = function(numNomSel) {
1922 jpm 802
	var nn = '';
803
	if (numNomSel == undefined) {
804
		nn = '<span class="alert-error">[non lié au référentiel]</span>';
805
	} else {
806
		nn = '<span class="nn">[nn'+numNomSel+']</span>';
807
	}
808
	return nn;
2701 mathias 809
};
1922 jpm 810
 
2697 mathias 811
// surcharge
812
WidgetSaisieSauvages.prototype.surChangementReferentiel = function() {
2700 mathias 813
	this.nomSciReferentiel = $('#referentiel').val();
1922 jpm 814
	$('#taxon').val('');
2701 mathias 815
};
1922 jpm 816
 
2697 mathias 817
// surcharge
818
WidgetSaisieSauvages.prototype.stockerObsData = function() {
819
	var lthis = this;
1922 jpm 820
	var nomHorsListe = $('#taxon-liste').val() == '?' ? true : false;
821
		nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special'),
2709 mathias 822
		numNomSel = nomHorsListe ? $('#taxon').data('numNomSel') : $('#taxon-liste').val();
823
	var nomSel = nomHorsListe ? $('#taxon').val() : $('#taxon-liste option:selected').data('nom-a-sauver'),
2708 mathias 824
		nomRet = nomHorsListe ? $('#taxon').data('nomRet') : this.taxons[numNomSel]['nom_ret'],
825
		numNomRet = nomHorsListe ? $('#taxon').data('numNomRet') : this.taxons[numNomSel]['num_nom_ret'],
826
		numTaxon = nomHorsListe ? $('#taxon').data('nt') : this.taxons[numNomSel]['num_taxon'],
827
		famille = nomHorsListe ? $('#taxon').data('famille') : this.taxons[numNomSel]['famille'],
2700 mathias 828
		referentiel = (numNomSel == undefined) ? '' : this.nomSciReferentiel,
2708 mathias 829
		notes = (nomSpecial ? this.taxons[numNomSel]['nom_fr'] + '. ' : '') + $('#notes').val();
2521 aurelien 830
 
831
		certitude = $('input[name=certitude]:checked').val();
832
		certitude = (certitude == undefined) ? '' : certitude;
2408 jpm 833
 
2697 mathias 834
	$('#liste-obs').data('obsId'+this.obsNbre, {
2408 jpm 835
		'date': $('#date').val(),
1922 jpm 836
		'notes': notes,
2408 jpm 837
 
1946 jpm 838
		'station': $('input[name="adresse"]').val(),
1922 jpm 839
		'latitude': $('#latitude').val(),
840
		'longitude': $('#longitude').val(),
841
		'commune_nom': $('#commune-nom').text(),
842
		'commune_code_insee': $('#commune-code-insee').text(),
2408 jpm 843
 
1922 jpm 844
		'nom_sel': nomSel,
845
		'num_nom_sel': numNomSel,
846
		'nom_ret': nomRet,
847
		'num_nom_ret': numNomRet,
848
		'num_taxon': numTaxon,
849
		'famille': famille,
850
		'referentiel': referentiel,
2521 aurelien 851
		'certitude': certitude,
2697 mathias 852
		'milieu': lthis.getMilieux(),
2408 jpm 853
 
1922 jpm 854
		// Ajout des champs images
2697 mathias 855
		'image_nom': lthis.getNomsImgsOriginales(),
2408 jpm 856
 
1922 jpm 857
		// Ajout des champs étendus de l'obs
2697 mathias 858
		'obs_etendue': lthis.getObsChpEtendus()
1922 jpm 859
	});
2700 mathias 860
	if (this.debug) {
2697 mathias 861
		console.log($('#liste-obs').data('obsId'+this.obsNbre));
1964 jpm 862
	}
2701 mathias 863
};
1922 jpm 864
 
2697 mathias 865
WidgetSaisieSauvages.prototype.getObsChpEtendus = function() {
1946 jpm 866
	var champs = [];
2697 mathias 867
	if (this.latLngDeb != undefined) {
868
		var latitudeDebutRue = {cle: 'latitudeDebutRue', label: 'Latitude du début de la rue', valeur: this.latLngDeb.lat().toFixed(5)};
1922 jpm 869
		champs.push(latitudeDebutRue);
2697 mathias 870
		var longitudeDebutRue = {cle: 'longitudeDebutRue', label: 'Longitude du début de la rue', valeur: this.latLngDeb.lng().toFixed(5)};
1922 jpm 871
		champs.push(longitudeDebutRue);
872
	}
2697 mathias 873
	if (this.latLngFin != undefined) {
874
		var latitudeFinRue = {cle: 'latitudeFinRue', label: 'Latitude de fin de la rue', valeur: this.latLngFin.lat().toFixed(5)};
1922 jpm 875
		champs.push(latitudeFinRue);
2697 mathias 876
		var longitudeFinRue = {cle: 'longitudeFinRue', label: 'Longitude de fin de la rue', valeur: this.latLngFin.lng().toFixed(5)};
1922 jpm 877
		champs.push(longitudeFinRue);
878
	}
2408 jpm 879
 
1922 jpm 880
	$('.obs-chp-etendu').each(function() {
881
		var valeur = $(this).val(),
882
			cle = $(this).attr('name'),
883
			label = $(this).data('label');
884
		if (valeur != '') {
885
			var chpEtendu = {cle: cle, label: label, valeur: valeur};
886
			champs.push(chpEtendu);
887
		}
888
	});
889
	return champs;
2701 mathias 890
};