Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
836 jpm 1
/*+--------------------------------------------------------------------------------------------------------+*/
2
// PARAMÊTRES et CONSTANTES
941 jpm 3
// Mettre à true pour afficher les messages de débogage
950 jpm 4
var DEBUG = false;
980 jpm 5
var communeImageUrl = 'http://www.tela-botanica.org/commun/icones/carto/commune.png';
6
var pointImageUrl = 'http://www.tela-botanica.org/commun/icones/carto/point2.png';
1035 aurelien 7
var groupeImageUrlTpl = 'http://localhost/service:cel:CelWidgetMap/icone-groupe?type={type}&nbre={nbre}'
836 jpm 8
var pointsOrigine = null;
9
var boundsOrigine = null;
10
var markerClusterer = null;
11
var map = null;
12
var infoBulle = new google.maps.InfoWindow();
980 jpm 13
var stations = null;
836 jpm 14
var pointClique = null;
980 jpm 15
var carteCentre = new google.maps.LatLng(25, 10);
836 jpm 16
var carteOptions = {
980 jpm 17
	zoom: 3,
18
	center:carteCentre,
836 jpm 19
	mapTypeId: google.maps.MapTypeId.ROADMAP,
20
	mapTypeControlOptions: {
21
		mapTypeIds: ['OSM',
22
		             google.maps.MapTypeId.ROADMAP,
23
		             google.maps.MapTypeId.HYBRID,
24
		             google.maps.MapTypeId.SATELLITE,
25
		             google.maps.MapTypeId.TERRAIN]
26
	}
27
};
28
var osmMapType = new google.maps.ImageMapType({
29
	getTileUrl: function(coord, zoom) {
30
		return "http://tile.openstreetmap.org/" +
31
		zoom + "/" + coord.x + "/" + coord.y + ".png";
32
	},
33
	tileSize: new google.maps.Size(256, 256),
34
	isPng: true,
35
	alt: "OpenStreetMap",
36
	name: "OSM",
37
	maxZoom: 19
38
});
980 jpm 39
var ctaLayer = null;
915 jpm 40
var pagineur = {'limite':50, 'start':0, 'total':0, 'stationId':null, 'format':'tableau'};
41
var station = {'commune':'', 'obsNbre':0};
42
var obsStation = new Array();
43
var obsPage = new Array();
939 jpm 44
var taxonsCarte = new Array();
980 jpm 45
var mgr = null;
46
var marqueursCache = new Array();
47
var zonesCache = new Array();
1035 aurelien 48
var requeteChargementPoints;
836 jpm 49
/*+--------------------------------------------------------------------------------------------------------+*/
50
// INITIALISATION DU CODE
51
 
52
//Déclenchement d'actions quand JQuery et le document HTML sont OK
53
$(document).ready(function() {
54
	initialiserWidget();
55
});
56
 
57
function initialiserWidget() {
980 jpm 58
	definirTailleTitre();
836 jpm 59
	initialiserAffichageCarte();
60
	initialiserAffichagePanneauLateral();
61
	initialiserCarte();
980 jpm 62
	initialiserGestionnaireMarqueurs()
836 jpm 63
	initialiserInfoBulle();
941 jpm 64
	initialiserFormulaireContact();
836 jpm 65
	chargerLimitesCommunales();
1032 aurelien 66
	attribuerListenerCarte();
67
	programmerRafraichissementCarte();
836 jpm 68
}
69
 
915 jpm 70
/*+--------------------------------------------------------------------------------------------------------+*/
71
// AFFICHAGE GÉNÉRAL
72
 
980 jpm 73
function afficherTitreCarte() {
74
	if (stations != null && taxonsCarte.length > 0) {
75
		var obsNbre = stations.stats.observations.formaterNombre();
76
		var plteNbre = taxonsCarte.length.formaterNombre();
77
		var communeNbre = stations.stats.communes.formaterNombre();
78
 
79
		var titre = obsNbre+' observation';
80
		titre += (obsNbre > 1) ? 's' : '' ;
81
 
82
		if (nt == '*') {
83
			titre += ' de '+plteNbre+' plante';
84
			titre += (plteNbre > 1) ? 's' : '' ;
85
		} else {
86
			if (taxonsCarte[0]) {
87
				var taxon = taxonsCarte[0];
88
				titre += ' pour '+taxon.nom;
89
			}
90
		}
91
 
92
		titre += ' sur '+communeNbre+' commune';
93
		titre += (communeNbre > 1) ? 's' : '' ;
94
 
95
		$('#carte-titre-infos').text(titre);
96
	}
836 jpm 97
}
98
 
980 jpm 99
function definirTailleTitre() {
100
	var largeurViewPort = $(window).width();
101
	var taille = null;
102
	if (largeurViewPort < 400) {
103
		taille = '0.8';
104
	} else if (largeurViewPort >= 400 && largeurViewPort < 800) {
105
		taille = '1.0';
106
	} else if (largeurViewPort >= 800) {
107
		taille = '1.6';
108
	}
109
	$("#carte-titre").css('font-size', taille+'em');
110
}
111
 
915 jpm 112
/*+--------------------------------------------------------------------------------------------------------+*/
113
// CARTE
114
 
836 jpm 115
function initialiserAffichageCarte() {
116
	$('#carte').height($(window).height() - 35);
117
	$('#carte').width($(window).width() - 24);
980 jpm 118
 
836 jpm 119
	if (nt != '*') {
120
		$('#carte').css('left', 0);
121
	}
122
}
123
 
