Subversion Repositories eFlore/Applications.cel

Rev

Rev 2708 | 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() {
1922 jpm 358
	if (navigator.geolocation) {
359
		navigator.geolocation.getCurrentPosition(function(position) {
360
			var latitude = position.coords.latitude,
361
				longitude = position.coords.longitude,
362
				nouvellePosition = new google.maps.LatLng(latitude, longitude);
2697 mathias 363
			this.initialiserMarkerDeb();
364
			this.deplacerMarkerDeb(nouvellePosition);
365
			this.map.setZoom(16);
1922 jpm 366
		});
367
	}
2701 mathias 368
};
1922 jpm 369
 
2697 mathias 370
/**
371
 * AUTO-COMPLÉTION Noms Scientifiques => OK
372
 * sélectionne un nom et puis qu'on le remplacer par un nom non valide
373
 * Garder la trace de la valeur permet de vider le nn lorsqu'on
374
 */
375
WidgetSaisieSauvages.prototype.ajouterAutocompletionNoms = function() {
376
	var lthis = this;
1922 jpm 377
	$('#taxon').autocomplete({
2408 jpm 378
		source: function(requete, add){
1922 jpm 379
			// la variable de requête doit être vidée car sinon le parametre "term" est ajouté
2408 jpm 380
 
2697 mathias 381
			var url = lthis.getUrlAutocompletionNomsSci();
1922 jpm 382
			$.getJSON(url, function(data) {
2697 mathias 383
				var suggestions = lthis.traiterRetourNomsSci(data);
2408 jpm 384
				add(suggestions);
1922 jpm 385
			});
386
		},
387
		html: true
388
	});
2408 jpm 389
 
1922 jpm 390
	$('#taxon').bind('autocompleteselect', function(event, ui) {
391
		$('#taxon').data(ui.item);
2697 mathias 392
		lthis.valeurChamp = $('#taxon').val();
1922 jpm 393
		if (ui.item.retenu == true) {
394
			$('#taxon').addClass('ns-retenu');
395
		} else {
396
			$('#taxon').removeClass('ns-retenu');
397
		}
398
	});
2529 aurelien 399
 
400
	$('#taxon').bind('keypress', function() {
2697 mathias 401
		if(lthis.valeurChamp != $('#taxon').val()) {
2529 aurelien 402
			$('#taxon').data('numNomSel', '');
403
		}
2697 mathias 404
		lthis.valeurChamp = $('#taxon').val();
2529 aurelien 405
	});
2701 mathias 406
};
1922 jpm 407
 
2697 mathias 408
WidgetSaisieSauvages.prototype.getUrlAutocompletionNomsSci = function() {
1922 jpm 409
	var mots = $('#taxon').val(),
2700 mathias 410
		url = this.serviceAutocompletionNomSciUrlTpl.replace('{referentiel}', this.nomSciReferentiel);
1922 jpm 411
	url = url.replace('{masque}', mots);
412
	return url;
2701 mathias 413
};
1922 jpm 414
 
2697 mathias 415
WidgetSaisieSauvages.prototype.traiterRetourNomsSci = function(data) {
1922 jpm 416
	var suggestions = [];
417
	if (data.resultat != undefined) {
418
		$.each(data.resultat, function(i, val) {
419
			val.nn = i;
420
			var nom = {label: '', value: '', nt: '', nomSel: '', nomSelComplet: '', numNomSel: '',
421
				nomRet: '', numNomRet: '', famille: '', retenu: false
422
			};
2700 mathias 423
			if (suggestions.length >= this.autocompletionElementsNbre) {
1922 jpm 424
				nom.label = '...';
425
				nom.value = $('#taxon').val();
426
				suggestions.push(nom);
427
				return false;
428
			} else {
429
				nom.label = val.nom_sci_complet;
430
				nom.value = val.nom_sci_complet;
431
				nom.nt = val.num_taxonomique;
432
				nom.nomSel = val.nom_sci;
433
				nom.nomSelComplet = val.nom_sci_complet;
434
				nom.numNomSel = val.nn;
435
				nom.nomRet = val.nom_retenu_complet;
436
				nom.numNomRet = val['nom_retenu.id'];
437
				nom.famille = val.famille;
438
				nom.retenu = (val.retenu == 'false') ? false : true;
2408 jpm 439
 
1922 jpm 440
				suggestions.push(nom);
441
			}
442
		});
443
	}
444
	return suggestions;
2701 mathias 445
};
1922 jpm 446
 
