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 |
});
|
915 |
jpm |
34 |
var pagineur = {'limite':50, 'start':0, 'total':0, 'stationId':null, 'format':'tableau'};
|
|
|
35 |
var station = {'commune':'', 'obsNbre':0};
|
|
|
36 |
var obsStation = new Array();
|
|
|
37 |
var obsPage = new Array();
|
836 |
jpm |
38 |
/*+--------------------------------------------------------------------------------------------------------+*/
|
|
|
39 |
// INITIALISATION DU CODE
|
|
|
40 |
|
|
|
41 |
//Déclenchement d'actions quand JQuery et le document HTML sont OK
|
|
|
42 |
$(document).ready(function() {
|
|
|
43 |
initialiserWidget();
|
|
|
44 |
});
|
|
|
45 |
|
|
|
46 |
function initialiserWidget() {
|
|
|
47 |
afficherStats();
|
|
|
48 |
initialiserAffichageCarte();
|
|
|
49 |
initialiserAffichagePanneauLateral();
|
|
|
50 |
|
|
|
51 |
initialiserCarte();
|
|
|
52 |
initialiserInfoBulle();
|
|
|
53 |
chargerLimitesCommunales();
|
|
|
54 |
rafraichirCarte();
|
|
|
55 |
}
|
|
|
56 |
|
915 |
jpm |
57 |
/*+--------------------------------------------------------------------------------------------------------+*/
|
|
|
58 |
// AFFICHAGE GÉNÉRAL
|
|
|
59 |
|
836 |
jpm |
60 |
function afficherStats() {
|
|
|
61 |
// Ajout du nombre de communes où des observations ont eu lieu
|
|
|
62 |
$('#commune-nbre').append(obs.stats.communes.formaterNombre());
|
|
|
63 |
// Ajout du nombre d'observations
|
|
|
64 |
$('#obs-nbre').append(obs.stats.observations.formaterNombre());
|
|
|
65 |
// Ajout du nombre de plantes
|
|
|
66 |
$('.plantes-nbre').append(plantesNbre.formaterNombre());
|
|
|
67 |
}
|
|
|
68 |
|
915 |
jpm |
69 |
/*+--------------------------------------------------------------------------------------------------------+*/
|
|
|
70 |
// CARTE
|
|
|
71 |
|
836 |
jpm |
72 |
function initialiserAffichageCarte() {
|
|
|
73 |
$('#carte').height($(window).height() - 35);
|
|
|
74 |
$('#carte').width($(window).width() - 24);
|
|
|
75 |
|
|
|
76 |
if (nt != '*') {
|
|
|
77 |
$('#carte').css('left', 0);
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
function initialiserCarte() {
|
|
|
82 |
map = new google.maps.Map(document.getElementById('carte'), carteOptions);
|
|
|
83 |
// Ajout de la couche OSM à la carte
|
|
|
84 |
map.mapTypes.set('OSM', osmMapType);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
function chargerLimitesCommunales() {
|
|
|
89 |
if (urlsLimitesCommunales != null) {
|
|
|
90 |
for (urlId in urlsLimitesCommunales) {
|
|
|
91 |
var url = urlsLimitesCommunales[urlId];
|
|
|
92 |
ctaLayer = new google.maps.KmlLayer(url, {preserveViewport: true});
|
|
|
93 |
ctaLayer.setMap(map);
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
function rafraichirCarte() {
|
|
|
99 |
var points = [];
|
|
|
100 |
var bounds = new google.maps.LatLngBounds();
|
|
|
101 |
for (var i = 0; i < obs.stats.communes; ++i) {
|
|
|
102 |
var maLatLng = new google.maps.LatLng(obs.points[i].coord_x, obs.points[i].coord_y);
|
|
|
103 |
var pointImage = new google.maps.MarkerImage(pointImageUrl, new google.maps.Size(24, 32));
|
|
|
104 |
var point = new google.maps.Marker({
|
|
|
105 |
position: maLatLng,
|
|
|
106 |
map: map,
|
|
|
107 |
icon: pointImage,
|
|
|
108 |
stationId: obs.points[i].id
|
|
|
109 |
});
|
|
|
110 |
|
|
|
111 |
bounds.extend(maLatLng);
|
|
|
112 |
|
|
|
113 |
google.maps.event.addListener(point, 'click', function() {
|
|
|
114 |
pointClique = this;
|
|
|
115 |
infoBulle.open(map, this);
|
|
|
116 |
|
|
|
117 |
var limites = map.getBounds();
|
|
|
118 |
var centre = limites.getCenter();
|
|
|
119 |
var nordEst = limites.getNorthEast();
|
|
|
120 |
var centreSudLatLng = new google.maps.LatLng(nordEst.lat(), centre.lng());
|
|
|
121 |
map.panTo(centreSudLatLng);
|
|
|
122 |
|
915 |
jpm |
123 |
afficherInfoBulle();
|
|
|
124 |
lancerChargementObs();
|
836 |
jpm |
125 |
});
|
|
|
126 |
|
|
|
127 |
points.push(point);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
if (pointsOrigine == null && boundsOrigine == null) {
|
|
|
131 |
pointsOrigine = points;
|
|
|
132 |
boundsOrigine = bounds;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
executerMarkerClusterer(points, bounds);
|
|
|
136 |
}
|
|
|
137 |
|
915 |
jpm |
138 |
function deplacerCartePointClique() {
|
|
|
139 |
map.panTo(pointClique.position);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
function executerMarkerClusterer(points, bounds) {
|
|
|
143 |
if (markerClusterer) {
|
|
|
144 |
markerClusterer.clearMarkers();
|
836 |
jpm |
145 |
}
|
915 |
jpm |
146 |
markerClusterer = new MarkerClusterer(map, points);
|
|
|
147 |
map.fitBounds(bounds);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/*+--------------------------------------------------------------------------------------------------------+*/
|
|
|
151 |
// INFO BULLE
|
|
|
152 |
|
|
|
153 |
function initialiserInfoBulle() {
|
|
|
154 |
google.maps.event.addListener(infoBulle, 'domready', initialiserContenuInfoBulle);
|
|
|
155 |
google.maps.event.addListener(infoBulle, 'closeclick', deplacerCartePointClique);
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
function afficherInfoBulle() {
|
|
|
159 |
var obsHtml = $("#tpl-obs").html();
|
|
|
160 |
infoBulle.setContent(obsHtml);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
function afficherMessageChargement(element) {
|
|
|
164 |
if ($('#chargement').get() == '') {
|
|
|
165 |
$('#tpl-chargement').tmpl().appendTo(element);
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
function supprimerMessageChargement() {
|
|
|
170 |
$('#chargement').remove();
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
function lancerChargementObs() {
|
|
|
174 |
var urlObsNbre = urlObsStation;
|
|
|
175 |
urlObsNbre = urlObsNbre.replace(/\{action\}/g, 'observations-nombre');
|
|
|
176 |
urlObsNbre = urlObsNbre.replace(/\{stationId\}/g, pointClique.stationId);
|
|
|
177 |
console.log("Lancement recup nombre");
|
|
|
178 |
$.getJSON(urlObsNbre, function(infos){
|
|
|
179 |
console.log("Lancement charger obs");
|
|
|
180 |
chargerObs(0, infos.obsNbre);
|
|
|
181 |
station = infos;
|
|
|
182 |
actualiserPagineur();
|
|
|
183 |
creerTitreInfoBulle();
|
|
|
184 |
});
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
function actualiserPagineur() {
|
|
|
188 |
pagineur.stationId = pointClique.stationId;
|
|
|
189 |
pagineur.total = station.obsNbre;
|
|
|
190 |
console.log(pagineur.total);
|
|
|
191 |
if (pagineur.total > 4) {
|
|
|
192 |
pagineur.format = 'tableau';
|
|
|
193 |
} else {
|
|
|
194 |
pagineur.format = 'liste';
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
function chargerObs(depart, total) {
|
|
|
199 |
if (depart < total) {
|
|
|
200 |
var limite = 300;
|
|
|
201 |
if (depart == 0) {
|
|
|
202 |
obsStation = new Array();
|
|
|
203 |
}
|
|
|
204 |
console.log("Chargement de "+depart+" à "+(depart+limite));
|
|
|
205 |
var urlObs = urlObsStation+'&start={start}&limit='+limite;
|
|
|
206 |
urlObs = urlObs.replace(/\{action\}/g, 'observations');
|
|
|
207 |
urlObs = urlObs.replace(/\{stationId\}/g, pointClique.stationId);
|
|
|
208 |
urlObs = urlObs.replace(/\{start\}/g, depart);
|
|
|
209 |
|
|
|
210 |
$.getJSON(urlObs, function(observations){
|
|
|
211 |
obsStation = obsStation.concat(observations.observations);
|
|
|
212 |
console.log("Chargement ok");
|
|
|
213 |
chargerObs(depart+limite, total);
|
|
|
214 |
});
|
|
|
215 |
} else {
|
|
|
216 |
if (pagineur.limite < total) {
|
|
|
217 |
afficherPagination();
|
|
|
218 |
} else {
|
|
|
219 |
surClicPagePagination(0, null);
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
function afficherPagination(observations) {
|
|
|
226 |
$(".navigation").pagination(pagineur.total, {
|
|
|
227 |
items_per_page:pagineur.limite,
|
|
|
228 |
callback:surClicPagePagination,
|
|
|
229 |
next_text:'Suivant',
|
|
|
230 |
prev_text:'Précédent',
|
|
|
231 |
prev_show_always:false,
|
|
|
232 |
num_edge_entries:1,
|
|
|
233 |
num_display_entries:5,
|
|
|
234 |
load_first_page:true
|
|
|
235 |
});
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
function surClicPagePagination(pageIndex, paginationConteneur) {
|
|
|
239 |
var index = pageIndex * pagineur.limite;
|
|
|
240 |
var indexMax = index + pagineur.limite;
|
|
|
241 |
pagineur.depart = index;
|
|
|
242 |
obsPage = new Array();
|
|
|
243 |
for(index; index < indexMax; index++) {
|
|
|
244 |
obsPage.push(obsStation[index]);
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
supprimerMessageChargement();
|
|
|
248 |
mettreAJourObservations();
|
|
|
249 |
return false;
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
function mettreAJourObservations() {
|
|
|
253 |
$("#obs-"+pagineur.format+"-lignes").empty();
|
|
|
254 |
$("#obs-vue-"+pagineur.format).css('display', 'block');
|
|
|
255 |
$(".obs-conteneur").css('counter-reset', 'item '+pagineur.depart);
|
|
|
256 |
$("#tpl-obs-"+pagineur.format).tmpl(obsPage).appendTo("#obs-"+pagineur.format+"-lignes");
|
|
|
257 |
|
|
|
258 |
// Actualisation de Fancybox
|
|
|
259 |
if (pagineur.format == 'liste') {
|
836 |
jpm |
260 |
ajouterGaleriePhoto("a.cel-img");
|
|
|
261 |
}
|
|
|
262 |
}
|
|
|
263 |
|
915 |
jpm |
264 |
function creerTitreInfoBulle() {
|
|
|
265 |
$("#obs-total").append(station.obsNbre);
|
|
|
266 |
$("#obs-commune").append(station.commune);
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
function initialiserContenuInfoBulle() {
|
|
|
270 |
afficherOnglets();
|
|
|
271 |
afficherMessageChargement('#observations');
|
|
|
272 |
ajouterTableauTriable("#obs-tableau");
|
|
|
273 |
afficherTextStationId();
|
|
|
274 |
corrigerLargeurInfoWindow();
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
function afficherOnglets() {
|
|
|
278 |
var $tabs = $('#obs').tabs();
|
|
|
279 |
$('#obs').bind('tabsselect', function(event, ui) {
|
|
|
280 |
if (ui.panel.id == 'obs-vue-tableau') {
|
|
|
281 |
surClicAffichageTableau();
|
|
|
282 |
} else if (ui.panel.id == 'obs-vue-liste') {
|
|
|
283 |
surClicAffichageListe();
|
|
|
284 |
}
|
|
|
285 |
});
|
|
|
286 |
$tabs.tabs('select', "#obs-vue-"+pagineur.format);
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
function afficherTextStationId() {
|
|
|
290 |
$('#obs-station-id').text(pointClique.stationId);
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
function corrigerLargeurInfoWindow() {
|
|
|
294 |
$("#info-bulle").width($("#info-bulle").width() - 16);
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
function surClicAffichageTableau(event) {
|
|
|
298 |
console.log('tableau');
|
|
|
299 |
pagineur.format = 'tableau';
|
|
|
300 |
mettreAJourObservations();
|
|
|
301 |
mettreAJourTableauTriable("#obs-tableau");
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
function surClicAffichageListe(event) {
|
|
|
305 |
console.log('liste');
|
|
|
306 |
pagineur.format = 'liste';
|
|
|
307 |
mettreAJourObservations();
|
|
|
308 |
ajouterGaleriePhoto("a.cel-img");
|
|
|
309 |
}
|
|
|
310 |
|
836 |
jpm |
311 |
function ajouterTableauTriable(element) {
|
|
|
312 |
// add parser through the tablesorter addParser method
|
|
|
313 |
$.tablesorter.addParser({
|
|
|
314 |
// Définition d'un id unique pour ce parsseur
|
|
|
315 |
id: 'date_cel',
|
|
|
316 |
is: function(s) {
|
915 |
jpm |
317 |
// doit retourner false si le parsseur n'est pas autodétecté
|
|
|
318 |
return /^\s*\d{2}[\/-]\d{2}[\/-]\d{4}\s*$/.test(s);
|
836 |
jpm |
319 |
},
|
915 |
jpm |
320 |
format: function(date) {
|
836 |
jpm |
321 |
// Transformation date jj/mm/aaaa en aaaa/mm/jj
|
915 |
jpm |
322 |
date = date.replace(/^\s*(\d{2})[\/-](\d{2})[\/-](\d{4})\s*$/, "$3/$2/$1");
|
836 |
jpm |
323 |
// Remplace la date par un nombre de millisecondes pour trier numériquement
|
915 |
jpm |
324 |
return $.tablesorter.formatFloat(new Date(date).getTime());
|
836 |
jpm |
325 |
},
|
|
|
326 |
// set type, either numeric or text
|
|
|
327 |
type: 'numeric'
|
|
|
328 |
});
|
|
|
329 |
$(element).tablesorter({
|
|
|
330 |
headers: {
|
915 |
jpm |
331 |
1: {
|
|
|
332 |
sorter:'date_cel'
|
|
|
333 |
}
|
|
|
334 |
}
|
|
|
335 |
});
|
836 |
jpm |
336 |
}
|
|
|
337 |
|
915 |
jpm |
338 |
function mettreAJourTableauTriable(element) {
|
|
|
339 |
$(element).trigger('update');
|
|
|
340 |
}
|
|
|
341 |
|
836 |
jpm |
342 |
function ajouterGaleriePhoto(element) {
|
|
|
343 |
$(element).fancybox({
|
915 |
jpm |
344 |
transitionIn:'elastic',
|
|
|
345 |
transitionOut:'elastic',
|
|
|
346 |
speedIn :600,
|
|
|
347 |
speedOut:200,
|
|
|
348 |
overlayShow:true,
|
|
|
349 |
titleShow:true,
|
|
|
350 |
titlePosition:'inside',
|
|
|
351 |
titleFormat:function (titre, currentArray, currentIndex, currentOpts) {
|
|
|
352 |
var motif = /urn:lsid:tela-botanica[.]org:cel:img([0-9]+)$/;
|
|
|
353 |
motif.exec(titre);
|
|
|
354 |
var id = RegExp.$1;
|
|
|
355 |
var info = $('#cel-info-'+id).clone().html();
|
|
|
356 |
var tpl =
|
|
|
357 |
'<div class="cel-legende">'+
|
|
|
358 |
'<p class="cel-legende-vei">'+'Image n°' + (currentIndex + 1) + ' sur ' + currentArray.length +'<\/p>'+
|
|
|
359 |
(titre && titre.length ? '<p>'+info+'<\/p>' : '' )+
|
|
|
360 |
'<\/div>';
|
|
|
361 |
return tpl;
|
836 |
jpm |
362 |
}
|
915 |
jpm |
363 |
}).live('click', function(e) {
|
|
|
364 |
if (e.stopPropagation) {
|
|
|
365 |
e.stopPropagation();
|
|
|
366 |
}
|
|
|
367 |
return false;
|
|
|
368 |
});
|
836 |
jpm |
369 |
}
|
|
|
370 |
|
915 |
jpm |
371 |
/*+--------------------------------------------------------------------------------------------------------+*/
|
|
|
372 |
// PANNEAU LATÉRAL
|
836 |
jpm |
373 |
|
915 |
jpm |
374 |
function initialiserAffichagePanneauLateral() {
|
|
|
375 |
$('#panneau-lateral').height($(window).height() - 35);
|
841 |
jpm |
376 |
|
915 |
jpm |
377 |
if (nt == '*') {
|
|
|
378 |
$('#pl-ouverture').bind('click', afficherPanneauLateral);
|
|
|
379 |
$('#pl-fermeture').bind('click', cacherPanneauLateral);
|
|
|
380 |
$('.taxon').live('click', filtrerParTaxon);
|
836 |
jpm |
381 |
}
|
|
|
382 |
}
|
|
|
383 |
|
915 |
jpm |
384 |
function afficherPanneauLateral() {
|
836 |
jpm |
385 |
$('#panneau-lateral').width(300);
|
|
|
386 |
$('#pl-contenu').css('display', 'block');
|
|
|
387 |
$('#pl-ouverture').css('display', 'none');
|
|
|
388 |
$('#pl-fermeture').css('display', 'block');
|
|
|
389 |
$('#carte').css('left', '300px');
|
|
|
390 |
|
|
|
391 |
google.maps.event.trigger(map, 'resize');
|
|
|
392 |
};
|
|
|
393 |
|
915 |
jpm |
394 |
function cacherPanneauLateral() {
|
836 |
jpm |
395 |
$('#panneau-lateral').width(24);
|
|
|
396 |
$('#pl-contenu').css('display', 'none');
|
|
|
397 |
$('#pl-ouverture').css('display', 'block');
|
|
|
398 |
$('#pl-fermeture').css('display', 'none');
|
|
|
399 |
$('#carte').css('left', '24px');
|
|
|
400 |
|
|
|
401 |
google.maps.event.trigger(map, 'resize');
|
|
|
402 |
};
|
|
|
403 |
|
|
|
404 |
function ouvrirPopUp(url, nom) {
|
|
|
405 |
window.open(url, nom, 'scrollbars=yes,width=650,height=600,directories=no,location=no,menubar=no,status=no,toolbar=no');
|
|
|
406 |
};
|
|
|
407 |
|
|
|
408 |
function filtrerParTaxon() {
|
|
|
409 |
var ntAFiltrer = $('.nt', this).text();
|
|
|
410 |
infoBulle.close();
|
|
|
411 |
$('#taxon-'+nt).removeClass('taxon-actif');
|
|
|
412 |
if (nt == ntAFiltrer) {
|
|
|
413 |
nt = '*';
|
|
|
414 |
executerMarkerClusterer(pointsOrigine, boundsOrigine);
|
|
|
415 |
} else {
|
|
|
416 |
var url = urlObsCarte+'&'+
|
|
|
417 |
'num_taxon='+ntAFiltrer+'&'+
|
|
|
418 |
'formatRetour=jsonp'+'&'+
|
|
|
419 |
'callback=?';
|
|
|
420 |
$.getJSON(url, function (observations) {
|
|
|
421 |
obs = observations;
|
|
|
422 |
nt = ntAFiltrer;
|
|
|
423 |
$('#taxon-'+nt).addClass('taxon-actif');
|
|
|
424 |
rafraichirCarte();
|
|
|
425 |
});
|
|
|
426 |
}
|
|
|
427 |
};
|
|
|
428 |
|
915 |
jpm |
429 |
/*+--------------------------------------------------------------------------------------------------------+*/
|
|
|
430 |
// FONCTIONS UTILITAIRES
|
|
|
431 |
|
|
|
432 |
function arreter(event) {
|
|
|
433 |
if (event.stopPropagation) {
|
|
|
434 |
event.stopPropagation();
|
|
|
435 |
} else if (window.event) {
|
|
|
436 |
window.event.cancelBubble = true;
|
|
|
437 |
}
|
|
|
438 |
return false;
|
|
|
439 |
}
|
|
|
440 |
|
836 |
jpm |
441 |
/**
|
|
|
442 |
* +-------------------------------------+
|
|
|
443 |
* Number.prototype.formaterNombre
|
|
|
444 |
* +-------------------------------------+
|
|
|
445 |
* Params (facultatifs):
|
|
|
446 |
* - Int decimales: nombre de decimales (exemple: 2)
|
|
|
447 |
* - String signe: le signe precedent les decimales (exemple: "," ou ".")
|
|
|
448 |
* - String separateurMilliers: comme son nom l'indique
|
|
|
449 |
* Returns:
|
|
|
450 |
* - String chaine formatee
|
|
|
451 |
* @author ::mastahbenus::
|
|
|
452 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org> : ajout détection auto entier/flotant
|
|
|
453 |
* @souce http://www.javascriptfr.com/codes/FORMATER-NOMBRE-FACON-NUMBER-FORMAT-PHP_40060.aspx
|
|
|
454 |
*/
|
|
|
455 |
Number.prototype.formaterNombre = function (decimales, signe, separateurMilliers) {
|
|
|
456 |
var _sNombre = String(this), i, _sRetour = "", _sDecimales = "";
|
|
|
457 |
|
|
|
458 |
function is_int(nbre) {
|
|
|
459 |
nbre = nbre.replace(',', '.');
|
|
|
460 |
return !(parseFloat(nbre)-parseInt(nbre) > 0);
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
if (decimales == undefined) {
|
|
|
464 |
if (is_int(_sNombre)) {
|
|
|
465 |
decimales = 0;
|
|
|
466 |
} else {
|
|
|
467 |
decimales = 2;
|
|
|
468 |
}
|
|
|
469 |
}
|
|
|
470 |
if (signe == undefined) {
|
|
|
471 |
if (is_int(_sNombre)) {
|
|
|
472 |
signe = '';
|
|
|
473 |
} else {
|
|
|
474 |
signe = '.';
|
|
|
475 |
}
|
|
|
476 |
}
|
|
|
477 |
if (separateurMilliers == undefined) {
|
|
|
478 |
separateurMilliers = ' ';
|
|
|
479 |
}
|
|
|
480 |
|
|
|
481 |
function separeMilliers (sNombre) {
|
|
|
482 |
var sRetour = "";
|
|
|
483 |
while (sNombre.length % 3 != 0) {
|
|
|
484 |
sNombre = "0"+sNombre;
|
|
|
485 |
}
|
|
|
486 |
for (i = 0; i < sNombre.length; i += 3) {
|
|
|
487 |
if (i == sNombre.length-1) separateurMilliers = '';
|
|
|
488 |
sRetour += sNombre.substr(i, 3) + separateurMilliers;
|
|
|
489 |
}
|
|
|
490 |
while (sRetour.substr(0, 1) == "0") {
|
|
|
491 |
sRetour = sRetour.substr(1);
|
|
|
492 |
}
|
|
|
493 |
return sRetour.substr(0, sRetour.lastIndexOf(separateurMilliers));
|
|
|
494 |
}
|
|
|
495 |
|
|
|
496 |
if (_sNombre.indexOf('.') == -1) {
|
|
|
497 |
for (i = 0; i < decimales; i++) {
|
|
|
498 |
_sDecimales += "0";
|
|
|
499 |
}
|
|
|
500 |
_sRetour = separeMilliers(_sNombre) + signe + _sDecimales;
|
|
|
501 |
} else {
|
|
|
502 |
var sDecimalesTmp = (_sNombre.substr(_sNombre.indexOf('.')+1));
|
|
|
503 |
if (sDecimalesTmp.length > decimales) {
|
|
|
504 |
var nDecimalesManquantes = sDecimalesTmp.length - decimales;
|
|
|
505 |
var nDiv = 1;
|
|
|
506 |
for (i = 0; i < nDecimalesManquantes; i++) {
|
|
|
507 |
nDiv *= 10;
|
|
|
508 |
}
|
|
|
509 |
_sDecimales = Math.round(Number(sDecimalesTmp) / nDiv);
|
|
|
510 |
}
|
|
|
511 |
_sRetour = separeMilliers(_sNombre.substr(0, _sNombre.indexOf('.')))+String(signe)+_sDecimales;
|
|
|
512 |
}
|
|
|
513 |
return _sRetour;
|
915 |
jpm |
514 |
}
|
|
|
515 |
|
|
|
516 |
function debug(objet) {
|
|
|
517 |
var msg = '';
|
|
|
518 |
if (objet != null) {
|
|
|
519 |
$.each(objet, function (cle, valeur) {
|
|
|
520 |
msg += cle+":"+valeur + "\n";
|
|
|
521 |
});
|
|
|
522 |
} else {
|
|
|
523 |
msg = "La variable vaut null.";
|
|
|
524 |
}
|
|
|
525 |
console.log(msg);
|
836 |
jpm |
526 |
}
|