124
function initialiserCarte() {
125
	map = new google.maps.Map(document.getElementById('carte'), carteOptions);
126
	// Ajout de la couche OSM à la carte
127
	map.mapTypes.set('OSM', osmMapType);
128
}
129
 
980 jpm 130
function initialiserGestionnaireMarqueurs() {
131
	mgr = new MarkerManager(map);
132
}
836 jpm 133
 
134
function chargerLimitesCommunales() {
135
	if (urlsLimitesCommunales != null) {
136
		for (urlId in urlsLimitesCommunales) {
137
			var url = urlsLimitesCommunales[urlId];
138
			ctaLayer = new google.maps.KmlLayer(url, {preserveViewport: true});
139
			ctaLayer.setMap(map);
140
		}
141
	}
142
}
980 jpm 143
var listener = null;
144
var timer = null;
1032 aurelien 145
function attribuerListenerCarte() {
980 jpm 146
	listener = google.maps.event.addListener(map, 'bounds_changed', function(){
1032 aurelien 147
		programmerRafraichissementCarte();
980 jpm 148
	});
1032 aurelien 149
	listener = google.maps.event.addListener(map, 'zoom_changed', function(){
150
		programmerRafraichissementCarte();
151
	});
980 jpm 152
}
1032 aurelien 153
function programmerRafraichissementCarte() {
154
	if(timer != null) {
155
        window.clearTimeout(timer);
1035 aurelien 156
    }
157
	if(requeteChargementPoints != null) {
158
		requeteChargementPoints.abort();
159
	}
1032 aurelien 160
	timer = window.setTimeout(function() {
161
		var zoom = map.getZoom();
1037 aurelien 162
		// ajout d'un facteur de correction pour charger légerement plus que nécessaire pour stabiliser les points
163
		// à voir si c'est efficace
164
		var facteurCorrection = 0.5;
165
		var NELatLng = (map.getBounds().getNorthEast().lat()+facteurCorrection)+'|'+(map.getBounds().getNorthEast().lng()+facteurCorrection);
166
		var SWLatLng = (map.getBounds().getSouthWest().lat()-facteurCorrection)+'|'+(map.getBounds().getSouthWest().lng()-facteurCorrection);
1032 aurelien 167
		chargerMarqueurs(zoom, NELatLng, SWLatLng);
168
    }, 400);
169
}
836 jpm 170
 
980 jpm 171
var premierChargement = true;
172
var marqueurs = new Array();
173
function chargerMarqueurs(zoom, NELatLng, SWLatLng) {
174
	var url = stationsUrl+
175
		'&zoom='+zoom+
176
		'&ne='+NELatLng+
177
		'&sw='+SWLatLng;
178
 
1032 aurelien 179
	if(infoBulleOuverte) {
180
		return;
181
	}
182
 
1035 aurelien 183
	if(requeteChargementPoints != null) {
184
		requeteChargementPoints.abort();
1037 aurelien 185
		cacherMessageChargementPoints();
1035 aurelien 186
	}
187
 
188
	afficherMessageChargementPoints();
189
	requeteChargementPoints = $.getJSON(url, function(data) {
190
		rafraichirMarqueurs(data);
191
		cacherMessageChargementPoints();
980 jpm 192
	});
193
}
194
 
1035 aurelien 195
function afficherMessageChargementPoints() {
196
	$('#zone-chargement-point').css('display','block');
197
}
198
 
199
function cacherMessageChargementPoints() {
200
	$('#zone-chargement-point').css('display','none');
201
}
202
 
203
function rafraichirMarqueurs(data) {
204
	$.each(marqueurs, function(index, marqueur) {
205
		marqueur.setMap(null);
206
	});
207
	marqueurs = new Array();
208
 
209
	stations = data;
210
	afficherTitreCarte();
211
 
212
	$.each(stations.points, function (index, station) {
213
		if(station != null) {
214
			marqueurs.push(creerMarqueur(station));
215
		}
216
	});
217
}
218
 
980 jpm 219
function creerMarqueur(station) {
1037 aurelien 220
	var titre = '';
221
	if(station.nbreMarqueur) {
222
		titre = station.nbreMarqueur+' points renseignés';
223
	} else {
224
		if(station.nom) {
225
			titre = station.nom;
226
		}
227
	}
228
	//var titre = station['nbreMarqueur'];
980 jpm 229
	var icone = attribuerImageMarqueur(station['id'], station['nbreMarqueur']);
230
	var latLng = new google.maps.LatLng(station['lat'], station['lng']);
231
	var marqueur = new google.maps.Marker({
232
		position: latLng,
233
		icon: icone,
234
		title: ''+titre,
235
		map: map,
236
		stationInfos: station
237
	});
238
	attribuerListenerClick(marqueur, station['id']);
239
	marqueur.setMap(map);
240
    return marqueur;
241
}
242
 
