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