-
Notifications
You must be signed in to change notification settings - Fork 0
/
thingerly-calendar.js
657 lines (521 loc) · 15.2 KB
/
thingerly-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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
Copyright (C) 2010 by Thingerly, Rob Flynn <rob@thingerly.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
(function($) {
// Class methods
// TODO: i18n
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'];
var months_abbr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
// TODO: Maybe make these changable later
var days_of_week = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
var days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var methods = {
'init' : function(params) {
return this.each(function()
{
var $this = $(this);
// Some Constants
var now = new Date();
// Default options
var options = {
'month' : now.getMonth(),
'year' : now.getFullYear(),
'transition' : 'slide',
'viewTransition' : 'zoom',
'speed' : 300,
'dayClick' : null,
'eventClick' : null,
'events' : []
};
// Merge in the user params
if (params)
{
$.extend(options, params);
}
var data = $this.data('thingerlyCalendar');
// If this is our first run on the element, set up some things
if (!data)
{
var calendar = $('<div />');
calendar.addClass('tc-wrapper');
var outer = $('<div />');
outer.addClass("tc-calendar");
outer.append(calendar);
renderSkeleton(calendar);
$this.append(outer);
var parsedEvents = [];
for (i = 0; i < options.events.length; i++)
{
var evt = options.events[i];
var evtdate = new Date(Date.parse(evt));
parsedEvents[parsedEvents.length] = evtdate;
}
$(this).data('thingerlyCalendar', {
'target' : $this,
'calendar' : calendar,
'view' : 'days',
'options' : options,
'now' : now,
'events' : parsedEvents
});
cdate = new Date(options.year, options.month, 1);
var view = getView($this, 'days', cdate);
jQuery('.tc-body', $this).replaceWith(view);
// Bind our buttons
jQuery(".tc-header-l", this).bind('click.thingerlyCalendar', function() {
var data = $this.data('thingerlyCalendar');
var cdate = getPageDate('prev', data);
var new_view = getView($this, data.view, cdate);
data.options.month = cdate.getMonth();
data.options.year = cdate.getFullYear();
showView($this, new_view, data.options.transition, 'prev');
});
jQuery(".tc-header-r", this).bind('click.thingerlyCalendar', function() {
var data = $this.data('thingerlyCalendar');
var cdate = getPageDate('next', data);
data.options.month = cdate.getMonth();
data.options.year = cdate.getFullYear();
var new_view = getView($this, data.view, cdate);
showView($this, new_view, data.options.transition, 'next');
});
jQuery(".tc-header-t", this).bind('click.thingerlyCalendar', function() {
var data = $this.data('thingerlyCalendar');
switch (data.view)
{
case 'days' : {
data.view = 'months';
view = getView($this, 'months', new Date(data.options.year, data.options.month, 1));
showView($this, view, data.options.viewTransition, 'out');
break;
}
case 'months' : {
data.view = 'years';
view = getView($this, 'years', new Date(data.options.year, data.options.month, 1));
showView($this, view, data.options.viewTransition, 'out');
break;
}
}
});
}
});
},
'addEvent': function(event_date)
{
var data = $(this).data('thingerlyCalendar');
data.events[data.events.length] = event_date;
p_event_date = new Date(Date.parse(event_date));
// TODO: This needs to be DRYed.
jQuery('.tc-day', data.calendar).each(function () {
if ($(this).attr('date') == p_event_date)
{
if (!$(this).hasClass("tc-event"))
{
$(this).addClass("tc-event");
if (data.options.eventClick)
{
$(this).bind('click.thingerlyCalendar', function() {
data.options.eventClick($(this).attr("date"));
});
}
}
}
});
}
};
function getPageDate(direction, data)
{
var cm = data.options.month;
var cy = data.options.year;
if (direction == 'prev')
{
if (data.view == 'days')
{
var pm = cm == 0 ? 11 : cm - 1;
var py = pm == 11 ? cy - 1 : cy;
return new Date(py, pm, 1);
}
else if (data.view == 'months')
{
return new Date(cy-1, cm, 1);
}
else if (data.view == 'years')
{
return new Date(cy-12, cm, 1);
}
}
else if (direction = 'next')
{
if (data.view == 'days')
{
var nm = cm == 11 ? 0 : cm + 1;
var ny = nm == 0 ? cy + 1 : cy;
return new Date(ny, nm, 1);
}
else if (data.view == 'months')
{
return new Date(cy+1, cm, 1);
}
else if (data.view == 'years')
{
return new Date(cy+12, cm, 1);
}
}
}
function showView(cal, view, transType, direction)
{
var old_view = jQuery('.tc-body', cal);
var new_view = view;
switch (transType)
{
case 'fade': {
transitionFade(cal, old_view, new_view, direction);
break;
}
case 'zoom': {
transitionZoom(cal, old_view, new_view, direction);
break;
}
case 'slide':
default: {
transitionSlide(cal, old_view, new_view, direction);
break;
}
}
}
function transitionSlide(cal, orig, repl, direction)
{
var data = cal.data('thingerlyCalendar');
repl.hide();
data.calendar.append(repl);
hdir = 'left';
sdir = 'right';
if (direction == 'prev' || direction == 'out')
{
hdir = 'right';
sdir = 'left';
}
orig.hide('slide', { 'direction' : hdir }, data.options.speed, function() {
orig.remove();
});
repl.show('slide', { 'direction' : sdir }, data.options.speed);
}
function transitionZoom(cal, orig, repl, direction)
{
var data = cal.data('thingerlyCalendar');
if (direction == 'prev' || direction == 'out')
{
per = 0;
orig.css("z-index", 5);
repl.css("z-index", 4);
data.calendar.append(repl);
orig.hide('scale', {percent: per}, data.options.speed, function() {
orig.remove();
});
}
else
{
per = 100;
orig.css("z-index", 4);
repl.css("z-index", 5);
repl.hide();
data.calendar.append(repl);
repl.show('scale', {percent: per}, data.options.speed, function() {
orig.remove();
});
}
// repl.show('scale', { 'direction' : 'right' }, data.options.speed);
}
function transitionFade(cal, orig, repl, direction)
{
var data = cal.data('thingerlyCalendar');
repl.fadeTo(1, 0, function() {
data.calendar.append(repl);
orig.fadeTo(data.options.speed, 0);
repl.fadeTo(data.options.speed, 1.0, function() {
orig.remove();
});
});
}
function getView(cal, type, d)
{
view = $('<div />');
view.addClass("tc-body");
switch (type)
{
case 'days': {
return renderDayView(cal, view, d);
}
case 'months': {
return renderMonthView(cal, view, d);
}
case 'years': {
return renderYearView(cal, view, d);
}
default: {
return renderDayView(cal, view, d);
}
}
}
function setCalendarTitle(cal, s)
{
jQuery('.tc-header-t', cal).html(s);
}
function renderYearView(cal, view, d)
{
var data = cal.data('thingerlyCalendar');
var curYear = d.getFullYear();
var startYear = curYear - 6;
var endYear = curYear + 5;
var year = startYear;
for (row = 0; row < 3; row++)
{
bigRow = $("<div/>");
bigRow.addClass("tc-row-bg");
if (row % 2 == 1)
{
bigRow.addClass("tc-odd");
}
else
{
bigRow.addClass("tc-even");
}
for (i = 0; i < 4; i++)
{
yDiv = $("<div />");
yDiv.addClass("tc-cell");
yDiv.addClass("tc-year");
yDiv.html(year);
yDiv.attr('y-no', year);
yDiv.bind('click.thingerlyCalendar', function() {
selectYear(cal, parseInt($(this).attr('y-no'), 10));
});
year++;
bigRow.append(yDiv);
}
view.append(bigRow);
}
setCalendarTitle(cal, startYear + " - " + endYear);
return view
}
function renderMonthView(cal, view, d)
{
var data = cal.data('thingerlyCalendar');
count = 0;
for (row = 0; row < 3; row++)
{
bigRow = $("<div/>");
bigRow.addClass("tc-row-bg");
if (row % 2 == 1)
{
bigRow.addClass("tc-odd");
}
else
{
bigRow.addClass("tc-even");
}
for (i = 0; i < 4; i++)
{
moDiv = $("<div />");
moDiv.addClass("tc-cell");
moDiv.addClass("tc-month");
moDiv.html(months_abbr[count]);
moDiv.attr('mo-no', count);
moDiv.attr('y-no', d.getFullYear());
moDiv.bind('click.thingerlyCalendar', function() {
selectMonth(cal, parseInt($(this).attr('mo-no'), 10), parseInt($(this).attr('y-no'), 10));
});
count++;
bigRow.append(moDiv);
}
view.append(bigRow);
}
setCalendarTitle(cal, (d.getFullYear()).toString());
return view
}
function selectYear(cal, year)
{
var data = cal.data('thingerlyCalendar');
data.view = 'months';
data.options.year = year;
var view = getView(cal, 'months', new Date(year, data.options.month, 1));
showView(cal, view, data.options.viewTransition, 'in');
}
function selectMonth(cal, month, year)
{
view = getView(cal, 'days', new Date(year, month, 1));
var data = cal.data('thingerlyCalendar');
data.view = 'days';
data.options.year = year;
data.options.month = month;
showView(cal, view, data.options.viewTransition, 'in');
}
function renderDayView(cal, view, d)
{
var data = cal.data('thingerlyCalendar');
// Render the month header, this consists of listing days of the week
v_head = $('<div />');
v_head.addClass("tc-month-h tc-row-sm tc-cf");
for (i = 0; i < days_of_week.length; i++)
{
dayDiv = $('<div />');
dayDiv.addClass("tc-cell tc-sub-h");
dayDiv.html(days_of_week[i]);
v_head.append(dayDiv);
}
view.append(v_head);
// Now we need to figure out how many days are in the month
// we are viewing, as well as how many were in last month.
// (We need last month's since we need to populate empty spaces
// on the calendar)
month = d.getMonth();
year = d.getFullYear();
// When did this month begin?
first_date = new Date(year, month, 1);
first_day = first_date.getDay();
var num_days = getNumDays(month, year);
// ANd now for the previous month
prev_month = month == 0 ? 11 : month - 1;
prev_year = prev_month == 11 ? year - 1 : year;
prev_num_days = getNumDays(prev_month, prev_year);
next_month = month == 11 ? 0 : month + 1;
next_year = next_month == 0 ? year + 1 : year;
// Set up some counters
var day_counter = 0, last_month_days = 0, next_month_days = 0;
// We will always use 6 week calendars. This will handle any number of weeks
// in a month and will keep the calendars from being different sizes.
for (week = 0; week < 6; week++)
{
weekDiv = $("<div />");
weekDiv.addClass("tc-week tc-row-sm tc-cf");
if (week % 2 == 0)
{
weekDiv.addClass("tc-even");
}
else
{
weekDiv.addClass("tc-odd");
}
// Draw each day
for (i = 0; i < 7; i++)
{
dayDiv = $("<div />");
dayDiv.addClass("tc-cell tc-day");
dayText = day_counter - last_month_days + 1;
if (day_counter < first_day)
{
// The month hasn't begun yet, show last month
dayDiv.addClass("tc-grey");
dayText = prev_num_days - first_day + day_counter + 1;
last_month_days++;
dayDiv.attr('date', new Date(prev_year, prev_month, dayText));
}
else if (dayText > num_days)
{
// We're out of the current month and into the next
dayDiv.addClass("tc-grey");
dayText = ++next_month_days;
dayDiv.attr('date', new Date(next_year, next_month, dayText));
}
else
{
if (dayText == data.now.getDate() && d.getFullYear() == data.now.getFullYear() && d.getMonth() == data.now.getMonth())
{
dayDiv.addClass("tc-today");
}
dayDiv.attr('date', new Date(year, month, dayText));
}
var cDayDate = dayDiv.attr('date');
for (var _i = 0; _i < data.events.length; _i++)
{
if (cDayDate == data.events[_i])
{
dayDiv.addClass("tc-event");
}
}
dayDiv.html(dayText);
weekDiv.append(dayDiv);
// increment our day counter
day_counter++;
}
// Append the week
view.append(weekDiv);
}
if (data.options.dayClick)
{
jQuery('.tc-day', view).bind('click.thingerlyCalendar', function() {
data.options.dayClick($(this).attr("date"));
});
}
if (data.options.eventClick)
{
jQuery('.tc-event', view).bind('click.thingerlyCalendar', function() {
data.options.eventClick($(this).attr("date"));
});
}
setCalendarTitle(cal, months[d.getMonth()] + " - " + (d.getFullYear()));
return view;
}
function renderSkeleton(el)
{
// Calendar Header
header = $('<div />');
header.addClass("tc-header tc-row-sm tc-cf");
leftButton = $('<div />', {
});
leftButton.addClass("tc-header-l");
middleButton = $('<div />');
middleButton.addClass("tc-header-t");
rightButton = $('<div />', {
});
rightButton.addClass("tc-header-r");
// Append buttons to header
header.append(leftButton);
header.append(middleButton);
header.append(rightButton);
body = $('<div />');
body.addClass("tc-body tc-cf");
// Append Header and Body to the wrapper
el.append(header);
el.append(body);
}
function getNumDays(month, year)
{
if ((month == 1) && (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)))
{
return 29;
}
return days_in_month[month];
}
// Set up our plugin
$.fn.thingerlyCalendar = function(method)
{
if (methods[method])
{
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else if (typeof method === 'object' || !method)
{
return methods.init.apply(this, arguments);
}
else
{
$.error('Method ' + method + ' does not exist for thingerlyCalendar');
}
};
})(jQuery);