forked from glauber-sampaio/datedropper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatedropper.min.js
12 lines (8 loc) · 8.95 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 a=(new Date).getFullYear(),i=(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:a,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)},u=100,f=/^#[0-9A-F]{6}$/i.test(t.color),c=function(d){return d.toString().substr(0,3)+t.years_multiple},v=0;f||(t.color="#f87a54"),t.maxYear<a&&(a=t.maxYear),$('<div class="dd_wrap" id="dd_'+_+'"><div class="dd_overlay"></div><div class="dd_"></div></div>').appendTo("body");var m=$("#dd_"+_),b=m.find(".dd_");dd_overlay=m.find(".dd_overlay"),$(window).on("resize",function(){b.css({top:r.offset().top+(r.height()+12),left:r.offset().left+(r.width()/2-u/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),b.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>'),b.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>'),b.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&&b.append('<div class="dd_r_"></div>'),b.append('<div class="dd_submit"></div>');var p=b.find(".dd_m_"),h=b.find(".dd_d_"),C=(b.find(".dd_y_w"),b.find(".dd_y_")),y=b.find(".dd_r_"),g=b.find(".dd_submit");switch(t.lang){case"si":var w=["januar","februar","marec","april","maj","junij","julij","avgust","september","oktober","november","december"],k=["nedelja","ponedeljek","torek","sreda","četrtek","petek","sobota"];break;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"hu":var w=["január","február","március","április","május","június","július","augusztus","szeptember","október","november","december"],k=["vasárnap","hétfő","kedd","szerda","csütörtök","péntek","szombat"];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;case"pt":var w=["Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro"],k=["Domingo","Segunda","Terça","Quarta","Quinta","Sexta","Sábado"];break;default:var w=["January","February","March","April","May","June","July","August","September","October","November","December"],k=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]}p.find(".dd_sl_").append("<ul></ul>");for(var L=1;12>=L;L++)months=w[L-1].substr(0,3),p.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 A=t.minYear;A<=t.maxYear;A++){var Y=A%t.years_multiple;0==Y&&y.find("ul").append('<li value="'+A+'"></li>')}var z=u/y.find("li").length;y.find("li").css({width:z+"%"}),y.find("li[value="+c(a)+"]").addClass("dd_sltd_")}var T=function(){h.find("li").eq(i-1).addClass("dd_sltd_"),p.find("li").eq(s).addClass("dd_sltd_"),C.find("li[value="+a+"]").addClass("dd_sltd_"),t.years_multiple&&y.find("li[value="+c(a)+"]").addClass("dd_sltd_")},N=function(){p.find(".dd_sl_").animate({scrollLeft:s*u},1200,"swing"),setTimeout(function(){h.find(".dd_sl_").animate({scrollLeft:(i-1)*u},1200,"swing"),setTimeout(function(){C.find(".dd_sl_").animate({scrollLeft:C.find(".dd_sl_ li[value="+a+"]").index()*u},1200,"swing")},200)},400)},O=function(){p.find(".dd_sl_").scrollLeft(s*u),h.find(".dd_sl_").scrollLeft((i-1)*u),C.find(".dd_sl_").scrollLeft(C.find(".dd_sl_ li[value="+a+"]").index()*u)},q=function(){p.find(".dd_sl_").scrollLeft(p.find("li.dd_sltd_").index()*u),h.find(".dd_sl_").scrollLeft(h.find("li.dd_sltd_").index()*u),C.find(".dd_sl_").scrollLeft(C.find("li.dd_sltd_").index()*u)};switch(T(),t.format){case"Y":p.hide(),h.hide();break;case"m":C.hide(),y.hide(),h.hide()}var G=function(){var e=h.find("li.dd_sltd_").attr("value"),a=p.find("li.dd_sltd_").attr("value"),i=C.find("li.dd_sltd_").attr("value"),l=y.find("li.dd_sltd_"),s=C.find("li.dd_sltd_").attr("data-filter");"true"==s&&"2"==a?(h.find("ul").width(29*u),(30==e||31==e)&&(h.find("li").removeClass("dd_sltd_"),h.find("li[value=29]").addClass("dd_sltd_"))):"true"!=s&&"2"==a?(h.find("ul").width(28*u),(29==e||30==e||31==e)&&(h.find("li").removeClass("dd_sltd_"),h.find("li[value=28]").addClass("dd_sltd_"))):"11"==a||"4"==a||"6"==a||"9"==a?(h.find("ul").width(30*u),31==e&&(h.find("li").removeClass("dd_sltd_"),h.find("li[value=30]").addClass("dd_sltd_"))):h.find("ul").width(31*u),h.find("li").each(function(){tod=$(this).attr("value"),d=new Date(a+"/"+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=l.next("li"),prev=l.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),b.addClass("dd_fadeout").removeClass("dd_"+t.animation),setTimeout(function(){b.hide().removeClass("dd_fadeout"),m.hide()},300)},Q=function(){b.addClass("dd_alert").removeClass("dd_"+t.animation),setTimeout(function(){b.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()*u},1200,"swing"),C.find("li").removeClass("dd_sltd_"),C.find("li[value="+d+"]").addClass("dd_sltd_"),G()}),b.find(".dd_sw_").each(function(){var d=$(this).find(".dd_sl_"),e=$(this).find(".dd_nav_"),a=d.find("li.dd_sltd_").index()*u,i=function(){scroll_left=d.scrollLeft(),scroll_left>=a+u/2&&(a+=u),scroll_left<=a-u/2&&(a-=u)};$(this).hover(function(){e.show()},function(){e.hide()}),d.find("ul").width(d.find("li").length*u),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()*u},200),d.find("li").removeClass("dd_sltd_"),obj.addClass("dd_sltd_"),G())}),d.on("touchend",function(){d.stop().animate({scrollLeft:a},200);var e=a/u;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()*u},200),d.find("li").removeClass("dd_sltd_"),$(this).addClass("dd_sltd_")})}),G(),r.click(function(){m.show(),b.css({top:r.offset().top+(r.height()+12),left:r.offset().left+(r.width()/2-u/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(){b.addClass("dd_fadeout").removeClass("dd_"+t.animation),setTimeout(function(){b.hide().removeClass("dd_fadeout"),m.hide()},300)}),g.click(function(){var d=h.find("li.dd_sltd_").attr("value"),e=p.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=i,d1d<10&&(d1d="0"+d1d),d1m=s+1,d1m<10&&(d1m="0"+d1m),d1y=a;var _=Date.parse(d1y+"-"+d1m+"-"+d1d)/1e3,o=Date.parse(r+"-"+e+"-"+d)/1e3;"from"==t.lock?_>o?Q():V(str):o>_?Q():V(str)}else V(str)})}};