Subversion Repositories Sites.obs-saisons.fr

Rev

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

Rev Author Line No. Line
168 aurelien 1
var urlBaseJrest = 'http://localhost/obs_saisons/applications/jrest/';
2
 
54 aurelien 3
/**
4
	************************************************************************************************
5
	************************************************************************************************
6
 
7
	   Fonction permettant de transformer les élements de la classe pliage en accordéon
8
 
9
	************************************************************************************************
10
	************************************************************************************************
11
**/
12
function initialiserElementsPliables() {
13
 
14
	$('.pliage h4').addClass('lien_pliage');
15
 
16
	$('.pliage ul').hide();
17
	$('.pliage > ul:first-child').hide();
188 aurelien 18
 
19
	$('.1er_element_date ul').show();
20
	$('.1er_element_date > ul:first-child').show();
54 aurelien 21
 
22
	$('.lien_pliage').bind('click', function() {
23
 
24
		$(this).nextAll('ul').slideToggle();
25
		return false;
26
	});
27
}
28
 
29
$('.pliage').ready(function() {
30
	initialiserElementsPliables();
31
});
32
 
188 aurelien 33
/**
34
************************************************************************************************
35
************************************************************************************************
54 aurelien 36
 
188 aurelien 37
   Fonctions pour la fiche individu
54 aurelien 38
 
188 aurelien 39
************************************************************************************************
40
************************************************************************************************
41
**/
54 aurelien 42
 
188 aurelien 43
observations = $('.observations_individu');
44
window.alert(dump(observations));
45
observations[0].ready(function() {
46
	window.alert(dump(observations));
47
	$(this).nextAll('ul').show();
48
});
49
 
50
 
54 aurelien 51
/**
52
	************************************************************************************************
53
	************************************************************************************************
54
 
55
	   Fonctions de gestion des onglets du formulaire de saisie d'espèce
56
 
57
	************************************************************************************************
58
	************************************************************************************************
59
**/
60
function initialiserOnglets() {
61
	$(".contenu_onglet").hide();
62
	$("ul.liste_onglets li:first").addClass("active").show();
63
	$(".contenu_onglet:first").show();
64
 
65
	$("ul.liste_onglets li").click(function() {
66
 
67
		$("ul.liste_onglets li").removeClass("active");
68
		$(this).addClass("active");
69
		$(".contenu_onglet").hide();
70
 
71
		var activeTab = $(this).find("a").attr("href");
72
		$(activeTab).fadeIn();
73
		return false;
74
	});
75
}
76
 
77
$('.contenu_onglet').ready(function() {
78
	initialiserOnglets();
79
});
80
 
81
function agrandirZoneCLicBoutonsRadios() {
82
 
83
	$('.formulaire_informations_espece_mini').addClass("element_cliquable");
84
 
85
	$('#form_saisie_espece').children('input[type="submit"]').hide();
86
	$('.formulaire_informations_espece_mini').children('input[type="radio"]').hide();
87
	$('.titre_type').hide();
88
 
89
	$('.formulaire_informations_espece_mini').bind('click',function() {
90
		$(this).children('input[type="radio"]').attr('checked','checked');
91
		$('.formulaire_informations_espece_mini').removeClass('element_clique');
92
		$(this).addClass('element_clique');
93
 
94
		$('#form_saisie_espece').submit();
95
	});
96
}
97
 
98
$('#form_saisie_espece').ready(function() {
99
	agrandirZoneCLicBoutonsRadios();
100
});
101
 
102
 
103
/**
104
	************************************************************************************************
105
	************************************************************************************************
106
 
107
	   Fonctions permettant de transformer les cases du tableau d'évenements en mini formulaire
108
 
109
	************************************************************************************************
110
	************************************************************************************************
111
**/
112
 
113
htmlEnCours = '';
114
elementEnCours = null;
115
 
116
function initialiserLignesCliquables() {
117
 
118
	$('.conteneur_element_modifier').hide();
119
 
120
	$(".date_observation_individu").each(function() {
121
 
122
		$(this).bind('click', function() {
123
 
124
			if($(this).hasClass('element_clique')) {
125
				return false;
126
			} else {
127
 
128
				conteneur_obs = $(this).children('span');
129
				id_formulaire = conteneur_obs.attr('id');
130
 
131
				remplacerElementDateParFormulaireAjax(id_formulaire, $(this));
132
 
133
				return false;
134
			}
135
		});
136
	});
137
}
138
 
