forked from sebastianknopf/jQuery-Calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.calendar.js
174 lines (138 loc) · 5.8 KB
/
jquery.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
/*
* jQuery-Calendar Plugin v1.1.0
*
* 2018 (c) Sebastian Knopf
* This software is licensed under the MIT license!
* View LICENSE.md for more information
*/
(function ($) {
$.fn.calendar = function (opts) {
var options = $.extend({
color: '#308B22',
months: ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
days: ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'],
onSelect: function (event) {}
}, $.fn.calendar.defaults, opts);
return this.each(function () {
var currentYear, currentMonth, currentDay, currentCalendar;
initCalendar($(this), options);
});
};
function initCalendar(wrapper, options) {
var color = options.color;
wrapper.addClass('calendar').empty();
var header = $('<header>').appendTo(wrapper);
header.addClass('calendar-header');
header.css({
background: color,
color: createContrast(color)
});
var buttonLeft = $('<span>').appendTo(header);
buttonLeft.addClass('button').addClass('left');
buttonLeft.html(' ⟨ ');
buttonLeft.bind('click', function () { currentCalendar = $(this).parents('.calendar'); selectMonth(false, options); });
buttonLeft.bind('mouseover', function () { $(this).css('background', createAccent(color, -20)); });
buttonLeft.bind('mouseout', function () { $(this).css('background', color); });
var headerLabel = $('<span>').appendTo(header);
headerLabel.addClass('header-label')
headerLabel.html(' Month Year ');
headerLabel.bind('click', function () {
currentCalendar = $(this).parents('.calendar');
selectMonth(null, options, new Date().getMonth(), new Date().getFullYear());
currentDay = new Date().getDate();
triggerSelectEvent(options.onSelect);
});
var buttonRight = $('<span>').appendTo(header);
buttonRight.addClass('button').addClass('right');
buttonRight.html(' ⟩ ');
buttonRight.bind('click', function () { currentCalendar = $(this).parents('.calendar'); selectMonth(true, options); });
buttonRight.bind('mouseover', function () { $(this).css('background', createAccent(color, -20)); });
buttonRight.bind('mouseout', function () { $(this).css('background', color); });
var dayNames = $('<table>').appendTo(wrapper);
dayNames.append('<thead><th>' + options.days.join('</th><th>') + '</th></thead>');
dayNames.css({
width: '100%'
});
var calendarFrame = $('<div>').appendTo(wrapper);
calendarFrame.addClass('calendar-frame');
headerLabel.click();
}
function selectMonth(next, options, month, year) {
var tmp = currentCalendar.find('.header-label').text().trim().split(' '), tmpYear = parseInt(tmp[1], 10);
currentMonth = month || ((next) ? ((tmp[0] === options.months[options.months.length - 1]) ? 0 : options.months.indexOf(tmp[0]) + 1) : ((tmp[0] === options.months[0]) ? 11 : options.months.indexOf(tmp[0]) - 1));
currentYear = year || ((next && currentMonth === 0) ? tmpYear + 1 : (!next && currentMonth === 11) ? tmpYear - 1 : tmpYear);
var calendar = createCalendar(currentMonth, currentYear, options), frame = calendar.frame();
currentCalendar.find('.calendar-frame').empty().append(frame);
currentCalendar.find('.header-label').text(calendar.label);
frame.on('click', 'td', function () {
$('td').removeClass('selected');
$(this).addClass('selected');
currentDay = $(this).text();
triggerSelectEvent(options.onSelect);
});
}
function createCalendar(month, year, options) {
var currentDay = 1, daysLeft = true,
startDay = new Date(year, month, currentDay).getDay() - 1,
lastDays = [31, (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
calendar = [];
var i = 0;
while(daysLeft) {
calendar[i] = [];
for(var d = 0; d < 7; d++) {
if(i == 0) {
if(d == startDay) {
calendar[i][d] = currentDay++;
startDay++;
} else if (startDay === -1) {
calendar[i][6] = currentDay++;
startDay++;
}
} else if(currentDay <= lastDays[month]) {
calendar[i][d] = currentDay++;
} else {
calendar[i][d] = '';
daysLeft = false;
}
if (currentDay > lastDays[month]) {
daysLeft = false;
}
}
i++;
}
var frame = $('<table>').addClass('current');
var frameBody = $('<tbody>').appendTo(frame);
for(var j = 0; j < calendar.length; j++) {
var frameRow = $('<tr>').appendTo(frameBody);
$.each(calendar[j], function (index, item) {
var frameItem = $('<td>').appendTo(frameRow);
frameItem.text(item);
});
}
$('td:empty', frame).addClass('disabled');
if(currentMonth === new Date().getMonth()) {
$('td', frame).filter(function () { return $(this).text() === new Date().getDate().toString(); }).addClass('today');
}
return { frame: function () { return frame.clone() }, label: options.months[month] + ' ' + year };
}
function triggerSelectEvent(event) {
var date = new Date(currentYear, currentMonth, currentDay);
var label = [];
label[0] = (date.getDate() < 10) ? '0' + date.getDate() : date.getDate();
label[1] = ((date.getMonth() + 1) < 10) ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
label[2] = (date.getFullYear());
if(event != undefined) {
event({date: date, label: label.join('.')});
}
}
function createContrast(color) {
if(color.length < 5) {
color += color.slice(1);
}
return (color.replace('#','0x')) > (0xffffff) ? '#222' : '#fff';
}
function createAccent(color, percent) {
var num = parseInt(color.slice(1),16), amt = Math.round(2.55 * percent), R = (num >> 16) + amt, G = (num >> 8 & 0x00FF) + amt, B = (num & 0x0000FF) + amt;
return '#' + (0x1000000 + (R < 255 ? R < 1 ? 0 : R : 255) * 0x10000 + (G < 255 ? G < 1 ? 0 : G : 255) * 0x100 + (B < 255 ? B < 1 ? 0 : B : 255)).toString(16).slice(1);
}
}(jQuery));