2697 mathias 447
// surcharge
448
WidgetSaisieSauvages.prototype.configurerFormValidator = function() {
1916 jpm 449
	$.validator.addMethod(
2408 jpm 450
		'dateCel',
451
		function (value, element) {
452
			return value == '' || (/^[0-9]{2}[-\/][0-9]{2}[-\/][0-9]{4}$/.test(value));
453
		},
1916 jpm 454
		'Format : jj/mm/aaaa. Date incomplète, utiliser 0, exemple : 00/12/2011.');
2408 jpm 455
 
1916 jpm 456
	$.extend($.validator.defaults, {
457
		ignore: [],// Forcer Jquery Validate à examiner les éléments avec en display:none;
458
		highlight: function(element) {
459
			$(element).closest('.control-group').removeClass('success').addClass('error');
460
		},
461
		success: function(element) {
462
			element.text('OK!').addClass('valid');
463
			element.closest('.control-group').removeClass('error').addClass('success');
2408 jpm 464
 
1916 jpm 465
			if (element.attr('id') == 'taxon' && $('#taxon').val() != '') {
466
				// Si le taxon n'est pas lié au référentiel, on vide le data associé
467
				if ($('#taxon').data('value') != $('#taxon').val()) {
468
					$('#taxon').data('numNomSel', '');
469
					$('#taxon').data('nomRet', '');
470
					$('#taxon').data('numNomRet', '');
471
					$('#taxon').data('nt', '');
472
					$('#taxon').data('famille', '');
473
				}
474
			}
475
		}
476
	});
2701 mathias 477
};
1916 jpm 478
 
2697 mathias 479
// surcharge
480
WidgetSaisieSauvages.prototype.definirReglesFormValidator = function() {
1916 jpm 481
	$('#form-observateur').validate({
482
		rules: {
483
			courriel: {
484
				required: true,
485
				email: true},
486
			courriel_confirmation: {
487
				required: true,
488
				equalTo: '#courriel'},
489
			prenom: {
490
				required: true},
491
			nom: {
492
				required: true}
493
		}
494
	});
495
	$('#form-obs').validate({
496
		rules: {
497
			station: {
498
				required: true},
499
			latitude : {
500
				required: true,
501
				range: [-90, 90]},
502
			longitude: {
503
				required: true,
504
				range: [-180, 180]},
505
			date: {
506
				required: true,
507
				'dateCel' : true},
1956 jpm 508
			coteRue: {
1916 jpm 509
				required: true},
510
			'taxon-liste': {
511
				required: true},
512
			'milieux[]': {
513
				required: true,
514
				minlength: 1}
515
		},
516
		errorPlacement: function(error, element) {
517
			if (element.attr('name') == 'date') {
518
				element.parent('.input-prepend').after(error);
519
			} else if (element.attr('name') == 'milieux[]') {
520
				error.insertAfter('#milieux-controls');
521
			} else {
522
				error.insertAfter(element);
523
			}
524
		},
525
		messages: {
526
			'milieu[]': 'Vous devez sélectionner au moins un milieu'
527
		}
528
	});
2701 mathias 529
};
1916 jpm 530
 
2697 mathias 531
WidgetSaisieSauvages.prototype.validerFormulaire = function() {
1946 jpm 532
	var observateur = $('#form-observateur').valid(),
533
		obs = $('#form-obs').valid(),
2697 mathias 534
		debRue = (this.latLngDeb == undefined || this.latLngDeb == this.latLngFin) ? false : true,
535
		finRue = (this.latLngFin == undefined || this.latLngDeb == this.latLngFin) ? false : true;
1946 jpm 536
	var ok = (observateur && obs && debRue && finRue) ? true : false;
1964 jpm 537
	//console.log('observateur:'+observateur+'-obs:'+obs+'-debRue:'+debRue+'('+latLngDeb+')-finRue:'+finRue+'('+latLngDeb+')');
1946 jpm 538
	return ok;
2701 mathias 539
};
1946 jpm 540
 
