-
Notifications
You must be signed in to change notification settings - Fork 69
/
bday-picker.js
197 lines (176 loc) · 7.38 KB
/
bday-picker.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
/*!
* jQuery Birthday Picker: v1.4 - 10/16/2011
* http://abecoffman.com/stuff/birthdaypicker
*
* Copyright (c) 2010 Abe Coffman
* Dual licensed under the MIT and GPL licenses.
*
*/
(function( $ ){
// plugin variables
var months = {
"short": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
"long": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] },
todayDate = new Date(),
todayYear = todayDate.getFullYear(),
todayMonth = todayDate.getMonth() + 1,
todayDay = todayDate.getDate();
$.fn.birthdaypicker = function( options ) {
var settings = {
"maxAge" : 120,
"minAge" : 0,
"futureDates" : false,
"maxYear" : todayYear,
"dateFormat" : "middleEndian",
"monthFormat" : "short",
"placeholder" : true,
"legend" : "",
"defaultDate" : false,
"fieldName" : "birthdate",
"fieldId" : "birthdate",
"hiddenDate" : true,
"onChange" : null,
"tabindex" : null
};
return this.each(function() {
if (options) { $.extend(settings, options); }
// Create the html picker skeleton
var $fieldset = $("<fieldset class='birthday-picker'></fieldset>"),
$year = $("<select class='birth-year' name='birth[year]'></select>"),
$month = $("<select class='birth-month' name='birth[month]'></select>"),
$day = $("<select class='birth-day' name='birth[day]'></select>");
if (settings["legend"]) { $("<legend>" + settings["legend"] + "</legend>").appendTo($fieldset); }
var tabindex = settings["tabindex"];
// Deal with the various Date Formats
if (settings["dateFormat"] == "bigEndian") {
$fieldset.append($year).append($month).append($day);
if (tabindex != null) {
$year.attr('tabindex', tabindex);
$month.attr('tabindex', tabindex++);
$day.attr('tabindex', tabindex++);
}
} else if (settings["dateFormat"] == "littleEndian") {
$fieldset.append($day).append($month).append($year);
if (tabindex != null) {
$day.attr('tabindex', tabindex);
$month.attr('tabindex', tabindex++);
$year.attr('tabindex', tabindex++);
}
} else {
$fieldset.append($month).append($day).append($year);
if (tabindex != null) {
$month.attr('tabindex', tabindex);
$day.attr('tabindex', tabindex++);
$year.attr('tabindex', tabindex++);
}
}
// Add the option placeholders if specified
if (settings["placeholder"]) {
$("<option value='0'>Year:</option>").appendTo($year);
$("<option value='0'>Month:</option>").appendTo($month);
$("<option value='0'>Day:</option>").appendTo($day);
}
var hiddenDate;
if (settings["defaultDate"]) {
var defDate = new Date(settings["defaultDate"] + "T00:00:00"),
defYear = defDate.getFullYear(),
defMonth = defDate.getMonth() + 1,
defDay = defDate.getDate();
if (defMonth<10) defMonth="0"+defMonth;
if (defDay<10) defDay="0"+defDay;
hiddenDate = defYear + "-" + defMonth + "-" + defDay;
}
// Create the hidden date markup
if (settings["hiddenDate"]) {
$("<input type='hidden' name='" + settings["fieldName"] + "'/>")
.attr("id", settings["fieldId"])
.val(hiddenDate)
.appendTo($fieldset);
}
// Build the initial option sets
var startYear = todayYear - settings["minAge"];
var endYear = todayYear - settings["maxAge"];
if (settings["futureDates"] && settings["maxYear"] != todayYear) {
if (settings["maxYear"] > 1000) { startYear = settings["maxYear"]; }
else { startYear = todayYear + settings["maxYear"]; }
}
for (var i=startYear; i>=endYear; i--) { $("<option></option>").attr("value", i).text(i).appendTo($year); }
for (var j=0; j<12; j++) { $("<option></option>").attr("value", j+1).text(months[settings["monthFormat"]][j]).appendTo($month); }
for (var k=1; k<32; k++) { $("<option></option>").attr("value", k).text(k).appendTo($day); }
$(this).append($fieldset);
// Set the default date if given
if (settings["defaultDate"]) {
var date = new Date(settings["defaultDate"] + "T00:00:00");
$year.val(date.getFullYear());
$month.val(date.getMonth() + 1);
$day.val(date.getDate());
}
// Update the option sets according to options and user selections
$fieldset.change(function() {
// todays date values
var todayDate = new Date(),
todayYear = todayDate.getFullYear(),
todayMonth = todayDate.getMonth() + 1,
todayDay = todayDate.getDate(),
// currently selected values
selectedYear = parseInt($year.val(), 10),
selectedMonth = parseInt($month.val(), 10),
selectedDay = parseInt($day.val(), 10),
// number of days in currently selected year/month
actMaxDay = (new Date(selectedYear, selectedMonth, 0)).getDate(),
// max values currently in the markup
curMaxMonth = parseInt($month.children(":last").val()),
curMaxDay = parseInt($day.children(":last").val());
// Dealing with the number of days in a month
// http://bugs.jquery.com/ticket/3041
if (curMaxDay > actMaxDay) {
while (curMaxDay > actMaxDay) {
$day.children(":last").remove();
curMaxDay--;
}
} else if (curMaxDay < actMaxDay) {
while (curMaxDay < actMaxDay) {
curMaxDay++;
$day.append("<option value=" + curMaxDay + ">" + curMaxDay + "</option>");
}
}
// Dealing with future months/days in current year
// or months/days that fall after the minimum age
if (!settings["futureDates"] && selectedYear == startYear) {
if (curMaxMonth > todayMonth) {
while (curMaxMonth > todayMonth) {
$month.children(":last").remove();
curMaxMonth--;
}
// reset the day selection
$day.children(":first").attr("selected", "selected");
}
if (selectedMonth === todayMonth) {
while (curMaxDay > todayDay) {
$day.children(":last").remove();
curMaxDay -= 1;
}
}
}
// Adding months back that may have been removed
// http://bugs.jquery.com/ticket/3041
if (selectedYear != startYear && curMaxMonth != 12) {
while (curMaxMonth < 12) {
$month.append("<option value=" + (curMaxMonth+1) + ">" + months[settings["monthFormat"]][curMaxMonth] + "</option>");
curMaxMonth++;
}
}
// update the hidden date
if ((selectedYear * selectedMonth * selectedDay) != 0) {
if (selectedMonth<10) selectedMonth="0"+selectedMonth;
if (selectedDay<10) selectedDay="0"+selectedDay;
hiddenDate = selectedYear + "-" + selectedMonth + "-" + selectedDay;
$(this).find('#'+settings["fieldId"]).val(hiddenDate);
if (settings["onChange"] != null) {
settings["onChange"](hiddenDate);
}
}
});
});
};
})( jQuery );