blob: 01bb0d75596ad3fe59237b3ba5abc5257def4add [file] [log] [blame]
Nils Diewalda1228622015-04-25 01:59:10 +00001/**
2 * Date picker for the
3 * Virtual Collection builder.
4 */
5define(['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 Diewaldbdf79c52015-04-29 23:47:13 +000032 set : function (year, month, day) {
33 this.select(year, month, day);
34 console.dir(this._selected);
35 },
36
Nils Diewalda1228622015-04-25 01:59:10 +000037 select : function (year, month, day) {
38 if (arguments.length >= 1) {
39 this._selected = {'year' : year};
40 if (arguments.length >= 2) {
41 this._selected['month'] = month;
42 if (arguments.length >= 3)
43 this._selected['day'] = day;
44 };
45 return this;
46 };
47 return this._selected;
48 },
49
50 show : function (year, month) {
51 var picker = d.createElement('div');
52 picker.classList.add('datepicker');
53 this._showYear = year;
54 this._showMonth = month;
Nils Diewalda1228622015-04-25 01:59:10 +000055 picker.appendChild(this._monthHelper());
Nils Diewaldbdf79c52015-04-29 23:47:13 +000056 picker.appendChild(this._yearHelper());
Nils Diewalda1228622015-04-25 01:59:10 +000057 picker.appendChild(this._dayHelper());
58 return picker;
59 },
60
61 incrYear : function () {
62 this._showYear++;
63 this._updateYear();
64 this._updateMonth();
65 this._updateDay();
66 return;
67 },
68
69 decrYear : function () {
70 this._showYear--;
71 this._updateYear();
72 this._updateMonth();
73 this._updateDay();
74 return;
75 },
76
77 incrMonth : function () {
78 this._showMonth++;
79 if (this._showMonth > 12) {
80 this._showMonth = 1;
81 this.incrYear();
82 }
83 else {
84 this._updateMonth();
85 this._updateDay();
86 };
87 },
88
89 decrMonth : function () {
90 this._showMonth--;
91 if (this._showMonth < 1) {
92 this._showMonth = 12;
93 this.decrYear();
94 }
95 else {
96 this._updateMonth();
97 this._updateDay();
98 };
99 },
100
101 _yearHelper : function () {
102 var year = d.createElement('div');
103 year.classList.add('year');
104
105 // Decrement year
106 year.appendChild(d.createElement('span'))
107 .onclick = this.decrYear.bind(this);
108
109 this._yElement = year.appendChild(d.createElement('span'));
110 this._yElement.appendChild(document.createTextNode(this._showYear));
111
Nils Diewaldbdf79c52015-04-29 23:47:13 +0000112 this._yElement.onclick = function () {
113 this.set(this._showYear);
114 }.bind(this);
115
Nils Diewalda1228622015-04-25 01:59:10 +0000116 // Increment year
117 year.appendChild(d.createElement('span'))
118 .onclick = this.incrYear.bind(this);
119
120 return year;
121 },
122
123 _updateYear : function () {
124 this._yElement.firstChild.data = this._showYear;
125 },
126
127 _monthHelper : function () {
128 var month = d.createElement('div');
129 month.classList.add('month');
130
131 // Decrement month
132 month.appendChild(d.createElement('span'))
133 .onclick = this.decrMonth.bind(this);
134
135 this._mElement = month.appendChild(d.createElement('span'));
136 this._mElement.appendChild(
137 document.createTextNode(loc.MONTH[this._showMonth-1])
138 );
Nils Diewaldbdf79c52015-04-29 23:47:13 +0000139 this._mElement.onclick = function () {
140 this.set(this._showYear, this._showMonth);
141 }.bind(this);
Nils Diewalda1228622015-04-25 01:59:10 +0000142
143 // Increment month
144 month.appendChild(d.createElement('span'))
145 .onclick = this.incrMonth.bind(this);
146
147 return month;
148 },
149
150 _updateMonth : function () {
151 this._mElement.firstChild.data = loc.MONTH[this._showMonth-1];
152 },
153
154 _dayHelper : function () {
155 var table = d.createElement('table');
156
157 var tr = table.appendChild(d.createElement('thead'))
158 .appendChild(d.createElement('tr'));
159 for (var i = 0; i < 7; i++) {
160 tr.appendChild(d.createElement('th'))
161 .appendChild(d.createTextNode(loc.WDAY[i]));
162 };
163
164 this._dBElement = this._dayBody();
165
166 table.appendChild(this._dBElement);
167 return table;
168 },
169
170 _dayBody : function () {
171 var showDate = new Date(this._showYear, this._showMonth - 1, 1, 0, 0, 0, 0);
172 var date = new Date(this._showYear, this._showMonth - 1, 1, 0, 0, 0, 0);
173 var today = new Date();
Nils Diewaldbdf79c52015-04-29 23:47:13 +0000174 var that = this;
175 var click = function () {
176 that.set(
177 that._showYear,
178 that._showMonth,
179 parseInt(this.firstChild.data)
180 );
181 };
Nils Diewalda1228622015-04-25 01:59:10 +0000182
183 // Skip back to the previous monday (may be in the last month)
184 date.setDate(date.getDate() - ((date.getDay() + 6) % 7));
185
186 var tb = d.createElement('tbody');
187
188 var s = this.select();
189
190 // Iterate over all days of the table
191 while (1) {
192
193 // Loop through the week
194 var tr = tb.appendChild(d.createElement('tr'));
195 for (var i = 0; i < 7; i++) {
196 var td = tr.appendChild(d.createElement('td'));
197
198 // Not part of the current month
Nils Diewaldbdf79c52015-04-29 23:47:13 +0000199 if (date.getMonth() !== showDate.getMonth()) {
Nils Diewalda1228622015-04-25 01:59:10 +0000200 td.classList.add('out');
Nils Diewaldbdf79c52015-04-29 23:47:13 +0000201 }
202 else {
203 td.onclick = click;
204 };
Nils Diewalda1228622015-04-25 01:59:10 +0000205
206 // This is the current day
207 if (date.getDate() === today.getDate() &&
208 date.getMonth() === today.getMonth() &&
209 date.getFullYear() === today.getFullYear()) {
210 td.classList.add('today');
211 };
212
213 // This is the day selected
214 if (s['day']) {
215 if (date.getDate() === s['day'] &&
216 date.getMonth() === s['month']-1 &&
217 date.getFullYear() === s['year']) {
218 td.classList.add('selected');
219 };
220 };
221
222 // Add the current day to the table
223 td.appendChild(
224 d.createTextNode(date.getDate())
225 );
226
227 // Next day
228 date.setDate(date.getDate() + 1);
229 };
230
231 if (date.getMonth() !== showDate.getMonth())
232 break;
233 };
234 return tb;
235 },
236
237 _updateDay : function () {
238 var newBody = this._dayBody();
239 this._dBElement.parentNode.replaceChild(
240 newBody,
241 this._dBElement
242 );
243 this._dBElement = newBody;
244 }
245 };
246});