2697 mathias 541
WidgetSaisieSauvages.prototype.surChangementTaxonListe = function() {
1916 jpm 542
	if ($('#taxon-liste').val() === '?') {
543
		$('#taxon-input-groupe').removeClass('hidden');
544
	} else {
545
		$('#taxon-input-groupe').addClass('hidden');
546
	}
2701 mathias 547
};
1916 jpm 548
 
2697 mathias 549
WidgetSaisieSauvages.prototype.surChangementValeurTaxon = function() {
2529 aurelien 550
	var nomHorsListe = $('#taxon-liste').val() == '?' ? true : false;
2697 mathias 551
	var nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special');
552
	var numNomSel = nomHorsListe ? $('#taxon').data('numNomSel') : $('#taxon-liste').val();
2529 aurelien 553
 
554
	// Un nom non valide entraine automatiquement une certitude "à déterminer"
555
	if(nomSpecial || !numNomSel) {
556
		$('#certitude-adeterminer').attr('checked', 'checked');
557
	} else {
558
		$('#certitude-adeterminer').removeAttr('checked');
559
	}
2701 mathias 560
};
2529 aurelien 561
 
2697 mathias 562
// surcharge
563
WidgetSaisieSauvages.prototype.ajouterObs = function() {
564
	if (this.validerFormulaire() == true) {
565
		this.obsNbre = this.obsNbre + 1;
566
		$('.obs-nbre').text(this.obsNbre);
1922 jpm 567
		$('.obs-nbre').triggerHandler('changement');
2697 mathias 568
		this.afficherObs();
569
		this.stockerObsData();
570
		this.supprimerMiniatures();
1922 jpm 571
	} else {
1956 jpm 572
		// Affichage de tous les panneau cachés avec champ obligatoire
2697 mathias 573
		var debRue = (this.latLngDeb == undefined || this.latLngDeb == this.latLngFin) ? false : true,
574
			finRue = (this.latLngFin == undefined || this.latLngDeb == this.latLngFin) ? false : true;
1922 jpm 575
		if (debRue == false || finRue == false) {
2700 mathias 576
			this.afficherPanneau('#dialogue-form-invalide-rue');
1922 jpm 577
		} else {
2700 mathias 578
			this.afficherPanneau('#dialogue-form-invalide');
1922 jpm 579
		}
2697 mathias 580
		this.montrerFormIdentite();
1922 jpm 581
	}
2697 mathias 582
};
1922 jpm 583
 
