events.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var ai1ec = (function(){
  2. var API = {}, json = null, events = [];
  3. //Categorie di eventi
  4. API.cats = {
  5. single: 34,
  6. repeat: 25
  7. };
  8. //Parser di un evento dell'array tornato da getXmlData
  9. API.event = {
  10. getId : function(event) {
  11. return event.uid || null;
  12. },
  13. getTitle : function(event) {
  14. return event.summary || null;
  15. },
  16. getDescription : function(event) {
  17. return event.description || "";
  18. },
  19. getInitDate : function(event) {
  20. return event.dtstart && event.dtstart._ ? event.dtstart._ : "";
  21. },
  22. getEndDate : function(event) {
  23. return event.dtend && event.dtend._ ? event.dtend._ : "";
  24. },
  25. getCats : function(event) {
  26. return event.categories || "";
  27. },
  28. getTags : function(event) {
  29. return event["x-tags"] && event["x-tags"]._ ? event["x-tags"]._ : "";
  30. },
  31. getUrl : function(event) {
  32. return event.url && event.url.$ && event.url.$.uri ? event.url.$.uri : "";
  33. },
  34. getContact : function(event) {
  35. return event.contact || "";
  36. },
  37. getGeo : function(event) {
  38. return event.geo || "";
  39. },
  40. };
  41. var getXmlData = function(url, callback) {
  42. $.ajax({
  43. url: url,
  44. dataType: 'xml',
  45. method: 'GET',
  46. success: function(res) {
  47. json = $.xml2json(res);
  48. events = json["#document"].vcalendar.vevent;
  49. console.log(events);
  50. return callback(events);
  51. },
  52. error: function(err){
  53. console.log("Err: status ", err.status);
  54. return callback(events);
  55. }
  56. });
  57. };
  58. var getDefaultUrl = function(catId){
  59. catId = catId || API.cats.single;
  60. return API.remote.site + API.remote.q + "&" + API.remote.catq + catId + "&" + API.remote.xmlq;
  61. }
  62. //Oggetto che gestisce attività remote
  63. API.remote = {
  64. site : 'http://www.ecn.org/xm24',
  65. q : '/?plugin=all-in-one-event-calendar&controller=ai1ec_exporter_controller&action=export_events',
  66. post: 'ai1ec_post_ids=',
  67. catq : 'ai1ec_cat_ids=',
  68. xmlq : 'xml=true',
  69. win : 'window=true',
  70. //post_ids
  71. //Torna un array con tutti gli eventi organizzati da xm24, un array vuoto se errore.
  72. getAllEvents : function(catId, callback) {
  73. var url = getDefaultUrl(catId);
  74. getXmlData(url, callback);
  75. },
  76. //Torna un array con gli eventi tra -30 e +30 giorni da oggi, un array vuoto se errore.
  77. getLastEvents : function(catId, callback) {
  78. var url = getDefaultUrl(catId) + '&' + API.remote.win;
  79. getXmlData(url, callback);
  80. },
  81. }
  82. return API;
  83. })();