1032 aurelien 243
function programmerRafraichissementCarteSauv() {
980 jpm 244
 
836 jpm 245
	var points = [];
246
	var bounds = new google.maps.LatLngBounds();
980 jpm 247
	for (var i = 0; i < stations.points.length; ++i) {
248
		var point = stations.points[i];
249
		var maLatLng = new google.maps.LatLng(point.lat, point.lng);
250
		var stationImage = attribuerImageMarqueur(point.id);
836 jpm 251
		var point = new google.maps.Marker({
252
			position: maLatLng,
253
			map: map,
980 jpm 254
			icon: stationImage,
255
			stationInfos: point
836 jpm 256
		});
257
		bounds.extend(maLatLng);
980 jpm 258
		google.maps.event.addListener(point, 'click', surClickMarqueur);
836 jpm 259
		points.push(point);
260
	}
261
 
262
	if (pointsOrigine == null && boundsOrigine == null) {
263
		pointsOrigine = points;
264
		boundsOrigine = bounds;
265
	}
266
 
267
	executerMarkerClusterer(points, bounds);
268
}
269
 
980 jpm 270
function attribuerImageMarqueur(id, nbreMarqueur) {
271
	var marqueurImage = null;
272
	if (etreMarqueurCommune(id)) {
273
		marqueurImage = new google.maps.MarkerImage(communeImageUrl, new google.maps.Size(24, 32));
274
	} else if (etreMarqueurStation(id)) {
275
		marqueurImage = new google.maps.MarkerImage(pointImageUrl, new google.maps.Size(16, 16));
276
	} else if (etreMarqueurGroupe(id)) {
277
		var type = 0;
278
		var largeur = 0;
279
		var hauteur = 0;
280
		if (nbreMarqueur != null) {
281
			if (nbreMarqueur >= 2 && nbreMarqueur < 100 ) {
282
				type = '1';
283
				largeur = 53;
284
				hauteur = 52;
285
			} else if (nbreMarqueur >= 100 && nbreMarqueur < 1000 ) {
286
				type = '2';
287
				largeur = 56;
288
				hauteur = 55;
289
			} else if (nbreMarqueur >= 1000 && nbreMarqueur < 10000 ) {
290
				type = '3';
291
				largeur = 66;
292
				hauteur = 65;
293
			} else if (nbreMarqueur >= 10000 && nbreMarqueur < 20000 ) {
294
				type = '4';
295
				largeur = 78;
296
				hauteur = 77;
297
			} else if (nbreMarqueur >= 20000) {
298
				type = '5';
299
				largeur = 66;
300
				hauteur = 65;
301
			}
302
		}
303
		groupeImageUrl = groupeImageUrlTpl.replace(/\{type\}/, type);
304
		groupeImageUrl = groupeImageUrl.replace(/\{nbre\}/, nbreMarqueur);
305
		marqueurImage = new google.maps.MarkerImage(groupeImageUrl, new google.maps.Size(largeur, hauteur));
306
	}
307
	return marqueurImage
308
}
309
 
310
function attribuerListenerClick(marqueur, id) {
311
	if (etreMarqueurCommune(id) || etreMarqueurStation(id)) {
312
		google.maps.event.addListener(marqueur, 'click', surClickMarqueur);
313
	} else if (etreMarqueurGroupe(id)) {
314
		google.maps.event.addListener(marqueur, 'click', surClickGroupe);
315
	}
316
}
317
 
1032 aurelien 318
function surClickMarqueur(event) {
1035 aurelien 319
 
320
	pointClique = this;
980 jpm 321
	infoBulle.open(map, this);
322
	actualiserPagineur();
1035 aurelien 323
 
324
	var limites = map.getBounds();
325
	var centre = limites.getCenter();
326
	var nordEst = limites.getNorthEast();
327
	var centreSudLatLng = new google.maps.LatLng(nordEst.lat(), centre.lng());
328
	map.panTo(centreSudLatLng);
329
 
980 jpm 330
	afficherInfoBulle();
331
}
332
 
333
function surClickGroupe() {
334
	map.setCenter(this.getPosition());
335
	var nouveauZoom = map.getZoom() + 2;
336
	map.setZoom(nouveauZoom);
337
	mgr.clearMarkers();
338
}
339
 
340
function etreMarqueurGroupe(id) {
341
	var groupe = false;
342
	var motif = /^GROUPE/;
343
	if (motif.test(id)) {
344
		groupe = true;
345
	}
346
	return groupe;
347
}
348
 
349
function etreMarqueurCommune(id) {
350
	var commune = false;
351
	var motif = /^COMMUNE:/;
352
	if (motif.test(id)) {
353
		commune = true;
354
	}
355
	return commune;
356
}
357
 
358
function etreMarqueurStation(id) {
359
	var station = false;
360
	var motif = /^STATION:/;
361
	if (motif.test(id)) {
362
		station = true;
363
	}
364
	return station;
365
}
366
 
367
function deplacerCarteSurPointClique() {
915 jpm 368
	map.panTo(pointClique.position);
369
}
370
 
371
function executerMarkerClusterer(points, bounds) {
372
	if (markerClusterer) {
373
		markerClusterer.clearMarkers();
836 jpm 374
	}
980 jpm 375
	markerClusterer = new MarkerClusterer(map, points, {gridSize: 50, maxZoom: 18});
915 jpm 376
	map.fitBounds(bounds);
377
}
378
 
379
/*+--------------------------------------------------------------------------------------------------------+*/
380
// INFO BULLE
1032 aurelien 381
var infoBulleOuverte = false;
915 jpm 382
function initialiserInfoBulle() {
383
	google.maps.event.addListener(infoBulle, 'domready', initialiserContenuInfoBulle);
980 jpm 384
	google.maps.event.addListener(infoBulle, 'closeclick', surFermetureInfoBulle);
385
	google.maps.event.addListener(infoBulle, 'content_changed', definirLargeurInfoBulle);
915 jpm 386
}
387
 