139
function initialiserCalendrierFormulaire() {
140
 
141
	anneeEnCours = $("#annee_en_cours").attr("value");
142
 
143
	if(typeof(anneeEnCours)=='undefined'){
144
		dateCourante = new Date();
145
		anneeEnCours = dateCourante.getFullYear();
146
	}
147
 
148
	$(".calendrier").each(function() {
149
		$(this).find("input").datepicker({
150
			disabled: true,
151
			altFormat: 'dd/mm/yyyy',
152
			minDate: '01/01/'+anneeEnCours,
153
			maxDate: '31/12/'+anneeEnCours
154
 
155
		});
156
	});
157
}
158
 
159
$(document).ready(function() {
160
	initialiserCalendrierFormulaire();
161
 
162
});
163
 
90 aurelien 164
function estUneDateValide(peutEtreUneDate) {
165
	var reg= new RegExp("^[0-3][0-9][/]{1}[0-1][0-2][/]{1}[0-9]{4}$","g");
166
	return reg.test(peutEtreUneDate);
167
}
168
 
54 aurelien 169
/** Fonctions de manipulation des éléments du tableau pour les transformer en élements cliquables et calendriers **/
170
function initialiserMiniCalendrierFormulaire(id_element) {
171
 
172
	anneeEnCours = jQuery.trim($("#annee_en_cours").html());
173
 
174
	if(typeof(anneeEnCours)=='undefined'){
175
		dateCourante = new Date();
176
		anneeEnCours = dateCourante.getFullYear();
177
	}
178
 
90 aurelien 179
	$('#'+id_element)
180
	.datepicker({
54 aurelien 181
		disabled: true,
90 aurelien 182
		constrainInput: true,
54 aurelien 183
		altFormat: 'dd/mm/yyyy',
184
		minDate: '01/01/'+anneeEnCours,
90 aurelien 185
		maxDate: '31/12/'+anneeEnCours,
186
		onSelect: function(dateText, inst) {
187
			$('#'+id_element).val(dateText);
188
			donnees_obs = collecterDonneesMiniFormulaire(id_formulaire);
189
			envoyerRequeteAjaxValidationMiniFormulaire(donnees_obs, id_formulaire);
190
		}
54 aurelien 191
	});
90 aurelien 192
 
193
	$('#'+id_element).datepicker( "show" );
194
 
195
	/*$('#'+id_element).keypress(function() {
196
		if(estUneDateValide($('#'+id_element).val())) {
197
			$('#'+id_element).removeClass('erreur_valeur');
198
		} else {
199
			$('#'+id_element).addClass('erreur_valeur');
200
		}
201
 
202
	});*/
54 aurelien 203
}
204
 
205
function remplacerElementDateParFormulaireAjax(id_element, conteneur_selectionne) {
206
 
207
	if(elementEnCours != null) {
208
		elementEnCours.removeClass('element_clique');
209
		elementEnCours.html(htmlEnCours);
210
	}
211
 
212
	elementEnCours = conteneur_selectionne;
213
	htmlEnCours = conteneur_selectionne.html();
214
 
215
	conteneur_obs = $('#'+id_element);
216
	valeur_date = conteneur_obs.html();
217
	valeur_date = valeur_date.replace('-','');
90 aurelien 218
 
54 aurelien 219
	conteneur_selectionne.addClass('element_clique');
90 aurelien 220
	conteneur_selectionne.html('<span class="calendrier"><input type="text" value="'+jQuery.trim(valeur_date)+'" class="calendrier" id="'+id_formulaire+'" size="7" maxlenght="10" /></div>');
54 aurelien 221
	initialiserMiniCalendrierFormulaire(id_formulaire);
222
 
223
}
224
 
225
function remplacerMiniFormulaireParElementDate(id_element) {
226
 
90 aurelien 227
	valeur_date_pour_stade = $('#'+id_element).val();
228
 
229
	/*if(!estUneDateValide(valeur_date_pour_stade)) {
230
		valeur_date_pour_stade = htmlEnCours;
231
	}*/
232
 
54 aurelien 233
	elementEnCours.html('<span id="'+id_element+'">'+valeur_date_pour_stade+' </span>');
234
	elementEnCours.removeClass('element_clique');
235
	elementEnCours = null;
100 aurelien 236
 
54 aurelien 237
}
238
 
239
 
