source: spip-zone/_plugins_/gcalendar/modeles/gcalendarpart.html @ 39807

Last change on this file since 39807 was 39807, checked in by superyms@…, 10 years ago

j'ai oublié d'ajouter le préfixe du plugin aux balises de texte du style <:prefixe_du_plugin:montexte:> au lieu de juste <:montexte:>

File size: 4.7 KB
Line 
1<script type="text/javascript">
2<!--
3/**
4 * Converts an xs:date or xs:dateTime formatted string into the local timezone
5 * and outputs a human-readable form of this date or date/time.
6 *
7 * @param {string} gCalTime is the xs:date or xs:dateTime formatted string
8 * @return {string} is the human-readable date or date/time string
9 */
10function formatGCalTime(gCalTime) { 
11  // text for regex matches
12  var remtxt = gCalTime;
13
14  function consume(retxt) {
15    var match = remtxt.match(new RegExp('^' + retxt));
16    if (match) {
17      remtxt = remtxt.substring(match[0].length);
18      return match[0];
19    }
20    return '';
21  }
22
23  // minutes of correction between gCalTime and GMT
24  var totalCorrMins = 0;
25
26  var year = consume('\\d{4}');
27  consume('-?');
28  var month = consume('\\d{2}');
29  consume('-?');
30  var dateMonth = consume('\\d{2}');
31  var timeOrNot = consume('T');
32
33  // if a DATE-TIME was matched in the regex
34  if (timeOrNot == 'T') {
35    var hours = consume('\\d{2}');
36    consume(':?');
37    var mins = consume('\\d{2}');
38    consume('(:\\d{2})?(\\.\\d{3})?');
39    var zuluOrNot = consume('Z');
40
41    // if time from server is not already in GMT, calculate offset
42    if (zuluOrNot != 'Z') {
43      var corrPlusMinus = consume('[\\+\\-]');
44      if (corrPlusMinus != '') {
45        var corrHours = consume('\\d{2}');
46        consume(':?');
47        var corrMins = consume('\\d{2}');
48        totalCorrMins = (corrPlusMinus=='-' ? 1 : -1) * 
49            (Number(corrHours) * 60 + 
50            (corrMins=='' ? 0 : Number(corrMins)));
51      }
52    } 
53
54    // get time since epoch and apply correction, if necessary
55    // relies upon Date object to convert the GMT time to the local
56    // timezone
57    var originalDateEpoch = Date.UTC(year, month - 1, dateMonth, hours, mins);
58    var gmtDateEpoch = originalDateEpoch + totalCorrMins * 1000 * 60;
59    var ld = new Date(gmtDateEpoch);
60
61    // date is originally in YYYY-MM-DD format
62    // time is originally in a 24-hour format
63    // this converts it to MM/DD hh:mm (AM|PM)
64        dateString =   ((ld.getDate()<10)?('0' + 
65        ld.getDate()):(ld.getDate()))+ '/'  + (((ld.getMonth() + 1)<10)?('0' + 
66        (ld.getMonth() + 1)):((ld.getMonth() + 1))) + ' ' + ((ld.getHours()<10)?('0' + 
67        ld.getHours()):(ld.getHours())) +':' + ((ld.getMinutes()<10)?('0' + 
68        ld.getMinutes()):(ld.getMinutes())); 
69  } else {
70    // if only a DATE was matched
71    dateString =  parseInt(dateMonth, 10) + '/' + parseInt(month, 10) ;
72  }
73  return dateString;
74}
75
76/**
77 * Creates an unordered list of events in a human-readable form
78 *
79 * @param {json} root is the root JSON-formatted content from GData
80 * @param {string} divId is the div in which the events are added
81 */ 
82function listEvents(root, divId) {
83  var feed = root.feed;
84  var events = document.getElementById(divId);
85
86  if (events.childNodes.length > 0) {
87    events.removeChild(events.childNodes[0]);
88  }       
89
90  // create a new unordered list
91  var ul = document.createElement('ul');
92
93  // loop through each event in the feed
94  for (var i = 0; i < feed.entry.length; i++) {
95    var entry = feed.entry[i];
96    var title = entry.title.$t;
97    var start = entry['gd$when'][0].startTime;
98
99    // get the URL to link to the event
100    for (var linki = 0; linki < entry['link'].length; linki++) {
101      if (entry['link'][linki]['type'] == 'text/html' &&
102          entry['link'][linki]['rel'] == 'alternate') {
103        var entryLinkHref = entry['link'][linki]['href'];
104      }
105    }
106
107    var dateString = formatGCalTime(start);
108    var li = document.createElement('li');
109
110    // if we have a link to the event, create an 'a' element
111    if (typeof entryLinkHref != 'undefined') {
112      entryLink = document.createElement('a');
113      entryLink.setAttribute('href', entryLinkHref);
114      entryLink.appendChild(document.createTextNode(title));
115 
116      li.appendChild(document.createTextNode(dateString + ' - '));
117          li.appendChild(entryLink);
118    } else {
119      li.appendChild(document.createTextNode(title + ' - ' + dateString));
120    }       
121
122    // append the list item onto the unordered list
123    ul.appendChild(li);
124  }
125  events.appendChild(ul);
126}
127
128/**
129 * Callback function for the GData json-in-script call
130 * Inserts the supplied list of events into a div of a pre-defined name
131 *
132 * @param {json} root is the JSON-formatted content from GData
133 */ 
134function insertAgenda(root) {
135  listEvents(root, 'agenda');
136}
137//-->
138</script>
139
140<div id="agenda"><p><:gcalendar:chargement_agenda:></p></div>
141
142<script type="text/javascript" src="http://www.google.com/calendar/feeds/#ENV{adragenda}/public/full?alt=json-in-script&amp;callback=insertAgenda&amp;orderby=starttime&amp;max-results=15&amp;singleevents=true&amp;sortorder=ascending&amp;futureevents=true"></script>
Note: See TracBrowser for help on using the repository browser.