source: spip-zone/_plugins_/gcalendar/gcalendar.html @ 39804

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

ajout du multilinguisme
langues : frnaçaise arabe anglais
plugin.xml plus clair
passage en mode test
mise à jour version 1.0.9

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