240
/** Fonctions d'envoi et de récupération des élements du formulaire ajax **/
241
function envoyerRequeteAjaxValidationMiniFormulaire(donnees_obs, id_formulaire) {
242
 
100 aurelien 243
	url_page_courante = document.URL;
244
	url_page_courante = url_page_courante.replace('module=Individu','module=Observation');
245
	url_page_courante = url_page_courante.replace('action=afficherListeIndividu','action=validerFormulaireModificationObservationAjax');
246
	url_ajax = url_page_courante.replace('action=validerFormulaireSaisieIndividu','action=validerFormulaireModificationObservationAjax');
247
 
248
	$.post(url_ajax, donnees_obs, function(obj_retour) {
249
 
54 aurelien 250
		if(obj_retour.reponse === 'OK') {
251
			remplacerMiniFormulaireParElementDate(id_formulaire);
252
		}
253
	});
254
}
255
 
256
function collecterDonneesMiniFormulaire(id_formulaire) {
257
 
258
	valeur_date_pour_stade = $('#'+id_formulaire).val();
259
 
260
	if(valeur_date_pour_stade == null || jQuery.trim(valeur_date_pour_stade) == '') {
261
		return false ;
262
	}
263
 
264
	id_individu_stade_obs = id_formulaire.split('_');
265
 
266
	stade_obs = 'observation_'+id_individu_stade_obs[3];
267
	date_annee_en_cours = jQuery.trim($("#annee_en_cours").html());
268
 
269
	donnees_obs = { "id_individu": id_individu_stade_obs[1],
90 aurelien 270
					"annee_en_cours": date_annee_en_cours
54 aurelien 271
					};
272
 
90 aurelien 273
	identifiant_observation = 'observation_'+id_individu_stade_obs[3];
54 aurelien 274
 
90 aurelien 275
	donnees_obs[identifiant_observation] = valeur_date_pour_stade;
276
 
54 aurelien 277
	return donnees_obs;
278
}
279
 
280
 
281
$('#saisie_liste_evenements').ready(function() {
282
	initialiserLignesCliquables();
283
});
284
 
285
 
286
 
287
 
288
/**
289
	************************************************************************************************
290
	************************************************************************************************
291
 
292
	    Fonctions concernant la carte permettant de pointer d'afficher l'emplacement des stations
293
 
294
	************************************************************************************************
295
	************************************************************************************************
296
**/
297
var map;
298
var marker;
100 aurelien 299
var liste_localite_en_cours;
300
var indice_commune_en_cours;
301
var liste_auto_completion_a_le_focus;
302
var timerRequeteAutocompletion;
303
var timerAffichageAutocompletion;
54 aurelien 304
 
100 aurelien 305
 
306
function cacherElementsRafraichissables() {
307
	$('.rafraichissable input').attr('disabled', 'disabled');
308
	$('.rafraichissable input[type="text"]').addClass("chargement");
309
}
310
 
311
function montrerElementsRafraichissables() {
312
	$('.rafraichissable input').removeAttr('disabled');
313
	$('.chargement').removeClass("chargement");
314
}
315
 
316
function ajouterAutoCompletionCommune() {
317
 
318
	$('input#station_commune').after('<div class="conteneur_suggestions"></div>');
319
	$('input#station_commune').parent().addClass('autocompletion');
320
	$('input#station_commune').attr('autocomplete','off');
321
	$('.conteneur_suggestions').hide();
322
	rendreListeAutoCompletionInteractive('input#station_commune');
323
}
324
 
54 aurelien 325
function ajouterListenerFormulaireSaisieLatLon() {
100 aurelien 326
 
54 aurelien 327
	$('input#localiser_lat_lon').click(function() {
328
		verifierEtLocaliserCoordonnees();
329
	});
330
}
331
 
332
function carteEstEnSaisie() {
333
	return ($('#conteneur_form_liens_lat_lon').length > 0);
334
}
335
 
336
function verifierEtLocaliserCoordonnees() {
337
 
338
	lat ;
339
	lon;
340
 
341
	if(carteEstEnSaisie()) {
342
		lat = $('#station_lat').val();
343
		lon = $('#station_lon').val();
100 aurelien 344
 
345
		obtenirInformationsPourCoordonnees(lat, lon);
54 aurelien 346
	} else {
347
		var lat = jQuery.trim($('#station_lat').html());
348
		var lon = jQuery.trim($('#station_lon').html());
349
	}
350
 
351
	if(jQuery.trim(lat) == '' || jQuery.trim(lon) == '') {
352
		return;
353
	}
354
 
355
	if(!isNaN(lat) && lat.length > 0 && !isNaN(lon) && lon.length > 0) {
356
 
357
	} else {
358
		window.alert("coordonnées invalides");
359
	}
360
 
361
	var positionMarker = new google.maps.LatLng(lat, lon);
362
 
363
	marker.setPosition(positionMarker);
364
	map.setCenter(positionMarker);
100 aurelien 365
	map.setZoom(12);
54 aurelien 366
}
367
 
