This repository has been archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 170
/
datedropper.min.js
12 lines (8 loc) · 8.32 KB
/
datedropper.min.js
1
2
3
4
5
6
7
8
9
10
11
12
//////////////////////////////////////
// DATEDROPPER Version 1 //
// Last Updates: 15/02/2015 //
// //
// Made with love by //
// Felice Gattuso //
//////////////////////////////////////
$.fn.dateDropper=function(e){if(this.is("input")&&"text"==this.attr("type")){var i=(new Date).getFullYear(),a=(new Date).getDate(),s=(new Date).getMonth(),t=$.extend({animate_current:!0,animation:"fadein",color:"#f87a54",format:"m/d/Y",lang:"en",lock:!1,maxYear:i,minYear:1970,placeholder:"Select date",years_multiple:!1},e),r=this,_=$(".dd_wrap").length+1,o=function(d){return!(d%4||!(d%100)&&d%400)},f=100,u=/^#[0-9A-F]{6}$/i.test(t.color),c=function(d){return d.toString().substr(0,3)+t.years_multiple},v=0;u||(t.color="#f87a54"),t.maxYear<i&&(i=t.maxYear),$('<div class="dd_wrap" id="dd_'+_+'"><div class="dd_overlay"></div><div class="dd_"></div></div>').appendTo("body");var m=$("#dd_"+_),p=m.find(".dd_");dd_overlay=m.find(".dd_overlay"),$(window).on("resize",function(){p.css({top:r.offset().top+(r.height()+12),left:r.offset().left+(r.width()/2-f/2)-2})}),$("<style>#dd_"+_+" .dd_ .dd_submit,#dd_"+_+" .dd_ .dd_r_ ul li { background-color: "+t.color+"; } #dd_"+_+" .dd_ .dd_d_ .dd_sl_ ul li em , #dd_"+_+" .dd_ .dd_d_ .dd_sl_ ul li.dd_sunday{ color: "+t.color+"; }</style>").appendTo("head"),r.attr({readonly:"readonly"}).addClass("dd_locked").val(t.placeholder),p.append('<div class="dd_sw_ dd_m_"><a class="dd_nav_ dd_prev_"></a><a class="dd_nav_ dd_next_"></a><div class="dd_sl_"></div></div>'),p.append('<div class="dd_sw_ dd_d_"><a class="dd_nav_ dd_prev_"></a><a class="dd_nav_ dd_next_"></a><div class="dd_sl_"></div></div>'),p.append('<div class="dd_sw_ dd_y_"><a class="dd_nav_ dd_prev_"></a><a class="dd_nav_ dd_next_"></a><div class="dd_sl_"></div></div>'),t.years_multiple&&p.append('<div class="dd_r_"></div>'),p.append('<div class="dd_submit"></div>');var b=p.find(".dd_m_"),h=p.find(".dd_d_"),C=(p.find(".dd_y_w"),p.find(".dd_y_")),y=p.find(".dd_r_"),g=p.find(".dd_submit");switch(t.lang){case"it":var w=["Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre"],k=["Domenica","Lunedì","Martedì","Mercoledì","Giovedì","Venerdì","Sabato"];break;case"es":var w=["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"],k=["Domingo","Lunes","Martes","Miércoles","Jueves","Viernes","Samedi"];break;case"de":var w=["Januar","Februar","Marz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember"],k=["Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag"];break;case"fr":var w=["Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre"],k=["Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi"];break;default:var w=["January","February","March","April","May","June","July","August","September","October","November","December"],k=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]}b.find(".dd_sl_").append("<ul></ul>");for(var L=1;12>=L;L++)months=w[L-1].substr(0,3),b.find("ul").append('<li value="'+L+'">'+months+"</li>");h.find(".dd_sl_").append("<ul></ul>");for(var S=1;31>=S;S++)ddd=10>S?"0"+S:S,h.find("ul").append('<li value="'+S+'">'+ddd+"<em ></em></li>");C.find(".dd_sl_").append("<ul></ul>");for(var J=t.minYear;J<=t.maxYear;J++)bissextile_return=o(J),C.find("ul").append('<li value="'+J+'" data-filter="'+bissextile_return+'">'+J+"</li>");if(t.years_multiple){y.append("<ul></ul>");for(var Y=t.minYear;Y<=t.maxYear;Y++){var A=Y%t.years_multiple;0==A&&y.find("ul").append('<li value="'+Y+'"></li>')}var T=f/y.find("li").length;y.find("li").css({width:T+"%"}),y.find("li[value="+c(i)+"]").addClass("dd_sltd_")}var z=function(){h.find("li").eq(a-1).addClass("dd_sltd_"),b.find("li").eq(s).addClass("dd_sltd_"),C.find("li[value="+i+"]").addClass("dd_sltd_"),t.years_multiple&&y.find("li[value="+c(i)+"]").addClass("dd_sltd_")},N=function(){b.find(".dd_sl_").animate({scrollLeft:s*f},1200,"swing"),setTimeout(function(){h.find(".dd_sl_").animate({scrollLeft:(a-1)*f},1200,"swing"),setTimeout(function(){C.find(".dd_sl_").animate({scrollLeft:C.find(".dd_sl_ li[value="+i+"]").index()*f},1200,"swing")},200)},400)},O=function(){b.find(".dd_sl_").scrollLeft(s*f),h.find(".dd_sl_").scrollLeft((a-1)*f),C.find(".dd_sl_").scrollLeft(C.find(".dd_sl_ li[value="+i+"]").index()*f)},q=function(){b.find(".dd_sl_").scrollLeft(b.find("li.dd_sltd_").index()*f),h.find(".dd_sl_").scrollLeft(h.find("li.dd_sltd_").index()*f),C.find(".dd_sl_").scrollLeft(C.find("li.dd_sltd_").index()*f)};switch(z(),t.format){case"Y":b.hide(),h.hide();break;case"m":C.hide(),y.hide(),h.hide()}var G=function(){var e=h.find("li.dd_sltd_").attr("value"),l=b.find("li.dd_sltd_").attr("value"),i=C.find("li.dd_sltd_").attr("value"),a=y.find("li.dd_sltd_"),s=C.find("li.dd_sltd_").attr("data-filter");"true"==s&&"2"==l?(h.find("ul").width(29*f),(30==e||31==e)&&(h.find("li").removeClass("dd_sltd_"),h.find("li[value=29]").addClass("dd_sltd_"))):"true"!=s&&"2"==l?(h.find("ul").width(28*f),(29==e||30==e||31==e)&&(h.find("li").removeClass("dd_sltd_"),h.find("li[value=28]").addClass("dd_sltd_"))):"11"==l||"4"==l||"6"==l||"9"==l?(h.find("ul").width(30*f),31==e&&(h.find("li").removeClass("dd_sltd_"),h.find("li[value=30]").addClass("dd_sltd_"))):h.find("ul").width(31*f),h.find("li").each(function(){tod=$(this).attr("value"),d=new Date(l+"/"+tod+"/"+i),x=d.getDay(),0==x?$(this).addClass("dd_sunday"):$(this).removeClass("dd_sunday"),$(this).find("em").html(k[x])}),t.years_multiple&&(next=a.next("li"),prev=a.prev("li"),i>=next.attr("value")?(v=next.attr("value"),y.find("li").removeClass("dd_sltd_"),next.addClass("dd_sltd_")):v>i&&(v=prev.attr("value"),y.find("li").removeClass("dd_sltd_"),prev.addClass("dd_sltd_")))},V=function(d){r.val(d),p.addClass("dd_fadeout").removeClass("dd_"+t.animation),setTimeout(function(){p.hide().removeClass("dd_fadeout"),m.hide()},300)},E=function(){p.addClass("dd_alert").removeClass("dd_"+t.animation),setTimeout(function(){p.removeClass("dd_alert")},500)};t.years_multiple&&y.find("li").on("click",function(){y.find("li").removeClass("dd_sltd_"),$(this).addClass("dd_sltd_");var d=$(this).attr("value");v=d,C.find(".dd_sl_").stop().animate({scrollLeft:C.find("li[value="+d+"]").index()*f},1200,"swing"),C.find("li").removeClass("dd_sltd_"),C.find("li[value="+d+"]").addClass("dd_sltd_"),G()}),p.find(".dd_sw_").each(function(){var d=$(this).find(".dd_sl_"),e=$(this).find(".dd_nav_"),l=d.find("li.dd_sltd_").index()*f,i=function(){scroll_left=d.scrollLeft(),scroll_left>=l+f/2&&(l+=f),scroll_left<=l-f/2&&(l-=f)};$(this).hover(function(){e.show()},function(){e.hide()}),d.find("ul").width(d.find("li").length*f),d.on("scroll mousemove",function(){i()}),e.click(function(){obj=$(this).hasClass("dd_next_")?d.find("li.dd_sltd_").next("li"):d.find("li.dd_sltd_").prev("li"),obj.length&&(d.stop().animate({scrollLeft:obj.index()*f},200),d.find("li").removeClass("dd_sltd_"),obj.addClass("dd_sltd_"),G())}),d.on("touchend",function(){d.stop().animate({scrollLeft:l},200);var e=l/f;d.find("li").removeClass("dd_sltd_"),d.find("li").eq(e).addClass("dd_sltd_"),G()}),d.find("li").click(function(){d.animate({scrollLeft:$(this).index()*f},200),d.find("li").removeClass("dd_sltd_"),$(this).addClass("dd_sltd_")})}),G(),r.click(function(){m.show(),p.css({top:r.offset().top+(r.height()+12),left:r.offset().left+(r.width()/2-f/2)-2}).show().addClass("dd_"+t.animation),r.hasClass("dd_locked")?(r.removeClass("dd_locked"),0!=t.animate_current?N():O()):q()}),dd_overlay.click(function(){p.addClass("dd_fadeout").removeClass("dd_"+t.animation),setTimeout(function(){p.hide().removeClass("dd_fadeout"),m.hide()},300)}),g.click(function(){var d=h.find("li.dd_sltd_").attr("value"),e=b.find("li.dd_sltd_").attr("value"),r=C.find("li.dd_sltd_").attr("value");if(10>d&&(d="0"+d),10>e&&(e="0"+e),x=new Date(e+"/"+d+"/"+r),x=x.getDay(),j=d.substr(1),D=k[x].substr(0,3),l=k[x],n=e.substr(1),M=w[n-1].substr(0,3),F=w[n-1],str=t.format.replace(/\b(Y)\b/i,r).replace(/\b(m)\b/i,e).replace(/\b(d)\b/i,d).replace(/\b(D)\b/i,D).replace(/\b(j)\b/i,j).replace(/\b(l)\b/i,l).replace(/\b(F)\b/i,F).replace(/\b(M)\b/i,M).replace(/\b(n)\b/i,n),t.lock){d1d=a,d1d<10&&(d1d="0"+d1d),d1m=s+1,d1m<10&&(d1m="0"+d1m),d1y=i;var _=Date.parse(d1y+"-"+d1m+"-"+d1d)/1e3,o=Date.parse(r+"-"+e+"-"+d)/1e3;"from"==t.lock?_>o?E():V(str):o>_?E():V(str)}else V(str)})}};