Subversion Repositories eFlore/Applications.cel

Rev

Rev 1244 | 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);
        
        var txt = 'Observation n°'+obs.num+'/'+bdd.length+' créée';
        $('#obs-saisie-infos').html('<p class="reponse ui-btn-inner ui-btn-corner-all">'+txt+'</p>')
                .fadeIn("slow")
                .delay(1600)
                .fadeOut("slow");
        
        event.stopPropagation();
        event.preventDefault();
}

function supprimerObs(event) {
        var cle = 'obs'+obs.num;
        bdd.removeItem(cle);
        
        var txt = 'Observation n°'+obs.num+' supprimée';
        $('#obs-saisie-infos').html('<p class="reponse ui-btn-inner ui-btn-corner-all">'+txt+'</p>')
                .fadeIn("slow")
                .delay(1600)
                .fadeOut("slow");
        
        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>'+
                                '<img src="http://www.tela-botanica.org/widget-test:cel:modules/saisie/squelettes/defaut/img/icones/pasdephoto.png" />'+
                                '<a href="#'+cle+'" data-split-icon="next" data-split-theme="a" title="Voir la fiche" data-obs-num="'+obs.num+'">'+
                                        '<strong>'+obs.nom+'</strong> observé le '+obs.date+' à lat : '+obs.lat+' lng : '+obs.lng+
                                '</a>'+
                                '<a href="#" title="Supprimer l\'observation" data-obs-num="'+obs.num+'">'+
                                        'Supprimer'+
                                '</a>'+
                        '</li>'
                );
        }
        $('#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();
    }  
}