980 jpm 388
function surFermetureInfoBulle() {
1032 aurelien 389
	infoBulleOuverte = false;
390
	programmerRafraichissementCarte();
980 jpm 391
}
392
 
393
function centrerInfoBulle() {
394
	var limites = map.getBounds();
395
	var centre = limites.getCenter();
396
	var nordEst = limites.getNorthEast();
397
	var centreSudLatLng = new google.maps.LatLng(nordEst.lat(), centre.lng());
398
	map.panTo(centreSudLatLng);
399
}
400
 
915 jpm 401
function afficherInfoBulle() {
402
	var obsHtml = $("#tpl-obs").html();
980 jpm 403
	var largeur = definirLargeurInfoBulle();
404
	obsHtml = obsHtml.replace(/\{largeur\}/, largeur);
915 jpm 405
	infoBulle.setContent(obsHtml);
1035 aurelien 406
	chargerObs(0, 0);
1032 aurelien 407
	infoBulleOuverte = true;
915 jpm 408
}
409
 
980 jpm 410
function definirLargeurInfoBulle() {
411
	var largeurViewPort = $(window).width();
412
	var lageurInfoBulle = null;
413
	if (largeurViewPort < 800) {
414
		largeurInfoBulle = 400;
415
	} else if (largeurViewPort >= 800 && largeurViewPort < 1200) {
416
		largeurInfoBulle = 500;
417
	} else if (largeurViewPort >= 1200) {
418
		largeurInfoBulle = 600;
419
	}
420
	return largeurInfoBulle;
421
}
422
 
915 jpm 423
function afficherMessageChargement(element) {
424
	if ($('#chargement').get() == '') {
425
		$('#tpl-chargement').tmpl().appendTo(element);
426
	}
427
}
428
 
1035 aurelien 429
function afficherMessageChargementTitreInfoBulle() {
430
	$("#obs-station-titre").text("Chargement des observations");
431
}
432
 
915 jpm 433
function supprimerMessageChargement() {
434
	$('#chargement').remove();
435
}
436
 
437
function chargerObs(depart, total) {
939 jpm 438
	if (depart == 0 || depart < total) {
915 jpm 439
		var limite = 300;
440
		if (depart == 0) {
1036 aurelien 441
			viderTableauObs();
915 jpm 442
		}
1035 aurelien 443
 
939 jpm 444
		var urlObs = observationsUrl+'&start={start}&limit='+limite;
980 jpm 445
		urlObs = urlObs.replace(/\{stationId\}/g, pointClique.stationInfos.id);
939 jpm 446
		urlObs = urlObs.replace(/\{nt\}/g, nt);
915 jpm 447
		urlObs = urlObs.replace(/\{start\}/g, depart);
448
 
449
		$.getJSON(urlObs, function(observations){
1036 aurelien 450
			surRetourChargementObs(observations, depart, total);
1035 aurelien 451
			chargerObs(depart+limite, observations.total);
915 jpm 452
		});
1036 aurelien 453
	}
454
}
455
 
456
function viderTableauObs() {
457
	obsStation = new Array();
458
	surClicPagePagination(0, null);
459
}
460
 
461
function surRetourChargementObs(observations, depart, total) {
462
	obsStation = obsStation.concat(observations.observations);
463
	if (depart == 0) {
464
		actualiserInfosStation(observations);
465
		creerTitreInfoBulle();
1035 aurelien 466
		selectionnerOnglet("#obs-vue-"+pagineur.format);
467
		surClicPagePagination(0, null);
915 jpm 468
	}
1036 aurelien 469
 
470
	if (pagineur.limite < total) {
471
		afficherPagination();
472
	}
473
	actualiserPagineur();
915 jpm 474
}
475
 
1035 aurelien 476
function actualiserInfosStation(infos) {
477
	pointClique.stationInfos.commune = infos.commune;
478
	pointClique.stationInfos.obsNbre = infos.total;
479
}
480
 
481
function creerTitreInfoBulle() {
482
	$("#obs-total").text(station.obsNbre);
483
	$("#obs-commune").text(station.commune);
484
	var titre = '';
485
	titre += pointClique.stationInfos.obsNbre+' observation';
486
	titre += (pointClique.stationInfos.obsNbre > 1) ? 's': '' ;
487
	titre += ' pour ';
488
	if (etreMarqueurCommune(pointClique.stationInfos.id)) {
489
		nomStation = 'la commune : ';
490
	} else {
491
		nomStation = 'la station : ';
492
	}
493
	titre += pointClique.stationInfos.nom;
494
	$("#obs-station-titre").text(titre);
495
}
496
 
939 jpm 497
function actualiserPagineur() {
980 jpm 498
	pagineur.stationId = pointClique.stationInfos.id;
1035 aurelien 499
	pagineur.total = pointClique.stationInfos.obsNbre;
939 jpm 500
	if (pagineur.total > 4) {
501
		pagineur.format = 'tableau';
502
	} else {
503
		pagineur.format = 'liste';
504
	}
505
}
506
 
915 jpm 507
function afficherPagination(observations) {
508
	$(".navigation").pagination(pagineur.total, {
509
		items_per_page:pagineur.limite,
510
		callback:surClicPagePagination,
511
		next_text:'Suivant',
512
		prev_text:'Précédent',
513
		prev_show_always:false,
514
		num_edge_entries:1,
953 jpm 515
		num_display_entries:4,
915 jpm 516
		load_first_page:true
517
	});
518
}
519
 