2697 mathias 584
// surcharge
585
WidgetSaisieSauvages.prototype.afficherObs = function() {
1922 jpm 586
	var numNomSel = ($('#taxon-liste').val() == '?') ? $('#taxon').data('numNomSel') : $('#taxon-liste').val(),
587
		nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special'),
588
		taxon = ($('#taxon-liste').val() == '?') ? $('#taxon').val() : $('#taxon-liste option:selected').data('nom-a-sauver'),
2700 mathias 589
		referentiel = (numNomSel == undefined) ? '' : '['+ this.nomSciReferentiel +']',
1922 jpm 590
		commune = $('#commune-nom').text(),
591
		codeInsee = $('#commune-code-insee').text(),
1946 jpm 592
		station = $('input[name="adresse"]').val(),
1922 jpm 593
		lat = $('input[name="latitude"]').val(),
594
		lng = $('input[name="longitude"]').val(),
595
		date = $('#date').val(),
2697 mathias 596
		milieux = this.getMilieux(),
597
		notes = (nomSpecial ? this.taxons[numNomSel]['nom_fr'] + ".<br />" : '') + $('#notes').val();
2408 jpm 598
 
1922 jpm 599
	$('#liste-obs').prepend(
2697 mathias 600
		'<div id="obs'+this.obsNbre+'" class="row-fluid obs obs'+this.obsNbre+'">' +
1946 jpm 601
			'<div class="span12">' +
602
				'<div class="well">' +
603
					'<div class="obs-action pull-right has-tooltip" data-placement="bottom" ' +
604
						'title="Supprimer cette observation de la liste à transmettre">' +
2697 mathias 605
						'<button class="btn btn-danger supprimer-obs" value="'+this.obsNbre+'" title="'+this.obsNbre+'">' +
1946 jpm 606
							'<i class="icon-trash icon-white"></i>' +
607
						'</button>' +
2408 jpm 608
					'</div> ' +
609
					'<div class="row-fluid">' +
1946 jpm 610
						'<div class="span2 obs-miniatures">' +
2697 mathias 611
						this.ajouterImgMiniatureAuTransfert() +
1922 jpm 612
						'</div>'+
1946 jpm 613
						'<div class="span7">' +
614
							'<ul class="unstyled">' +
1922 jpm 615
								'<li>'+
616
									'<span class="nom-sci">' + taxon + '</span> ' +
2697 mathias 617
									this.formaterNumNomSel(numNomSel) +
1946 jpm 618
									' observé à <br />' +
1993 jpm 619
									'<span class="commune">' + commune + '</span> ' +
620
									'(' + codeInsee + '), ' +
1946 jpm 621
									'<span class="station">' + station + '</span><br /> ' +
1922 jpm 622
									' le ' +
623
									'<span class="date">' + date + '</span>' +
624
								'</li>' +
625
								'<li>' +
1946 jpm 626
									'Milieux : ' + milieux + ' ' + ' ; ' +
1922 jpm 627
								'</li>' +
628
								'<li>' +
2408 jpm 629
									'Notes : ' + notes +
1946 jpm 630
								'</li>' +
631
							'</ul>' +
632
						'</div>' +
633
					'</div>' +
634
				'</div>' +
1922 jpm 635
			'</div>'+
636
		'</div>');
2697 mathias 637
	$('#zone-liste-obs').removeClass("hidden");
638
};
1922 jpm 639
 
2697 mathias 640
WidgetSaisieSauvages.prototype.getMilieux = function() {
1922 jpm 641
	var milieuxStr = '',
642
		milieux = [];
643
	$('.cb-milieux:checked').each(function() {
644
		milieux.push($(this).val());
645
	});
2408 jpm 646
 
1922 jpm 647
	milieuxStr = Array.prototype.slice.call(milieux).join(', ');
648
	return milieuxStr;
2697 mathias 649
};
1922 jpm 650
 
2697 mathias 651
WidgetSaisieSauvages.prototype.ajouterImgMiniatureAuTransfert = function() {
1922 jpm 652
	var html = '',
653
		miniatures = '',
654
		indicateurs = '',
655
		premiere = true,
656
		numero = 1;
657
	if ($('#miniatures img').length == 0) {
2700 mathias 658
		html = '<img class="miniature" alt="Aucune photo"src="'+ this.pasDePhotoIconeUrl +'" />';
1922 jpm 659
	} else if ($('#miniatures img').length >= 1) {
660
		$('#miniatures img').each(function() {
661
			var visible = premiere ? 'miniature-selectionnee' : 'miniature-cachee',
662
				css = $(this).hasClass('b64') ? 'miniature b64' : 'miniature',
663
				src = $(this).attr('src'),
664
				alt = $(this).attr('alt');
2408 jpm 665
 
1922 jpm 666
			var miniature = '<img class="'+css+' '+visible+'"  alt="'+alt+'"src="'+src+'" />';
667
			miniatures += miniature;
2408 jpm 668
 
1922 jpm 669
			var indicateurActif = premiere ? 'active' : '';
670
			var indicateur = '<li class="' + indicateurActif + '" data-numero="' + numero++ + '"></li>';
671
			indicateurs += indicateur;
2408 jpm 672
 
1922 jpm 673
			premiere = false;
674
		});
2408 jpm 675
 
1922 jpm 676
		if ($('#miniatures img').length == 1) {
677
			html = miniatures;
678
		} else {
2408 jpm 679
			html =
1922 jpm 680
				'<div class="defilement">' +
681
					miniatures +
682
					'<a class="defilement-control-zone gauche">' +
683
					'	<span class="defilement-control gauche hidden">&#60;</span>' +
684
					'</a>' +
685
					'<a class="defilement-control-zone droite">' +
686
					'	<span class="defilement-control droite hidden">&#62;</span>' +
687
					'</a>' +
688
					'<ol class="defilement-indicateurs">' + indicateurs + '</ol>' +
689
				'</div>';
690
		}
691
	}
692
	return html;
2697 mathias 693
};
1922 jpm 694
 
