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