-
Notifications
You must be signed in to change notification settings - Fork 10
/
nepali-date-picker.min.js
1 lines (1 loc) · 18 KB
/
nepali-date-picker.min.js
1
(function(e){function t(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 a(){let t=I.length;if(t<1)e(".andp-datepicker-container").removeClass("open").hide();else{if(I=I.sort(function(e,t){return e=e.split("/").reverse().join(""),t=t.split("/").reverse().join(""),e>t?1:e<t?-1:0}),b)S.attr("value",I[0]).val(I[0]).change();else{for(e('input.andp-hidden-dates[data-cur_cal_id="'+m+'"]').remove(),i=0;i<=t-1;i++)c(I[i]);var a="";let n=O(m,"show_all_dates");a=1==n?S.is(":input")?I.join(", "):"<span>"+I.join("</span><span>")+"</span>":t>1?t+" dates selected":I[0],S.is(":input")?S.attr("value",a).val(a):S.html(a)}e(".andp-datepicker-container").removeClass("open").hide(),selected_date=e(this).data("date")}}function n(t){e(".andp-datepicker-container").removeClass("open").hide();var a=e('.andp-datepicker-container[data-cur_cal_id="'+m+'"]');if(a.length>0)return E=a.find(".andp-year-select"),N=a.find(".andp-month-select"),C=a.find(".andp-days-numbers"),a.addClass("open").show(),void s();var n='<div class="andp-datepicker-container" data-cur_cal_id="'+m+'" data-locale="'+w+'">';for(n+='<div class = "andp-header">',n+='<button type = "button" class = "andp-prev andp-change-months"></button>',n+='<select class = "andp-month-select"> </select>',n+='<select class = "andp-year-select"> </select>',n+='<button type = "button" class = "andp-next andp-change-months"></button> ',n+="</div>",n+='<div class="andp-body">',n+="np"==w?'<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>',n+='<div class = "andp-days-numbers"> </div>',b||(control_key="mac"==L?"CMD":"CTRL",n+='<div class="andp-info" style="display:none"><i class="mdi mdi-information text-primary"></i> '+("en"===w?"Press <strong>"+control_key+"</strong> or <strong>Shift</strong> key for multiple selection ":"<strong>"+control_key+"</strong> अथवा <strong>Shift</strong> दबाउनुश")+"</div>"),n+='<div class="andp-action-btns">',b||(n+='<button type="button" class="apply-date" data-cur_cal_id="'+m+'">'+("np"===w?"सेभ गर्नु":"Apply")+"</button>"),n+="</div>",n+="</div>",n+="</div>",_.append(n),a=e('.andp-datepicker-container[data-cur_cal_id="'+m+'"]'),E=a.find(".andp-year-select"),N=a.find(".andp-month-select"),C=a.find(".andp-days-numbers"),append_html='<option value = "01" '+("01"==D?"selected":" ")+" > "+("np"===w?"बैशाख":"Baisakh")+"</option>",append_html+='<option value = "02" '+("02"==D?"selected":"")+" > "+("np"===w?"जेठ":"Jestha")+"</option>",append_html+='<option value = "03" '+("03"==D?"selected":"")+" > "+("np"===w?"असार":"Asar")+"</option>",append_html+='<option value = "04" '+("04"==D?"selected":"")+" > "+("np"===w?"साउन":"Shrawan")+"</option>",append_html+='<option value = "05" '+("05"==D?"selected":"")+" > "+("np"===w?"भदौ":"Bhadra")+"</option>",append_html+='<option value = "06" '+("06"==D?"selected":"")+" > "+("np"===w?"असोज":"Ashoj")+"</option>",append_html+='<option value = "07" '+("07"==D?"selected":"")+" > "+("np"===w?"कार्तिक":"Kartik")+"</option>",append_html+='<option value = "08" '+("08"==D?"selected":"")+" > "+("np"===w?"मंसिर":"Mangsir")+"</option>",append_html+='<option value = "09" '+("09"==D?"selected":"")+" > "+("np"===w?"पुष":"Poush")+"</option>",append_html+='<option value = "10" '+("10"==D?"selected":"")+" > "+("np"===w?"माघ":"Magh")+"</option>",append_html+='<option value = "11" '+("11"==D?"selected":"")+" > "+("np"===w?"फागुन":"Falgun")+"</option>",append_html+='<option value = "12" '+("12"==D?"selected":"")+" > "+("np"===w?"चैत":"Chaitra")+"</option>",N.append(append_html),i=k;i<=Y;i++)append_html='<option value="'+i+'"',i==y&&(append_html+=" selected"),append_html+=">"+P(i)+"</option>",E.append(append_html);l(),e('.andp-datepicker-container[data-cur_cal_id="'+m+'"]').addClass("open"),s()}function s(){var t=S.offset(),a=S.outerHeight(),n=e(window).width(),i=S.outerWidth(),s=e(".andp-datepicker-container").outerWidth();if(s+t.left+10>n){var l=n-(t.left+i);e('.andp-datepicker-container[data-cur_cal_id="'+m+'"]').css({top:t.top+a,right:l,left:"inherit"})}else e('.andp-datepicker-container[data-cur_cal_id="'+m+'"]').css({top:t.top+a,left:t.left,right:"inherit"})}function l(){month=N.val(),year=E.val(),C.html("");var e=new u;e.setNepaliDate(year,month,1);var t=e.getDay(),a=d(year,month);append_html="";var n=1,s=1,l=parseInt(t)-2,o=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<k&&(last_year=k,last_month=1)),next_month=parseInt(month)+1,next_year=parseInt(year);var r=d(last_year,last_month);if(1==n&&(append_html+='<div class="andp-column">'),i<t)append_html+='<div class="old-dates"> '+P(parseInt(r-l))+" </div>",l-=1;else if(s<=a){let e=s<10?"0"+s:s,t=year+"-"+month+"-"+e,a=I.indexOf(t),n=f.compareDate(t,R),i=f.compareDate(t,A),l="";l=2==n?" disabled old-dates":1==i?" disabled old-dates":"day",append_html+='<div class="'+l+(a>=0?" selected":"")+'" data-date="'+t+'">'+P(s)+"</div>",s++}else append_html+='<div class="old-dates"> '+P(o)+"</div>",o++;7==n&&(append_html+="</div>",n=0),n++}C.append(append_html)}function d(e,t){var a=new u;if(!(e<k||e>Y||t<1||t>12)){e=e-k,t=t-1;return a.nepaliMonths[e][t]}}function o(e,t){e=e.split("-"),t=t.split("-");var a=new u;return a.setNepaliDate(e[0],e[1],e[2]),a.getNepaliDateDifference(t[0],t[1],t[2])}function r(e,t){e=e.split("-"),t=t.split("-");var a=new u;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}function h(e){e=e.split("-"),year=parseInt(e[0]),month=parseInt(e[1]);var t=parseInt(d(year,month));return day=parseInt(e[2])+1,day>t&&(day=1,month+=1,month>12&&(month=1,year+=1)),year+"-"+month+"-"+day}function p(a,n=!1){a=t(a);var i=I.indexOf(a),s=e('.andp-datepicker-container[data-cur_cal_id="'+m+'"]'),l=s.find('.day[data-date="'+a+'"]');n?l.addClass("soft-select"):i<0?(I.push(a),l.addClass("selected")):(I.splice(i,1),l.removeClass("selected"))}function c(e){j.append('<input class="andp-hidden-dates" type="hidden" data-cur_cal_id="'+m+'" name="'+T+'[]" value="'+e+'">')}function u(){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],[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,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),n},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}}const _=e("body"),f=new u;f.setCurrentDate();let v=f.nepaliYear+"-"+f.nepaliMonth+"-"+f.nepaliDate;e("form").attr("autocomplete","off");var g=1,m="";const y=f.getNepaliYear(),D=f.getNepaliMonth();var M,k=2e3,Y=2098,b=0,w="np",E="",N="",C="",I=[],x="",T="",L="win",S="",j="",A="",R="",F=[];const K=(e,t)=>{F.forEach((a,n)=>{a.cal_id===e&&(F[n]={cal_id:e,...t})})},O=(e,t=null)=>{let a=null;for(const a of F)if(a.cal_id===e)return t?a[t]||null:a;return a};e.fn.nepaliDatePicker=function(a){var i=e.extend({locale:"np",single:!1,show_all_dates:!1,date_before:"",date_after:""},a);navigator.platform.toUpperCase().indexOf("MAC")>=0&&(L="mac"),e(this).each(function(){let t=e(this);const a="cal-"+g;e(this).attr("data-cur_cal_id",a),F.push({cal_id:a,...i}),g++,e(this).addClass("andp-date-picker");var n=e.trim(e(this).attr("value"));let s=e(this).data("single"),l=e(this).data("show_all_dates"),d=e(this).data("date_before"),o=e(this).data("date_after"),r=e(this).data("locale");if(K(a,{single:s||i.single,show_all_dates:l||i.show_all_dates,date_before:d||i.date_before,date_after:o||i.date_after,locale:r||i.locale}),b=!0===s||1===s?1:0,n&&!s){j=e(this).parents("form"),T=e(this).attr("name");let i=n.split(",");if(i.forEach(function(e,t){m=a,c(e.trim())}),!0!==l)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=n.split(","),a="<span>"+e.join("</span><span>")+"</span>";t.append(a)}}}),e(this).click(function(){M=this,I=[],S=e(this),m=e(this).attr("data-cur_cal_id");let a=O(m);if(b=a.single,w=a.locale,R=a.date_after,A=a.date_before,R){let e=R.split("-");k=parseInt(e[0])}if(A){let e=A.split("-");Y=parseInt(e[0])}if(n(this),b){let a=t(e(this).val());a.length>0?(older_date_ar=a.split("-"),N.val(older_date_ar[1]).change(),E.val(older_date_ar[0]).change(),p(a)):p(v,!0)}else{j=e(this).parents("form"),T=e(this).attr("name"),T?e(this).removeAttr("name","").attr("data-name",T):T=e(this).attr("data-name");var i=e('input.andp-hidden-dates[data-cur_cal_id="'+m+'"]'),s=i.length;if(s>0)if(1==s)selected_date=t(i.eq(0).val()),older_date_ar=selected_date.split("-"),N.val(older_date_ar[1]).change(),E.val(older_date_ar[0]).change(),p(selected_date);else{older_date=e('input.andp-hidden-dates[data-cur_cal_id="'+m+'"]').last();older_date.length;older_date=t(older_date.val()),older_date&&older_date.length>0&&(older_date_ar=older_date.split("-"),N.val(older_date_ar[1]).change(),E.val(older_date_ar[0]).change()),i.each(function(){let t=e(this).val();p(t)})}else p(v,!0)}}),_.on("change",".andp-month-select, .andp-year-select",function(){l()})},_.on("click",".andp-datepicker-container.open .andp-change-months",function(t){selected_month=parseInt(N.val()),selected_year=parseInt(E.val()),e(this).hasClass("andp-next")?(selected_month+=1,selected_month>12&&(selected_month=1,selected_year+=1,selected_year>Y&&(selected_year=Y,selected_month=12))):(selected_month-=1,selected_month<1&&(selected_month=12,selected_year-=1,selected_year<k&&(selected_year=k,selected_month=1))),selected_month<10&&(selected_month="0"+selected_month),selected_year<10&&(selected_year="0"+selected_year),N.val(selected_month).change(),E.val(selected_year).change()});const P=e=>"np"===w?e.toLocaleString("ne-NP",{useGrouping:!1}):e;_.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 n=e('.andp-datepicker-container[data-cur_cal_id="'+m+'"]');if(b)I=[],n.find(".andp-column .day").removeClass("selected"),p(selected_date),n.find(".andp-info").hide(),a();else if(t.shiftKey){var s=I.length;if(s>0){selected_date=e(this).data("date"),x=I[s-1];var l=r(selected_date,x)?x:selected_date,d=l,c=o(selected_date,x);for(I=[],n.find(".andp-column .day").removeClass("selected"),p(d),i=1;i<=c;i++)d=h(d),p(d)}}else t.ctrlKey||t.metaKey?p(selected_date):(I=[],n.find(".andp-column .day").removeClass("selected"),p(selected_date),n.find(".andp-info").show());e("document").trigger("andp_date_selected",[I,M])}}),_.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()}),_.on("click",".andp-datepicker-container.open .apply-date",function(){a()}),e(document).keydown(function(t){const a=e(".andp-datepicker-container.open"),n=a.length;if(n)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);