2697 mathias 695
WidgetSaisieSauvages.prototype.defilerMiniatures = function(element) {
1922 jpm 696
	var miniatureSelectionne = element.siblings('img.miniature-selectionnee');
697
	miniatureSelectionne.removeClass('miniature-selectionnee').addClass('miniature-cachee');
698
	var miniatureAffichee = miniatureSelectionne;
2408 jpm 699
 
1922 jpm 700
	var indicateurActif = element.parent().find('.defilement-indicateurs .active');
701
	indicateurActif.removeClass('active');
2408 jpm 702
 
1922 jpm 703
	if (element.hasClass('defilement-control-zone') && element.hasClass('gauche')) {
704
		if (miniatureSelectionne.prev('.miniature').length != 0) {
705
			miniatureAffichee = miniatureSelectionne.prev('.miniature');
706
			indicateurActif.prev().addClass('active');
707
		} else {
708
			miniatureAffichee = miniatureSelectionne.siblings('.miniature').last();
709
			indicateurActif.siblings().last().addClass('active');
710
		}
711
	} else {
712
		if (miniatureSelectionne.next('.miniature').length != 0) {
713
			miniatureAffichee = miniatureSelectionne.next('.miniature');
714
			indicateurActif.next().addClass('active');
715
		} else {
716
			miniatureAffichee = miniatureSelectionne.siblings('.miniature').first();
717
			indicateurActif.siblings().first().addClass('active');
718
		}
719
	}
720
	miniatureAffichee.addClass('miniature-selectionnee').removeClass('miniature-cachee');
2701 mathias 721
};
1922 jpm 722
 
2697 mathias 723
WidgetSaisieSauvages.prototype.formaterNumNomSel = function(numNomSel) {
1922 jpm 724
	var nn = '';
725
	if (numNomSel == undefined) {
726
		nn = '<span class="alert-error">[non lié au référentiel]</span>';
727
	} else {
728
		nn = '<span class="nn">[nn'+numNomSel+']</span>';
729
	}
730
	return nn;
2701 mathias 731
};
1922 jpm 732
 
2697 mathias 733
// surcharge
734
WidgetSaisieSauvages.prototype.surChangementReferentiel = function() {
2700 mathias 735
	this.nomSciReferentiel = $('#referentiel').val();
1922 jpm 736
	$('#taxon').val('');
2701 mathias 737
};
1922 jpm 738
 
