Subversion Repositories eFlore/Applications.cel

Rev

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