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 | |
| 32 | select : function (year, month, day) { |
| 33 | if (arguments.length >= 1) { |
| 34 | this._selected = {'year' : year}; |
| 35 | if (arguments.length >= 2) { |
| 36 | this._selected['month'] = month; |
| 37 | if (arguments.length >= 3) |
| 38 | this._selected['day'] = day; |
| 39 | }; |
| 40 | return this; |
| 41 | }; |
| 42 | return this._selected; |
| 43 | }, |
| 44 | |
| 45 | show : function (year, month) { |
| 46 | var picker = d.createElement('div'); |
| 47 | picker.classList.add('datepicker'); |
| 48 | this._showYear = year; |
| 49 | this._showMonth = month; |
| 50 | picker.appendChild(this._yearHelper()); |
| 51 | picker.appendChild(this._monthHelper()); |
| 52 | picker.appendChild(this._dayHelper()); |
| 53 | return picker; |
| 54 | }, |
| 55 | |
| 56 | incrYear : function () { |
| 57 | this._showYear++; |
| 58 | this._updateYear(); |
| 59 | this._updateMonth(); |
| 60 | this._updateDay(); |
| 61 | return; |
| 62 | }, |
| 63 | |
| 64 | decrYear : function () { |
| 65 | this._showYear--; |
| 66 | this._updateYear(); |
| 67 | this._updateMonth(); |
| 68 | this._updateDay(); |
| 69 | return; |
| 70 | }, |
| 71 | |
| 72 | incrMonth : function () { |
| 73 | this._showMonth++; |
| 74 | if (this._showMonth > 12) { |
| 75 | this._showMonth = 1; |
| 76 | this.incrYear(); |
| 77 | } |
| 78 | else { |
| 79 | this._updateMonth(); |
| 80 | this._updateDay(); |
| 81 | }; |
| 82 | }, |
| 83 | |
| 84 | decrMonth : function () { |
| 85 | this._showMonth--; |
| 86 | if (this._showMonth < 1) { |
| 87 | this._showMonth = 12; |
| 88 | this.decrYear(); |
| 89 | } |
| 90 | else { |
| 91 | this._updateMonth(); |
| 92 | this._updateDay(); |
| 93 | }; |
| 94 | }, |
| 95 | |
| 96 | _yearHelper : function () { |
| 97 | var year = d.createElement('div'); |
| 98 | year.classList.add('year'); |
| 99 | |
| 100 | // Decrement year |
| 101 | year.appendChild(d.createElement('span')) |
| 102 | .onclick = this.decrYear.bind(this); |
| 103 | |
| 104 | this._yElement = year.appendChild(d.createElement('span')); |
| 105 | this._yElement.appendChild(document.createTextNode(this._showYear)); |
| 106 | |
| 107 | // Increment year |
| 108 | year.appendChild(d.createElement('span')) |
| 109 | .onclick = this.incrYear.bind(this); |
| 110 | |
| 111 | return year; |
| 112 | }, |
| 113 | |
| 114 | _updateYear : function () { |
| 115 | this._yElement.firstChild.data = this._showYear; |
| 116 | }, |
| 117 | |
| 118 | _monthHelper : function () { |
| 119 | var month = d.createElement('div'); |
| 120 | month.classList.add('month'); |
| 121 | |
| 122 | // Decrement month |
| 123 | month.appendChild(d.createElement('span')) |
| 124 | .onclick = this.decrMonth.bind(this); |
| 125 | |
| 126 | this._mElement = month.appendChild(d.createElement('span')); |
| 127 | this._mElement.appendChild( |
| 128 | document.createTextNode(loc.MONTH[this._showMonth-1]) |
| 129 | ); |
| 130 | |
| 131 | // Increment month |
| 132 | month.appendChild(d.createElement('span')) |
| 133 | .onclick = this.incrMonth.bind(this); |
| 134 | |
| 135 | return month; |
| 136 | }, |
| 137 | |
| 138 | _updateMonth : function () { |
| 139 | this._mElement.firstChild.data = loc.MONTH[this._showMonth-1]; |
| 140 | }, |
| 141 | |
| 142 | _dayHelper : function () { |
| 143 | var table = d.createElement('table'); |
| 144 | |
| 145 | var tr = table.appendChild(d.createElement('thead')) |
| 146 | .appendChild(d.createElement('tr')); |
| 147 | for (var i = 0; i < 7; i++) { |
| 148 | tr.appendChild(d.createElement('th')) |
| 149 | .appendChild(d.createTextNode(loc.WDAY[i])); |
| 150 | }; |
| 151 | |
| 152 | this._dBElement = this._dayBody(); |
| 153 | |
| 154 | table.appendChild(this._dBElement); |
| 155 | return table; |
| 156 | }, |
| 157 | |
| 158 | _dayBody : function () { |
| 159 | var showDate = new Date(this._showYear, this._showMonth - 1, 1, 0, 0, 0, 0); |
| 160 | var date = new Date(this._showYear, this._showMonth - 1, 1, 0, 0, 0, 0); |
| 161 | var today = new Date(); |
| 162 | |
| 163 | // Skip back to the previous monday (may be in the last month) |
| 164 | date.setDate(date.getDate() - ((date.getDay() + 6) % 7)); |
| 165 | |
| 166 | var tb = d.createElement('tbody'); |
| 167 | |
| 168 | var s = this.select(); |
| 169 | |
| 170 | // Iterate over all days of the table |
| 171 | while (1) { |
| 172 | |
| 173 | // Loop through the week |
| 174 | var tr = tb.appendChild(d.createElement('tr')); |
| 175 | for (var i = 0; i < 7; i++) { |
| 176 | var td = tr.appendChild(d.createElement('td')); |
| 177 | |
| 178 | // Not part of the current month |
| 179 | if (date.getMonth() !== showDate.getMonth()) |
| 180 | td.classList.add('out'); |
| 181 | |
| 182 | // This is the current day |
| 183 | if (date.getDate() === today.getDate() && |
| 184 | date.getMonth() === today.getMonth() && |
| 185 | date.getFullYear() === today.getFullYear()) { |
| 186 | td.classList.add('today'); |
| 187 | }; |
| 188 | |
| 189 | // This is the day selected |
| 190 | if (s['day']) { |
| 191 | if (date.getDate() === s['day'] && |
| 192 | date.getMonth() === s['month']-1 && |
| 193 | date.getFullYear() === s['year']) { |
| 194 | td.classList.add('selected'); |
| 195 | }; |
| 196 | }; |
| 197 | |
| 198 | // Add the current day to the table |
| 199 | td.appendChild( |
| 200 | d.createTextNode(date.getDate()) |
| 201 | ); |
| 202 | |
| 203 | // Next day |
| 204 | date.setDate(date.getDate() + 1); |
| 205 | }; |
| 206 | |
| 207 | if (date.getMonth() !== showDate.getMonth()) |
| 208 | break; |
| 209 | }; |
| 210 | return tb; |
| 211 | }, |
| 212 | |
| 213 | _updateDay : function () { |
| 214 | var newBody = this._dayBody(); |
| 215 | this._dBElement.parentNode.replaceChild( |
| 216 | newBody, |
| 217 | this._dBElement |
| 218 | ); |
| 219 | this._dBElement = newBody; |
| 220 | } |
| 221 | }; |
| 222 | }); |