Subversion Repositories eFlore/Applications.cel

Rev

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