diff --git a/www/js/events.js b/www/js/events.js new file mode 100644 index 0000000..f669346 --- /dev/null +++ b/www/js/events.js @@ -0,0 +1,106 @@ +var ai1ec = (function(){ + var API = {}, json = null, events = []; + + //Categorie di eventi + API.cats = { + single: 34, + repeat: 25 + }; + + //Parser di un evento dell'array tornato da getXmlData + API.event = { + getId : function(event) { + return event.uid || null; + }, + + getTitle : function(event) { + return event.summary || null; + }, + + getDescription : function(event) { + return event.description || ""; + }, + + getInitDate : function(event) { + return event.dtstart._ || ""; + }, + + getEndDate : function(event) { + return event.dtend._ || ""; + }, + + getCats : function(event) { + return event.categories || ""; + }, + + getTags : function(event) { + return event["x-tags"]._ || ""; + }, + + getUrl : function(event) { + return event.url.$.uri || ""; + }, + + getContact : function(event) { + return event.contact || ""; + }, + + getGeo : function(event) { + return event.geo || ""; + }, + }; + + var getXmlData = function(url, callback) { + $.ajax({ + url: url, + dataType: 'xml', + method: 'GET', + success: function(res) { + json = $.xml2json(res); + events = json["#document"].vcalendar.vevent; + console.log(events); + return callback(events); + }, + error: function(err){ + console.log("Err: status ", err.status); + return callback(events); + } + }); + }; + + var getDefaultUrl = function(catId){ + catId = catId || API.cats.single; + + return API.remote.site + API.remote.q + "&" + API.remote.catq + catId + "&" + API.remote.xmlq; + } + + //Oggetto che gestisce attività remote + API.remote = { + site : 'http://www.ecn.org/xm24', + q : '/?plugin=all-in-one-event-calendar&controller=ai1ec_exporter_controller&action=export_events', + post: 'ai1ec_post_ids=', + catq : 'ai1ec_cat_ids=', + xmlq : 'xml=true', + win : 'window=true', + //post_ids + + //Torna un array con tutti gli eventi organizzati da xm24, un array vuoto se errore. + getAllEvents : function(catId) { + var url = getDefaultUrl(catId); + getXmlData(url, function(events){ + return events; + }); + }, + + //Torna un array con gli eventi tra -30 e +30 giorni da oggi, un array vuoto se errore. + getLastEvents : function(catId) { + var url = getDefaultUrl(catId) + '&' + API.remote.win; + getXmlData(url, function(events){ + return events; + }); + }, + + } + + return API; +})(); \ No newline at end of file