forked from thingsinjars/jQTouch-Calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jqt.calendar.js
executable file
·345 lines (282 loc) · 10.5 KB
/
jqt.calendar.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
/*
jQTouch Calendar Extension
based on jQTouch iCal alpha by Bruno Alexandre
Use this markup:
<div id="any_id">
<ul>
<li><time datetime="2009-02-17T05:00Z">Task text here</time></li>
</ul>
</div>
and this to initialise:
<script type="text/javascript" charset="utf-8">
var jQT = new $.jQTouch({});
$(function() {
$('#any_id').getCalendar();
});
</script>
you can also call getCalendar with an options object
$('#any_id').getCalendar({date:variable_x, weekstart:variable_y});
Options:
date: Date around which to render the initial calendar. Shows this date as selected (default: new Date())
days: Array of titles for the columns (default: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'])
months: Array of month names (default: ['January','February','March','April','May','June','July','August','September','October','November','December'])
weekstart: index of the position in the days array the week is to start on (default: 1)
*/
(function($) {
if ($.jQTouch) {
$.jQTouch.addExtension(function Calendar(jQT){
// Load the calendar for the given date
jQuery.fn.getCalendar = function(options) {
var defaults = {
date : new Date(),
days : ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
months: ['January','February','March','April','May','June','July','August','September','October','November','December'],
weekstart: 1,
noEvents: 'No Events'
}
var settings = $.extend({}, defaults, options);
return this.each(function(){
var $el = $(this);
//Add class for styling
$el.addClass('jqt_calendar');
//Save settings
$el.data('settings', settings);
//Read events from markup
$el.loadEvents();
// clear existing calendar
$el.empty();
// append generated calendar markup
$el.append($el.generateCalendar(settings.date.getMonth(),settings.date.getFullYear()));
//Run through dates and add a class to those with events
$el.attachEvents();
// set all clicks (don't use live or tap to avoid bugs)
$el.setBindings();
// Highlight today if we're on this month
$el.setToday();
// If the selected date has events, load them
$el.setSelected(settings.date);
});
};
//Markup Reading
jQuery.fn.loadEvents = function() {
$el = $(this);
if($el.data('events')) {
//We've already processed these.
//might look into merging arrays later
} else {
var events = {};
$el.find('time').each(function(index) {
var task_datetime = $(this).attr('datetime');
var time_marker_index = task_datetime.indexOf('T');
var task_day = task_datetime.substring(0, time_marker_index).replace(/-0/g,'-');
var task_time = task_datetime.substring(time_marker_index+1,task_datetime.length-1);
var task_text = $(this).html();
if(!$.isArray(events[task_day])) {
events[task_day] = []
}
events[task_day].push({time:task_time, text:task_text});
});
$el.data('events', events);
}
}
//Markup Generation
jQuery.fn.dayMarkup = function(format,day,month,year,column) {
var this_day = $('<td/>');
if ( format == 0 ) {
this_day.addClass('prevmonth');
} else if ( format == 9 ) {
this_day.addClass('nextmonth');
}
if ( column==0 || column==6 ) {
this_day.addClass('weekend');
}
this_day.attr('datetime',year+'-'+(month+1)+'-'+day);
this_day.html(day);
return this_day;
}
jQuery.fn.monthLength = function(month, year) {
var dd = new Date(year, month, 0);
return dd.getDate();
}
jQuery.fn.monthMarkup= function(month, year) {
var $el = $(this);
var settings = $el.data('settings');
var c = new Date();
c.setDate(1);c.setMonth(month);c.setFullYear(year);
var x = parseInt(settings.weekstart,10);
var s = (c.getDay()-x)%7;
if (s<0) {
s+=7;
}
var dm = $el.monthLength(month,year);
var this_month = $('<table/>');
this_month.data('month',month+1);
this_month.data('year',year);
this_month.attr('cellspacing', 0);
var table_head = $('<thead/>');
var table_row = $('<tr/>');
$('<th>' + settings.days[(0+x)%7]+'</th>').addClass('goto-prevmonth').appendTo(table_row);
$('<th>' + settings.days[(1+x)%7]+'</th>').appendTo(table_row);
$('<th>' + settings.days[(2+x)%7]+'</th>').appendTo(table_row);
$('<th><span>' + settings.months[month] + ' ' + year + '</span>' + settings.days[(3+x)%7] + '</th>').appendTo(table_row);
$('<th>' + settings.days[(4+x)%7]+'</th>').appendTo(table_row);
$('<th>' + settings.days[(5+x)%7]+'</th>').appendTo(table_row);
$('<th>' + settings.days[(6+x)%7]+'</th>').addClass('goto-nextmonth').appendTo(table_row);
table_head.append(table_row);
this_month.append(table_head);
this_month.append('<tfoot><tr><th colspan="7"> </th></tr></tfoot>');
var table_body = $('<tbody/>');
table_row = $('<tr/>');
//Add remaining days from previous month
for ( var i=s; i>0; i-- ) {
var this_y = (month-1)<0?year-1:year;
table_row.append($el.dayMarkup(0, dm-i+1 , (month+11)%12, this_y, (s-i+x)%7));
}
//Add this month
dm = $el.monthLength(month+1,year);
for(var i=1; i <= dm; i++) {
if ( (s%7) == 0 ) {
table_body.append(table_row.clone());
table_row.empty();
s = 0;
}
table_row.append($el.dayMarkup(1, i , month, year, (s+x)%7));
s++;
}
//Add start of next month
var j=1;
for ( var i=s; i<7; i++ ) {
var this_y = (month+1)>11?year+1:year;
table_row.append($el.dayMarkup(9, j , (month+1)%12, this_y, (i+x)%7));
j++;
}
table_body.append(table_row);
this_month.append(table_body);
return this_month;
}
jQuery.fn.generateCalendar = function(month, year) {
var $el = $(this);
var markup = $el.monthMarkup(month, year).after('<ul class="events"></ul>');
return markup;
}
//Tasks/Events
jQuery.fn.attachEvents = function() {
return this.each(function(){
var $el = $(this);
$el.find('td').each( function(index) {
clickedDate = $el.getCellDate($(this));
if($el.hasEvent(clickedDate)) {
$(this).addClass('date_has_event');
}
});
});
}
jQuery.fn.hasEvent = function(date) {
//Before doing anything here, we need to parse the events from the code
var $el = $(this);
var key = $el.dateToString(date);
return $el.data('events') && $el.data('events')[key] && $el.data('events')[key].length;
}
jQuery.fn.getCellDate = function(dateCell) {
var date = $(dateCell).attr('datetime');
return $(this).stringToDate(date);
}
jQuery.fn.stringToDate = function(dateString) {
var a = dateString.split('-');
return new Date(a[0],(a[1]-1),a[2]);
}
jQuery.fn.dateToString = function(date) {
return date.getFullYear()+"-"+(date.getMonth() + 1)+"-"+date.getDate();
}
jQuery.fn.getEvents = function(date) {
var $el = $(this);
var d = date.getDate();
var m = date.getMonth() + 1; // zero index based
var y = date.getFullYear();
$el.find('.events').empty().append($el.generateEvents(y, m, d));
}
jQuery.fn.getNoEvents = function() {
$(this).find('.events').empty();
$el.find('.events').empty().append("<li class='no-event'>"+$el.data('settings').noEvents+"</li>");
}
jQuery.fn.generateEvents = function(year, month, day) {
var $el = $(this);
var returnable = '';
$.each($el.data('events')[""+year+"-"+month+"-"+day+""], function(index, value){
returnable += '<li><span>' + value.time + '</span>'+value.text+'</li>';
});
return returnable;
}
//DOM events & Calendar Manipulation
jQuery.fn.setBindings = function() {
var $el = $(this);
// Days
$el.find('td').bind("click", function() {
$el.removeSelectedCell();
$(this).addClass('selected');
var clickedDate = $el.getCellDate($(this));
$el.setToday();
if( $(this).hasClass('date_has_event') ) {
$el.getEvents(clickedDate);
} else {
$el.getNoEvents();
}
if(jQT.bars) {
jQT.setPageHeight();
}
if( $(this).hasClass('prevmonth') || $(this).hasClass('nextmonth') ) {
$el.getCalendar({date:clickedDate});
}
});
// load previous Month
$el.find(".goto-prevmonth").bind("click", function() {
$el.loadMonthDelta(-1);
});
// load next Month
$el.find(".goto-nextmonth").bind("click", function() {
$el.loadMonthDelta(1);
});
}
jQuery.fn.removeSelectedCell = function() {
$(this).find('.selected').removeClass('selected');
}
jQuery.fn.setToday = function() {
var $el = $(this);
// var date = $el.data('settings').date;
var date = new Date();
$el.find('td[datetime='+date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()+']').addClass('today');
}
jQuery.fn.setSelected = function(date) {
$el = $(this);
$el.removeSelectedCell();
$el.find('td').each(function() {
var clickedDate = $el.getCellDate($(this));
if( !$(this).hasClass("prevmonth") && !$(this).hasClass("nextmonth") && ($el.sameDay(date, clickedDate)) ) {
$(this).addClass('selected');
if ( $(this).hasClass('date_has_event') ) {
$el.getEvents(date);
} else {
$el.getNoEvents();
}
}
});
$el.setToday();
}
jQuery.fn.sameDay = function(date1, date2) {
return (date1.getDate && date2.getDate) && date1.getDate() == date2.getDate() && date1.getMonth() == date2.getMonth() && date1.getFullYear() == date2.getFullYear()
}
jQuery.fn.loadMonthDelta = function(delta) {
$el = $(this);
if($el.find('.selected').length>=1) {
var day = $(this).stringToDate($el.find('.selected').attr('datetime')).getDate();
} else {
var day = $(this).stringToDate($el.find('.today').attr('datetime')).getDate();
}
var month = $el.find('table').data('month');
var year = $el.find('table').data('year');
var newDay = new Date(year, (month-1)+delta, day);
$el.getCalendar( $.extend({}, $el.data('settings'), {date:newDay}) );
}
});
}
})(jQuery);