Wegwichtel/api/database.js
2026-06-16 09:17:23 +02:00

57 lines
3.5 KiB
JavaScript
Executable File

/****************************************************************************************************************
* CLASS DB: contains all data about routes and linked pois *
* *
* Events: body.databaseLoaded(database:DB) *
* *
* Returns: Array of routes in format *
* Object{ *
* id:Number, *
* name:String, *
* image:String, *
* languages:Array(String), *
* pois:Array(Object{ *
* lat:Number, *
* lon:Number, *
* desc:String, *
* range:Number, *
* audio:Number *
* } *
* } *
****************************************************************************************************************/
function DB() {
var that = this;
this._routes = []; // format: {id:0, name:'', image:'', languages:[], pois:[{lat:0, lon:0, desc:'', range:0, audio:0}]}
this._completeTracks = Array();
this._init = function() {
$('#database-loading').slideDown({duration: 500});
console.debug('loading database');
$.getJSON('database.php', {'get':'routes'}, function(data) {
console.debug('database: routes received');
data.forEach(function(track) {
//console.debug(id, track);
that._completeTracks[track.id] = false;
$.getJSON('database.php', {'get':'pois','id':track.id}, function(pois) {
console.debug(track.name, pois);
// console.debug('database: '+pois.length+' pois for '+track.name+' received');
track.pois = pois;
that._routes[track.id] = (track);
that._completeTracks[track.id] = true;
//console.debug('track done:', track);
if (that._completeTracks.reduce(function(prev, curr) { return (prev && curr); }, true)) { //tracks complete ?
$('#database-loading').slideUp({duration: 500});
console.debug('database loaded');
$('body').trigger('databaseLoaded', that);
}
});
});
});
}
this._init();
return this._routes;
}