Subversion Repositories eFlore/Applications.cel

Rev

Rev 841 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
836 jpm 1
/*+--------------------------------------------------------------------------------------------------------+*/
2
// PARAMÊTRES et CONSTANTES
3
var pointImageUrl = 'http://chart.apis.google.com/chart?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png';
4
var pointsOrigine = null;
5
var boundsOrigine = null;
6
var markerClusterer = null;
7
var map = null;
8
var infoBulle = new google.maps.InfoWindow();
9
var pointClique = null;
10
var carteCentre = new google.maps.LatLng(46.4, 3.10);
11
var carteOptions = {
12
	zoom: 6,
13
	mapTypeId: google.maps.MapTypeId.ROADMAP,
14
	mapTypeControlOptions: {
15
		mapTypeIds: ['OSM',
16
		             google.maps.MapTypeId.ROADMAP,
17
		             google.maps.MapTypeId.HYBRID,
18
		             google.maps.MapTypeId.SATELLITE,
19
		             google.maps.MapTypeId.TERRAIN]
20
	}
21
};
22
var ctaLayer = null;
23
var osmMapType = new google.maps.ImageMapType({
24
	getTileUrl: function(coord, zoom) {
25
		return "http://tile.openstreetmap.org/" +
26
		zoom + "/" + coord.x + "/" + coord.y + ".png";
27
	},
28
	tileSize: new google.maps.Size(256, 256),
29
	isPng: true,
30
	alt: "OpenStreetMap",
31
	name: "OSM",
32
	maxZoom: 19
33
});
34
 
35
/*+--------------------------------------------------------------------------------------------------------+*/
36
// INITIALISATION DU CODE
37
 
38
 
39
//Déclenchement d'actions quand JQuery et le document HTML sont OK
40
$(document).ready(function() {
41
	initialiserWidget();
42
});
43
/*+--------------------------------------------------------------------------------------------------------+*/
44
// FONCTIONS
45
 
46
function initialiserWidget() {
47
	afficherStats();
48
	initialiserAffichageCarte();
49
	initialiserAffichagePanneauLateral();
50
 
51
	initialiserCarte();
52
	initialiserInfoBulle();
53
	chargerLimitesCommunales();
54
	rafraichirCarte();
55
}
56
 
57
function afficherStats() {
58
	// Ajout du nombre de communes où des observations ont eu lieu
59
	$('#commune-nbre').append(obs.stats.communes.formaterNombre());
60
	// Ajout du nombre d'observations
61
	$('#obs-nbre').append(obs.stats.observations.formaterNombre());
62
	// Ajout du nombre de plantes
63
	$('.plantes-nbre').append(plantesNbre.formaterNombre());
64
}
65
 
66
function initialiserAffichageCarte() {
67
	$('#carte').height($(window).height() - 35);
68
	$('#carte').width($(window).width() - 24);
69
 
70
	if (nt != '*') {
71
		$('#carte').css('left', 0);
72
	}
73
}
74
 
75
function initialiserAffichagePanneauLateral() {
76
	$('#panneau-lateral').height($(window).height() - 35);
77
 
78
	if (nt == '*') {
79
		$('#pl-ouverture').bind('click', afficher);
80
		$('#pl-fermeture').bind('click', cacher);
81
		$('.taxon').live('click', filtrerParTaxon);
82
	}
83
}
84
 
85
function initialiserCarte() {
86
	map = new google.maps.Map(document.getElementById('carte'), carteOptions);
87
	// Ajout de la couche OSM à la carte
88
	map.mapTypes.set('OSM', osmMapType);
89
}
90
 
91
function initialiserInfoBulle() {
92
	google.maps.event.addListener(infoBulle, 'domready', modifierContenuInfoBulle);
93
	google.maps.event.addListener(infoBulle, 'closeclick', deplacerCartePointClique);
94
}
95
 
96
function chargerLimitesCommunales() {
97
	if (urlsLimitesCommunales != null) {
98
		for (urlId in urlsLimitesCommunales) {
99
			var url = urlsLimitesCommunales[urlId];
100
			ctaLayer = new google.maps.KmlLayer(url, {preserveViewport: true});
101
			ctaLayer.setMap(map);
102
		}
103
	}
104
}
105
 
