Bug 31261: Introduce flatpickr-futureinclusive
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / includes / calendar.inc
1 [% USE Asset %]
2 [% USE Koha %]
3 [% USE raw %]
4 <!-- calendar.inc -->
5 [% FILTER collapse %]
6 <script>
7     var debug    = "[% debug | html %]";
8     var dateformat_pref = "[% Koha.Preference('dateformat') | html %]";
9     var flatpickr_dateformat_string = "";
10     switch ( dateformat_pref ){
11         case "us":
12             flatpickr_dateformat_string = "m/d/Y";
13             break;
14         case "metric":
15             flatpickr_dateformat_string = "d/m/Y";
16             break;
17         case "dmydot":
18             flatpickr_dateformat_string = "d.m.Y";
19             break;
20         default:
21             flatpickr_dateformat_string = "Y-m-d";
22     }
23     var sentmsg = 0;
24     var bidi = [% IF(bidi) %] true[% ELSE %] false[% END %];
25     var calendarFirstDayOfWeek = '[% Koha.Preference('CalendarFirstDayOfWeek') | html %]';
26     var flatpickr_timeformat_string = [% IF Koha.Preference('TimeFormat') == '12hr' %]"G:i K"[% ELSE %]"H:i"[% END %];
27     var flatpickr_timeformat = [% IF Koha.Preference('TimeFormat') == '12hr' %]false[% ELSE %]true[% END %];
28 </script>
29 <!-- / calendar.inc -->
30 [% Asset.js("js/calendar.js") | $raw %]
31 [% Asset.js("lib/flatpickr/flatpickr.min.js") | $raw %]
32 [% Asset.js("lib/flatpickr/shortcut-buttons-flatpickr.min.js") | $raw %]
33 <script>
34     flatpickr.l10ns.default.weekdays = flatpickr_weekdays;
35     flatpickr.l10ns.default.months   = flatpickr_months;
36     let flatpickr_defaults = {
37         allowInput: true,
38         dateFormat: "Y-m-d",
39         altInput: true,
40         altFormat: flatpickr_dateformat_string,
41         altInputClass: 'flatpickr-input',
42         nextArrow: '<i class="fa fa-fw fa-arrow-right"></i>',
43         prevArrow: '<i class="fa fa-fw fa-arrow-left"></i>',
44         time_24hr: flatpickr_timeformat,
45         defaultHour: 23,
46         defaultMinute: 59,
47         locale: {
48             "firstDayOfWeek": calendarFirstDayOfWeek
49         },
50         onReady: function( selectedDates, dateStr, instance ){
51             /* When flatpickr instance is created, automatically append a "clear date" link */
52             $(instance.input).find('~input.flatpickr:first')
53                 /* Add a wrapper element so that we can prevent the clear button from wrapping */
54                 .wrap("<span class='flatpickr_wrapper'></span>")
55                 .attr("autocomplete", "off")
56                 .after( $("<a/>")
57                     .attr("href","#")
58                     .addClass("clear_date")
59                     .on("click", function(e){
60                         e.preventDefault();
61                         instance.clear();
62                     })
63                     .addClass("fa fa-fw fa-remove")
64                     .attr("aria-hidden", true)
65                     .attr("aria-label", _("Clear date") )
66                 ).keydown(function(e) {
67                     var key = (event.keyCode ? event.keyCode : event.which);
68                     if ( key == 40 ) {
69                         instance.set('allowInput',false);
70                     }
71                 });
72         },
73         onClose: function( selectedDates, dateText, instance) {
74             validate_date( dateText, instance );
75             var thisInput = instance.input;
76             if ( thisInput.hasAttribute('data-date_to') ) {
77                 var endPicker = document.querySelector("#"+thisInput.dataset.date_to)._flatpickr;
78                 endPicker.set('minDate', selectedDates[0]);
79             }
80
81             let = on_close_focus = $(thisInput).data('flatpickr-on-close-focus');
82             if ( on_close_focus ) {
83                 $(on_close_focus).focus();
84             }
85         },
86         plugins: [
87           ShortcutButtonsPlugin({
88             button: [
89               {
90                 label: _("Yesterday")
91               },
92               {
93                 label: _("Today")
94               },
95               {
96                 label: _("Tomorrow")
97               }
98             ],
99             label: _("or"),
100             onClick: (index, fp) => {
101               let date;
102               let hh = 23, mm = 59;
103               switch (index) {
104                 case 0:
105                   date = new Date().fp_incr(-1);
106                   break;
107                 case 1:
108                   date = new Date();
109                   if ( $(fp.input).data("flatpickr-pastinclusive") === true ) {
110                     hh = date.getHours();
111                     mm = date.getMinutes();
112                   }
113                   break;
114                 case 2:
115                   date = new Date().fp_incr(1);
116                   break;
117               }
118               date.setHours(hh, mm, 0, 0);
119               fp.setDate(date);
120             }
121           })
122         ]
123     };
124
125     flatpickr.setDefaults(flatpickr_defaults);
126
127     $(document).ready(function(){
128         $(".flatpickr").each(function(){
129             let options = {};
130             let refresh_max_date = 0;
131             let disable_buttons = [];
132
133             if( $(this).data("flatpickr-futureinclusive") === true
134              || $(this).data("flatpickr-futuredate") === true ) {
135                 let original_date = $(this).val();
136                 if ( original_date ) {
137                     original_date = Date_from_syspref( original_date ).getTime();
138                     let tomorrow = new Date().fp_incr(1).getTime();
139
140                     options['enable'] = [function(date){
141                         date = date.getTime();
142                         if ( date == original_date ) return true;
143                         if ( date >= tomorrow)       return true;
144                     }];
145                 }
146                 else {
147                     if( $(this).data("flatpickr-futureinclusive") === true ) {
148                         options['minDate'] = new Date().setHours(00, 00, 00, 00);
149                     } else {
150                         options['minDate'] = new Date().fp_incr(1);
151                     }
152                 }
153
154                 disable_buttons.push(0); /* Yesterday */
155
156                 if ( $(this).data("flatpickr-futuredate") === true ) {
157                     disable_buttons.push(1); /* Today */
158                 }
159             }
160             if( $(this).data("flatpickr-pastinclusive") === true ) {
161                 options['maxDate'] = new Date(); /* Not today or hh:mm will be 00:00 */
162                 refresh_max_date = 1;
163                 disable_buttons.push(2); /* Tomorrow */
164             }
165             if( $(this).data("flatpickr-pastdate") === true ) {
166                 options['maxDate'] = new Date().fp_incr(-1).setHours(23, 59, 00, 00);
167                 disable_buttons.push(1); /* Today */
168                 disable_buttons.push(2); /* Tomorrow */
169             }
170             if ( $(this).data('flatpickr-enable-time') === true ) {
171                 options['enableTime'] = true;
172                 options['dateFormat'] = "Y-m-d H:i";
173                 options['altFormat'] = flatpickr_dateformat_string + " " + flatpickr_timeformat_string;
174             }
175
176             let fp = $(this).flatpickr(options);
177
178             $(disable_buttons).each(function(index, value){
179                 $(fp.calendarContainer).find(".shortcut-buttons-flatpickr-button[data-index='"+value+"']").prop("disabled", "disabled");
180             });
181
182             if ( refresh_max_date ) {
183                 /* Refresh the maxDate every 30 secondes to make sure the user will not
184                    be stuck with the minute passed.
185                    Adding 1 minute to not introduce a gap.
186                    Example: last update at 40s, a new minute passed at 00.
187                    Between 00 and 10s the user won't be able click 'Today'.
188                 */
189                 setInterval(() => {
190                     let now = new Date();
191                     fp.set("maxDate", now.setMinutes(now.getMinutes() + 1));
192                 }, 30000);
193             }
194         });
195     });
196 </script>
197 [% END %]