-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
145 lines (130 loc) · 4.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
(function($) {
$.fn.gCalReader = function(options) {
var $div = $(this);
var defaults = $.extend({
calendarId: 'en.usa#[email protected]',
apiKey: 'Public_API_Key',
dateFormat: 'LongDate',
errorMsg: 'No events in calendar',
maxEvents: 50,
futureEventsOnly: true,
sortDescending: true
},
options);
var s = '';
var feedUrl = 'https://www.googleapis.com/calendar/v3/calendars/' +
encodeURIComponent(defaults.calendarId.trim()) +'/events?key=' + defaults.apiKey +
'&orderBy=startTime&singleEvents=true';
if(defaults.futureEventsOnly) {
feedUrl+='&timeMin='+ new Date().toISOString();
}
$.ajax({
url: feedUrl,
dataType: 'json',
success: function(data) {
if(defaults.sortDescending){
data.items = data.items.reverse();
}
data.items = data.items.slice(0, defaults.maxEvents);
$.each(data.items, function(e, item) {
var eventdate = item.start.dateTime || item.start.date ||'';
var summary = item.summary || '';
var description = item.description;
var location = item.location;
var eventDate = formatDate(eventdate, defaults.dateFormat.trim());
s ='<div class="eventtitle">'+ summary +'</div>';
s +='<div class="eventdate"> When: '+ eventDate +'</div>';
if(location) {
s +='<div class="location">Where: '+ location +'</div>';
}
if(description) {
s +='<div class="description">'+ description +'</div>';
}
$($div).append('<li>' + s + '</li>');
});
},
error: function(xhr, status) {
$($div).append('<p>' + status +' : '+ defaults.errorMsg +'</p>');
}
});
function formatDate(strDate, strFormat) {
var fd, arrDate, am, time;
var calendar = {
months: {
full: ['', 'January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October',
'November', 'December'
],
short: ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
]
},
days: {
full: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday'
],
short: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat',
'Sun'
]
}
};
if (strDate.length > 10) {
arrDate = /(\d+)\-(\d+)\-(\d+)T(\d+)\:(\d+)/.exec(strDate);
am = (arrDate[4] < 12);
time = am ? (parseInt(arrDate[4]) + ':' + arrDate[5] + ' AM') : (
arrDate[4] - 12 + ':' + arrDate[5] + ' PM');
if (time.indexOf('0') === 0) {
if (time.indexOf(':00') === 1) {
if (time.indexOf('AM') === 5) {
time = 'MIDNIGHT';
} else {
time = 'NOON';
}
} else {
time = time.replace('0:', '12:');
}
}
} else {
arrDate = /(\d+)\-(\d+)\-(\d+)/.exec(strDate);
time = 'Time not present in feed.';
}
var year = parseInt(arrDate[1]);
var month = parseInt(arrDate[2]);
var dayNum = parseInt(arrDate[3]);
var d = new Date(year, month - 1, dayNum);
switch (strFormat) {
case 'ShortTime':
fd = time;
break;
case 'ShortDate':
fd = month + '/' + dayNum + '/' + year;
break;
case 'LongDate':
fd = calendar.days.full[d.getDay()] + ' ' + calendar.months.full[
month] + ' ' + dayNum + ', ' + year;
break;
case 'LongDate+ShortTime':
fd = calendar.days.full[d.getDay()] + ' ' + calendar.months.full[
month] + ' ' + dayNum + ', ' + year + ' ' + time;
break;
case 'ShortDate+ShortTime':
fd = month + '/' + dayNum + '/' + year + ' ' + time;
break;
case 'DayMonth':
fd = calendar.days.short[d.getDay()] + ', ' + calendar.months.full[
month] + ' ' + dayNum;
break;
case 'MonthDay':
fd = calendar.months.full[month] + ' ' + dayNum;
break;
case 'YearMonth':
fd = calendar.months.full[month] + ' ' + year;
break;
default:
fd = calendar.days.full[d.getDay()] + ' ' + calendar.months.short[
month] + ' ' + dayNum + ', ' + year + ' ' + time;
}
return fd;
}
};
}(jQuery));