100 aurelien 368
function obtenirInformationsPourCoordonnees(lat, lon) {
369
 
370
	lat = jQuery.trim(lat);
371
	lon = jQuery.trim(lon);
372
 
373
	if(isNaN(lat) || lat.length <= 0 || isNaN(lon) || lon.length <= 0) {
374
		return;
375
	}
376
 
377
	cacherElementsRafraichissables();
378
 
168 aurelien 379
	$.get(urlBaseJrest+'OdsCommune/informationsPourCoordonnees/?lat='+lat+'&lon='+lon, function(data) {
100 aurelien 380
 
381
		infos_localites = jQuery.parseJSON(data);
382
		$('#station_alt').val(infos_localites.alt);
383
 
196 aurelien 384
		if(infos_localites.commune != null && infos_localites.commune != '') {
385
		  $('#station_commune').val(infos_localites.commune);
386
		  $('#station_code_insee').val(infos_localites.code_insee);
100 aurelien 387
		}
388
 
389
		montrerElementsRafraichissables();
390
 
391
	});
392
 
393
}
394
 
395
function obtenirInformationsPourCommune(nom_commune) {
396
 
397
	if(jQuery.trim(nom_commune) == '') {
398
		$('.conteneur_suggestions').hide();
399
		return;
400
	}
401
 
168 aurelien 402
	$.get(urlBaseJrest+'OdsCommune/informationsPourCommune/?commune='+nom_commune, function(data) {
100 aurelien 403
 
404
		infos_localites = jQuery.parseJSON(data);
405
		afficherListeAutoCompletion(infos_localites);
406
	});
407
}
408
 
409
function afficherListeAutoCompletion(tableau_localites) {
410
 
411
	liste_localite_en_cours = tableau_localites;
412
 
413
	html_liste_localite = '<ul class="liste_suggestions">';
414
 
415
	for(i = 0; i< tableau_localites.length; i++) {
416
		html_liste_localite += '<li id="commune_'+i+'" class="element_auto_completion_commune">'+tableau_localites[i]['commune']+'  ('+tableau_localites[i]['dpt']+')</li>';
417
	}
418
 
419
	html_liste_localite += '</ul>';
420
 
421
	$('.conteneur_suggestions').html(html_liste_localite);
422
 
423
	$('.element_auto_completion_commune').hover(function() {
424
		indice = $(this).attr('id').split('_')[1];
425
		mettreEnSurbrillanceCommune(indice);
426
	});
427
 
428
	$('.element_auto_completion_commune').click(function() {
429
		indice = $(this).attr('id').split('_')[1];
430
		selectionnerCommune(indice);
431
	});
432
 
433
	if(tableau_localites.length > 0) {
434
		$('.conteneur_suggestions').show();
435
 
436
		mettreEnSurbrillanceCommune(0);
437
	}
438
}
439
 
440
function mettreEnSurbrillanceCommune(indice) {
441
 
442
	if(indice_commune_en_cours != null) {
443
		$('#commune_'+indice_commune_en_cours).removeClass('element_selectionne');
444
	}
445
	$('#commune_'+indice).addClass('element_selectionne');
446
	indice_commune_en_cours = indice;
447
 
448
	reprogrammerTimerPourCacherListeAutoCompletion();
449
}
450
 
451
function rendreListeAutoCompletionInteractive(selecteur) {
452
 
453
	$(selecteur).keyup(function(event) {
454
		if(event.which == 8 || (event.which >= 48 && event.which <= 90)) {
455
 
456
			reprogrammerTimerPourCacherListeAutoCompletion();
457
			reprogrammerTimerPourLancerRequeteCommune();
458
		}
459
	});
460
 
461
	$(selecteur).keydown(function(event) {
462
 
463
		reprogrammerTimerPourCacherListeAutoCompletion();
464
 
465
		// entree
466
		if(event.which==13){
467
			selectionnerCommune(indice_commune_en_cours);
468
			event.preventDefault();
469
		}
470
 
471
		// haut
472
		if(event.which == 38){
473
			if(indice_commune_en_cours > 0) {
474
				mettreEnSurbrillanceCommune(indice_commune_en_cours - 1);
475
			}
476
		}
477
 
478
		// bas
479
		if(event.which == 40){
480
			if(indice_commune_en_cours < liste_localite_en_cours.length - 1) {
481
				mettreEnSurbrillanceCommune(indice_commune_en_cours + 1);
482
			}
483
		}
484
	});
485
 
486
	$('.conteneur_suggestions').blur(function() {
487
		$('.conteneur_suggestions').hide();
488
	});
489
}
490
 
