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