520
function surClicPagePagination(pageIndex, paginationConteneur) {
521
	var index = pageIndex * pagineur.limite;
522
	var indexMax = index + pagineur.limite;
523
	pagineur.depart = index;
524
	obsPage = new Array();
525
    for(index; index < indexMax; index++) {
526
    	obsPage.push(obsStation[index]);
527
    }
528
 
529
    supprimerMessageChargement();
530
    mettreAJourObservations();
531
	return false;
532
}
533
 
534
function mettreAJourObservations() {
535
	$("#obs-"+pagineur.format+"-lignes").empty();
536
   	$("#obs-vue-"+pagineur.format).css('display', 'block');
537
   	$(".obs-conteneur").css('counter-reset', 'item '+pagineur.depart);
538
	$("#tpl-obs-"+pagineur.format).tmpl(obsPage).appendTo("#obs-"+pagineur.format+"-lignes");
539
 
540
	// Actualisation de Fancybox
941 jpm 541
	ajouterFomulaireContact("a.contact");
915 jpm 542
	if (pagineur.format == 'liste') {
836 jpm 543
		ajouterGaleriePhoto("a.cel-img");
544
	}
545
}
546
 
915 jpm 547
function initialiserContenuInfoBulle() {
980 jpm 548
	afficherMessageChargement('#observations');
549
	cacherContenuOnglets();
915 jpm 550
	afficherOnglets();
551
	ajouterTableauTriable("#obs-tableau");
552
	afficherTextStationId();
553
	corrigerLargeurInfoWindow();
554
}
555
 
980 jpm 556
function cacherContenuOnglets() {
557
	$("#obs-vue-tableau").css("display", "none");
558
	$("#obs-vue-liste").css("display", "none");
559
}
560
 
915 jpm 561
function afficherOnglets() {
562
	var $tabs = $('#obs').tabs();
563
	$('#obs').bind('tabsselect', function(event, ui) {
564
		if (ui.panel.id == 'obs-vue-tableau') {
565
			surClicAffichageTableau();
566
		} else if (ui.panel.id == 'obs-vue-liste') {
567
			surClicAffichageListe();
568
		}
569
	});
980 jpm 570
	if (pointClique.stationInfos.nbre > 4) {
571
		$tabs.tabs('select', "#obs-vue-tableau");
572
	} else {
573
		$tabs.tabs('select', "#obs-vue-liste");
574
	}
575
 
915 jpm 576
}
577
 
939 jpm 578
function selectionnerOnglet(onglet) {
980 jpm 579
	$(onglet).css('display', 'block');
939 jpm 580
	$('#obs').tabs('select', onglet);
581
}
582
 
915 jpm 583
function afficherTextStationId() {
980 jpm 584
	$('#obs-station-id').text(pointClique.stationInfos.id);
915 jpm 585
}
586
 
587
function corrigerLargeurInfoWindow() {
953 jpm 588
	$("#info-bulle").width($("#info-bulle").width() - 17);
915 jpm 589
}
590
 
591
function surClicAffichageTableau(event) {
592
	pagineur.format = 'tableau';
593
	mettreAJourObservations();
594
	mettreAJourTableauTriable("#obs-tableau");
595
}
596
 
597
function surClicAffichageListe(event) {
598
	pagineur.format = 'liste';
599
	mettreAJourObservations();
600
	ajouterGaleriePhoto("a.cel-img");
601
}
602
 
836 jpm 603
function ajouterTableauTriable(element) {
604
	// add parser through the tablesorter addParser method
605
	$.tablesorter.addParser({
606
		// Définition d'un id unique pour ce parsseur
607
		id: 'date_cel',
608
		is: function(s) {
915 jpm 609
			// doit retourner false si le parsseur n'est pas autodétecté
610
			return /^\s*\d{2}[\/-]\d{2}[\/-]\d{4}\s*$/.test(s);
836 jpm 611
		},
915 jpm 612
		format: function(date) {
836 jpm 613
			// Transformation date jj/mm/aaaa en aaaa/mm/jj
915 jpm 614
			date = date.replace(/^\s*(\d{2})[\/-](\d{2})[\/-](\d{4})\s*$/, "$3/$2/$1");
836 jpm 615
			// Remplace la date par un nombre de millisecondes pour trier numériquement
915 jpm 616
			return $.tablesorter.formatFloat(new Date(date).getTime());
836 jpm 617
		},
618
		// set type, either numeric or text
619
		type: 'numeric'
620
	});
621
	$(element).tablesorter({
622
        headers: {
915 jpm 623
			1: {
624
            	sorter:'date_cel'
625
        	}
626
    	}
627
	});
836 jpm 628
}
629
 
915 jpm 630
function mettreAJourTableauTriable(element) {
631
	$(element).trigger('update');
632
}
633
 