106
function rafraichirCarte() {
107
	var points = [];
108
	var bounds = new google.maps.LatLngBounds();
109
	for (var i = 0; i < obs.stats.communes; ++i) {
110
		var maLatLng = new google.maps.LatLng(obs.points[i].coord_x, obs.points[i].coord_y);
111
		var pointImage = new google.maps.MarkerImage(pointImageUrl, new google.maps.Size(24, 32));
112
		var point = new google.maps.Marker({
113
			position: maLatLng,
114
			map: map,
115
			icon: pointImage,
116
			stationId: obs.points[i].id
117
		});
118
 
119
		bounds.extend(maLatLng);
120
 
121
		google.maps.event.addListener(point, 'click', function() {
122
			pointClique =  this;
123
 
124
			infoBulle.open(map, this);
125
 
126
			var limites = map.getBounds();
127
			var centre = limites.getCenter();
128
			var nordEst = limites.getNorthEast();
129
			var centreSudLatLng = new google.maps.LatLng(nordEst.lat(), centre.lng());
130
			map.panTo(centreSudLatLng);
131
 
132
			afficherMsgChargement();
133
			chargerFormatObs(this.stationId, '*');
134
		});
135
 
136
		points.push(point);
137
	}
138
 
139
	if (pointsOrigine == null && boundsOrigine == null) {
140
		pointsOrigine = points;
141
		boundsOrigine = bounds;
142
	}
143
 
144
	executerMarkerClusterer(points, bounds);
145
}
146
 
147
function modifierContenuInfoBulle() {
148
	// Onglet Tableau : Jquery => TableSorter
149
	if ($("#observations table").get() != '') {
150
		ajouterTableauTriable("#observations table");
151
	}
152
	// Onglet Liste : Jquery => FancyBox
153
	if ($("#observations ol").get() != '') {
154
		ajouterGaleriePhoto("a.cel-img");
155
	}
156
}
157
 
158
function ajouterTableauTriable(element) {
159
	// add parser through the tablesorter addParser method
160
	$.tablesorter.addParser({
161
		// Définition d'un id unique pour ce parsseur
162
		id: 'date_cel',
163
		is: function(s) {
164
			// retourne false si le parsseur n'est pas autodétecté
165
			return false;
166
		},
167
		format: function(s) {
168
			// Transformation date jj/mm/aaaa en aaaa/mm/jj
169
			s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$2/$1");
170
			// Remplace la date par un nombre de millisecondes pour trier numériquement
171
			return $.tablesorter.formatFloat(new Date(s).getTime());
172
		},
173
		// set type, either numeric or text
174
		type: 'numeric'
175
	});
176
	$(element).tablesorter({
177
        headers: {
178
            1: {
179
                sorter:'date_cel'
180
            }
181
        }
182
    });
183
}
184
 
185
function ajouterGaleriePhoto(element) {
186
	$(element).fancybox({
187
		transitionIn : 'elastic',
188
		transitionOut : 'elastic',
189
		speedIn	 : 600,
190
		speedOut : 200,
191
		overlayShow : true
192
	}).live('click', function(e) {
193
		if (e.stopPropagation) {
194
			e.stopPropagation();
195
		}
196
		return false;
197
	});
198
}
199
 
200
function deplacerCartePointClique() {
201
	map.panTo(pointClique.position);
202
}
203
 
204
function executerMarkerClusterer(points, bounds) {
205
	if (markerClusterer) {
206
		markerClusterer.clearMarkers();
207
	}
208
	markerClusterer = new MarkerClusterer(map, points);
209
	map.fitBounds(bounds);
210
}
211
 
212
function afficherMsgChargement() {
213
	var chargement = document.getElementById('chargement').cloneNode(true);
214
	chargement.setAttribute('id', 'chargement-copie');
215
	infoBulle.setContent(chargement);
216
}
217
 
218
function chargerFormatObs(stationId, format) {
219
	var url = urlObsStation+
220
		'&format='+format+'&'+
221
		'station='+stationId;
222
	$.get(url, function(observations){
223
		infoBulle.setContent(observations);
224
	});
225
}
226
 
227
function arreter(event) {
228
	if (event.stopPropagation) {
229
		event.stopPropagation();
230
	} else if (window.event) {
231
		window.event.cancelBubble = true;
232
	}
233
	return false;
234
}
235
 
236
function afficher() {
237
	$('#panneau-lateral').width(300);
238
	$('#pl-contenu').css('display', 'block');
239
	$('#pl-ouverture').css('display', 'none');
240
	$('#pl-fermeture').css('display', 'block');
241
	$('#carte').css('left', '300px');
242
 
243
	google.maps.event.trigger(map, 'resize');
244
};
245
 
