Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Date picker for the |
| 3 | * Virtual Collection builder. |
| 4 | */ |
| 5 | define(['util'], function () { |
| 6 | "use strict"; |
| 7 | |
| 8 | var loc = KorAP.Locale; |
| 9 | |
| 10 | loc.WDAY = loc.WDAY || [ |
| 11 | 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su' |
| 12 | ]; |
| 13 | |
| 14 | loc.MONTH = loc.MONTH || [ |
| 15 | 'January', 'February', 'March', 'April', |
| 16 | 'May', 'June', 'July', 'August', |
| 17 | 'September', 'October', 'November', |
| 18 | 'December' |
| 19 | ]; |
| 20 | |
| 21 | var d = document; |
| 22 | |
| 23 | return { |
| 24 | create : function () { |
| 25 | return Object.create(this)._init(); |
| 26 | }, |
| 27 | |
| 28 | _init : function () { |
| 29 | return this; |
| 30 | }, |
| 31 | |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 32 | set : function (year, month, day) { |
| 33 | this.select(year, month, day); |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame^] | 34 | if (this._click !== undefined) |
| 35 | this._click(this._selected); |
| 36 | else |
| 37 | console.dir(this._selected); |
| 38 | }, |
| 39 | |
| 40 | onclick : function (cb) { |
| 41 | this._click = cb; |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 42 | }, |
| 43 | |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 44 | select : function (year, month, day) { |
| 45 | if (arguments.length >= 1) { |
| 46 | this._selected = {'year' : year}; |
| 47 | if (arguments.length >= 2) { |
| 48 | this._selected['month'] = month; |
| 49 | if (arguments.length >= 3) |
| 50 | this._selected['day'] = day; |
| 51 | }; |
| 52 | return this; |
| 53 | }; |
| 54 | return this._selected; |
| 55 | }, |
| 56 | |
| 57 | show : function (year, month) { |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame^] | 58 | this._element = d.createElement('div'); |
| 59 | this._element.classList.add('datepicker'); |
| 60 | this._showYear = year ? year : (this._selected['year'] ? this._selected['year'] : 2012); |
| 61 | this._showMonth = month ? month : (this._selected['month'] ? this._selected['month'] : 2); |
| 62 | this._element.appendChild(this._monthHelper()); |
| 63 | this._element.appendChild(this._yearHelper()); |
| 64 | this._element.appendChild(this._dayHelper()); |
| 65 | return this._element; |
| 66 | }, |
| 67 | |
| 68 | element : function () { |
| 69 | return this._element; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 70 | }, |
| 71 | |
| 72 | incrYear : function () { |
| 73 | this._showYear++; |
| 74 | this._updateYear(); |
| 75 | this._updateMonth(); |
| 76 | this._updateDay(); |
| 77 | return; |
| 78 | }, |
| 79 | |
| 80 | decrYear : function () { |
| 81 | this._showYear--; |
| 82 | this._updateYear(); |
| 83 | this._updateMonth(); |
| 84 | this._updateDay(); |
| 85 | return; |
| 86 | }, |
| 87 | |
| 88 | incrMonth : function () { |
| 89 | this._showMonth++; |
| 90 | if (this._showMonth > 12) { |
| 91 | this._showMonth = 1; |
| 92 | this.incrYear(); |
| 93 | } |
| 94 | else { |
| 95 | this._updateMonth(); |
| 96 | this._updateDay(); |
| 97 | }; |
| 98 | }, |
| 99 | |
| 100 | decrMonth : function () { |
| 101 | this._showMonth--; |
| 102 | if (this._showMonth < 1) { |
| 103 | this._showMonth = 12; |
| 104 | this.decrYear(); |
| 105 | } |
| 106 | else { |
| 107 | this._updateMonth(); |
| 108 | this._updateDay(); |
| 109 | }; |
| 110 | }, |
| 111 | |
| 112 | _yearHelper : function () { |
| 113 | var year = d.createElement('div'); |
| 114 | year.classList.add('year'); |
| 115 | |
| 116 | // Decrement year |
| 117 | year.appendChild(d.createElement('span')) |
| 118 | .onclick = this.decrYear.bind(this); |
| 119 | |
| 120 | this._yElement = year.appendChild(d.createElement('span')); |
| 121 | this._yElement.appendChild(document.createTextNode(this._showYear)); |
| 122 | |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 123 | this._yElement.onclick = function () { |
| 124 | this.set(this._showYear); |
| 125 | }.bind(this); |
| 126 | |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 127 | // Increment year |
| 128 | year.appendChild(d.createElement('span')) |
| 129 | .onclick = this.incrYear.bind(this); |
| 130 | |
| 131 | return year; |
| 132 | }, |
| 133 | |
| 134 | _updateYear : function () { |
| 135 | this._yElement.firstChild.data = this._showYear; |
| 136 | }, |
| 137 | |
| 138 | _monthHelper : function () { |
| 139 | var month = d.createElement('div'); |
| 140 | month.classList.add('month'); |
| 141 | |
| 142 | // Decrement month |
| 143 | month.appendChild(d.createElement('span')) |
| 144 | .onclick = this.decrMonth.bind(this); |
| 145 | |
| 146 | this._mElement = month.appendChild(d.createElement('span')); |
| 147 | this._mElement.appendChild( |
| 148 | document.createTextNode(loc.MONTH[this._showMonth-1]) |
| 149 | ); |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 150 | this._mElement.onclick = function () { |
| 151 | this.set(this._showYear, this._showMonth); |
| 152 | }.bind(this); |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 153 | |
| 154 | // Increment month |
| 155 | month.appendChild(d.createElement('span')) |
| 156 | .onclick = this.incrMonth.bind(this); |
| 157 | |
| 158 | return month; |
| 159 | }, |
| 160 | |
| 161 | _updateMonth : function () { |
| 162 | this._mElement.firstChild.data = loc.MONTH[this._showMonth-1]; |
| 163 | }, |
| 164 | |
| 165 | _dayHelper : function () { |
| 166 | var table = d.createElement('table'); |
| 167 | |
| 168 | var tr = table.appendChild(d.createElement('thead')) |
| 169 | .appendChild(d.createElement('tr')); |
| 170 | for (var i = 0; i < 7; i++) { |
| 171 | tr.appendChild(d.createElement('th')) |
| 172 | .appendChild(d.createTextNode(loc.WDAY[i])); |
| 173 | }; |
| 174 | |
| 175 | this._dBElement = this._dayBody(); |
| 176 | |
| 177 | table.appendChild(this._dBElement); |
| 178 | return table; |
| 179 | }, |
| 180 | |
| 181 | _dayBody : function () { |
| 182 | var showDate = new Date(this._showYear, this._showMonth - 1, 1, 0, 0, 0, 0); |
| 183 | var date = new Date(this._showYear, this._showMonth - 1, 1, 0, 0, 0, 0); |
| 184 | var today = new Date(); |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 185 | var that = this; |
| 186 | var click = function () { |
| 187 | that.set( |
| 188 | that._showYear, |
| 189 | that._showMonth, |
| 190 | parseInt(this.firstChild.data) |
| 191 | ); |
| 192 | }; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 193 | |
| 194 | // Skip back to the previous monday (may be in the last month) |
| 195 | date.setDate(date.getDate() - ((date.getDay() + 6) % 7)); |
| 196 | |
| 197 | var tb = d.createElement('tbody'); |
| 198 | |
| 199 | var s = this.select(); |
| 200 | |
| 201 | // Iterate over all days of the table |
| 202 | while (1) { |
| 203 | |
| 204 | // Loop through the week |
| 205 | var tr = tb.appendChild(d.createElement('tr')); |
| 206 | for (var i = 0; i < 7; i++) { |
| 207 | var td = tr.appendChild(d.createElement('td')); |
| 208 | |
| 209 | // Not part of the current month |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 210 | if (date.getMonth() !== showDate.getMonth()) { |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 211 | td.classList.add('out'); |
Nils Diewald | bdf79c5 | 2015-04-29 23:47:13 +0000 | [diff] [blame] | 212 | } |
| 213 | else { |
| 214 | td.onclick = click; |
| 215 | }; |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 216 | |
| 217 | // This is the current day |
| 218 | if (date.getDate() === today.getDate() && |
| 219 | date.getMonth() === today.getMonth() && |
| 220 | date.getFullYear() === today.getFullYear()) { |
| 221 | td.classList.add('today'); |
| 222 | }; |
| 223 | |
| 224 | // This is the day selected |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame^] | 225 | if (s && s['day']) { |
Nils Diewald | a122862 | 2015-04-25 01:59:10 +0000 | [diff] [blame] | 226 | if (date.getDate() === s['day'] && |
| 227 | date.getMonth() === s['month']-1 && |
| 228 | date.getFullYear() === s['year']) { |
| 229 | td.classList.add('selected'); |
| 230 | }; |
| 231 | }; |
| 232 | |
| 233 | // Add the current day to the table |
| 234 | td.appendChild( |
| 235 | d.createTextNode(date.getDate()) |
| 236 | ); |
| 237 | |
| 238 | // Next day |
| 239 | date.setDate(date.getDate() + 1); |
| 240 | }; |
| 241 | |
| 242 | if (date.getMonth() !== showDate.getMonth()) |
| 243 | break; |
| 244 | }; |
| 245 | return tb; |
| 246 | }, |
| 247 | |
| 248 | _updateDay : function () { |
| 249 | var newBody = this._dayBody(); |
| 250 | this._dBElement.parentNode.replaceChild( |
| 251 | newBody, |
| 252 | this._dBElement |
| 253 | ); |
| 254 | this._dBElement = newBody; |
| 255 | } |
| 256 | }; |
| 257 | }); |