-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyui.calendar.js
103 lines (100 loc) · 3.39 KB
/
yui.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
/**
* Initialize YUI Calendar widgets
*
* @author Chad Kieffer, ckieffer at gmail dot com
*
* Based on and inspired by posts from Brad Harris http://bit.ly/aOmbYA
* and Christian Heilmann bit.ly/aFyJ20
*
* @see http://developer.yahoo.com/yui/calendar/
* @see http://2tbsp.com/yui_calendar_widget
*
* @todo Properly set the current date value on the calendar for the current field
* Progressive enhancement
* @todo Add "choose" button/link dynamically through the widget
* @todo Apply the calendar container div to the input instead of requiring it be hardcoded
* Interactivity/Accessibility
* @todo Close the calendar when toggle button is clicked a second time, or clicking anywhere
* @todo Trigger the date picker when a date field gains focus
* @todo Evaluate and add accessibility features
*/
YuiCalendar = {
// Default YUI calendar widget options
// Used if yuiConfig.calendar isn't set
config: {
title: 'Choose a date:',
close: true,
navigator: true
},
dateFormat: 'MM/DD/YYYY',
calendarWidget: null,
container: 'calendar_container',
calendarID: 'yui_calendar',
activeInput: null,
btn: null,
btnContainer: null,
btnContainerClass: 'btn_container',
btnClassRegEx: new RegExp('choose_date'),
initialize: function() {
var self = YuiCalendar;
self.setConfig();
self.renderContainer();
YAHOO.util.Event.delegate(this, 'click', self.click, 'button');
},
click: function(e) {
var self = YuiCalendar;
self.btn = YAHOO.util.Event.getTarget(e);
if (self.btn.id.match(self.btnClassRegEx)) {
self.btnContainer = YAHOO.util.Dom.getAncestorByClassName(self.btn, self.btnContainerClass);
self.activeInput = YAHOO.util.Dom.getPreviousSibling(self.btnContainer);
if (self.calendarWidget === null) {
self.renderCalendar();
}
self.calendarWidget.show();
self.positionCalendar();
}
},
setConfig: function() {
if (typeof(yuiConfig) != 'undefined' && typeof(yuiConfig.calendar) != 'undefined') {
this.config = yuiConfig.calendar;
if (typeof(yuiConfig.calendar.dateFormat) == 'undefined') {
this.config.dateFormat = this.dateFormat;
}
} else {
this.config = YuiCalendar.config;
}
},
renderContainer: function() {
var container = document.createElement('div');
container.id = this.container;
container.style.display = 'none';
document.body.appendChild(container);
},
renderCalendar: function() {
this.calendarWidget = new YAHOO.widget.Calendar(
this.calendarID,
this.container,
this.config
);
this.calendarWidget.selectEvent.subscribe(this.populateDateField, this, true);
this.calendarWidget.render();
},
positionCalendar: function() {
var position = YAHOO.util.Dom.getXY(YAHOO.util.Dom.get(this.activeInput));
position[1] = position[1] + 25;
YAHOO.util.Dom.setXY(this.container, position);
},
populateDateField: function() {
var date = this.calendarWidget.getSelectedDates()[0];
var formattedDate = this.config.dateFormat.replace(/MM/i, date.getMonth());
formattedDate = formattedDate.replace(/DD/i, date.getDate());
formattedDate = formattedDate.replace(/YYYY/i, date.getFullYear());
YAHOO.util.Dom.get(this.activeInput).value = formattedDate;
this.calendarWidget.hide();
},
hide: function() {
if (this.calendarWidget !== null) {
this.calendarWidget.hide();
}
}
};