2697 mathias 739
// surcharge
740
WidgetSaisieSauvages.prototype.stockerObsData = function() {
741
	var lthis = this;
1922 jpm 742
	var nomHorsListe = $('#taxon-liste').val() == '?' ? true : false;
743
		nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special'),
2709 mathias 744
		numNomSel = nomHorsListe ? $('#taxon').data('numNomSel') : $('#taxon-liste').val();
745
	var nomSel = nomHorsListe ? $('#taxon').val() : $('#taxon-liste option:selected').data('nom-a-sauver'),
2708 mathias 746
		nomRet = nomHorsListe ? $('#taxon').data('nomRet') : this.taxons[numNomSel]['nom_ret'],
747
		numNomRet = nomHorsListe ? $('#taxon').data('numNomRet') : this.taxons[numNomSel]['num_nom_ret'],
748
		numTaxon = nomHorsListe ? $('#taxon').data('nt') : this.taxons[numNomSel]['num_taxon'],
749
		famille = nomHorsListe ? $('#taxon').data('famille') : this.taxons[numNomSel]['famille'],
2700 mathias 750
		referentiel = (numNomSel == undefined) ? '' : this.nomSciReferentiel,
2708 mathias 751
		notes = (nomSpecial ? this.taxons[numNomSel]['nom_fr'] + '. ' : '') + $('#notes').val();
2521 aurelien 752
 
753
		certitude = $('input[name=certitude]:checked').val();
754
		certitude = (certitude == undefined) ? '' : certitude;
2408 jpm 755
 
2697 mathias 756
	$('#liste-obs').data('obsId'+this.obsNbre, {
2408 jpm 757
		'date': $('#date').val(),
1922 jpm 758
		'notes': notes,
2408 jpm 759
 
1946 jpm 760
		'station': $('input[name="adresse"]').val(),
1922 jpm 761
		'latitude': $('#latitude').val(),
762
		'longitude': $('#longitude').val(),
763
		'commune_nom': $('#commune-nom').text(),
764
		'commune_code_insee': $('#commune-code-insee').text(),
2408 jpm 765
 
1922 jpm 766
		'nom_sel': nomSel,
767
		'num_nom_sel': numNomSel,
768
		'nom_ret': nomRet,
769
		'num_nom_ret': numNomRet,
770
		'num_taxon': numTaxon,
771
		'famille': famille,
772
		'referentiel': referentiel,
2521 aurelien 773
		'certitude': certitude,
2697 mathias 774
		'milieu': lthis.getMilieux(),
2408 jpm 775
 
1922 jpm 776
		// Ajout des champs images
2697 mathias 777
		'image_nom': lthis.getNomsImgsOriginales(),
2408 jpm 778
 
1922 jpm 779
		// Ajout des champs étendus de l'obs
2697 mathias 780
		'obs_etendue': lthis.getObsChpEtendus()
1922 jpm 781
	});
2700 mathias 782
	if (this.debug) {
2697 mathias 783
		console.log($('#liste-obs').data('obsId'+this.obsNbre));
1964 jpm 784
	}
2701 mathias 785
};
1922 jpm 786
 
2697 mathias 787
WidgetSaisieSauvages.prototype.getObsChpEtendus = function() {
1946 jpm 788
	var champs = [];
2697 mathias 789
	if (this.latLngDeb != undefined) {
790
		var latitudeDebutRue = {cle: 'latitudeDebutRue', label: 'Latitude du début de la rue', valeur: this.latLngDeb.lat().toFixed(5)};
1922 jpm 791
		champs.push(latitudeDebutRue);
2697 mathias 792
		var longitudeDebutRue = {cle: 'longitudeDebutRue', label: 'Longitude du début de la rue', valeur: this.latLngDeb.lng().toFixed(5)};
1922 jpm 793
		champs.push(longitudeDebutRue);
794
	}
2697 mathias 795
	if (this.latLngFin != undefined) {
796
		var latitudeFinRue = {cle: 'latitudeFinRue', label: 'Latitude de fin de la rue', valeur: this.latLngFin.lat().toFixed(5)};
1922 jpm 797
		champs.push(latitudeFinRue);
2697 mathias 798
		var longitudeFinRue = {cle: 'longitudeFinRue', label: 'Longitude de fin de la rue', valeur: this.latLngFin.lng().toFixed(5)};
1922 jpm 799
		champs.push(longitudeFinRue);
800
	}
2408 jpm 801
 
1922 jpm 802
	$('.obs-chp-etendu').each(function() {
803
		var valeur = $(this).val(),
804
			cle = $(this).attr('name'),
805
			label = $(this).data('label');
806
		if (valeur != '') {
807
			var chpEtendu = {cle: cle, label: label, valeur: valeur};
808
			champs.push(chpEtendu);
809
		}
810
	});
811
	return champs;
2701 mathias 812
};