-
Notifications
You must be signed in to change notification settings - Fork 69
/
bday-picker.min.js
9 lines (9 loc) · 3.11 KB
/
bday-picker.min.js
1
2
3
4
5
6
7
8
9
/*
* 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(a){var b={"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"]},c=new Date,d=c.getFullYear(),e=c.getMonth()+1,f=c.getDate();a.fn.birthdaypicker=function(c){var e={maxAge:120,minAge:0,futureDates:false,maxYear:d,dateFormat:"middleEndian",monthFormat:"short",placeholder:true,legend:"",defaultDate:false,fieldName:"birthdate",fieldId:"birthdate",hiddenDate:true,onChange:null,tabindex:null};return this.each(function(){if(c){a.extend(e,c)}var f=a("<fieldset class='birthday-picker'></fieldset>"),g=a("<select class='birth-year' name='birth[year]'></select>"),h=a("<select class='birth-month' name='birth[month]'></select>"),i=a("<select class='birth-day' name='birth[day]'></select>");if(e["legend"]){a("<legend>"+e["legend"]+"</legend>").appendTo(f)}var j=e["tabindex"];if(e["dateFormat"]=="bigEndian"){f.append(g).append(h).append(i);if(j!=null){g.attr("tabindex",j);h.attr("tabindex",j++);i.attr("tabindex",j++)}}else if(e["dateFormat"]=="littleEndian"){f.append(i).append(h).append(g);if(j!=null){i.attr("tabindex",j);h.attr("tabindex",j++);g.attr("tabindex",j++)}}else{f.append(h).append(i).append(g);if(j!=null){h.attr("tabindex",j);i.attr("tabindex",j++);g.attr("tabindex",j++)}}if(e["placeholder"]){a("<option value='0'>Year:</option>").appendTo(g);a("<option value='0'>Month:</option>").appendTo(h);a("<option value='0'>Day:</option>").appendTo(i)}var k;if(e["defaultDate"]){var l=new Date(e["defaultDate"]),m=l.getFullYear(),n=l.getMonth()+1,o=l.getDate();k=m+"-"+n+"-"+o}if(e["hiddenDate"]){a("<input type='hidden' name='"+e["fieldName"]+"'/>").attr("id",e["fieldId"]).val(k).appendTo(f)}var p=d-e["minAge"];var q=d-e["maxAge"];if(e["futureDates"]&&e["maxYear"]!=d){if(e["maxYear"]>1e3){p=e["maxYear"]}else{p=d+e["maxYear"]}}while(p>=q){a("<option></option>").attr("value",p).text(p).appendTo(g);p--}for(var r=0;r<12;r++){a("<option></option>").attr("value",r+1).text(b[e["monthFormat"]][r]).appendTo(h)}for(var s=1;s<32;s++){a("<option></option>").attr("value",s).text(s).appendTo(i)}a(this).append(f);if(e["defaultDate"]){var t=new Date(e["defaultDate"]);g.val(t.getFullYear());h.val(t.getMonth()+1);i.val(t.getDate())}f.change(function(){var c=new Date,d=c.getFullYear(),f=c.getMonth()+1,j=c.getDate(),l=g.val(),m=h.val(),n=i.val(),o=(new Date(l,m,0)).getDate(),p=parseInt(h.children(":last").val()),q=parseInt(i.children(":last").val());if(q>o){while(q>o){i.children(":last").remove();q--}}else if(q<o){while(q<o){q++;i.append("<option value="+q+">"+q+"</option>")}}if(!e["futureDates"]&&l==d){if(p>f){while(p>f){h.children(":last").remove();p--}i.children(":first").attr("selected","selected")}}if(l!=d&&p!=12){while(p<12){h.append("<option value="+(p+1)+">"+b[e["monthFormat"]][p]+"</option>");p++}}if(l*m*n!=0){k=l+"-"+m+"-"+n;a(this).find("#"+e["fieldId"]).val(k);if(e["onChange"]!=null){e["onChange"](k)}}})})}})(jQuery)