-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery-ical.js
375 lines (308 loc) · 11.9 KB
/
jquery-ical.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* First plugin
*
* @author Maarten Hus
*/
(function($){
var eventdates = {};
$.fn.ical = function(options){
$.fn.ical.defaults = {
daynames: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'], //default short names for the days of the week
monthnames: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
startdate: new Date(), // The date the calender should take as start point
displayDaysInLastRow: true,
eventdates: {},
startOnSunday: false,
beforeDay: function (insdate) {},
beforeMonth: function(insdate) {},
beforeYear: function(insdate) {}
};
var options = $.extend({}, $.fn.ical.defaults, options);
return this.each(function(){
var obj = $(this); //get the object
eventdates = options.eventdates;
var insdate = options.startdate; //The date that gets used for calculating the month
createCalendar(obj, insdate);
});
/**
* Create the calendar
*/
function createCalendar(obj, insdate){
obj.html('');
createNavigation(obj, insdate);
createTable(obj); //create table
addDatesToTable(obj, insdate);
codabubble();
};
/**
* Create the navigation and handle its clicks
*/
function createNavigation(obj, insdate){
obj.append("<div><span id = 'currentmonth'>"+ options.monthnames[insdate.getMonth()] +"</span> <span id = 'currentyear'>"+ insdate.getFullYear() +"</span></div>" +
"<div><span id ='icalprev'><</span><span id ='icalnext'>></span></div>");
$("#icalnext", obj).click(function()
{
var month = insdate.getMonth() + 1;
if (month > 11)
{
month = 0;
var year = insdate.getFullYear() + 1;
options.beforeYear(formatDate(year, month, 1));
}
else
{
var year = insdate.getFullYear();
}
options.beforeMonth(formatDate(year, month, 1));
date = new Date(year, month, 1);
createCalendar(obj, date);
});
$("#icalprev", obj).click(function()
{
var month = insdate.getMonth() - 1;
if (month < 0)
{
month = 11;
var year = insdate.getFullYear() - 1;
options.beforeYear(formatDate(year, month, 1));
}
else
{
var year = insdate.getFullYear();
}
options.beforeMonth(formatDate(year, month, 1));
date = new Date(year, month, 1);
createCalendar(obj, date);
});
};
/**
* Create the table for the calendar
*/
function createTable(obj){
if (options.displayDaysInLastRow) {
obj.append("<table class='icaltable' cellspacing='0'><thead><tr></tr></thead><tfoot><tr></tr></tfoot></table>"); //add a table
}
else {
obj.append("<table class='icaltable' cellspacing='0'><thead><tr></tr></thead><tfoot></tfoot></table>"); //add a table
}
for (var i = 0; i < options.daynames.length; i++)
{
$(".icaltable tr", obj).append("<th>"+ options.daynames[i] +"</th>"); //add the day header
}
};
function addDatesToTable(obj, insdate){
var month = insdate.getMonth();
var year = insdate.getFullYear();
var days = getDaysInMonth(year, month);
var first = getFirstDayOfMonth(year, month); // 0 - 6
var last = getLastDayOfMonth(year, month, days);// 0 - 6
var afterpadding = 6 - last; // week minus the last day of the month = afterpadding
var firstrow = true;
var startOnSunday = 0;
if (options.startOnSunday){
if (first == 6)
{
startOnSunday = -6;
}
else
{
startOnSunday = 1;
}
}
for (var i = 1; i <= days; i++) //each day in month
{
if ((first + i - 1 + startOnSunday) % 7 === 0 || firstrow === true ) //add new tr for each new monday our if i is zero
{
$(".icaltable", obj).append("<tr></tr>");
}
for(var j = 0; j < first + startOnSunday && firstrow; j++) //add pre padding
{
$(".icaltable tr:last", obj).append("<td class = 'padding'></td");
}
firstrow = false; //no more pre padding
var month = getMonthNumber($("#currentmonth", obj).text());
var year = $("#currentyear", obj).text();
var formatdate = formatDate(year, month, i);
var jsondates = getEventDates(formatdate)
if (jsondates.length === 0)
{
options.beforeDay(formatdate);
$(".icaltable tr:last", obj).append("<td id = '"+formatdate+"'>"+i+"</td"); //add day
}
else
{
var firstEvent = true;
for (var k = 0; k < jsondates.length; k++)
{
var datejson = jsondates[k];
options.beforeDay(formatdate);
//alert(datejson.title);
var str = "<li><span class='title'>"+ datejson.title +"</span><span class='desc'>"+ datejson.desc +"</span></li>"
if (firstEvent)
{
$(".icaltable tr:last", obj).append("<td class='date_has_event " + datejson.className + "' id = '"+ formatdate +"'>"+i+"<div class='events'><ul id ='ul-"+ formatdate +"'>"+ str +"</ul></div></td>"); //add day
firstEvent = false;
}
else
{
$("#ul-" + formatdate, obj).append(str);
}
}
}
};
if (options.startOnSunday){
startOnSunday = 1;
}
for (var i = 0; i < afterpadding - startOnSunday; i++) //add after padding
{
$(".icaltable tr:last", obj).append("<td class = 'padding'></td>");
}
highlightToday(obj);
};
function getMonthNumber(month){
for (var i = 0; i < options.monthnames.length; i++)
{
if (options.monthnames[i] === month)
{
return i;
}
}
};
function getDaysInMonth(year, month){
return 32 - new Date(year, month, 32).getDate();
};
function highlightToday(obj){
var today = new Date();
today = formatDate(today.getFullYear(), today.getMonth(), today.getDate());
$("#"+today, obj).addClass("today");
};
function getEventDates(date){
var results = [];
for (var i = 0; i < eventdates.length; i++)
{
var evaldate = evaluateEventDate(eventdates[i]["date"], date);
if (date === evaldate)
{
results.push(eventdates[i]);
}
}
return results;
};
function evaluateEventDate(eventdate, date){
var eventdate = eventdate.split('-');
var date = date.split('-');
if (eventdate[0] === 'yyyy')
{
eventdate[0] = date[0];
}
if (eventdate[1] === 'mm')
{
eventdate[1] = date[1];
}
if (eventdate[2] === 'dd')
{
eventdate[2] = date[2];
}
return eventdate[0]+'-'+eventdate[1]+'-'+eventdate[2];
};
function getLastDayOfMonth(year, month, days){
var date = new Date(year, month, days);
if (date.getDay() == 0)//we start on monday!
{
return 6;
}
else
{
return date.getDay() -1;
}
};
function getFirstDayOfMonth(year, month){
var date = new Date(year, month, 1);
if (date.getDay() == 0) //we start on monday!
{
return 6;
}
else
{
return date.getDay() -1;
}
};
function formatDate (year, month, day){
return year+'-'+formatMonth(month)+'-'+formatDay(day);
};
function formatMonth(month){
month = month + 1;
if (month < 10)
{
month = '0'+month;
}
return month;
};
function formatDay(day){
if (day < 10)
{
day = '0'+day;
}
return day;
};
function codabubble(){ //Stefano Verna
$('.date_has_event').each(function () {
// options
var distance = 10;
var time = 250;
var hideDelay = 500;
var hideDelayTimer = null;
// tracker
var beingShown = false;
var shown = false;
var trigger = $(this);
var popup = $('.events ul', this).css('opacity', 0);
// set the mouseover and mouseout on both element
$([trigger.get(0), popup.get(0)]).mouseover(function () {
// stops the hide event if we move from the trigger to the popup element
if (hideDelayTimer) clearTimeout(hideDelayTimer);
// don't trigger the animation again if we're being shown, or already visible
if (beingShown || shown) {
return;
} else {
beingShown = true;
// reset position of popup box
popup.css({
bottom: -20,
left: -76,
display: 'block' // brings the popup back in to view
})
// (we're using chaining on the popup) now animate it's opacity and position
.animate({
bottom: '+=' + distance + 'px',
opacity: 1
}, time, 'swing', function() {
// once the animation is complete, set the tracker variables
beingShown = false;
shown = true;
});
}
}).mouseout(function () {
// reset the timer if we get fired again - avoids double animations
if (hideDelayTimer) clearTimeout(hideDelayTimer);
// store the timer so that it can be cleared in the mouseover if required
hideDelayTimer = setTimeout(function () {
hideDelayTimer = null;
popup.animate({
bottom: '-=' + distance + 'px',
opacity: 0
}, time, 'swing', function () {
// once the animate is complete, set the tracker variables
shown = false;
// hide the popup entirely after the effect (opacity alone doesn't do the job)
popup.css('display', 'none');
});
}, hideDelay);
});
});
};
};
$.fn.ical.changeEventDates = function(array){
eventdates = array;
};
})(jQuery);