|
17 | 17 | 'use strict'; |
18 | 18 |
|
19 | 19 | var DAYS = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']; |
| 20 | + |
| 21 | + /** |
| 22 | + * How far back a day can still be chosen. |
| 23 | + * |
| 24 | + * The grid used to stop at today, which is right for a booking being taken |
| 25 | + * over the phone and wrong for the other half of what this form does: |
| 26 | + * writing down a hire that already happened. A car goes out, comes back, and |
| 27 | + * the paperwork is caught up at the end of the week -- and the days that |
| 28 | + * hire actually ran were greyed out. |
| 29 | + * |
| 30 | + * Nothing else was stopping it. The date boxes carry no min and the server |
| 31 | + * has no rule about it, so a past date typed by hand always saved. This only |
| 32 | + * ever blocked the clicking. |
| 33 | + * |
| 34 | + * A week rather than forever: far enough to catch up the week's paperwork, |
| 35 | + * near enough that a mistyped year does not quietly land a booking in 2019. |
| 36 | + * Days before it stay disabled, and the month arrows still page back as far |
| 37 | + * as anyone wants to look. |
| 38 | + */ |
| 39 | + var BACKDATE_DAYS = 7; |
| 40 | + |
| 41 | + /** The earliest day the grid will let anyone click. */ |
| 42 | + function earliestSelectable() { |
| 43 | + var d = new Date(); |
| 44 | + d.setDate(d.getDate() - BACKDATE_DAYS); |
| 45 | + return iso(d); |
| 46 | + } |
20 | 47 | var ranges = {}; // vehicle id -> [[from, to], ...] |
21 | 48 | var loaded = false; |
22 | 49 |
|
|
72 | 99 | /** Draws one month into `host`, for `input`. */ |
73 | 100 | function draw(host, input, busy, month) { |
74 | 101 | var today = iso(new Date()); |
| 102 | + var earliest = earliestSelectable(); |
75 | 103 | var first = new Date(month.getFullYear(), month.getMonth(), 1); |
76 | 104 | var count = new Date(month.getFullYear(), month.getMonth() + 1, 0).getDate(); |
77 | 105 | var lead = (first.getDay() + 6) % 7; |
|
91 | 119 | var date = new Date(month.getFullYear(), month.getMonth(), d); |
92 | 120 | var key = iso(date); |
93 | 121 | var out = busy.has(key); |
94 | | - var past = key < today; |
| 122 | + var past = key < earliest; |
| 123 | + // Backdated but still selectable: shown for what it is, so nobody |
| 124 | + // records last Tuesday's hire thinking they booked next Tuesday's. |
| 125 | + var back = !past && key < today; |
95 | 126 | var on = key === input.value; |
96 | 127 | cells += '<button type="button" class="cal-day' + |
97 | | - (on ? ' is-on' : '') + (out ? ' is-busy' : '') + '"' + |
| 128 | + (on ? ' is-on' : '') + (out ? ' is-busy' : '') + (back ? ' is-back' : '') + '"' + |
98 | 129 | (out || past ? ' disabled' : '') + |
99 | 130 | ' data-date="' + key + '"' + |
100 | 131 | ' aria-label="' + date.toLocaleDateString('en-IN', { day: 'numeric', month: 'long' }) + |
101 | | - (out ? ' — already booked' : '') + '">' + d + '</button>'; |
| 132 | + (out ? ' — already booked' : '') + (back ? ' — a past date' : '') + '">' + d + '</button>'; |
102 | 133 | } |
103 | 134 |
|
104 | 135 | host.innerHTML = head + names + '<div class="cal-grid">' + cells + '</div>' + |
105 | | - '<p class="cal-key"><span class="cal-swatch" aria-hidden="true"></span>Already booked</p>'; |
| 136 | + '<p class="cal-key"><span class="cal-swatch" aria-hidden="true"></span>Already booked' + |
| 137 | + '<span class="cal-swatch cal-swatch-back" aria-hidden="true"></span>Past date</p>'; |
106 | 138 |
|
107 | 139 | host.querySelectorAll('.cal-nav').forEach(function (b) { |
108 | 140 | b.addEventListener('click', function () { |
|
0 commit comments