836 jpm 634
function ajouterGaleriePhoto(element) {
635
	$(element).fancybox({
915 jpm 636
		transitionIn:'elastic',
637
		transitionOut:'elastic',
638
		speedIn	:600,
639
		speedOut:200,
640
		overlayShow:true,
641
		titleShow:true,
642
		titlePosition:'inside',
643
		titleFormat:function (titre, currentArray, currentIndex, currentOpts) {
644
			var motif = /urn:lsid:tela-botanica[.]org:cel:img([0-9]+)$/;
645
			motif.exec(titre);
646
			var id = RegExp.$1;
647
			var info = $('#cel-info-'+id).clone().html();
648
			var tpl =
649
				'<div class="cel-legende">'+
650
				'<p class="cel-legende-vei">'+'Image n°' + (currentIndex + 1) + ' sur ' + currentArray.length +'<\/p>'+
651
				(titre && titre.length ? '<p>'+info+'<\/p>' : '' )+
652
				'<\/div>';
653
			return tpl;
836 jpm 654
		}
1032 aurelien 655
	}).live('click', function(e) {
656
		if (e.stopPropagation) {
657
			e.stopPropagation();
658
		}
659
		return false;
660
	});
836 jpm 661
}
662
 
941 jpm 663
function ajouterFomulaireContact(element) {
664
	$(element).fancybox({
665
		transitionIn:'elastic',
666
		transitionOut:'elastic',
667
		speedIn	:600,
668
		speedOut:200,
669
		scrolling: 'no',
670
		titleShow: false,
671
		onStart: function(selectedArray, selectedIndex, selectedOpts) {
672
			var element = selectedArray[selectedIndex];
673
 
674
			var motif = / contributeur-([0-9]+)$/;
675
			motif.exec($(element).attr('class'));
676
			var id = RegExp.$1;
950 jpm 677
			//console.log('Destinataire id : '+id);
941 jpm 678
			$("#fc_destinataire_id").attr('value', id);
679
 
680
			var motif = / obs-([0-9]+) /;
681
			motif.exec($(element).attr('class'));
682
			var id = RegExp.$1;
950 jpm 683
			//console.log('Obs id : '+id);
941 jpm 684
			chargerInfoObsPourMessage(id);
685
		},
686
		onCleanup: function() {
950 jpm 687
			//console.log('Avant fermeture fancybox');
941 jpm 688
			$("#fc_destinataire_id").attr('value', '');
689
			$("#fc_sujet").attr('value', '');
690
			$("#fc_message").text('');
691
		},
692
		onClosed: function(e) {
950 jpm 693
			//console.log('Fermeture fancybox');
941 jpm 694
			if (e.stopPropagation) {
695
				e.stopPropagation();
696
			}
697
			return false;
698
		}
699
	});
700
}
701
 
702
function chargerInfoObsPourMessage(idObs) {
980 jpm 703
	var nomSci = jQuery.trim($(".cel-obs-"+idObs+" .nom-sci:eq(0)").text());
704
	var date = jQuery.trim($(".cel-obs-"+idObs+" .date:eq(0)").text());
705
	var lieu = jQuery.trim($(".cel-obs-"+idObs+" .lieu:eq(0)").text());
953 jpm 706
	var sujet = "Observation #"+idObs+" de "+nomSci;
707
	var message = "\n\n\n\n\n\n\n\n--\nConcerne l'observation de \""+nomSci+'" du "'+date+'" au lieu "'+lieu+'".';
941 jpm 708
	$("#fc_sujet").attr('value', sujet);
709
	$("#fc_message").text(message);
710
}
711
 
712
function initialiserFormulaireContact() {
950 jpm 713
	//console.log('Initialisation du form contact');
941 jpm 714
	$("#form-contact").validate({
715
		rules: {
716
			fc_sujet : "required",
717
			fc_message : "required",
953 jpm 718
			fc_utilisateur_courriel : {
719
				required : true,
720
				email : true}
941 jpm 721
		}
722
	});
723
	$("#form-contact").bind("submit", envoyerCourriel);
950 jpm 724
	$("#fc_annuler").bind("click", function() {$.fancybox.close();});
941 jpm 725
 
726
}
727
 
