-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnepali-date-picker.js
1353 lines (1113 loc) · 37.7 KB
/
nepali-date-picker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function ($) {
const $body = $('body');
// initialize date converter
const converter = new DateConverter();
converter.setCurrentDate();
let today =
converter.nepaliYear +
'-' +
converter.nepaliMonth +
'-' +
converter.nepaliDate;
// disable autocomplete
$('form').attr('autocomplete', 'off');
// give each calendar instance a unique id
var cal_no = 1;
var cur_cal_id = ''; // cur selected calendar id
// set default date to this month
const this_year = converter.getNepaliYear();
const this_month = converter.getNepaliMonth();
// calendar config
var start_year = 2000;
var end_year = 2098;
// is this single datepicker
var single_datepicker = 0;
var locale = 'np';
// calendar ui selector
var $year_select = '';
var $month_select = '';
var $days_container = '';
// for multiple selection
var user_selected_dates = []; // this will hold all the user selected dates
var last_captured_date = ''; // get last captured date, will be used to select days in multiple selection
var input_field_name = '';
var os = 'win'; // show different message according to os
var $selector = ''; // currently active selector
var $form = ''; // parent form of selected selector
var date_before = '';
var date_after = '';
var sel_input;
var settings = [];
const updateSettings = (cal_id, newData) => {
settings.forEach((setting, index) => {
if (setting.cal_id === cal_id) {
settings[index] = { cal_id: cal_id, ...newData };
}
});
};
const getSettingsValue = (cal_id, key = null) => {
let value = null; // Use null to indicate no match
// Iterate through each setting
for (const setting of settings) {
if (setting.cal_id === cal_id) {
// If no key is provided, return the entire setting object
if (!key) {
return setting;
}
// Return the specific key's value
return setting[key] || null; // Handle missing keys safely
}
}
return value; // Return null if no matching cal_id is found
};
$.fn.nepaliDatePicker = function (options) {
var defaults = $.extend(
{
// These are the defaults.
locale: 'np', // en
single: false, // enable single or multi date picker
show_all_dates: false, // show all selected dates as input field value
date_before: '', // dates before this date will be disabled
date_after: '', // dates after this date will be disabled
},
options
);
// detect OS
if (navigator.platform.toUpperCase().indexOf('MAC') >= 0) {
os = 'mac';
}
// give calendar a unique id
$(this).each(function () {
let $this = $(this);
const cur_cal = 'cal-' + cal_no;
// give unique id to each calendar instance
$(this).attr('data-cur_cal_id', cur_cal);
// update settings
settings.push({
cal_id: cur_cal,
...defaults,
});
cal_no++;
// add common class to all input fields
$(this).addClass('andp-date-picker');
// this will run only once
// generate input hidden fields if date value already exist
var default_value = $.trim($(this).attr('value'));
let data_single = $(this).data('single');
let data_show_all_dates = $(this).data('show_all_dates');
let data_date_before = $(this).data('date_before');
let data_date_after = $(this).data('date_after');
let data_locale = $(this).data('locale');
updateSettings(cur_cal, {
single: data_single || defaults.single,
show_all_dates: data_show_all_dates || defaults.show_all_dates,
date_before: data_date_before || defaults.date_before,
date_after: data_date_after || defaults.date_after,
locale: data_locale || defaults.locale,
});
if (data_single === true || data_single === 1) {
single_datepicker = 1;
} else {
single_datepicker = 0;
}
if (default_value && !data_single) {
// set form
$form = $(this).parents('form');
// this will be used to generate hidden input fields with same input name
input_field_name = $(this).attr('name');
let default_dates = default_value.split(',');
default_dates.forEach(function (item, index) {
cur_cal_id = cur_cal;
generate_hidden_input_fields(item.trim());
});
if (data_show_all_dates !== true) {
if (default_dates.length > 1) {
output_msg = default_dates.length + ' dates selected';
} else {
output_msg = default_dates[0];
}
// show message to main selector field
$(this).attr('value', output_msg);
} else {
if (!$this.is('input')) {
// input type is not "input".
// show all default values into selected container.
let default_dates = default_value.split(',');
let temp_markup =
'<span>' + default_dates.join('</span><span>') + '</span>';
$this.append(temp_markup);
}
}
}
});
// when user clicks in input / selector field
$(this).click(function () {
// update globally.
sel_input = this;
user_selected_dates = [];
$selector = $(this);
cur_cal_id = $(this).attr('data-cur_cal_id');
let setting = getSettingsValue(cur_cal_id);
single_datepicker = setting.single;
locale = setting.locale;
date_after = setting.date_after;
date_before = setting.date_before;
if (date_after) {
let date_after_ar = date_after.split('-');
start_year = parseInt(date_after_ar[0]);
}
if (date_before) {
let date_before_ar = date_before.split('-');
end_year = parseInt(date_before_ar[0]);
}
// initiate calendar ui
init(this);
if (single_datepicker) {
// inline calendar
let selected_date = format_date_yyyy_mm_dd($(this).val());
// add this date into selected dates arary
// switch calendar to selected month and year
if (selected_date.length > 0) {
older_date_ar = selected_date.split('-');
$month_select.val(older_date_ar[1]).change();
$year_select.val(older_date_ar[0]).change();
select_date(selected_date);
} else {
// select default date
select_date(today, true);
}
} else {
// multi select calendar
$form = $(this).parents('form');
// this will be used to generate hidden input fields with same input name
input_field_name = $(this).attr('name');
if (input_field_name) {
// remove name attr from selector
$(this).removeAttr('name', '').attr('data-name', input_field_name);
} else {
// input_field_name is missing
// get it from data-name
input_field_name = $(this).attr('data-name');
}
var $hidden_publish_dates = $(
'input.andp-hidden-dates[data-cur_cal_id="' + cur_cal_id + '"]'
);
var total_hidden_dates = $hidden_publish_dates.length;
if (total_hidden_dates > 0) {
if (total_hidden_dates == 1) {
selected_date = format_date_yyyy_mm_dd(
$hidden_publish_dates.eq(0).val()
);
older_date_ar = selected_date.split('-');
$month_select.val(older_date_ar[1]).change();
$year_select.val(older_date_ar[0]).change();
// mark all selected dates as selexted in calendar ui.
select_date(selected_date);
} else {
// last selected date
older_date = $(
'input.andp-hidden-dates[data-cur_cal_id="' + cur_cal_id + '"]'
).last();
let total_older_date = older_date.length;
older_date = format_date_yyyy_mm_dd(older_date.val());
// switch calendar to last month and year of selected date
if (older_date && older_date.length > 0) {
older_date_ar = older_date.split('-');
$month_select.val(older_date_ar[1]).change();
$year_select.val(older_date_ar[0]).change();
}
$hidden_publish_dates.each(function () {
let sel_date = $(this).val();
// mark all selected dates as selexted in calendar ui.
select_date(sel_date);
});
}
} else {
// select default date
select_date(today, true);
}
}
});
// update days when month or year is changed
$body.on('change', '.andp-month-select, .andp-year-select', function () {
generate_days();
});
};
// change months on button click
$body.on(
'click',
'.andp-datepicker-container.open .andp-change-months',
function (event) {
// show next month
selected_month = parseInt($month_select.val());
selected_year = parseInt($year_select.val());
if ($(this).hasClass('andp-next')) {
// next month
selected_month = selected_month + 1;
if (selected_month > 12) {
selected_month = 1;
selected_year = selected_year + 1;
if (selected_year > end_year) {
selected_year = end_year;
selected_month = 12;
}
}
} else {
// previous month
selected_month = selected_month - 1;
if (selected_month < 1) {
selected_month = 12;
selected_year = selected_year - 1;
if (selected_year < start_year) {
selected_year = start_year;
selected_month = 1;
}
}
}
if (selected_month < 10) {
selected_month = '0' + selected_month;
}
if (selected_year < 10) {
selected_year = '0' + selected_year;
}
$month_select.val(selected_month).change();
$year_select.val(selected_year).change();
}
);
const localizeNumber = (number) => {
if (locale === 'np') {
return number.toLocaleString('ne-NP', { useGrouping: false });
} else {
return number;
}
};
// if clicked in days when datepicker is open
$body.on(
'click',
'.andp-datepicker-container.open .andp-days-numbers .day',
function (event) {
selected_day = $(this).text();
selected_date = $(this).data('date');
if ($(this).hasClass('disabled')) {
return;
}
var $sel_calendar = $(
'.andp-datepicker-container[data-cur_cal_id="' + cur_cal_id + '"]'
);
// disable shift or ctrl key on single_datepicker
if (single_datepicker) {
// reset user_selected_dates
user_selected_dates = [];
// remove all other selected class
$sel_calendar.find('.andp-column .day').removeClass('selected');
// select date function
select_date(selected_date);
$sel_calendar.find('.andp-info').hide();
update_sel_date_in_ui();
} else {
// multiple selection
if (event.shiftKey) {
// shift key pressed
var total_captured_dates = user_selected_dates.length;
if (total_captured_dates > 0) {
selected_date = $(this).data('date');
last_captured_date = user_selected_dates[total_captured_dates - 1];
// get older date
var smaller_date = find_older_date(
selected_date,
last_captured_date
)
? last_captured_date
: selected_date;
var next_date = smaller_date;
var days_difference = get_days_difference(
selected_date,
last_captured_date
);
// reset all captured dates
user_selected_dates = [];
$sel_calendar.find('.andp-column .day').removeClass('selected');
select_date(next_date);
for (i = 1; i <= days_difference; i++) {
next_date = get_next_day(next_date);
select_date(next_date);
}
}
} else if (event.ctrlKey || event.metaKey) {
// ctrl or cmd key pressed
select_date(selected_date);
} else {
// no ctrl, shift or cmd key was presed ---------------------
// reset user_selected_dates
user_selected_dates = [];
// remove other selected class
$sel_calendar.find('.andp-column .day').removeClass('selected');
// select correct day
select_date(selected_date);
// show message for multiple selection
$sel_calendar.find('.andp-info').show();
}
}
// let other application get selected date through custom event.
$('document').trigger('andp_date_selected', [
user_selected_dates,
sel_input,
]);
}
);
// close datepicker when clicked outside
$body.on('click', function (e) {
var container = $('.andp-datepicker-container, .andp-date-picker');
// if the target of the click isn't the container nor a descendant of the container
if (!container.is(e.target) && container.has(e.target).length === 0) {
$('.andp-datepicker-container').removeClass('open').hide();
}
});
// insert/update date only if appy-date button was clicked
$body.on('click', '.andp-datepicker-container.open .apply-date', function () {
update_sel_date_in_ui();
});
function format_date_yyyy_mm_dd(date) {
if (date.length < 1) {
return '';
}
let date_ar = date.split('-');
let new_date = date_ar[0] + '-';
new_date += date_ar[1].length == 1 ? '0' + date_ar[1] : date_ar[1];
new_date += '-';
new_date += date_ar[2].length == 1 ? '0' + date_ar[2] : date_ar[2];
return new_date;
}
function update_sel_date_in_ui() {
// do not proceed if no date was selected
let total_user_selected_dates = user_selected_dates.length;
if (total_user_selected_dates < 1) {
$('.andp-datepicker-container').removeClass('open').hide();
return;
}
// some date were selected, proceed ------------------
// sort date
user_selected_dates = user_selected_dates.sort(function (a, b) {
a = a.split('/').reverse().join('');
b = b.split('/').reverse().join('');
return a > b ? 1 : a < b ? -1 : 0;
});
if (single_datepicker) {
$selector
.attr('value', user_selected_dates[0])
.val(user_selected_dates[0])
.change();
} else {
// destroy previous hidden input fields
$(
'input.andp-hidden-dates[data-cur_cal_id="' + cur_cal_id + '"]'
).remove();
for (i = 0; i <= total_user_selected_dates - 1; i++) {
// generate new hidden input fields
generate_hidden_input_fields(user_selected_dates[i]);
}
var output_msg = ''; //user_selected_dates[0];
let show_all_dates = getSettingsValue(cur_cal_id, 'show_all_dates');
if (show_all_dates == true) {
if ($selector.is(':input')) {
output_msg = user_selected_dates.join(', ');
} else {
output_msg =
'<span>' + user_selected_dates.join('</span><span>') + '</span>';
}
} else {
if (total_user_selected_dates > 1) {
output_msg = total_user_selected_dates + ' dates selected';
} else {
output_msg = user_selected_dates[0];
}
}
// show message to main selector field
if ($selector.is(':input')) {
$selector.attr('value', output_msg).val(output_msg);
} else {
$selector.html(output_msg);
}
}
$('.andp-datepicker-container').removeClass('open').hide();
selected_date = $(this).data('date');
}
function init(this_sel) {
// close other instance of calendar
$('.andp-datepicker-container').removeClass('open').hide();
// check if calendar ui has already been generated for selected cur_cal_id
var $sel_calendar = $(
'.andp-datepicker-container[data-cur_cal_id="' + cur_cal_id + '"]'
);
if ($sel_calendar.length > 0) {
$year_select = $sel_calendar.find('.andp-year-select');
$month_select = $sel_calendar.find('.andp-month-select');
$days_container = $sel_calendar.find('.andp-days-numbers');
$sel_calendar.addClass('open').show();
fix_calendar_alignment();
return;
}
var template =
'<div class="andp-datepicker-container" data-cur_cal_id="' +
cur_cal_id +
'" data-locale="' +
locale +
'">';
template += '<div class = "andp-header">';
template +=
'<button type = "button" class = "andp-prev andp-change-months"></button>';
template += '<select class = "andp-month-select"> </select>';
template += '<select class = "andp-year-select"> </select>';
template +=
'<button type = "button" class = "andp-next andp-change-months"></button> ';
template += '</div>';
template += '<div class="andp-body">';
if (locale == 'np') {
template +=
'<div class = "andp-days-names"> <div> आ </div> <div> सो </div> <div> मं </div> <div> बु </div> <div> बि </div> <div> शु </div> <div> श </div> </div>';
} else {
template +=
'<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>';
}
template += '<div class = "andp-days-numbers"> </div>';
if (!single_datepicker) {
if (os == 'mac') {
control_key = 'CMD';
} else {
control_key = 'CTRL';
}
template +=
'<div class="andp-info" style="display:none"><i class="mdi mdi-information text-primary"></i> ' +
(locale === 'en'
? 'Press <strong>' +
control_key +
'</strong> or <strong>Shift</strong> key for multiple selection '
: '<strong>' +
control_key +
'</strong> अथवा <strong>Shift</strong> दबाउनुश') +
'</div>';
}
template += '<div class="andp-action-btns">';
if (!single_datepicker) {
template +=
'<button type="button" class="apply-date" data-cur_cal_id="' +
cur_cal_id +
'">' +
(locale === 'np' ? 'सेभ गर्नु' : 'Apply') +
'</button>';
}
template += '</div>';
template += '</div>';
template += '</div>';
// insert into DOM
$body.append(template);
// re-initiate var, wont work otherwise
$sel_calendar = $(
'.andp-datepicker-container[data-cur_cal_id="' + cur_cal_id + '"]'
);
$year_select = $sel_calendar.find('.andp-year-select');
$month_select = $sel_calendar.find('.andp-month-select');
$days_container = $sel_calendar.find('.andp-days-numbers');
// add month into month select
append_html =
'<option value = "01" ' +
('01' == this_month ? 'selected' : ' ') +
' > ' +
(locale === 'np' ? 'बैशाख' : 'Baisakh') +
'</option>';
append_html +=
'<option value = "02" ' +
('02' == this_month ? 'selected' : '') +
' > ' +
(locale === 'np' ? 'जेठ' : 'Jestha') +
'</option>';
append_html +=
'<option value = "03" ' +
('03' == this_month ? 'selected' : '') +
' > ' +
(locale === 'np' ? 'असार' : 'Asar') +
'</option>';
append_html +=
'<option value = "04" ' +
('04' == this_month ? 'selected' : '') +
' > ' +
(locale === 'np' ? 'साउन' : 'Shrawan') +
'</option>';
append_html +=
'<option value = "05" ' +
('05' == this_month ? 'selected' : '') +
' > ' +
(locale === 'np' ? 'भदौ' : 'Bhadra') +
'</option>';
append_html +=
'<option value = "06" ' +
('06' == this_month ? 'selected' : '') +
' > ' +
(locale === 'np' ? 'असोज' : 'Ashoj') +
'</option>';
append_html +=
'<option value = "07" ' +
('07' == this_month ? 'selected' : '') +
' > ' +
(locale === 'np' ? 'कार्तिक' : 'Kartik') +
'</option>';
append_html +=
'<option value = "08" ' +
('08' == this_month ? 'selected' : '') +
' > ' +
(locale === 'np' ? 'मंसिर' : 'Mangsir') +
'</option>';
append_html +=
'<option value = "09" ' +
('09' == this_month ? 'selected' : '') +
' > ' +
(locale === 'np' ? 'पुष' : 'Poush') +
'</option>';
append_html +=
'<option value = "10" ' +
('10' == this_month ? 'selected' : '') +
' > ' +
(locale === 'np' ? 'माघ' : 'Magh') +
'</option>';
append_html +=
'<option value = "11" ' +
('11' == this_month ? 'selected' : '') +
' > ' +
(locale === 'np' ? 'फागुन' : 'Falgun') +
'</option>';
append_html +=
'<option value = "12" ' +
('12' == this_month ? 'selected' : '') +
' > ' +
(locale === 'np' ? 'चैत' : 'Chaitra') +
'</option>';
$month_select.append(append_html);
// add year into year select
for (i = start_year; i <= end_year; i++) {
append_html = '<option value="' + i + '"';
if (i == this_year) {
append_html += ' selected';
}
append_html += '>' + localizeNumber(i) + '</option>';
$year_select.append(append_html);
}
generate_days();
$(
'.andp-datepicker-container[data-cur_cal_id="' + cur_cal_id + '"]'
).addClass('open');
fix_calendar_alignment();
}
function fix_calendar_alignment() {
// fix calendar layout and position in dom
var elem_pos = $selector.offset();
var elem_height = $selector.outerHeight();
var document_width = $(window).width();
var selector_width = $selector.outerWidth();
var calendar_width = $('.andp-datepicker-container').outerWidth();
if (calendar_width + elem_pos.left + 10 > document_width) {
var right_offset = document_width - (elem_pos.left + selector_width);
$('.andp-datepicker-container[data-cur_cal_id="' + cur_cal_id + '"]').css(
{
top: elem_pos.top + elem_height,
right: right_offset,
left: 'inherit',
}
);
} else {
$('.andp-datepicker-container[data-cur_cal_id="' + cur_cal_id + '"]').css(
{
top: elem_pos.top + elem_height,
left: elem_pos.left,
right: 'inherit',
}
);
}
}
function generate_days() {
month = $month_select.val();
year = $year_select.val();
$days_container.html('');
var selected_date_obj = new DateConverter();
selected_date_obj.setNepaliDate(year, month, 1);
var month_start_day = selected_date_obj.getDay();
var total_days_in_selected_month = getDaysInMonth(year, month);
append_html = '';
var y = 1; // year
var j = 1;
var k = parseInt(month_start_day) - 2;
var l = 1;
for (i = 1; i <= 42; i++) {
last_month = parseInt(month) - 1;
last_year = parseInt(year);
if (last_month < 1) {
last_month = 12;
last_year = last_year - 1;
if (last_year < start_year) {
last_year = start_year;
last_month = 1;
}
}
next_month = parseInt(month) + 1;
next_year = parseInt(year);
var total_days_in_last_month = getDaysInMonth(last_year, last_month);
if (y == 1) {
append_html += '<div class="andp-column">';
}
if (i < month_start_day) {
append_html +=
'<div class="old-dates"> ' +
localizeNumber(parseInt(total_days_in_last_month - k)) +
' </div>';
k = k - 1;
} else {
if (j <= total_days_in_selected_month) {
let day = j < 10 ? '0' + j : j;
let proper_date = year + '-' + month + '-' + day;
let ar_index = user_selected_dates.indexOf(proper_date);
let compare_date_after = converter.compareDate(
proper_date,
date_after
);
let compare_date_before = converter.compareDate(
proper_date,
date_before
);
let css_class = '';
if (compare_date_after == 2 ? ' disabled old-dates' : '') {
css_class = ' disabled old-dates';
} else if (compare_date_before == 1 ? ' disabled old-dates' : '') {
css_class = ' disabled old-dates';
} else {
css_class = 'day';
}
// ( ( ar_index >= 0 ) ? ' selected' : '' ) = mark selected days as selected, even after calendar close or year/month change.
append_html +=
'<div class="' +
css_class +
(ar_index >= 0 ? ' selected' : '') +
'" data-date="' +
proper_date +
'">' +
localizeNumber(j) +
'</div>';
j++;
} else {
append_html +=
'<div class="old-dates"> ' + localizeNumber(l) + '</div>';
l++;
}
}
if (y == 7) {
append_html += '</div>';
y = 0;
}
y++;
}
$days_container.append(append_html);
}
function getDaysInMonth(year, month) {
var converter = new DateConverter();
if (year < start_year || year > end_year) return;
if (month < 1 || month > 12) return;
var year = year - start_year;
var month = month - 1;
return converter.nepaliMonths[year][month];
}
function get_days_difference(date_1, date_2) {
date_1 = date_1.split('-');
date_2 = date_2.split('-');
var converter = new DateConverter();
converter.setNepaliDate(date_1[0], date_1[1], date_1[2]);
return converter.getNepaliDateDifference(date_2[0], date_2[1], date_2[2]);
}
function find_older_date(date_1, date_2) {
date_1 = date_1.split('-');
date_2 = date_2.split('-');
var converter = new DateConverter();
converter.setNepaliDate(date_1[0], date_1[1], date_1[2]);
var date_1_eng = [
converter.getEnglishYear(),
converter.getEnglishMonth(),
converter.getEnglishDate(),
];
converter.setNepaliDate(date_2[0], date_2[1], date_2[2]);
var date_2_eng = [
converter.getEnglishYear(),
converter.getEnglishMonth(),
converter.getEnglishDate(),
];
var firstDate = new Date(date_1_eng[0], date_1_eng[1], date_1_eng[2]);
var secondDate = new Date(date_2_eng[0], date_2_eng[1], date_2_eng[2]);
if (firstDate > secondDate) {
return 1;
} else {
return false;
}
}
function get_next_day(date_1) {
date_1 = date_1.split('-');
year = parseInt(date_1[0]);
month = parseInt(date_1[1]);
var days_in_month = parseInt(getDaysInMonth(year, month));
day = parseInt(date_1[2]) + 1;
if (day > days_in_month) {
day = 1;
month = month + 1;
if (month > 12) {
month = 1;
year = year + 1;
}
}
return year + '-' + month + '-' + day;
}
function select_date(selected_date, soft_select = false) {
selected_date = format_date_yyyy_mm_dd(selected_date);
var ar_index = user_selected_dates.indexOf(selected_date); // check if selected_date already exists in user_selected_date
var $sel_calendar = $(
'.andp-datepicker-container[data-cur_cal_id="' + cur_cal_id + '"]'
);
var $this = $sel_calendar.find('.day[data-date="' + selected_date + '"]');
if (soft_select) {
$this.addClass('soft-select');
} else {
if (ar_index < 0) {
// date does not exist in user_selected_dates array
// add selected date into user_selected_dates array
user_selected_dates.push(selected_date);
// mark this day as selected
$this.addClass('selected');
} else {
// date already added
// remove this date from array
user_selected_dates.splice(ar_index, 1);
// mark as not selected
$this.removeClass('selected');
}
}
}
function generate_hidden_input_fields(value) {
$form.append(
'<input class="andp-hidden-dates" type="hidden" data-cur_cal_id="' +
cur_cal_id +
'" name="' +
input_field_name +
'[]" value="' +
value +
'">'
);
}
$(document).keydown(function (event) {
const $calendar = $('.andp-datepicker-container.open');
const is_open = $calendar.length;
// check if datepicker is open
if (!is_open) {
return;
}
switch (event.which) {
case 37: // Left arrow key
$calendar.find('.andp-prev.andp-change-months').click();
break;
case 39: // Right arrow key
$calendar.find('.andp-next.andp-change-months').click();
break;
}
});
function DateConverter() {
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], //2000
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], //2001