Subversion Repositories eFlore/Applications.cel

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

//+----------------------------------------------------------------------------------------------------------+
// Initialisation de Jquery mobile
$(document).bind("mobileinit", function(){
  $.mobile.defaultPageTransition = "fade";
});

//+----------------------------------------------------------------------------------------------------------+
// Géolocalisation
var gps = navigator.geolocation;

$(document).ready(function() {
        $('#geolocaliser').on('click', geolocaliser);   
});

function geolocaliser(event) {
        if (gps) {
            navigator.geolocation.getCurrentPosition(surSuccesGeoloc, surErreurGeoloc);
        } else {
            var erreur = {code:'0', message:'Géolocalisation non supportée par le navigateur'};
            surErreurGeoloc(erreur);
        }
        
        event.stopPropagation();
        event.preventDefault();
}
function surSuccesGeoloc(position){
        if (position){
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;
        $('#lat').val(lat);
        $('#lng').val(lng);
    }
}
function surErreurGeoloc(error){
        alert("Echec de la géolocalisation, code: " + error.code + " message: "+ error.message);
}
//+----------------------------------------------------------------------------------------------------------+
// Local Storage
var bdd = window.localStorage;
bdd.clear();
$(document).ready(function() {
        $('#sauver-obs').on('click', ajouterObs);
        $('body').on('pageshow', '#liste', chargerListeObs);
});

function ajouterObs(event) {
        var obs = {num:0, date:'', lat:'', lng:'', nom:''};
        obs.num = (bdd.length + 1);
        obs.date = $('#date').val();
        obs.lat = $('#lat').val();
        obs.lng = $('#lng').val();
        obs.nom = $('#nom').val();
        
        var cle = 'obs'+obs.num;
        var val = JSON.stringify(obs);
        bdd.setItem(cle, val);
        
        alert(bdd.getItem(cle));
        event.stopPropagation();
        event.preventDefault();
}

function chargerListeObs() {
        $('#liste-obs').empty();
        var nbre = bdd.length;
        for (var i = 0; i < nbre; i++) {
                var cle = 'obs'+(i+1);
                var obs = JSON.parse(bdd.getItem(cle));
                $('#liste-obs').append('<li>'+
                        '<a href="#'+cle+'">'+
                        '<strong>'+obs.nom+'</strong> observé le '+obs.date+' à lat : '+obs.lat+' lng : '+obs.lng+'</li>'+
                        '</a>'
                );
        }
        $('#liste-obs').listview('refresh');
}


//+----------------------------------------------------------------------------------------------------------+
// Manifest Cache
var appCache = window.applicationCache;

$(document).ready(function() {
        appCache.addEventListener('updateready', function() {
                alert('Mise à jour :'+appCache.status);
        });  
        if (appCache.status === appCache.UPDATEREADY) {  
                surMiseAJourCache();
        }
});

function surMiseAJourCache() {  
        // Browser downloaded a new app cache.
    // Swap it in and reload the page to get the new hotness.
        appCache.swapCache();
    if (confirm('A new version of this site is available. Load it?')) {
      window.location.reload();
    }  
}