728
function envoyerCourriel() {
950 jpm 729
	//console.log('Formulaire soumis');
941 jpm 730
	if ($("#form-contact").valid()) {
950 jpm 731
		//console.log('Formulaire valide');
941 jpm 732
		//$.fancybox.showActivity();
733
		var destinataireId = $("#fc_destinataire_id").attr('value');
734
		var urlMessage = "http://www.tela-botanica.org/service:annuaire:Utilisateur/"+destinataireId+"/message"
735
		var erreurMsg = "";
736
		var donnees = new Array();
737
		$.each($(this).serializeArray(), function (index, champ) {
738
			var cle = champ.name;
739
			cle = cle.replace(/^fc_/, '');
953 jpm 740
 
980 jpm 741
			if (cle == 'sujet') {
953 jpm 742
				champ.value += " - Carnet en ligne - Tela Botanica";
743
			}
744
			if (cle == 'message') {
745
				champ.value += "\n--\n"+
980 jpm 746
					"Ce message vous est envoyé par l'intermédiaire du widget carto "+
953 jpm 747
					"du Carnet en Ligne du réseau Tela Botanica.\n"+
748
					"http://www.tela-botanica.org/widget:cel:carto";
749
			}
750
 
941 jpm 751
			donnees[index] = {'name':cle,'value':champ.value};
752
		});
753
		$.ajax({
754
			type : "POST",
755
			cache : false,
756
			url : urlMessage,
757
			data : donnees,
758
			beforeSend : function() {
759
				$(".msg").remove();
760
			},
761
			success : function(data) {
762
				$("#fc-zone-dialogue").append('<pre class="msg info">'+data.message+'</pre>');
763
			},
764
			error : function(jqXHR, textStatus, errorThrown) {
765
				erreurMsg += "Erreur Ajax :\ntype : "+textStatus+' '+errorThrown+"\n";
766
				reponse = jQuery.parseJSON(jqXHR.responseText);
767
				if (reponse != null) {
768
					$.each(reponse, function (cle, valeur) {
769
						erreurMsg += valeur + "\n";
770
					});
771
				}
772
			},
773
			complete : function(jqXHR, textStatus) {
774
				var debugMsg = '';
775
				if (jqXHR.getResponseHeader("X-DebugJrest-Data") != '') {
776
					debugInfos = jQuery.parseJSON(jqXHR.getResponseHeader("X-DebugJrest-Data"));
777
					if (debugInfos != null) {
778
						$.each(debugInfos, function (cle, valeur) {
779
							debugMsg += valeur + "\n";
780
						});
781
					}
782
				}
783
				if (erreurMsg != '') {
784
					$("#fc-zone-dialogue").append('<p class="msg">'+
785
							'Une erreur est survenue lors de la transmission de votre message.'+'<br />'+
786
							'Vous pouvez signaler le disfonctionnement à <a href="'+
787
							'mailto:cel@tela-botanica.org'+'?'+
980 jpm 788
							'subject=Disfonctionnement du widget carto'+
941 jpm 789
							"&body="+erreurMsg+"\nDébogage :\n"+debugMsg+
790
							'">cel@tela-botanica.org</a>.'+
791
							'</p>');
792
				}
793
				if (DEBUG) {
794
					console.log('Débogage : '+debugMsg);
795
				}
950 jpm 796
				//console.log('Débogage : '+debugMsg);
797
				//console.log('Erreur : '+erreurMsg);
941 jpm 798
			}
799
		});
800
	}
801
	return false;
802
}
915 jpm 803
/*+--------------------------------------------------------------------------------------------------------+*/
804
// PANNEAU LATÉRAL
836 jpm 805
 
915 jpm 806
function initialiserAffichagePanneauLateral() {
807
	$('#panneau-lateral').height($(window).height() - 35);
841 jpm 808
 
915 jpm 809
	if (nt == '*') {
810
		$('#pl-ouverture').bind('click', afficherPanneauLateral);
811
		$('#pl-fermeture').bind('click', cacherPanneauLateral);
836 jpm 812
	}
939 jpm 813
	chargerTaxons(0, 0);
836 jpm 814
}
815
 
939 jpm 816
function chargerTaxons(depart, total) {
817
	if (depart == 0 || depart < total) {
818
		var limite = 7000;
950 jpm 819
		//console.log("Chargement des taxons de "+depart+" à "+(depart+limite));
939 jpm 820
		var urlTax = taxonsUrl+'&start={start}&limit='+limite;
821
		urlTax = urlTax.replace(/\{start\}/g, depart);
822
		$.getJSON(urlTax, function(infos) {
823
			taxonsCarte = taxonsCarte.concat(infos.taxons);
950 jpm 824
			//console.log("Nbre taxons :"+taxonsCarte.length);
939 jpm 825
			chargerTaxons(depart+limite, infos.total);
826
		});
827
	} else {
828
		if (nt == '*') {
829
			afficherTaxons();
830
		}
980 jpm 831
		afficherTitreCarte();
939 jpm 832
	}
833
}
834
 
835
function afficherTaxons() {
980 jpm 836
	$(".plantes-nbre").text(taxonsCarte.length);
939 jpm 837
	$("#tpl-taxons-liste").tmpl({'taxons':taxonsCarte}).appendTo("#pl-corps");
838
	$('.taxon').live('click', filtrerParTaxon);
839
}
840
 
841
 
915 jpm 842
function afficherPanneauLateral() {
836 jpm 843
	$('#panneau-lateral').width(300);
844
	$('#pl-contenu').css('display', 'block');
845
	$('#pl-ouverture').css('display', 'none');
846
	$('#pl-fermeture').css('display', 'block');
847
	$('#carte').css('left', '300px');
848
 
849
	google.maps.event.trigger(map, 'resize');
850
};
851
 
915 jpm 852
function cacherPanneauLateral() {
836 jpm 853
	$('#panneau-lateral').width(24);
854
	$('#pl-contenu').css('display', 'none');
855
	$('#pl-ouverture').css('display', 'block');
856
	$('#pl-fermeture').css('display', 'none');
857
	$('#carte').css('left', '24px');
858
 
859
	google.maps.event.trigger(map, 'resize');
860
};
861
 
862
function filtrerParTaxon() {
863
	var ntAFiltrer = $('.nt', this).text();
864
	infoBulle.close();
1037 aurelien 865
	var zoom = map.getZoom();
866
	var NELatLng = map.getBounds().getNorthEast().lat()+'|'+map.getBounds().getNorthEast().lng();
867
	var SWLatLng = map.getBounds().getSouthWest().lat()+'|'+map.getBounds().getSouthWest().lng();
868
 
836 jpm 869
	$('#taxon-'+nt).removeClass('taxon-actif');
870
	if (nt == ntAFiltrer) {
871
		nt = '*';
1037 aurelien 872
		stationsUrl = stationsUrl.replace(/num_taxon=[*0-9]+/, 'num_taxon='+nt);
873
		chargerMarqueurs(zoom, NELatLng, SWLatLng);
836 jpm 874
	} else {
1035 aurelien 875
		stationsUrl = stationsUrl.replace(/num_taxon=[*0-9]+/, 'num_taxon='+ntAFiltrer);
876
		url = stationsUrl;
877
		url += '&zoom='+zoom+
878
			'&ne='+NELatLng+
879
			'&sw='+SWLatLng;
880
		requeteChargementPoints = $.getJSON(url, function (stationsFiltrees) {
939 jpm 881
			stations = stationsFiltrees;
836 jpm 882
			nt = ntAFiltrer;
883
			$('#taxon-'+nt).addClass('taxon-actif');
1035 aurelien 884
			rafraichirMarqueurs(stations);
836 jpm 885
		});
886
	}
887
};
888
 
