Subversion Repositories Sites.obs-saisons.fr

Rev

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

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