Compare commits

...

3 commits

4 changed files with 270 additions and 0 deletions

View file

@ -34,6 +34,9 @@
<!--script src="js/swipe-page.js"></script-->
<script src="js/main.js"></script>
<script src="js/vendor/jquery.mobile-1.4.5.min.js"></script>
<script src="js/vendor/xml2json.js"></script>
<script src="js/events.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script src="js/vendor/push.js"></script>
<script type="text/javascript">
@ -171,6 +174,13 @@
<!--p class="ui-li-aside">BlackBerry</p-->
</a>
</li>
<li>
<a class="nav" href="#events">
<img src="./image/xm24-particolare2.jpeg" class="ui-li-thumb">
<h2>Eventi</h2>
<p>Bla Bla blabla</p>
</a>
</li>
</ul>
</center>
<!--a class="minibox orange nav" href="#eventi"> Eventi</a>
@ -211,6 +221,18 @@
</div>
</div>
<div data-role="page" id="events" data-prev="index" data-next="" data-dom-cache="true" data-theme="a" class="ui-page">
<div role="main" class="ui-content">
<div class="ui-body">
<p class="introlettera">EVENTI</p>
<p id="mostralista" class="listatovirgole listevoci">
<img class="gifloader" src="./image/ajax_loader_red_512.gif" />
</p>
</div>
<!-- chiude body-->
</div>
<!--chiude content -->
</div>
<div data-role="page" id="ilove" data-prev="index" data-next="" data-dom-cache="true" data-theme="a" class="ui-page">

106
www/js/events.js Normal file
View file

@ -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;
})();

View file

@ -68,6 +68,12 @@ function alterContent(url) {
$("#navfooter").removeClass("hide");
myJsonCategory(8, "#mostralistainfo");
break;
case '#events':
$("#navheader").removeClass("hide");
$("#navfooter").removeClass("hide");
ai1ec.remote.getAllEvents(ai1ec.cats.single);
ai1ec.remote.getLastEvents(ai1ec.cats.single);
break;
case '#aggiorna':
$("#navheader").removeClass("hide");
$("#navfooter").removeClass("hide");

136
www/js/vendor/xml2json.js vendored Normal file
View file

@ -0,0 +1,136 @@
/**
* jQuery plugin to convert a given $.ajax response xml object to json.
*
* @example var json = $.xml2json(response);
*/
(function() {
// default options based on https://github.com/Leonidas-from-XIV/node-xml2js
var defaultOptions = {
attrkey: '$',
charkey: '_',
normalize: false,
explicitArray: false
};
// extracted from jquery
function parseXML(data) {
var xml, tmp;
if (!data || typeof data !== "string") {
return null;
}
try {
if (window.DOMParser) { // Standard
tmp = new DOMParser();
xml = tmp.parseFromString(data, "text/xml");
} else { // IE
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = "false";
xml.loadXML(data);
}
} catch (e) {
xml = undefined;
}
if (!xml || !xml.documentElement || xml.getElementsByTagName("parsererror").length) {
throw new Error("Invalid XML: " + data);
}
return xml;
}
function normalize(value, options){
if (!!options.normalize){
return (value || '').trim();
}
return value;
}
function xml2jsonImpl(xml, options) {
var i, result = {}, attrs = {}, node, child, name;
result[options.attrkey] = attrs;
if (xml.attributes && xml.attributes.length > 0) {
for (i = 0; i < xml.attributes.length; i++){
var item = xml.attributes.item(i);
attrs[item.nodeName] = item.value;
}
}
// element content
if (xml.childElementCount === 0) {
result[options.charkey] = normalize(xml.textContent, options);
}
for (i = 0; i < xml.childNodes.length; i++) {
node = xml.childNodes[i];
if (node.nodeType === 1) {
if (node.attributes.length === 0 && node.childElementCount === 0){
child = normalize(node.textContent, options);
} else {
child = xml2jsonImpl(node, options);
}
name = node.nodeName;
if (result.hasOwnProperty(name)) {
// For repeating elements, cast/promote the node to array
var val = result[name];
if (!Array.isArray(val)) {
val = [val];
result[name] = val;
}
val.push(child);
} else if(options.explicitArray === true) {
result[name] = [child];
} else {
result[name] = child;
}
}
}
return result;
}
/**w
* Converts an xml document or string to a JSON object.
*
* @param xml
*/
function xml2json(xml, options) {
var n;
if (!xml) {
return xml;
}
options = options || {};
for(n in defaultOptions) {
if(defaultOptions.hasOwnProperty(n) && options[n] === undefined) {
options[n] = defaultOptions[n];
}
}
if (typeof xml === 'string') {
xml = parseXML(xml).documentElement;
}
var root = {};
if (xml.attributes && xml.attributes.length === 0 && xml.childElementCount === 0){
root[xml.nodeName] = normalize(xml.textContent, options);
} else {
root[xml.nodeName] = xml2jsonImpl(xml, options);
}
return root;
}
if (typeof jQuery !== 'undefined') {
jQuery.extend({xml2json: xml2json});
} else if (typeof module !== 'undefined') {
module.exports = xml2json;
} else if (typeof window !== 'undefined') {
window.xml2json = xml2json;
}
})();