|
| 1 | +/** |
| 2 | + * ican.openacalendar.org Event List Widget |
| 3 | + * @link http://ican.openacalendar.org/ OpenACalendar Open Source Software |
| 4 | + * @license http://ican.openacalendar.org/license.html 3-clause BSD |
| 5 | + * @copyright (c) 2013-2014, JMB Technology Limited, http://jmbtechnology.co.uk/ |
| 6 | + * @author James Baster <[email protected]> |
| 7 | + */ |
| 8 | + |
| 9 | +var OpenACalendarWidgetListEvents = { |
| 10 | + callBackFunctionCount: 0, |
| 11 | + place: function(divid, site, options) { |
| 12 | + var usingOptions = { |
| 13 | + eventCount: 5, |
| 14 | + title: 'Events', |
| 15 | + maxStringLength: 300, |
| 16 | + groupID: undefined, |
| 17 | + areaID: undefined, |
| 18 | + openInNewWindow: true, |
| 19 | + sslAvailable: false |
| 20 | + } |
| 21 | + for (var prop in options) { |
| 22 | + if (options.hasOwnProperty(prop)) { |
| 23 | + usingOptions[prop] = options[prop]; |
| 24 | + } |
| 25 | + } |
| 26 | + var div = document.getElementById(divid); |
| 27 | + if (!div) return; |
| 28 | + var moreURL; |
| 29 | + if (usingOptions.groupID) { |
| 30 | + moreURL = "http://"+site+"/group/"+usingOptions.groupID; |
| 31 | + } else if (usingOptions.venueID) { |
| 32 | + moreURL = "http://"+site+"/venue/"+usingOptions.venueID; |
| 33 | + } else if (usingOptions.countryCode) { |
| 34 | + moreURL = "http://"+site+"/country/"+usingOptions.countryCode.toUpperCase(); |
| 35 | + } else if (usingOptions.areaID) { |
| 36 | + moreURL = "http://"+site+"/area/"+usingOptions.areaID; |
| 37 | + } else { |
| 38 | + moreURL = "http://"+site+"/"; |
| 39 | + } |
| 40 | + |
| 41 | + var target = usingOptions['openInNewWindow'] ? ' target="_BLANK"' : ''; |
| 42 | + |
| 43 | + div.innerHTML = '<div class="OpenACalendarWidgetListEventsData">'+ |
| 44 | + '<div class="OpenACalendarWidgetListEventsHeader"><a href="'+moreURL+'" '+target+' id="'+divid+'Title">'+OpenACalendarWidgetListEvents.escapeHTML(usingOptions.title)+'</a></div>'+ |
| 45 | + '<div class="OpenACalendarWidgetListEventsEvents" id="'+divid+'Data">Loading</div>'+ |
| 46 | + '<div class="OpenACalendarWidgetListEventsFooter">'+ |
| 47 | + '<div class="OpenACalendarWidgetListEventsFooterMore"><a href="'+moreURL+'" '+target+'>See more ...</a></div>'+ |
| 48 | + '<div class="OpenACalendarWidgetListEventsFooterCredit">Powered by <a href="http://ican.hasacalendar.co.uk" '+target+'>Has A Calendar</a></div>'+ |
| 49 | + '</div>'+ |
| 50 | + '</div>'; |
| 51 | + var dataDiv = document.getElementById(divid+"Data"); |
| 52 | + var headTag = document.getElementsByTagName('head').item(0); |
| 53 | + |
| 54 | + OpenACalendarWidgetListEvents.callBackFunctionCount++; |
| 55 | + window["OpenACalendarWidgetListEventsCallBackFunction"+OpenACalendarWidgetListEvents.callBackFunctionCount] = function(data) { |
| 56 | + var html = ''; |
| 57 | + var limit = Math.min(data.data.length, usingOptions.eventCount); |
| 58 | + if (limit <= 0) { |
| 59 | + html = '<div class="OpenACalendarWidgetListEventsEventNone">No events</div>'; |
| 60 | + } else { |
| 61 | + for (var i=0;i<limit;i++) { |
| 62 | + html += OpenACalendarWidgetListEvents.htmlFromEvent(data.data[i], usingOptions.maxStringLength, target); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + dataDiv.innerHTML=html; |
| 67 | + |
| 68 | + if (!usingOptions.title) { |
| 69 | + var titleDiv = document.getElementById(divid+"Title"); |
| 70 | + titleDiv.innerHTML = data.title; |
| 71 | + } |
| 72 | + } |
| 73 | + var url = "http://"; |
| 74 | + if (usingOptions.sslAvailable && "https:" == document.location.protocol) { |
| 75 | + url = "https://"; |
| 76 | + } |
| 77 | + if (usingOptions.groupID) { |
| 78 | + url += site+"/api1/group/"+usingOptions.groupID+"/events.jsonp"; |
| 79 | + } else if (usingOptions.areaID) { |
| 80 | + url += site+"/api1/area/"+usingOptions.areaID+"/events.jsonp"; |
| 81 | + } else if (usingOptions.venueID) { |
| 82 | + url += site+"/api1/venue/"+usingOptions.venueID+"/events.jsonp"; |
| 83 | + } else if (usingOptions.countryCode) { |
| 84 | + url += site+"/api1/country/"+usingOptions.countryCode.toUpperCase()+"/events.jsonp"; |
| 85 | + } else { |
| 86 | + url += site+"/api1/events.jsonp"; |
| 87 | + } |
| 88 | + |
| 89 | + var script = document.createElement("script"); |
| 90 | + script.type = "text/javascript"; |
| 91 | + script.src = url+"?callback=OpenACalendarWidgetListEventsCallBackFunction"+OpenACalendarWidgetListEvents.callBackFunctionCount; |
| 92 | + headTag.appendChild(script); |
| 93 | + }, |
| 94 | + htmlFromEvent: function(event, maxLength, target) { |
| 95 | + var html = '<div class="OpenACalendarWidgetListEventsEvent">' |
| 96 | + html += '<div class="OpenACalendarWidgetListEventsDate">'+event.start.displaylocal+'</div>'; |
| 97 | + html += '<div class="OpenACalendarWidgetListEventsSummary"><a href="'+event.siteurl+'" '+target+'>'+OpenACalendarWidgetListEvents.escapeHTML(event.summaryDisplay)+'</a></div>'; |
| 98 | + html += '<div class="OpenACalendarWidgetListEventsDescription">'+OpenACalendarWidgetListEvents.escapeHTMLNewLine(event.description, maxLength)+'</div>'; |
| 99 | + html += '<a class="OpenACalendarWidgetListEventsMoreLink" href="'+event.siteurl+'" '+target+'>More Info</a>'; |
| 100 | + html += '<div class="OpenACalendarWidgetListEventsClear"></div>'; |
| 101 | + return html+'</div>'; |
| 102 | + }, |
| 103 | + escapeHTML: function(str) { |
| 104 | + var div = document.createElement('div'); |
| 105 | + div.appendChild(document.createTextNode(str)); |
| 106 | + return div.innerHTML; |
| 107 | + }, |
| 108 | + escapeHTMLNewLine: function(str, maxLength) { |
| 109 | + var div = document.createElement('div'); |
| 110 | + div.appendChild(document.createTextNode(str)); |
| 111 | + var out = div.innerHTML; |
| 112 | + if (out.length > maxLength) { |
| 113 | + out = out.substr(0,maxLength)+" ..."; |
| 114 | + } |
| 115 | + return out.replace(/\n/g,'<br>'); |
| 116 | + } |
| 117 | +}; |
| 118 | + |
0 commit comments