Subversion Repositories eFlore/Applications.cel

Rev

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