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 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 10 | /* |
| 11 | * Localizations |
| 12 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 13 | var loc = KorAP.Locale; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 14 | loc.WDAY = loc.WDAY || [ |
| 15 | 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su' |
| 16 | ]; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 17 | loc.MONTH = loc.MONTH || [ |
| 18 | 'January', 'February', 'March', 'April', |
| 19 | 'May', 'June', 'July', 'August', |
| 20 | 'September', 'October', 'November', |
| 21 | 'December' |
| 22 | ]; |
| 23 | |
| 24 | var d = document; |
| 25 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 26 | // The datepicker class |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 27 | return { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Create a new datepicker view. |
| 31 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 32 | create : function () { |
| 33 | return Object.create(this)._init(); |
| 34 | }, |
| 35 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 36 | // Init datepicker |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 37 | _init : function () { |
Nils Diewald | 652e5f4 | 2015-05-10 18:11:45 +0000 | [diff] [blame] | 38 | this._selected = []; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 39 | return this; |
| 40 | }, |
| 41 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 42 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Get or select a specific date. |
| 45 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 46 | select : function (year, month, day) { |
| 47 | if (arguments.length >= 1) { |
| 48 | this._selected = {'year' : year}; |
| 49 | if (arguments.length >= 2) { |
| 50 | this._selected['month'] = month; |
| 51 | if (arguments.length >= 3) |
| 52 | this._selected['day'] = day; |
| 53 | }; |
| 54 | return this; |
| 55 | }; |
| 56 | return this._selected; |
| 57 | }, |
| 58 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Select a specific date and |
| 62 | * init the accompanied action. |
| 63 | */ |
| 64 | set : function (year, month, day) { |
| 65 | this.select(year, month, day); |
| 66 | if (this._click !== undefined) |
| 67 | this._click(this._selected); |
| 68 | else |
| 69 | console.dir(this._selected); |
| 70 | }, |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Set the action for clicking as a callback. |
| 75 | * The callback will retrieve a an object with |
| 76 | * an optional year attribute, |
| 77 | * an optional month attribute, |
| 78 | * and an optional day attribute |
| 79 | */ |
| 80 | onclick : function (cb) { |
| 81 | this._click = cb; |
| 82 | }, |
| 83 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 84 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 85 | /** |
| 86 | * Show the datepicker. |
| 87 | * Will either show the selected year/month |
| 88 | * or the current date. |
| 89 | * Will return the element for appending to the dom. |
| 90 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 91 | show : function (year, month) { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 92 | var e = this._element = d.createElement('div'); |
| 93 | e.setAttribute('tabindex', 0); |
| 94 | e.style.outline = 0; |
| 95 | e.classList.add('datepicker'); |
| 96 | |
| 97 | var today = new Date(); |
| 98 | |
| 99 | // Show year |
| 100 | this._showYear = year ? year : |
| 101 | (this._selected['year'] ? this._selected['year'] : |
Nils Diewald | 652e5f4 | 2015-05-10 18:11:45 +0000 | [diff] [blame] | 102 | today.getYear() + 1900); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 103 | |
| 104 | // Show month |
| 105 | this._showMonth = month ? month : |
| 106 | (this._selected['month'] ? this._selected['month'] : |
| 107 | (today.getMonth() + 1)); |
| 108 | |
| 109 | // Append all helpers |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 110 | this._element.appendChild(this._monthHelper()); |
| 111 | this._element.appendChild(this._yearHelper()); |
| 112 | this._element.appendChild(this._dayHelper()); |
| 113 | return this._element; |
| 114 | }, |
| 115 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 116 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 117 | /** |
| 118 | * Get the HTML element associated with the datepicker. |
| 119 | */ |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 120 | element : function () { |
| 121 | return this._element; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 122 | }, |
| 123 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 124 | |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 125 | /** |
| 126 | * Get the current date in string format. |
| 127 | */ |
| 128 | today : function () { |
| 129 | var today = new Date(); |
| 130 | var str = today.getYear() + 1900; |
| 131 | var m = today.getMonth() + 1; |
| 132 | var d = today.getDate(); |
| 133 | str += '-' + (m < 10 ? '0' + m : m); |
| 134 | str += '-' + (d < 10 ? '0' + d : d); |
| 135 | return str; |
| 136 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 137 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 138 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 139 | /** |
| 140 | * Increment the year. |
| 141 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 142 | incrYear : function () { |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 143 | if (this._showYear < 9999) { |
| 144 | this._showYear++; |
| 145 | this._updateYear(); |
| 146 | this._updateMonth(); |
| 147 | this._updateDay(); |
| 148 | return this; |
| 149 | }; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 150 | return; |
| 151 | }, |
| 152 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 153 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 154 | /** |
| 155 | * Decrement the year. |
| 156 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 157 | decrYear : function () { |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 158 | if (this._showYear > 0) { |
| 159 | this._showYear--; |
| 160 | this._updateYear(); |
| 161 | this._updateMonth(); |
| 162 | this._updateDay(); |
| 163 | return this; |
| 164 | }; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 165 | return; |
| 166 | }, |
| 167 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 168 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 169 | /** |
| 170 | * Increment the month. |
| 171 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 172 | incrMonth : function () { |
| 173 | this._showMonth++; |
| 174 | if (this._showMonth > 12) { |
| 175 | this._showMonth = 1; |
| 176 | this.incrYear(); |
| 177 | } |
| 178 | else { |
| 179 | this._updateMonth(); |
| 180 | this._updateDay(); |
| 181 | }; |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 182 | return this; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 183 | }, |
| 184 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 185 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 186 | /** |
| 187 | * Decrement the month. |
| 188 | */ |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 189 | decrMonth : function () { |
| 190 | this._showMonth--; |
| 191 | if (this._showMonth < 1) { |
| 192 | this._showMonth = 12; |
| 193 | this.decrYear(); |
| 194 | } |
| 195 | else { |
| 196 | this._updateMonth(); |
| 197 | this._updateDay(); |
| 198 | }; |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 199 | |
| 200 | return this; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 201 | }, |
| 202 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 203 | |
| 204 | // Create the year helper element. |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 205 | _yearHelper : function () { |
| 206 | var year = d.createElement('div'); |
| 207 | year.classList.add('year'); |
| 208 | |
| 209 | // Decrement year |
| 210 | year.appendChild(d.createElement('span')) |
| 211 | .onclick = this.decrYear.bind(this); |
| 212 | |
| 213 | this._yElement = year.appendChild(d.createElement('span')); |
| 214 | this._yElement.appendChild(document.createTextNode(this._showYear)); |
| 215 | |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 216 | this._yElement.onclick = function () { |
| 217 | this.set(this._showYear); |
| 218 | }.bind(this); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 219 | this._selectYear(); |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 220 | |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 221 | // Increment year |
| 222 | year.appendChild(d.createElement('span')) |
| 223 | .onclick = this.incrYear.bind(this); |
| 224 | |
| 225 | return year; |
| 226 | }, |
| 227 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 228 | // Update the year helper view. |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 229 | _updateYear : function () { |
| 230 | this._yElement.firstChild.data = this._showYear; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 231 | this._selectYear(); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 232 | }, |
| 233 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 234 | |
| 235 | // Check if the viewed year is current |
| 236 | _selectYear : function () { |
| 237 | if (this._showYear === this.select()['year']) |
| 238 | this._yElement.classList.add('selected'); |
| 239 | else |
| 240 | this._yElement.classList.remove('selected'); |
| 241 | }, |
| 242 | |
| 243 | |
| 244 | // Create the month helper element. |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 245 | _monthHelper : function () { |
| 246 | var month = d.createElement('div'); |
| 247 | month.classList.add('month'); |
| 248 | |
| 249 | // Decrement month |
| 250 | month.appendChild(d.createElement('span')) |
| 251 | .onclick = this.decrMonth.bind(this); |
| 252 | |
| 253 | this._mElement = month.appendChild(d.createElement('span')); |
| 254 | this._mElement.appendChild( |
| 255 | document.createTextNode(loc.MONTH[this._showMonth-1]) |
| 256 | ); |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 257 | this._mElement.onclick = function () { |
| 258 | this.set(this._showYear, this._showMonth); |
| 259 | }.bind(this); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 260 | |
| 261 | this._selectMonth(); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 262 | |
| 263 | // Increment month |
| 264 | month.appendChild(d.createElement('span')) |
| 265 | .onclick = this.incrMonth.bind(this); |
| 266 | |
| 267 | return month; |
| 268 | }, |
| 269 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 270 | // Update the month helper view. |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 271 | _updateMonth : function () { |
| 272 | this._mElement.firstChild.data = loc.MONTH[this._showMonth-1]; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 273 | this._selectMonth(); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 274 | }, |
| 275 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 276 | |
| 277 | // Check if the viewed month is current |
| 278 | _selectMonth : function () { |
| 279 | if (this._showYear === this.select()['year'] && |
| 280 | this._showMonth === this.select()['month']) |
| 281 | this._mElement.classList.add('selected'); |
| 282 | else |
| 283 | this._mElement.classList.remove('selected'); |
| 284 | }, |
| 285 | |
| 286 | |
| 287 | // Create the day (calendar) helper element. |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 288 | _dayHelper : function () { |
| 289 | var table = d.createElement('table'); |
| 290 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 291 | // Localized day view |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 292 | var tr = table.appendChild(d.createElement('thead')) |
| 293 | .appendChild(d.createElement('tr')); |
| 294 | for (var i = 0; i < 7; i++) { |
| 295 | tr.appendChild(d.createElement('th')) |
| 296 | .appendChild(d.createTextNode(loc.WDAY[i])); |
| 297 | }; |
| 298 | |
| 299 | this._dBElement = this._dayBody(); |
| 300 | |
| 301 | table.appendChild(this._dBElement); |
| 302 | return table; |
| 303 | }, |
| 304 | |
| 305 | _dayBody : function () { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 306 | var showDate = new Date( |
| 307 | this._showYear, |
| 308 | this._showMonth - 1, |
| 309 | 1, |
| 310 | 0, |
| 311 | 0, |
| 312 | 0, |
| 313 | 0 |
| 314 | ); |
| 315 | var date = new Date( |
| 316 | this._showYear, |
| 317 | this._showMonth - 1, |
| 318 | 1, |
| 319 | 0, |
| 320 | 0, |
| 321 | 0, |
| 322 | 0 |
| 323 | ); |
| 324 | var today = new Date(); |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 325 | var that = this; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 326 | |
| 327 | // What happens, in case someone clicks |
| 328 | // on a date |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 329 | var click = function () { |
| 330 | that.set( |
| 331 | that._showYear, |
| 332 | that._showMonth, |
| 333 | parseInt(this.firstChild.data) |
| 334 | ); |
| 335 | }; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 336 | |
| 337 | // Skip back to the previous monday (may be in the last month) |
| 338 | date.setDate(date.getDate() - ((date.getDay() + 6) % 7)); |
| 339 | |
| 340 | var tb = d.createElement('tbody'); |
| 341 | |
| 342 | var s = this.select(); |
| 343 | |
| 344 | // Iterate over all days of the table |
| 345 | while (1) { |
| 346 | |
| 347 | // Loop through the week |
| 348 | var tr = tb.appendChild(d.createElement('tr')); |
| 349 | for (var i = 0; i < 7; i++) { |
| 350 | var td = tr.appendChild(d.createElement('td')); |
| 351 | |
| 352 | // Not part of the current month |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 353 | if (date.getMonth() !== showDate.getMonth()) { |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 354 | td.classList.add('out'); |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 355 | } |
| 356 | else { |
| 357 | td.onclick = click; |
| 358 | }; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 359 | |
| 360 | // This is the current day |
| 361 | if (date.getDate() === today.getDate() && |
| 362 | date.getMonth() === today.getMonth() && |
| 363 | date.getFullYear() === today.getFullYear()) { |
| 364 | td.classList.add('today'); |
| 365 | }; |
| 366 | |
| 367 | // This is the day selected |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 368 | if (s && s['day']) { |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 369 | if (date.getDate() === s['day'] && |
| 370 | date.getMonth() === s['month']-1 && |
| 371 | date.getFullYear() === s['year']) { |
| 372 | td.classList.add('selected'); |
| 373 | }; |
| 374 | }; |
| 375 | |
| 376 | // Add the current day to the table |
| 377 | td.appendChild( |
| 378 | d.createTextNode(date.getDate()) |
| 379 | ); |
| 380 | |
| 381 | // Next day |
| 382 | date.setDate(date.getDate() + 1); |
| 383 | }; |
| 384 | |
| 385 | if (date.getMonth() !== showDate.getMonth()) |
| 386 | break; |
| 387 | }; |
| 388 | return tb; |
| 389 | }, |
| 390 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 391 | // Update the calendar view |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 392 | _updateDay : function () { |
| 393 | var newBody = this._dayBody(); |
| 394 | this._dBElement.parentNode.replaceChild( |
| 395 | newBody, |
| 396 | this._dBElement |
| 397 | ); |
| 398 | this._dBElement = newBody; |
| 399 | } |
| 400 | }; |
| 401 | }); |