491
function reprogrammerTimerPourCacherListeAutoCompletion() {
492
 
493
	if(timerAffichageAutocompletion != null) {
494
		window.clearTimeout(timerAffichageAutocompletion);
495
	}
496
 
497
	timerAffichageAutocompletion = window.setTimeout(function() {
498
		$('.conteneur_suggestions').hide();
499
	}, 2000);
500
}
501
 
502
function reprogrammerTimerPourLancerRequeteCommune() {
503
 
504
	if(timerRequeteAutocompletion != null) {
505
		window.clearTimeout(timerRequeteAutocompletion);
506
	}
507
 
508
	timerRequeteAutocompletion = window.setTimeout(function() {
509
		obtenirInformationsPourCommune($('input#station_commune').val());
510
	}, 350);
511
}
512
 
513
function selectionnerCommune(indice) {
514
 
515
	infos_commune = liste_localite_en_cours[indice];
516
 
517
	$('input#station_commune').val(infos_commune['commune']);
518
	$('input#station_lat').val(infos_commune['lat']);
519
	$('input#station_lon').val(infos_commune['lon']);
168 aurelien 520
	$('input#station_code_insee').val(infos_commune['code_insee']);
100 aurelien 521
 
522
	verifierEtLocaliserCoordonnees();
523
 
524
	$('.conteneur_suggestions').hide();
525
}
526
 
54 aurelien 527
function mettreAJourValeursFormulaire(latlon) {
528
 
529
	latlon = latlon.toString().split(',');
530
 
100 aurelien 531
	lat = latlon[0].replace('(', '');
532
	lon = latlon[1].replace(')', '');
533
 
54 aurelien 534
	$('#station_lat').val(latlon[0].replace('(', ''));
535
	$('#station_lon').val(latlon[1].replace(')', ''));
100 aurelien 536
 
537
	obtenirInformationsPourCoordonnees(lat, lon);
54 aurelien 538
}
539
 
540
function initialiserCarte() {
541
 
542
	if($('#map_canvas').length == 0) {
543
		return;
544
	}
545
 
546
	var latlng = new google.maps.LatLng(47.0504, 2.2347);
547
	var myOptions = {
548
		zoom: 6,
549
		center: latlng,
550
		mapTypeId: google.maps.MapTypeId.HYBRID
551
	};
552
 
553
	map = new google.maps.Map(document.getElementById("map_canvas"),
554
    	myOptions);
555
 
556
	marker = new google.maps.Marker({
557
	      position: latlng,
558
	      title:""
559
	});
560
 
561
	if (carteEstEnSaisie()) {
562
 
563
			marker.setDraggable(true);
564
 
100 aurelien 565
			google.maps.event.addListener(marker, 'dragend', function(event) {
54 aurelien 566
				mettreAJourValeursFormulaire(marker.getPosition());
567
			});
568
			$('#conteneur_form_liens_lat_lon').ready(function() {
569
				verifierEtLocaliserCoordonnees();
570
				ajouterListenerFormulaireSaisieLatLon();
100 aurelien 571
				ajouterAutoCompletionCommune();
54 aurelien 572
			});
573
	} else {
574
		marker.setDraggable(false);
575
		$('#conteneur_liens_lat_lon').ready(function() {
576
			verifierEtLocaliserCoordonnees();
577
		});
578
	}
579
 
580
	marker.setClickable(true);
581
 
582
	// To add the marker to the map, call setMap();
583
	marker.setMap(map);
584
}
585
 
586
$('#map_canvas').ready(function() {
587
	initialiserCarte();
100 aurelien 588
});
589
 
590
 
591
/**
592
************************************************************************************************
593
************************************************************************************************
594
 
595
    Fonctions de debug permattant d'afficher les objets javascript à la manière de print_r
596
 
597
************************************************************************************************
598
************************************************************************************************
599
**/
600
 
601
function dump(arr,level) {
602
	var dumped_text = "";
603
	if(!level) level = 0;
604
 
605
	//The padding given at the beginning of the line.
606
	var level_padding = "";
607
	for(var j=0;j<level+1;j++) level_padding += "    ";
608
 
609
	if(typeof(arr) == 'object') { //Array/Hashes/Objects
610
	 for(var item in arr) {
611
	  var value = arr[item];
612
 
613
	  if(typeof(value) == 'object') { //If it is an array,
614
	   dumped_text += level_padding + "'" + item + "' ...\n";
615
	   dumped_text += dump(value,level+1);
616
	  } else {
617
	   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
618
	  }
619
	 }
620
	} else { //Stings/Chars/Numbers etc.
621
	 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
622
	}
623
	return dumped_text;
624
}