915 jpm 889
/*+--------------------------------------------------------------------------------------------------------+*/
890
// FONCTIONS UTILITAIRES
891
 
980 jpm 892
function ouvrirPopUp(element, nomPopUp, event) {
893
	var options =
894
		'width=650,'+
895
		'height=600,'+
896
		'scrollbars=yes,'+
897
		'directories=no,'+
898
		'location=no,'+
899
		'menubar=no,'+
900
		'status=no,'+
901
		'toolbar=no';
902
	var popUp = window.open(element.href, nomPopUp, options);
903
	if (window.focus) {
904
		popUp.focus();
905
	}
906
	return arreter(event);
907
};
908
 
909
function ouvrirNouvelleFenetre(element, event) {
910
	window.open(element.href);
911
	return arreter(event);
912
}
913
 
915 jpm 914
function arreter(event) {
915
	if (event.stopPropagation) {
916
		event.stopPropagation();
917
	} else if (window.event) {
918
		window.event.cancelBubble = true;
919
	}
980 jpm 920
	if (event.preventDefault) {
921
		event.preventDefault();
922
	}
923
	event.returnValue = false;
915 jpm 924
	return false;
925
}
926
 
836 jpm 927
/**
928
 * +-------------------------------------+
929
 * Number.prototype.formaterNombre
930
 * +-------------------------------------+
931
 * Params (facultatifs):
932
 * - Int decimales: nombre de decimales (exemple: 2)
933
 * - String signe: le signe precedent les decimales (exemple: "," ou ".")
934
 * - String separateurMilliers: comme son nom l'indique
935
 * Returns:
936
 * - String chaine formatee
937
 * @author	::mastahbenus::
938
 * @author	Jean-Pascal MILCENT <jpm@tela-botanica.org> : ajout détection auto entier/flotant
939
 * @souce	http://www.javascriptfr.com/codes/FORMATER-NOMBRE-FACON-NUMBER-FORMAT-PHP_40060.aspx
940
 */
941
Number.prototype.formaterNombre = function (decimales, signe, separateurMilliers) {
942
	var _sNombre = String(this), i, _sRetour = "", _sDecimales = "";
943
 
944
	function is_int(nbre) {
945
		nbre = nbre.replace(',', '.');
946
		return !(parseFloat(nbre)-parseInt(nbre) > 0);
947
	}
948
 
949
	if (decimales == undefined) {
950
		if (is_int(_sNombre)) {
951
			decimales = 0;
952
		} else {
953
			decimales = 2;
954
		}
955
	}
956
	if (signe == undefined) {
957
		if (is_int(_sNombre)) {
958
			signe = '';
959
		} else {
960
			signe = '.';
961
		}
962
	}
963
	if (separateurMilliers == undefined) {
964
		separateurMilliers = ' ';
965
	}
966
 
967
	function separeMilliers (sNombre) {
968
		var sRetour = "";
969
		while (sNombre.length % 3 != 0) {
970
			sNombre = "0"+sNombre;
971
		}
972
		for (i = 0; i < sNombre.length; i += 3) {
973
			if (i == sNombre.length-1) separateurMilliers = '';
974
			sRetour += sNombre.substr(i, 3) + separateurMilliers;
975
		}
976
		while (sRetour.substr(0, 1) == "0") {
977
			sRetour = sRetour.substr(1);
978
		}
979
		return sRetour.substr(0, sRetour.lastIndexOf(separateurMilliers));
980
	}
981
 
982
	if (_sNombre.indexOf('.') == -1) {
983
		for (i = 0; i < decimales; i++) {
984
			_sDecimales += "0";
985
		}
986
		_sRetour = separeMilliers(_sNombre) + signe + _sDecimales;
987
	} else {
988
		var sDecimalesTmp = (_sNombre.substr(_sNombre.indexOf('.')+1));
989
		if (sDecimalesTmp.length > decimales) {
990
			var nDecimalesManquantes = sDecimalesTmp.length - decimales;
991
			var nDiv = 1;
992
			for (i = 0; i < nDecimalesManquantes; i++) {
993
				nDiv *= 10;
994
			}
995
			_sDecimales = Math.round(Number(sDecimalesTmp) / nDiv);
996
		}
997
		_sRetour = separeMilliers(_sNombre.substr(0, _sNombre.indexOf('.')))+String(signe)+_sDecimales;
998
	}
999
	return _sRetour;
915 jpm 1000
}
1001
 
1002
function debug(objet) {
1003
	var msg = '';
1004
	if (objet != null) {
1005
		$.each(objet, function (cle, valeur) {
1006
			msg += cle+":"+valeur + "\n";
1007
		});
1008
	} else {
1009
		msg = "La variable vaut null.";
1010
	}
1011
	console.log(msg);
836 jpm 1012
}