Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 1 | /** |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 2 | * Date picker for the |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 3 | * Virtual Collection builder. |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 4 | * |
| 5 | * @author Nils Diewald |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 6 | */ |
| 7 | define(['util'], function () { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 8 | |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 9 | "use strict"; |
| 10 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 11 | KorAP._validDateMatchRE = new RegExp("^(?:[lg]?eq|ne)$"); |
| 12 | KorAP._validDateRE = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$"); |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 13 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 14 | /* |
| 15 | * Localizations |
| 16 | */ |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 17 | const loc = KorAP.Locale; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 18 | loc.WDAY = loc.WDAY || [ |
| 19 | 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su' |
| 20 | ]; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 21 | loc.MONTH = loc.MONTH || [ |
| 22 | 'January', 'February', 'March', 'April', |
| 23 | 'May', 'June', 'July', 'August', |
| 24 | 'September', 'October', 'November', |
| 25 | 'December' |
| 26 | ]; |
| 27 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 28 | const d = document; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 29 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 30 | // The datepicker class |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 31 | return { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Create a new datepicker view. |
| 35 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 36 | create : function () { |
| 37 | return Object.create(this)._init(); |
| 38 | }, |
| 39 | |
Akron | 516b6f9 | 2018-01-03 17:46:59 +0100 | [diff] [blame] | 40 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 41 | // Init datepicker |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 42 | _init : function () { |
Nils Diewald | 652e5f4 | 2015-05-10 18:11:45 +0000 | [diff] [blame] | 43 | this._selected = []; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 44 | return this; |
| 45 | }, |
| 46 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 47 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 48 | /** |
| 49 | * Get or select a specific date. |
| 50 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 51 | select : function (year, month, day) { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 52 | const t = this; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 53 | if (arguments.length >= 1) { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 54 | t._selected = {'year' : year}; |
| 55 | t._showYear = year; |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 56 | if (arguments.length >= 2) { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 57 | t._selected['month'] = month; |
| 58 | t._showMonth = month; |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 59 | if (arguments.length >= 3) { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 60 | t._selected['day'] = day; |
| 61 | t._showDay = day; |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 62 | }; |
| 63 | }; |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 64 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 65 | return t; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 66 | }; |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 67 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 68 | return t._selected; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 69 | }, |
| 70 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Select a specific date and |
| 74 | * init the accompanied action. |
| 75 | */ |
| 76 | set : function (year, month, day) { |
| 77 | this.select(year, month, day); |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 78 | this._store(); |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 79 | }, |
| 80 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 81 | |
| 82 | // Store the selected value |
| 83 | _store : function () { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 84 | if (this._click !== undefined) |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 85 | this._click(this._selected); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 86 | else |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 87 | console.dir(this._selected); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 88 | }, |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 89 | |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 90 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 91 | /** |
| 92 | * Set the action for clicking as a callback. |
| 93 | * The callback will retrieve a an object with |
| 94 | * an optional year attribute, |
| 95 | * an optional month attribute, |
| 96 | * and an optional day attribute |
| 97 | */ |
| 98 | onclick : function (cb) { |
| 99 | this._click = cb; |
| 100 | }, |
| 101 | |
Akron | 516b6f9 | 2018-01-03 17:46:59 +0100 | [diff] [blame] | 102 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 103 | /** |
| 104 | * The associated input field. |
| 105 | */ |
Akron | 516b6f9 | 2018-01-03 17:46:59 +0100 | [diff] [blame] | 106 | input : function () { |
| 107 | return this._input; |
| 108 | }, |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 109 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 110 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 111 | /** |
| 112 | * Show the datepicker. |
| 113 | * Will either show the selected year/month |
| 114 | * or the current date. |
| 115 | * Will return the element for appending to the dom. |
| 116 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 117 | show : function (year, month) { |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 118 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 119 | const e = this._element = d.createElement('div'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 120 | e.setAttribute('tabindex', 0); |
| 121 | e.style.outline = 0; |
| 122 | e.classList.add('datepicker'); |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 123 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 124 | const today = new Date(); |
| 125 | const t = this; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 126 | |
| 127 | // Show year |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 128 | t._showYear = (year !== undefined) ? year : |
| 129 | (t._selected['year'] ? this._selected['year'] : |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 130 | today.getYear() + 1900); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 131 | |
| 132 | // Show month |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 133 | t._showMonth = month ? month : |
| 134 | (t._selected['month'] ? t._selected['month'] : |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 135 | (today.getMonth() + 1)); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 136 | |
| 137 | // Append all helpers |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 138 | e.appendChild(t._monthHelper()); |
| 139 | e.appendChild(t._yearHelper()); |
| 140 | e.appendChild(t._dayHelper()); |
| 141 | t._input = e.appendChild(t._stringHelper()); |
Akron | 516b6f9 | 2018-01-03 17:46:59 +0100 | [diff] [blame] | 142 | |
| 143 | // Always focus |
| 144 | e.addEventListener( |
| 145 | 'mousedown', |
| 146 | function (ev) { |
| 147 | this._inField = true |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 148 | }.bind(t) |
Akron | 516b6f9 | 2018-01-03 17:46:59 +0100 | [diff] [blame] | 149 | ); |
| 150 | |
| 151 | e.addEventListener( |
| 152 | 'mouseup', |
| 153 | function (ev) { |
| 154 | this._inField = false; |
| 155 | this._input.focus(); |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 156 | }.bind(t) |
Akron | 516b6f9 | 2018-01-03 17:46:59 +0100 | [diff] [blame] | 157 | ); |
| 158 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 159 | t._input.addEventListener( |
Akron | 516b6f9 | 2018-01-03 17:46:59 +0100 | [diff] [blame] | 160 | 'blur', |
| 161 | function (ev) { |
| 162 | if (!this._inField) { |
| 163 | if (this.fromString(this._input.value)) { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 164 | this._store(); |
Akron | 516b6f9 | 2018-01-03 17:46:59 +0100 | [diff] [blame] | 165 | }; |
| 166 | }; |
| 167 | ev.halt(); |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 168 | }.bind(t) |
Akron | 516b6f9 | 2018-01-03 17:46:59 +0100 | [diff] [blame] | 169 | ); |
| 170 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 171 | t._input.focus(); |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 172 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 173 | return t._element; |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 174 | }, |
| 175 | |
Akron | c59f732 | 2016-04-20 13:46:05 +0200 | [diff] [blame] | 176 | _stringHelper : function () { |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 177 | |
| 178 | // Create element |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 179 | // Add input field |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 180 | const input = d.createElement('input'); |
Akron | c59f732 | 2016-04-20 13:46:05 +0200 | [diff] [blame] | 181 | input.value = this.toString(); |
| 182 | input.setAttribute('tabindex', 0); |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 183 | |
Akron | c59f732 | 2016-04-20 13:46:05 +0200 | [diff] [blame] | 184 | input.addEventListener( |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 185 | 'keyup', |
| 186 | function (e) { |
| 187 | if (this.fromString(input.value)) { |
| 188 | this._updateYear(); |
| 189 | this._updateMonth(); |
| 190 | this._updateDay(); |
| 191 | }; |
| 192 | }.bind(this) |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 193 | ); |
| 194 | |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 195 | input.addEventListener( |
| 196 | 'keypress', |
| 197 | function (e) { |
| 198 | if (e.keyCode == 13) { |
| 199 | if (this.fromString(input.value)) |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 200 | this._store(); |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 201 | |
| 202 | e.halt(); |
| 203 | return false; |
| 204 | } |
| 205 | }.bind(this) |
| 206 | ) |
| 207 | |
Akron | c59f732 | 2016-04-20 13:46:05 +0200 | [diff] [blame] | 208 | return input; |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 209 | }, |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 210 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 211 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 212 | /** |
| 213 | * Get the HTML element associated with the datepicker. |
| 214 | */ |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 215 | element : function () { |
| 216 | return this._element; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 217 | }, |
| 218 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 219 | |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 220 | /** |
| 221 | * Get the current date in string format. |
| 222 | */ |
| 223 | today : function () { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 224 | const today = new Date(); |
| 225 | let str = today.getYear() + 1900; |
| 226 | const m = today.getMonth() + 1; |
| 227 | const d = today.getDate(); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 228 | str += '-' + (m < 10 ? '0' + m : m); |
| 229 | str += '-' + (d < 10 ? '0' + d : d); |
| 230 | return str; |
| 231 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 232 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 233 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 234 | /** |
| 235 | * Stringification |
| 236 | */ |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 237 | toString : function () { |
| 238 | // There are values selected |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 239 | let v = ''; |
| 240 | const s = this._selected; |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 241 | if (s['year']) { |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 242 | v += s['year']; |
| 243 | if (s['month']) { |
| 244 | v += '-'; |
| 245 | v += s['month'] < 10 ? '0' + s['month'] : s['month']; |
| 246 | if (s['day']) { |
| 247 | v += '-'; |
| 248 | v += s['day'] < 10 ? '0' + s['day'] : s['day']; |
| 249 | }; |
| 250 | }; |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 251 | }; |
| 252 | return v; |
| 253 | }, |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 254 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 255 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 256 | /** |
| 257 | * Increment the year. |
| 258 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 259 | incrYear : function () { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 260 | const t = this; |
| 261 | if (t._showYear < 9999) { |
| 262 | t._showYear++; |
| 263 | t._updateYear(); |
| 264 | t._updateMonth(); |
| 265 | t._updateDay(); |
| 266 | return t; |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 267 | }; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 268 | return; |
| 269 | }, |
| 270 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 271 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 272 | /** |
| 273 | * Decrement the year. |
| 274 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 275 | decrYear : function () { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 276 | const t = this; |
| 277 | if (t._showYear > 0) { |
| 278 | t._showYear--; |
| 279 | t._updateYear(); |
| 280 | t._updateMonth(); |
| 281 | t._updateDay(); |
| 282 | return t; |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 283 | }; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 284 | return; |
| 285 | }, |
| 286 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 287 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 288 | /** |
| 289 | * Increment the month. |
| 290 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 291 | incrMonth : function () { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 292 | const t = this; |
| 293 | t._showMonth++; |
| 294 | if (t._showMonth > 12) { |
| 295 | t._showMonth = 1; |
| 296 | t.incrYear(); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 297 | } |
| 298 | else { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 299 | t._updateMonth(); |
| 300 | t._updateDay(); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 301 | }; |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 302 | return t; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 303 | }, |
| 304 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 305 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 306 | /** |
| 307 | * Decrement the month. |
| 308 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 309 | decrMonth : function () { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 310 | const t = this; |
| 311 | t._showMonth--; |
| 312 | if (t._showMonth < 1) { |
| 313 | t._showMonth = 12; |
| 314 | t.decrYear(); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 315 | } |
| 316 | else { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 317 | t._updateMonth(); |
| 318 | t._updateDay(); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 319 | }; |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 320 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 321 | return t; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 322 | }, |
| 323 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 324 | |
| 325 | // Create the year helper element. |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 326 | _yearHelper : function () { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 327 | const t = this; |
| 328 | const year = d.createElement('div'); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 329 | year.classList.add('year'); |
| 330 | |
| 331 | // Decrement year |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 332 | year.addE('span') |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 333 | .onclick = t.decrYear.bind(t); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 334 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 335 | t._yElement = year.addE('span'); |
| 336 | t._yElement.addT(t._showYear); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 337 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 338 | t._yElement.onclick = function () { |
| 339 | t.set(t._showYear); |
| 340 | }.bind(t); |
| 341 | t._selectYear(); |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 342 | |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 343 | // Increment year |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 344 | year.addE('span') |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 345 | .onclick = t.incrYear.bind(t); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 346 | |
| 347 | return year; |
| 348 | }, |
| 349 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 350 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 351 | // Update the year helper view. |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 352 | _updateYear : function () { |
| 353 | this._yElement.firstChild.data = this._showYear; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 354 | this._selectYear(); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 355 | }, |
| 356 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 357 | |
| 358 | // Check if the viewed year is current |
| 359 | _selectYear : function () { |
| 360 | if (this._showYear === this.select()['year']) |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 361 | this._yElement.classList.add('selected'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 362 | else |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 363 | this._yElement.classList.remove('selected'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 364 | }, |
| 365 | |
| 366 | |
| 367 | // Create the month helper element. |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 368 | _monthHelper : function () { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 369 | const t = this; |
| 370 | const month = d.createElement('div'); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 371 | month.classList.add('month'); |
| 372 | |
| 373 | // Decrement month |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 374 | month.addE('span') |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 375 | .onclick = t.decrMonth.bind(t); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 376 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 377 | t._mElement = month.addE('span'); |
| 378 | t._mElement.addT(loc.MONTH[t._showMonth-1]); |
| 379 | t._mElement.onclick = function () { |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 380 | this.set(this._showYear, this._showMonth); |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 381 | }.bind(t); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 382 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 383 | t._selectMonth(); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 384 | |
| 385 | // Increment month |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 386 | month.addE('span') |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 387 | .onclick = t.incrMonth.bind(t); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 388 | |
| 389 | return month; |
| 390 | }, |
| 391 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 392 | // Update the month helper view. |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 393 | _updateMonth : function () { |
Akron | c59f732 | 2016-04-20 13:46:05 +0200 | [diff] [blame] | 394 | if (this._showMonth === undefined || this._showMonth > 12) |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 395 | this._showMonth = 1; |
Akron | c59f732 | 2016-04-20 13:46:05 +0200 | [diff] [blame] | 396 | |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 397 | this._mElement.firstChild.data = loc.MONTH[this._showMonth-1]; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 398 | this._selectMonth(); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 399 | }, |
| 400 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 401 | |
| 402 | // Check if the viewed month is current |
| 403 | _selectMonth : function () { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 404 | const t = this; |
| 405 | if (t._showYear === t.select()['year'] && |
| 406 | t._showMonth === t.select()['month']) |
| 407 | t._mElement.classList.add('selected'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 408 | else |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 409 | t._mElement.classList.remove('selected'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 410 | }, |
| 411 | |
| 412 | |
| 413 | // Create the day (calendar) helper element. |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 414 | _dayHelper : function () { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 415 | const table = d.createElement('table'); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 416 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 417 | // Localized day view |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 418 | const tr = table.addE('thead').addE('tr'); |
| 419 | for (let i = 0; i < 7; i++) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 420 | tr.addE('th').addT(loc.WDAY[i]); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 421 | }; |
| 422 | |
| 423 | this._dBElement = this._dayBody(); |
| 424 | |
| 425 | table.appendChild(this._dBElement); |
| 426 | return table; |
| 427 | }, |
| 428 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 429 | |
| 430 | // Create day body for calendar table |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 431 | _dayBody : function () { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 432 | const showDate = new Date( |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 433 | this._showYear, |
| 434 | this._showMonth - 1, |
| 435 | 1, |
| 436 | 0, |
| 437 | 0, |
| 438 | 0, |
| 439 | 0 |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 440 | ); |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 441 | const date = new Date( |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 442 | this._showYear, |
| 443 | this._showMonth - 1, |
| 444 | 1, |
| 445 | 0, |
| 446 | 0, |
| 447 | 0, |
| 448 | 0 |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 449 | ); |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 450 | const today = new Date(); |
| 451 | const that = this; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 452 | |
| 453 | // What happens, in case someone clicks |
| 454 | // on a date |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 455 | const click = function () { |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 456 | that.set( |
| 457 | that._showYear, |
| 458 | that._showMonth, |
| 459 | parseInt(this.firstChild.data) |
| 460 | ); |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 461 | }; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 462 | |
| 463 | // Skip back to the previous monday (may be in the last month) |
| 464 | date.setDate(date.getDate() - ((date.getDay() + 6) % 7)); |
| 465 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 466 | const tb = d.createElement('tbody'); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 467 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 468 | const s = this.select(); |
| 469 | |
| 470 | let tr, i, td; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 471 | |
| 472 | // Iterate over all days of the table |
| 473 | while (1) { |
| 474 | |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 475 | // Loop through the week |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 476 | tr = tb.addE('tr'); |
| 477 | for (i = 0; i < 7; i++) { |
| 478 | td = tr.addE('td'); |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 479 | |
| 480 | // Not part of the current month |
| 481 | if (date.getMonth() !== showDate.getMonth()) { |
| 482 | td.classList.add('out'); |
| 483 | } |
| 484 | else { |
| 485 | td.onclick = click; |
| 486 | }; |
| 487 | |
| 488 | // This is the current day |
| 489 | if (date.getDate() === today.getDate() && |
| 490 | date.getMonth() === today.getMonth() && |
| 491 | date.getFullYear() === today.getFullYear()) { |
| 492 | td.classList.add('today'); |
| 493 | }; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 494 | |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 495 | // This is the day selected |
| 496 | if (s && s['day']) { |
| 497 | if (date.getDate() === s['day'] && |
| 498 | date.getMonth() === s['month']-1 && |
| 499 | date.getFullYear() === s['year']) { |
| 500 | td.classList.add('selected'); |
| 501 | }; |
| 502 | }; |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 503 | |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 504 | // Add the current day to the table |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 505 | td.addT(date.getDate()); |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 506 | |
| 507 | // Next day |
| 508 | date.setDate(date.getDate() + 1); |
| 509 | }; |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 510 | |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 511 | if (date.getMonth() !== showDate.getMonth()) |
| 512 | break; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 513 | }; |
| 514 | return tb; |
| 515 | }, |
| 516 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 517 | // Update the calendar view |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 518 | _updateDay : function () { |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 519 | const newBody = this._dayBody(); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 520 | this._dBElement.parentNode.replaceChild( |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 521 | newBody, |
| 522 | this._dBElement |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 523 | ); |
| 524 | this._dBElement = newBody; |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 525 | }, |
| 526 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 527 | |
| 528 | /** |
| 529 | * Parse date from string. |
| 530 | */ |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 531 | fromString : function (v) { |
Akron | c59f732 | 2016-04-20 13:46:05 +0200 | [diff] [blame] | 532 | if (v === undefined) |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 533 | return false; |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 534 | |
Akron | c59f732 | 2016-04-20 13:46:05 +0200 | [diff] [blame] | 535 | if (!KorAP._validDateRE.test(v)) |
Akron | d5620d4 | 2017-12-06 15:30:28 +0100 | [diff] [blame] | 536 | return false; |
Akron | 56ea7e3 | 2016-04-20 12:25:52 +0200 | [diff] [blame] | 537 | |
Akron | ff78fef | 2020-10-13 12:00:03 +0200 | [diff] [blame] | 538 | const d = v.split('-', 3); |
Akron | c59f732 | 2016-04-20 13:46:05 +0200 | [diff] [blame] | 539 | d[0] = parseInt(d[0]); |
| 540 | if (d[1]) d[1] = parseInt(d[1]); |
| 541 | if (d[2]) d[2] = parseInt(d[2]); |
| 542 | |
| 543 | // Select values |
| 544 | this.select(d[0], d[1], d[2]); |
| 545 | return true; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 546 | } |
| 547 | }; |
| 548 | }); |