246
function cacher() {
247
	$('#panneau-lateral').width(24);
248
	$('#pl-contenu').css('display', 'none');
249
	$('#pl-ouverture').css('display', 'block');
250
	$('#pl-fermeture').css('display', 'none');
251
	$('#carte').css('left', '24px');
252
 
253
	google.maps.event.trigger(map, 'resize');
254
};
255
 
256
function ouvrirPopUp(url, nom) {
257
	window.open(url, nom, 'scrollbars=yes,width=650,height=600,directories=no,location=no,menubar=no,status=no,toolbar=no');
258
};
259
 
260
function filtrerParTaxon() {
261
	var ntAFiltrer = $('.nt', this).text();
262
	infoBulle.close();
263
	$('#taxon-'+nt).removeClass('taxon-actif');
264
	if (nt == ntAFiltrer) {
265
		nt = '*';
266
		executerMarkerClusterer(pointsOrigine, boundsOrigine);
267
	} else {
268
		var url = urlObsCarte+'&'+
269
			'num_taxon='+ntAFiltrer+'&'+
270
			'formatRetour=jsonp'+'&'+
271
			'callback=?';
272
		$.getJSON(url, function (observations) {
273
			obs = observations;
274
			nt = ntAFiltrer;
275
			$('#taxon-'+nt).addClass('taxon-actif');
276
			rafraichirCarte();
277
		});
278
	}
279
};
280
 
281
/**
282
 * +-------------------------------------+
283
 * Number.prototype.formaterNombre
284
 * +-------------------------------------+
285
 * Params (facultatifs):
286
 * - Int decimales: nombre de decimales (exemple: 2)
287
 * - String signe: le signe precedent les decimales (exemple: "," ou ".")
288
 * - String separateurMilliers: comme son nom l'indique
289
 * Returns:
290
 * - String chaine formatee
291
 * @author	::mastahbenus::
292
 * @author	Jean-Pascal MILCENT <jpm@tela-botanica.org> : ajout détection auto entier/flotant
293
 * @souce	http://www.javascriptfr.com/codes/FORMATER-NOMBRE-FACON-NUMBER-FORMAT-PHP_40060.aspx
294
 */
295
Number.prototype.formaterNombre = function (decimales, signe, separateurMilliers) {
296
	var _sNombre = String(this), i, _sRetour = "", _sDecimales = "";
297
 
298
	function is_int(nbre) {
299
		nbre = nbre.replace(',', '.');
300
		return !(parseFloat(nbre)-parseInt(nbre) > 0);
301
	}
302
 
303
	if (decimales == undefined) {
304
		if (is_int(_sNombre)) {
305
			decimales = 0;
306
		} else {
307
			decimales = 2;
308
		}
309
	}
310
	if (signe == undefined) {
311
		if (is_int(_sNombre)) {
312
			signe = '';
313
		} else {
314
			signe = '.';
315
		}
316
	}
317
	if (separateurMilliers == undefined) {
318
		separateurMilliers = ' ';
319
	}
320
 
321
	function separeMilliers (sNombre) {
322
		var sRetour = "";
323
		while (sNombre.length % 3 != 0) {
324
			sNombre = "0"+sNombre;
325
		}
326
		for (i = 0; i < sNombre.length; i += 3) {
327
			if (i == sNombre.length-1) separateurMilliers = '';
328
			sRetour += sNombre.substr(i, 3) + separateurMilliers;
329
		}
330
		while (sRetour.substr(0, 1) == "0") {
331
			sRetour = sRetour.substr(1);
332
		}
333
		return sRetour.substr(0, sRetour.lastIndexOf(separateurMilliers));
334
	}
335
 
336
	if (_sNombre.indexOf('.') == -1) {
337
		for (i = 0; i < decimales; i++) {
338
			_sDecimales += "0";
339
		}
340
		_sRetour = separeMilliers(_sNombre) + signe + _sDecimales;
341
	} else {
342
		var sDecimalesTmp = (_sNombre.substr(_sNombre.indexOf('.')+1));
343
		if (sDecimalesTmp.length > decimales) {
344
			var nDecimalesManquantes = sDecimalesTmp.length - decimales;
345
			var nDiv = 1;
346
			for (i = 0; i < nDecimalesManquantes; i++) {
347
				nDiv *= 10;
348
			}
349
			_sDecimales = Math.round(Number(sDecimalesTmp) / nDiv);
350
		}
351
		_sRetour = separeMilliers(_sNombre.substr(0, _sNombre.indexOf('.')))+String(signe)+_sDecimales;
352
	}
353
	return _sRetour;
354
}