UNPKG

nepali-multi-date-picker

Version:

A simple yet powerful jquery date picker based in Nepali calendar. Supports both single and multiple date selections in dual language.

2 lines (1 loc) 18.3 kB
!function(e){const t=e("body"),a=new R;a.setCurrentDate();let n=a.nepaliYear+"-"+a.nepaliMonth+"-"+a.nepaliDate;e("form").attr("autocomplete","off");var s=1,l="";const d=a.getNepaliYear(),r=a.getNepaliMonth();var o,h=2e3,p=2098,c=0,u="np",_="",f="",v="",g=[],m="",y="",D="win",M="",k="",Y="",w="",b=[];const E=(e,t=null)=>{for(const a of b)if(a.cal_id===e)return t?a[t]||null:a;return null};e.fn.nepaliDatePicker=function(a){var m=e.extend({locale:"np",single:!1,show_all_dates:!1,date_before:"",date_after:""},a);navigator.platform.toUpperCase().indexOf("MAC")>=0&&(D="mac"),e(this).each((function(){let t=e(this);const a="cal-"+s;e(this).attr("data-cur_cal_id",a),b.push({cal_id:a,...m}),s++,e(this).addClass("andp-date-picker");var n=e.trim(e(this).attr("value"));let i=e(this).data("single"),d=e(this).data("show_all_dates"),r=e(this).data("date_before"),o=e(this).data("date_after"),h=e(this).data("locale");var p,u;if(p=a,u={single:i||m.single,show_all_dates:d||m.show_all_dates,date_before:r||m.date_before,date_after:o||m.date_after,locale:h||m.locale},b.forEach(((e,t)=>{e.cal_id===p&&(b[t]={cal_id:p,...u})})),c=!0===i||1===i?1:0,n&&!i){k=e(this).parents("form"),y=e(this).attr("name");let i=n.split(",");if(i.forEach((function(e,t){l=a,A(e.trim())})),!0!==d)i.length>1?output_msg=i.length+" dates selected":output_msg=i[0],e(this).attr("value",output_msg);else if(!t.is("input")){let e="<span>"+n.split(",").join("</span><span>")+"</span>";t.append(e)}}})),e(this).click((function(){o=this,g=[],M=e(this),l=e(this).attr("data-cur_cal_id");let a=E(l);if(c=a.single,u=a.locale,w=a.date_after,Y=a.date_before,w){let e=w.split("-");h=parseInt(e[0])}if(Y){let e=Y.split("-");p=parseInt(e[0])}if(function(a){e(".andp-datepicker-container").removeClass("open").hide();var n=e('.andp-datepicker-container[data-cur_cal_id="'+l+'"]');if(n.length>0)return _=n.find(".andp-year-select"),f=n.find(".andp-month-select"),v=n.find(".andp-days-numbers"),n.addClass("open").show(),void x();var s='<div class="andp-datepicker-container" data-cur_cal_id="'+l+'" data-locale="'+u+'">';s+='<div class = "andp-header">',s+='<button type = "button" class = "andp-prev andp-change-months"></button>',s+='<select class = "andp-month-select"> </select>',s+='<select class = "andp-year-select"> </select>',s+='<button type = "button" class = "andp-next andp-change-months"></button> ',s+="</div>",s+='<div class="andp-body">',s+="np"==u?'<div class = "andp-days-names"> <div> आ </div> <div> सो </div> <div> मं </div> <div> बु </div> <div> बि </div> <div> शु </div> <div> श </div> </div>':'<div class = "andp-days-names"> <div> SUN </div> <div> MON </div> <div> TUE </div> <div> WED </div> <div> THU </div> <div> FRI </div> <div> SAT </div> </div>';s+='<div class = "andp-days-numbers"> </div>',c||(control_key="mac"==D?"CMD":"CTRL",s+='<div class="andp-info" style="display:none"><i class="mdi mdi-information text-primary"></i> '+("en"===u?"Press <strong>"+control_key+"</strong> or <strong>Shift</strong> key for multiple selection ":"<strong>"+control_key+"</strong> अथवा <strong>Shift</strong> दबाउनुश")+"</div>");s+='<div class="andp-action-btns">',c||(s+='<button type="button" class="apply-date" data-cur_cal_id="'+l+'">'+("np"===u?"सेभ गर्नु":"Apply")+"</button>");for(s+="</div>",s+="</div>",s+="</div>",t.append(s),n=e('.andp-datepicker-container[data-cur_cal_id="'+l+'"]'),_=n.find(".andp-year-select"),f=n.find(".andp-month-select"),v=n.find(".andp-days-numbers"),append_html='<option value = "01" '+("01"==r?"selected":" ")+" > "+("np"===u?"बैशाख":"Baisakh")+"</option>",append_html+='<option value = "02" '+("02"==r?"selected":"")+" > "+("np"===u?"जेठ":"Jestha")+"</option>",append_html+='<option value = "03" '+("03"==r?"selected":"")+" > "+("np"===u?"असार":"Asar")+"</option>",append_html+='<option value = "04" '+("04"==r?"selected":"")+" > "+("np"===u?"साउन":"Shrawan")+"</option>",append_html+='<option value = "05" '+("05"==r?"selected":"")+" > "+("np"===u?"भदौ":"Bhadra")+"</option>",append_html+='<option value = "06" '+("06"==r?"selected":"")+" > "+("np"===u?"असोज":"Ashoj")+"</option>",append_html+='<option value = "07" '+("07"==r?"selected":"")+" > "+("np"===u?"कार्तिक":"Kartik")+"</option>",append_html+='<option value = "08" '+("08"==r?"selected":"")+" > "+("np"===u?"मंसिर":"Mangsir")+"</option>",append_html+='<option value = "09" '+("09"==r?"selected":"")+" > "+("np"===u?"पुष":"Poush")+"</option>",append_html+='<option value = "10" '+("10"==r?"selected":"")+" > "+("np"===u?"माघ":"Magh")+"</option>",append_html+='<option value = "11" '+("11"==r?"selected":"")+" > "+("np"===u?"फागुन":"Falgun")+"</option>",append_html+='<option value = "12" '+("12"==r?"selected":"")+" > "+("np"===u?"चैत":"Chaitra")+"</option>",f.append(append_html),i=h;i<=p;i++)append_html='<option value="'+i+'"',i==d&&(append_html+=" selected"),append_html+=">"+N(i)+"</option>",_.append(append_html);T(),e('.andp-datepicker-container[data-cur_cal_id="'+l+'"]').addClass("open"),x()}(),c){let t=C(e(this).val());t.length>0?(older_date_ar=t.split("-"),f.val(older_date_ar[1]).change(),_.val(older_date_ar[0]).change(),j(t)):j(n,!0)}else{k=e(this).parents("form"),(y=e(this).attr("name"))?e(this).removeAttr("name","").attr("data-name",y):y=e(this).attr("data-name");var s=e('input.andp-hidden-dates[data-cur_cal_id="'+l+'"]'),m=s.length;if(m>0)if(1==m)selected_date=C(s.eq(0).val()),older_date_ar=selected_date.split("-"),f.val(older_date_ar[1]).change(),_.val(older_date_ar[0]).change(),j(selected_date);else{older_date=e('input.andp-hidden-dates[data-cur_cal_id="'+l+'"]').last();older_date.length;older_date=C(older_date.val()),older_date&&older_date.length>0&&(older_date_ar=older_date.split("-"),f.val(older_date_ar[1]).change(),_.val(older_date_ar[0]).change()),s.each((function(){j(e(this).val())}))}else j(n,!0)}})),t.on("change",".andp-month-select, .andp-year-select",(function(){T()}))},t.on("click",".andp-datepicker-container.open .andp-change-months",(function(t){selected_month=parseInt(f.val()),selected_year=parseInt(_.val()),e(this).hasClass("andp-next")?(selected_month+=1,selected_month>12&&(selected_month=1,selected_year+=1,selected_year>p&&(selected_year=p,selected_month=12))):(selected_month-=1,selected_month<1&&(selected_month=12,selected_year-=1,selected_year<h&&(selected_year=h,selected_month=1))),selected_month<10&&(selected_month="0"+selected_month),selected_year<10&&(selected_year="0"+selected_year),f.val(selected_month).change(),_.val(selected_year).change()}));const N=e=>"np"===u?e.toLocaleString("ne-NP",{useGrouping:!1}):e;function C(e){if(e.length<1)return"";let t=e.split("-"),a=t[0]+"-";return a+=1==t[1].length?"0"+t[1]:t[1],a+="-",a+=1==t[2].length?"0"+t[2]:t[2],a}function I(){let t=g.length;if(t<1)e(".andp-datepicker-container").removeClass("open").hide();else{if(g=g.sort((function(e,t){return(e=e.split("/").reverse().join(""))>(t=t.split("/").reverse().join(""))?1:e<t?-1:0})),c)M.attr("value",g[0]).val(g[0]).change();else{for(e('input.andp-hidden-dates[data-cur_cal_id="'+l+'"]').remove(),i=0;i<=t-1;i++)A(g[i]);var a="";a=1==E(l,"show_all_dates")?M.is(":input")?g.join(", "):"<span>"+g.join("</span><span>")+"</span>":t>1?t+" dates selected":g[0],M.is(":input")?M.attr("value",a).val(a):M.html(a)}e(".andp-datepicker-container").removeClass("open").hide(),selected_date=e(this).data("date")}}function x(){var t=M.offset(),a=M.outerHeight(),n=e(window).width(),i=M.outerWidth();if(e(".andp-datepicker-container").outerWidth()+t.left+10>n){var s=n-(t.left+i);e('.andp-datepicker-container[data-cur_cal_id="'+l+'"]').css({top:t.top+a,right:s,left:"inherit"})}else e('.andp-datepicker-container[data-cur_cal_id="'+l+'"]').css({top:t.top+a,left:t.left,right:"inherit"})}function T(){month=f.val(),year=_.val(),v.html("");var e=new R;e.setNepaliDate(year,month,1);var t=e.getDay(),n=L(year,month);append_html="";var s=1,l=1,d=parseInt(t)-2,r=1;for(i=1;i<=42;i++){last_month=parseInt(month)-1,last_year=parseInt(year),last_month<1&&(last_month=12,last_year-=1,last_year<h&&(last_year=h,last_month=1)),next_month=parseInt(month)+1,next_year=parseInt(year);var o=L(last_year,last_month);if(1==s&&(append_html+='<div class="andp-column">'),i<t)append_html+='<div class="old-dates"> '+N(parseInt(o-d))+" </div>",d-=1;else if(l<=n){let e=year+"-"+month+"-"+(l<10?"0"+l:l),t=g.indexOf(e),n=a.compareDate(e,w),i=a.compareDate(e,Y),s="";s=2==n||1==i?" disabled old-dates":"day",append_html+='<div class="'+s+(t>=0?" selected":"")+'" data-date="'+e+'">'+N(l)+"</div>",l++}else append_html+='<div class="old-dates"> '+N(r)+"</div>",r++;7==s&&(append_html+="</div>",s=0),s++}v.append(append_html)}function L(e,t){var a=new R;if(!(e<h||e>p||t<1||t>12)){e=e-h,t=t-1;return a.nepaliMonths[e][t]}}function S(e){e=e.split("-"),year=parseInt(e[0]),month=parseInt(e[1]);var t=parseInt(L(year,month));return day=parseInt(e[2])+1,day>t&&(day=1,month+=1,month>12&&(month=1,year+=1)),year+"-"+month+"-"+day}function j(t,a=!1){t=C(t);var n=g.indexOf(t),i=e('.andp-datepicker-container[data-cur_cal_id="'+l+'"]').find('.day[data-date="'+t+'"]');a?i.addClass("soft-select"):n<0?(g.push(t),i.addClass("selected")):(g.splice(n,1),i.removeClass("selected"))}function A(e){k.append('<input class="andp-hidden-dates" type="hidden" data-cur_cal_id="'+l+'" name="'+y+'[]" value="'+e+'">')}function R(){this.englishMonths=[31,28,31,30,31,30,31,31,30,31,30,31],this.englishLeapMonths=[31,29,31,30,31,30,31,31,30,31,30,31],this.nepaliMonths=[[30,32,31,32,31,30,30,30,29,30,29,31],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[30,32,31,32,31,30,30,30,29,30,29,31],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[31,31,31,32,31,31,29,30,30,29,29,31],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[31,31,31,32,31,31,29,30,30,29,30,30],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[31,31,31,32,31,31,29,30,30,29,30,30],[31,31,32,31,31,31,30,29,30,29,30,30],[31,32,31,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,30,29,31],[31,31,31,32,31,31,30,29,30,29,30,30],[31,31,32,31,31,31,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,30],[31,32,31,32,31,30,30,30,29,30,29,31],[31,31,31,32,31,31,30,29,30,29,30,30],[31,31,32,31,31,31,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[30,32,31,32,31,30,30,30,29,30,29,31],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,31,32,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[30,32,31,32,31,30,30,30,29,30,29,31],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[30,32,31,32,31,31,29,30,30,29,29,31],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[31,31,31,32,31,31,29,30,30,29,30,30],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[31,31,31,32,31,31,29,30,30,29,30,30],[31,31,32,31,31,31,30,29,30,29,30,30],[31,32,31,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[31,31,31,32,31,31,30,29,30,29,30,30],[31,31,32,31,31,31,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,30],[31,32,31,32,31,30,30,30,29,30,29,31],[31,31,31,32,31,31,30,29,30,29,30,30],[31,31,32,31,31,31,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,30],[31,32,31,32,31,30,30,30,29,30,29,31],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,31,32,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[30,32,31,32,31,30,30,30,29,30,29,31],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[30,32,31,32,31,31,29,30,29,30,29,31],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[31,31,31,32,31,31,29,30,30,29,29,31],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[31,31,31,32,31,31,29,30,30,29,30,30],[31,31,32,31,31,31,30,29,30,29,30,30],[31,32,31,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,31],[31,31,31,32,31,31,30,29,30,29,30,30],[31,31,32,31,31,31,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,30],[31,32,31,32,31,30,30,30,29,30,29,31],[31,31,31,32,31,31,30,29,30,29,30,30],[31,31,32,31,31,31,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,29,30,30],[31,31,31,32,31,30,30,30,29,30,30,30],[31,31,32,31,31,31,30,29,30,29,30,30],[31,31,32,31,31,30,30,30,29,30,30,30],[31,31,32,31,31,30,30,30,29,30,30,30],[31,32,31,32,30,31,30,30,29,30,30,30],[30,32,31,32,31,30,30,30,29,30,30,30],[31,31,32,31,31,31,30,30,29,30,30,30],[30,31,32,32,30,31,30,30,29,30,30,30],[30,32,31,32,31,30,30,30,29,30,30,30],[30,32,31,32,31,30,30,30,29,30,30,30],[31,31,32,31,31,31,30,30,29,30,30,30],[30,31,32,32,31,30,30,30,29,30,30,30],[30,32,31,32,31,30,30,30,29,30,30,30],[31,31,32,31,31,30,30,30,29,30,30,30],[31,31,32,31,31,31,30,29,30,30,30,30],[30,31,32,32,31,30,30,29,30,29,30,30],[31,32,31,32,31,30,30,30,29,30,30,30],[31,31,32,31,31,31,29,30,29,30,29,31],[31,31,32,31,31,31,30,29,29,30,30,30]],this.setCurrentDate=function(){var e=new Date;this.setEnglishDate(e.getFullYear(),e.getMonth()+1,e.getDate())},this.setEnglishDate=function(e,t,a){if(!this.isEnglishRange(e,t,a))throw new Exception("Invalid date format.");this.englishYear=e,this.englishMonth=t,this.englishDate=a,this.nepaliYear=2e3,this.nepaliMonth=1,this.nepaliDate=1;for(var n=this.getEnglishDateDifference(1943,4,14),i=0;n>=this.nepaliYearDays(i);)this.nepaliYear++,n-=this.nepaliYearDays(i),i++;for(var s=0;n>=this.nepaliMonths[i][s];)n-=this.nepaliMonths[i][s],this.nepaliMonth++,s++;this.nepaliDate=this.nepaliDate+n,this.getDay()},this.toEnglishString=function(e){return void 0===e&&(e="-"),this.englishYear+e+this.englishMonth+e+this.englishDate},this.getEnglishDateDifference=function(e,t,a){var n=this.countTotalEnglishDays(this.englishYear,this.englishMonth,this.englishDate)-this.countTotalEnglishDays(e,t,a);return n<0?-n:n},this.countTotalEnglishDays=function(e,t,a){for(var n=365*e+a,i=0;i<t-1;i++)n+=this.englishMonths[i];return n+=this.countleap(e,t)},this.countleap=function(e,t){return t<=2&&e--,Math.floor(e/4)-Math.floor(e/100)+Math.floor(e/400)},this.isEnglishRange=function(e,t,a){return!(e<1944||e>2042)&&(!(t<1||t>12)&&!(a<1||a>31))},this.isLeapYear=function(e){return e%4==0&&(e%100!=0||e%400==0)},this.setNepaliDate=function(e,t,a){if(this.isNepaliRange(e,t,a)){this.nepaliYear=e,this.nepaliMonth=t,this.nepaliDate=a,this.englishYear=1944,this.englishMonth=1,this.englishDate=1;for(var n=this.getNepaliDateDifference(2e3,9,17);n>=(this.isLeapYear(this.englishYear)?366:365);)n-=this.isLeapYear(this.englishYear)?366:365,this.englishYear++;for(var i=this.isLeapYear(this.englishYear)?this.englishLeapMonths:this.englishMonths,s=0;n>=i[s];)this.englishMonth++,n-=i[s],s++;this.englishDate=this.englishDate+n,this.getDay()}else console.log("Invalid Date Format")},this.toNepaliString=function(e){return void 0===e&&(e="-"),this.nepaliYear+e+this.nepaliMonth+e+this.nepaliDate},this.getNepaliDateDifference=function(e,t,a){var n=this.countTotalNepaliDays(this.nepaliYear,this.nepaliMonth,this.nepaliDate)-this.countTotalNepaliDays(e,t,a);return n<0?-n:n},this.countTotalNepaliDays=function(e,t,a){var n=0;if(e<2e3)return 0;n+=a-1;for(var i=e-2e3,s=0;s<t-1;s++)n+=this.nepaliMonths[i][s];for(s=0;s<i;s++)n+=this.nepaliYearDays(s);return n},this.nepaliYearDays=function(e){for(var t=0,a=0;a<12;a++)t+=this.nepaliMonths[e][a];return t},this.isNepaliRange=function(e,t,a){return!(e<2e3||e>2098)&&(!(t<1||t>12)&&!(a<1||a>this.nepaliMonths[e-2e3][t-1]))},this.getDay=function(){var e=this.getEnglishDateDifference(1943,4,14);return this.weekDay=(3+e%7)%7+1,this.weekDay},this.getEnglishYear=function(){return this.englishYear},this.getEnglishMonth=function(){return this.englishMonth},this.getEnglishDate=function(){return this.englishDate},this.getNepaliYear=function(){return this.nepaliYear},this.getNepaliMonth=function(){return this.nepaliMonth},this.getNepaliDate=function(){return this.nepaliDate},this.compareDate=function(e,t){if(!e||!t)return 0;const[a,n,i]=e.split("-").map(Number),[s,l,d]=t.split("-").map(Number);return a<s?2:a>s?1:n<l?2:n>l?1:i<d?2:i>d?1:0}}t.on("click",".andp-datepicker-container.open .andp-days-numbers .day",(function(t){if(selected_day=e(this).text(),selected_date=e(this).data("date"),!e(this).hasClass("disabled")){var a=e('.andp-datepicker-container[data-cur_cal_id="'+l+'"]');if(c)g=[],a.find(".andp-column .day").removeClass("selected"),j(selected_date),a.find(".andp-info").hide(),I();else if(t.shiftKey){var n=g.length;if(n>0){selected_date=e(this).data("date"),m=g[n-1];var s=function(e,t){e=e.split("-"),t=t.split("-");var a=new R;a.setNepaliDate(e[0],e[1],e[2]);var n=[a.getEnglishYear(),a.getEnglishMonth(),a.getEnglishDate()];a.setNepaliDate(t[0],t[1],t[2]);var i=[a.getEnglishYear(),a.getEnglishMonth(),a.getEnglishDate()],s=new Date(n[0],n[1],n[2]),l=new Date(i[0],i[1],i[2]);return s>l&&1}(selected_date,m)?m:selected_date,d=s,r=function(e,t){e=e.split("-"),t=t.split("-");var a=new R;return a.setNepaliDate(e[0],e[1],e[2]),a.getNepaliDateDifference(t[0],t[1],t[2])}(selected_date,m);for(g=[],a.find(".andp-column .day").removeClass("selected"),j(d),i=1;i<=r;i++)j(d=S(d))}}else t.ctrlKey||t.metaKey?j(selected_date):(g=[],a.find(".andp-column .day").removeClass("selected"),j(selected_date),a.find(".andp-info").show());e("document").trigger("andp_date_selected",[g,o])}})),t.on("click",(function(t){var a=e(".andp-datepicker-container, .andp-date-picker");a.is(t.target)||0!==a.has(t.target).length||e(".andp-datepicker-container").removeClass("open").hide()})),t.on("click",".andp-datepicker-container.open .apply-date",(function(){I()})),e(document).keydown((function(t){const a=e(".andp-datepicker-container.open");if(a.length)switch(t.which){case 37:a.find(".andp-prev.andp-change-months").click();break;case 39:a.find(".andp-next.andp-change-months").click()}}))}(jQuery);