-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjquery.calendar.js
382 lines (352 loc) · 9.17 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
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
/**
* jQuery.calendar
*
* @version 1.0.4
* @author rew <rewish.org@gmail.com>
* @link http://rewish.org/javascript/jquery_calendar
* @license http://rewish.org/license/mit The MIT License
*/
(function($) {
$.fn.calendar = function(option) {
return this.each(function() {
(new Calendar).init($(this), option).build().show();
});
};
var _weekName = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
var _today = new Date;
function Calendar() {}
Calendar.prototype = $.extend({
init: function(elem, option) {
this.setOption(option);
this.elem = $('<div/>')
.addClass(this.option.cssClass)
.css('z-index', 2);
this.wrap = $('<div/>')
.append(this.elem)
.css({
position: 'relative',
overflow: 'hidden'
});
elem.append(this.wrap);
this.view = {};
this.preloadEvents = {};
return this
.buildNavi()
.buildTable()
.buildCaption()
.buildTodayLink();
},
setOption: function(option) {
if (this.option && !option) {
return this;
}
if (this.option) {
$.extend(this.option, option);
return this;
}
this.option = $.extend({
lang : 'ja',
year : _today.getFullYear(),
month: _today.getMonth() + 1,
week: {
en: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
ja: ['\u65e5', '\u6708', '\u706b', '\u6c34', '\u6728', '\u91d1', '\u571f']
},
caption: {
en: '%Y-%M',
ja: '%Y\u5e74%M\u6708'
},
navi: {
en: ['Prev', 'Next'],
ja: ['\u524d\u306e\u6708', '\u6b21\u306e\u6708']
},
todayLink: {
en: 'Today [%Y-%M-%D]',
ja: '\u4eca\u65e5 [%Y\u5e74%M\u6708%D\u65e5]'
},
moveTime : 700,
events : {},
hideOther: false,
cssClass : 'jqueryCalendar',
// Callback functions
addDay : function() {},
addEvent: function(td, evt) {
var elem = typeof evt.url !== 'undefined'
? $('<a/>').attr('href', evt.url) : $('<span/>');
if (evt.id) {
elem.attr('id', 'event-' + evt.id);
}
if (evt.title) {
elem.attr('title', evt.title);
}
elem.text(td.text());
td.text('').append(elem).addClass('event');
},
beforeMove : function() {},
afterMove : function() {},
preloadEvent: function() {}
}, option);
return this;
},
buildNavi: function() {
if (!this.option.navi) {
return this;
}
var self = this;
var list = function(className, number, text) {
var date = new Date(self.option.year, (self.option.month + number) - 1, 1);
var link = $('<a/>')
.text(text)
.attr('href', 'javascript:void(0)')
.click(function() {
self.move(number);
return false;
});
return $('<li/>').addClass(className).append(link);
};
var text = typeof this.option.navi === 'object'
? this.option.navi[this.option.lang] : this.option.navi;
this.elem.append(
$('<ul/>')
.addClass('navi')
.append(list('prev', -1, text[0]),
list('next', 1, text[1]))
);
return this;
},
buildTable: function() {
this.tr = $('<tr/>');
this.td = $('<td/>');
// table
this.table = $('<table/>');
// thead
var week = [];
var weekName = this.option.week[this.option.lang] || this.option.week;
for (var i = 0, w; w = weekName[i]; i++) {
week[week.length] = '<th class="'+ _weekName[i] +'">'+ w +'</th>';
}
this.thead = $('<thead/>').append(this.tr.clone().html(week.join('')))
// tbody
this.tbody = $('<tbody/>');
this.elem.append(
$('<div/>')
.addClass('main')
.append(
this.table
.addClass('calendar')
.append(this.thead)
.append(this.tbody)
)
);
return this;
},
buildCaption: function() {
if (this.option.caption && !this.caption) {
this.caption = $('<div/>').addClass('caption');
this.table.before(this.caption);
}
return this;
},
buildTodayLink: function() {
var date = this.getKey(_today).split('-');
var linkText = typeof this.option.todayLink === 'object'
? this.option.todayLink[this.option.lang] : this.option.todayLink;
var self = this;
this.table.after(
$('<div/>').addClass('todayLink').append(
$('<a/>')
.text(
linkText
.replace(/%Y/i, date[0])
.replace(/%M/i, date[1])
.replace(/%D/i, date[2])
)
.attr('href', 'javascript:void(0)')
.click(function() {
self.option.year = _today.getFullYear();
self.option.month = _today.getMonth() + 1;
self.rebuild().show().resetWrap();
})
)
);
return this;
},
build: function() {
this.prevFill();
this.current = new Date(this.option.year, this.option.month - 1, 1);
var last = new Date(this.option.year, this.option.month, 0).getDate();
for (var day = 1; day <= last; day++) {
this.current.setDate(day);
this.option.day = day;
this.option.addDay(this.addDay(this.current, 'currentMonth'));
}
this.option.day = null;
this.nextFill();
this.addEvent();
return this;
},
rebuild: function() {
this.tbody.empty();
this.view = {};
return this.build();
},
prevFill: function() {
var
prev = new Date(this.option.year, this.option.month - 1, 0),
last = prev.getDate(),
day = last - prev.getDay();
if (last - day >= 6) return this;
for (; day <= last; day++) {
prev.setDate(day);
this.addDay(prev, 'otherMonth', this.option.hideOther);
}
return this;
},
nextFill: function() {
var
next = new Date(this.option.year, this.option.month, 1),
last = 7 - next.getDay();
if (last >= 7) return this;
for (var day = 1; day <= last; day++) {
next.setDate(day);
this.addDay(next, 'otherMonth', this.option.hideOther);
}
return this;
},
addDay: function(date, className, hide) {
var key = className === 'otherMonth'
? className + this.getKey(date)
: this.getKey(date);
this.view[key] = this.td.clone()
.addClass(className)
.addClass(_weekName[date.getDay()]);
// White space for (IE <= 7) "empty-cells" fix
this.view[key].text(hide ? ' ' : date.getDate());
if (key !== 'otherMonth') {
this.view[key].attr('id', ['calendar', this.getKey(date)].join('-'));
}
return this.view[key];
},
getKey: function(date, returnArray) {
if (typeof date === 'string') {
date = date.split('-');
}
var key = [
date[0] || date.getFullYear(),
('0' + (date[1] || date.getMonth() + 1)).slice(-2),
('0' + (date[2] || date.getDate())).slice(-2)
];
if (returnArray === true) {
return key;
}
return key.join('-')
},
addEvent: function() {
var self = this;
$.each(self.option.events, function(date, event) {
var td = self.view[self.getKey(date)];
try {
self.option.addEvent(td, event);
} catch(e) {}
});
return this;
},
show: function() {
var today = this.getKey(_today), tr, count = 0, self = this;
$.each(self.view, function(key) {
if (count % 7 === 0 || count === 0) {
tr = count % 2 === 0
? self.tr.clone().addClass('even')
: self.tr.clone().addClass('odd');
self.tbody.append(tr);
}
if (key === today && !key.match('otherMonth')) {
this.addClass('today');
}
tr.append(this);
count++;
});
this.setCaption();
try {
var date = this.getKey(this.current).split('-');
this.preloadEvents = this.option.preloadEvent(date[0], date[1]);
} catch(e) {}
return this;
},
setCaption: function() {
if (!this.option.caption) {
return this;
}
this.caption.text(this.getCaption(this.current));
return this;
},
getCaption: function(date) {
date = this.getKey(date).split('-');
var caption = typeof this.option.caption === 'object'
? this.option.caption[this.option.lang] : this.option.caption;
return caption
.replace(/%Y/i, date[0])
.replace(/%M/i, date[1])
},
move: function(number) {
var width = this.elem.innerWidth();
var pos = this.elem.position();
var clone = this.elem.clone().css({
position: 'absolute',
top: pos.top + 'px',
left: pos.left + 'px',
zIndex: 1
});
this.resetWrap();
this.wrap.append(clone);
// Changing month
this.option.month = this.option.month + number;
// Before callback
var date = new Date(this.option.year, this.option.month - 1, 1);
date = this.getKey(date, true);
this.option.beforeMove(this.option, date[0], date[1]);
// Set Event
this.setPreloadEvent(number);
this.rebuild().show();
// Moving animation
var time = this.option.moveTime;
this.wrap.animate({height: this.elem.innerHeight()}, time);
// Next moving
if ((number + '').charAt(0) !== '-') {
clone.animate({marginLeft: '-' + width + 'px'}, time, function() {
clone.remove();
});
// Prev moving
} else {
this.elem.css({
position: 'absolute',
marginLeft: '-' + width + 'px'
});
var self = this;
this.elem.animate({marginLeft: 0}, time, function() {
clone.remove();
self.elem.css('position', 'static');
});
}
this.option.afterMove(this.option, date[0], date[1]); // Callback
},
resetWrap: function() {
this.wrap.css({
width : this.elem.innerWidth() + 'px',
height: this.elem.innerHeight() + 'px'
});
return this;
},
setPreloadEvent: function(number) {
var type = number === 1 ? 'next' : 'prev';
try {
if (typeof this.preloadEvents[type] === 'object') {
return this.option.events = this.preloadEvents[type];
}
if (typeof this.preloadEvents === 'object') {
return this.option.events = this.preloadEvents;
}
} catch(e) {}
}
}, Calendar.prototype);
})(jQuery);