blob: 8d79bdbcd285d17823b43970478511c34da77d42 [file] [log] [blame]
Nils Diewalda1228622015-04-25 01:59:10 +00001/**
Akronff78fef2020-10-13 12:00:03 +02002 * Date picker for the
Nils Diewalda1228622015-04-25 01:59:10 +00003 * Virtual Collection builder.
Nils Diewald7148c6f2015-05-04 15:07:53 +00004 *
5 * @author Nils Diewald
Nils Diewalda1228622015-04-25 01:59:10 +00006 */
7define(['util'], function () {
Akronff78fef2020-10-13 12:00:03 +02008
Nils Diewalda1228622015-04-25 01:59:10 +00009 "use strict";
10
Akronff78fef2020-10-13 12:00:03 +020011 KorAP._validDateMatchRE = new RegExp("^(?:[lg]?eq|ne)$");
12 KorAP._validDateRE = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$");
Akrond5620d42017-12-06 15:30:28 +010013
Nils Diewald7148c6f2015-05-04 15:07:53 +000014 /*
15 * Localizations
16 */
Akronff78fef2020-10-13 12:00:03 +020017 const loc = KorAP.Locale;
Nils Diewalda1228622015-04-25 01:59:10 +000018 loc.WDAY = loc.WDAY || [
19 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'
20 ];
Nils Diewalda1228622015-04-25 01:59:10 +000021 loc.MONTH = loc.MONTH || [
22 'January', 'February', 'March', 'April',
23 'May', 'June', 'July', 'August',
24 'September', 'October', 'November',
25 'December'
26 ];
27
Akronff78fef2020-10-13 12:00:03 +020028 const d = document;
Nils Diewalda1228622015-04-25 01:59:10 +000029
Nils Diewald7148c6f2015-05-04 15:07:53 +000030 // The datepicker class
Nils Diewalda1228622015-04-25 01:59:10 +000031 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +000032
33 /**
34 * Create a new datepicker view.
35 */
Nils Diewalda1228622015-04-25 01:59:10 +000036 create : function () {
37 return Object.create(this)._init();
38 },
39
Akron516b6f92018-01-03 17:46:59 +010040
Nils Diewald7148c6f2015-05-04 15:07:53 +000041 // Init datepicker
Nils Diewalda1228622015-04-25 01:59:10 +000042 _init : function () {
Nils Diewald652e5f42015-05-10 18:11:45 +000043 this._selected = [];
Nils Diewalda1228622015-04-25 01:59:10 +000044 return this;
45 },
46
Nils Diewalda79b2682015-05-18 18:34:06 +000047
Nils Diewald7148c6f2015-05-04 15:07:53 +000048 /**
49 * Get or select a specific date.
50 */
Nils Diewalda1228622015-04-25 01:59:10 +000051 select : function (year, month, day) {
Akronff78fef2020-10-13 12:00:03 +020052 const t = this;
Nils Diewalda1228622015-04-25 01:59:10 +000053 if (arguments.length >= 1) {
Akronff78fef2020-10-13 12:00:03 +020054 t._selected = {'year' : year};
55 t._showYear = year;
Akrond5620d42017-12-06 15:30:28 +010056 if (arguments.length >= 2) {
Akronff78fef2020-10-13 12:00:03 +020057 t._selected['month'] = month;
58 t._showMonth = month;
Akrond5620d42017-12-06 15:30:28 +010059 if (arguments.length >= 3) {
Akronff78fef2020-10-13 12:00:03 +020060 t._selected['day'] = day;
61 t._showDay = day;
Akrond5620d42017-12-06 15:30:28 +010062 };
63 };
Akron56ea7e32016-04-20 12:25:52 +020064
Akronff78fef2020-10-13 12:00:03 +020065 return t;
Nils Diewalda1228622015-04-25 01:59:10 +000066 };
Akron56ea7e32016-04-20 12:25:52 +020067
Akronff78fef2020-10-13 12:00:03 +020068 return t._selected;
Nils Diewalda1228622015-04-25 01:59:10 +000069 },
70
Nils Diewald7148c6f2015-05-04 15:07:53 +000071
72 /**
73 * Select a specific date and
74 * init the accompanied action.
75 */
76 set : function (year, month, day) {
77 this.select(year, month, day);
Akronff78fef2020-10-13 12:00:03 +020078 this._store();
Akron56ea7e32016-04-20 12:25:52 +020079 },
80
Akronff78fef2020-10-13 12:00:03 +020081
82 // Store the selected value
83 _store : function () {
Nils Diewald7148c6f2015-05-04 15:07:53 +000084 if (this._click !== undefined)
Akrond5620d42017-12-06 15:30:28 +010085 this._click(this._selected);
Nils Diewald7148c6f2015-05-04 15:07:53 +000086 else
Akrond5620d42017-12-06 15:30:28 +010087 console.dir(this._selected);
Nils Diewald7148c6f2015-05-04 15:07:53 +000088 },
Akron0b489ad2018-02-02 16:49:32 +010089
Akrond5620d42017-12-06 15:30:28 +010090
Nils Diewald7148c6f2015-05-04 15:07:53 +000091 /**
92 * Set the action for clicking as a callback.
93 * The callback will retrieve a an object with
94 * an optional year attribute,
95 * an optional month attribute,
96 * and an optional day attribute
97 */
98 onclick : function (cb) {
99 this._click = cb;
100 },
101
Akron516b6f92018-01-03 17:46:59 +0100102
Akronff78fef2020-10-13 12:00:03 +0200103 /**
104 * The associated input field.
105 */
Akron516b6f92018-01-03 17:46:59 +0100106 input : function () {
107 return this._input;
108 },
Akron56ea7e32016-04-20 12:25:52 +0200109
Akron0b489ad2018-02-02 16:49:32 +0100110
Nils Diewald7148c6f2015-05-04 15:07:53 +0000111 /**
112 * Show the datepicker.
113 * Will either show the selected year/month
114 * or the current date.
115 * Will return the element for appending to the dom.
116 */
Nils Diewalda1228622015-04-25 01:59:10 +0000117 show : function (year, month) {
Akron56ea7e32016-04-20 12:25:52 +0200118
Akronff78fef2020-10-13 12:00:03 +0200119 const e = this._element = d.createElement('div');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000120 e.setAttribute('tabindex', 0);
121 e.style.outline = 0;
122 e.classList.add('datepicker');
Akrond5620d42017-12-06 15:30:28 +0100123
Akronff78fef2020-10-13 12:00:03 +0200124 const today = new Date();
125 const t = this;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000126
127 // Show year
Akronff78fef2020-10-13 12:00:03 +0200128 t._showYear = (year !== undefined) ? year :
129 (t._selected['year'] ? this._selected['year'] :
Akrond5620d42017-12-06 15:30:28 +0100130 today.getYear() + 1900);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000131
132 // Show month
Akronff78fef2020-10-13 12:00:03 +0200133 t._showMonth = month ? month :
134 (t._selected['month'] ? t._selected['month'] :
Akrond5620d42017-12-06 15:30:28 +0100135 (today.getMonth() + 1));
Nils Diewald7148c6f2015-05-04 15:07:53 +0000136
137 // Append all helpers
Akronff78fef2020-10-13 12:00:03 +0200138 e.appendChild(t._monthHelper());
139 e.appendChild(t._yearHelper());
140 e.appendChild(t._dayHelper());
141 t._input = e.appendChild(t._stringHelper());
Akron516b6f92018-01-03 17:46:59 +0100142
143 // Always focus
144 e.addEventListener(
145 'mousedown',
146 function (ev) {
147 this._inField = true
Akronff78fef2020-10-13 12:00:03 +0200148 }.bind(t)
Akron516b6f92018-01-03 17:46:59 +0100149 );
150
151 e.addEventListener(
152 'mouseup',
153 function (ev) {
154 this._inField = false;
155 this._input.focus();
Akronff78fef2020-10-13 12:00:03 +0200156 }.bind(t)
Akron516b6f92018-01-03 17:46:59 +0100157 );
158
Akronff78fef2020-10-13 12:00:03 +0200159 t._input.addEventListener(
Akron516b6f92018-01-03 17:46:59 +0100160 'blur',
161 function (ev) {
162 if (!this._inField) {
163 if (this.fromString(this._input.value)) {
Akronff78fef2020-10-13 12:00:03 +0200164 this._store();
Akron516b6f92018-01-03 17:46:59 +0100165 };
166 };
167 ev.halt();
Akronff78fef2020-10-13 12:00:03 +0200168 }.bind(t)
Akron516b6f92018-01-03 17:46:59 +0100169 );
170
Akronff78fef2020-10-13 12:00:03 +0200171 t._input.focus();
Akron56ea7e32016-04-20 12:25:52 +0200172
Akronff78fef2020-10-13 12:00:03 +0200173 return t._element;
Nils Diewald87507832015-05-01 23:36:41 +0000174 },
175
Akronc59f7322016-04-20 13:46:05 +0200176 _stringHelper : function () {
Akron56ea7e32016-04-20 12:25:52 +0200177
178 // Create element
Akron56ea7e32016-04-20 12:25:52 +0200179 // Add input field
Akronff78fef2020-10-13 12:00:03 +0200180 const input = d.createElement('input');
Akronc59f7322016-04-20 13:46:05 +0200181 input.value = this.toString();
182 input.setAttribute('tabindex', 0);
Akron56ea7e32016-04-20 12:25:52 +0200183
Akronc59f7322016-04-20 13:46:05 +0200184 input.addEventListener(
Akrond5620d42017-12-06 15:30:28 +0100185 'keyup',
186 function (e) {
187 if (this.fromString(input.value)) {
188 this._updateYear();
189 this._updateMonth();
190 this._updateDay();
191 };
192 }.bind(this)
Akron56ea7e32016-04-20 12:25:52 +0200193 );
194
Akrond5620d42017-12-06 15:30:28 +0100195 input.addEventListener(
196 'keypress',
197 function (e) {
198 if (e.keyCode == 13) {
199 if (this.fromString(input.value))
Akronff78fef2020-10-13 12:00:03 +0200200 this._store();
Akrond5620d42017-12-06 15:30:28 +0100201
202 e.halt();
203 return false;
204 }
205 }.bind(this)
206 )
207
Akronc59f7322016-04-20 13:46:05 +0200208 return input;
Akron56ea7e32016-04-20 12:25:52 +0200209 },
Nils Diewalda79b2682015-05-18 18:34:06 +0000210
Akronff78fef2020-10-13 12:00:03 +0200211
Nils Diewald7148c6f2015-05-04 15:07:53 +0000212 /**
213 * Get the HTML element associated with the datepicker.
214 */
Nils Diewald87507832015-05-01 23:36:41 +0000215 element : function () {
216 return this._element;
Nils Diewalda1228622015-04-25 01:59:10 +0000217 },
218
Akron0b489ad2018-02-02 16:49:32 +0100219
Nils Diewald845282c2015-05-14 07:53:03 +0000220 /**
221 * Get the current date in string format.
222 */
223 today : function () {
Akronff78fef2020-10-13 12:00:03 +0200224 const today = new Date();
225 let str = today.getYear() + 1900;
226 const m = today.getMonth() + 1;
227 const d = today.getDate();
Nils Diewald845282c2015-05-14 07:53:03 +0000228 str += '-' + (m < 10 ? '0' + m : m);
229 str += '-' + (d < 10 ? '0' + d : d);
230 return str;
231 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000232
Akron0b489ad2018-02-02 16:49:32 +0100233
Akronff78fef2020-10-13 12:00:03 +0200234 /**
235 * Stringification
236 */
Akron56ea7e32016-04-20 12:25:52 +0200237 toString : function () {
238 // There are values selected
Akronff78fef2020-10-13 12:00:03 +0200239 let v = '';
240 const s = this._selected;
Akron56ea7e32016-04-20 12:25:52 +0200241 if (s['year']) {
Akrond5620d42017-12-06 15:30:28 +0100242 v += s['year'];
243 if (s['month']) {
244 v += '-';
245 v += s['month'] < 10 ? '0' + s['month'] : s['month'];
246 if (s['day']) {
247 v += '-';
248 v += s['day'] < 10 ? '0' + s['day'] : s['day'];
249 };
250 };
Akron56ea7e32016-04-20 12:25:52 +0200251 };
252 return v;
253 },
Nils Diewalda79b2682015-05-18 18:34:06 +0000254
Akron0b489ad2018-02-02 16:49:32 +0100255
Nils Diewald7148c6f2015-05-04 15:07:53 +0000256 /**
257 * Increment the year.
258 */
Nils Diewalda1228622015-04-25 01:59:10 +0000259 incrYear : function () {
Akronff78fef2020-10-13 12:00:03 +0200260 const t = this;
261 if (t._showYear < 9999) {
262 t._showYear++;
263 t._updateYear();
264 t._updateMonth();
265 t._updateDay();
266 return t;
Nils Diewalda79b2682015-05-18 18:34:06 +0000267 };
Nils Diewalda1228622015-04-25 01:59:10 +0000268 return;
269 },
270
Nils Diewalda79b2682015-05-18 18:34:06 +0000271
Nils Diewald7148c6f2015-05-04 15:07:53 +0000272 /**
273 * Decrement the year.
274 */
Nils Diewalda1228622015-04-25 01:59:10 +0000275 decrYear : function () {
Akronff78fef2020-10-13 12:00:03 +0200276 const t = this;
277 if (t._showYear > 0) {
278 t._showYear--;
279 t._updateYear();
280 t._updateMonth();
281 t._updateDay();
282 return t;
Nils Diewalda79b2682015-05-18 18:34:06 +0000283 };
Nils Diewalda1228622015-04-25 01:59:10 +0000284 return;
285 },
286
Nils Diewalda79b2682015-05-18 18:34:06 +0000287
Nils Diewald7148c6f2015-05-04 15:07:53 +0000288 /**
289 * Increment the month.
290 */
Nils Diewalda1228622015-04-25 01:59:10 +0000291 incrMonth : function () {
Akronff78fef2020-10-13 12:00:03 +0200292 const t = this;
293 t._showMonth++;
294 if (t._showMonth > 12) {
295 t._showMonth = 1;
296 t.incrYear();
Nils Diewalda1228622015-04-25 01:59:10 +0000297 }
298 else {
Akronff78fef2020-10-13 12:00:03 +0200299 t._updateMonth();
300 t._updateDay();
Nils Diewalda1228622015-04-25 01:59:10 +0000301 };
Akronff78fef2020-10-13 12:00:03 +0200302 return t;
Nils Diewalda1228622015-04-25 01:59:10 +0000303 },
304
Nils Diewalda79b2682015-05-18 18:34:06 +0000305
Nils Diewald7148c6f2015-05-04 15:07:53 +0000306 /**
307 * Decrement the month.
308 */
Nils Diewalda1228622015-04-25 01:59:10 +0000309 decrMonth : function () {
Akronff78fef2020-10-13 12:00:03 +0200310 const t = this;
311 t._showMonth--;
312 if (t._showMonth < 1) {
313 t._showMonth = 12;
314 t.decrYear();
Nils Diewalda1228622015-04-25 01:59:10 +0000315 }
316 else {
Akronff78fef2020-10-13 12:00:03 +0200317 t._updateMonth();
318 t._updateDay();
Nils Diewalda1228622015-04-25 01:59:10 +0000319 };
Nils Diewalda79b2682015-05-18 18:34:06 +0000320
Akronff78fef2020-10-13 12:00:03 +0200321 return t;
Nils Diewalda1228622015-04-25 01:59:10 +0000322 },
323
Nils Diewald7148c6f2015-05-04 15:07:53 +0000324
325 // Create the year helper element.
Nils Diewalda1228622015-04-25 01:59:10 +0000326 _yearHelper : function () {
Akronff78fef2020-10-13 12:00:03 +0200327 const t = this;
328 const year = d.createElement('div');
Nils Diewalda1228622015-04-25 01:59:10 +0000329 year.classList.add('year');
330
331 // Decrement year
Akron0b489ad2018-02-02 16:49:32 +0100332 year.addE('span')
Akronff78fef2020-10-13 12:00:03 +0200333 .onclick = t.decrYear.bind(t);
Nils Diewalda1228622015-04-25 01:59:10 +0000334
Akronff78fef2020-10-13 12:00:03 +0200335 t._yElement = year.addE('span');
336 t._yElement.addT(t._showYear);
Nils Diewalda1228622015-04-25 01:59:10 +0000337
Akronff78fef2020-10-13 12:00:03 +0200338 t._yElement.onclick = function () {
339 t.set(t._showYear);
340 }.bind(t);
341 t._selectYear();
Nils Diewaldbdf79c52015-04-29 23:47:13 +0000342
Nils Diewalda1228622015-04-25 01:59:10 +0000343 // Increment year
Akron0b489ad2018-02-02 16:49:32 +0100344 year.addE('span')
Akronff78fef2020-10-13 12:00:03 +0200345 .onclick = t.incrYear.bind(t);
Nils Diewalda1228622015-04-25 01:59:10 +0000346
347 return year;
348 },
349
Akronff78fef2020-10-13 12:00:03 +0200350
Nils Diewald7148c6f2015-05-04 15:07:53 +0000351 // Update the year helper view.
Nils Diewalda1228622015-04-25 01:59:10 +0000352 _updateYear : function () {
353 this._yElement.firstChild.data = this._showYear;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000354 this._selectYear();
Nils Diewalda1228622015-04-25 01:59:10 +0000355 },
356
Nils Diewald7148c6f2015-05-04 15:07:53 +0000357
358 // Check if the viewed year is current
359 _selectYear : function () {
360 if (this._showYear === this.select()['year'])
Akrond5620d42017-12-06 15:30:28 +0100361 this._yElement.classList.add('selected');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000362 else
Akrond5620d42017-12-06 15:30:28 +0100363 this._yElement.classList.remove('selected');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000364 },
365
366
367 // Create the month helper element.
Nils Diewalda1228622015-04-25 01:59:10 +0000368 _monthHelper : function () {
Akronff78fef2020-10-13 12:00:03 +0200369 const t = this;
370 const month = d.createElement('div');
Nils Diewalda1228622015-04-25 01:59:10 +0000371 month.classList.add('month');
372
373 // Decrement month
Akron0b489ad2018-02-02 16:49:32 +0100374 month.addE('span')
Akronff78fef2020-10-13 12:00:03 +0200375 .onclick = t.decrMonth.bind(t);
Nils Diewalda1228622015-04-25 01:59:10 +0000376
Akronff78fef2020-10-13 12:00:03 +0200377 t._mElement = month.addE('span');
378 t._mElement.addT(loc.MONTH[t._showMonth-1]);
379 t._mElement.onclick = function () {
Akrond5620d42017-12-06 15:30:28 +0100380 this.set(this._showYear, this._showMonth);
Akronff78fef2020-10-13 12:00:03 +0200381 }.bind(t);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000382
Akronff78fef2020-10-13 12:00:03 +0200383 t._selectMonth();
Nils Diewalda1228622015-04-25 01:59:10 +0000384
385 // Increment month
Akron0b489ad2018-02-02 16:49:32 +0100386 month.addE('span')
Akronff78fef2020-10-13 12:00:03 +0200387 .onclick = t.incrMonth.bind(t);
Nils Diewalda1228622015-04-25 01:59:10 +0000388
389 return month;
390 },
391
Nils Diewald7148c6f2015-05-04 15:07:53 +0000392 // Update the month helper view.
Nils Diewalda1228622015-04-25 01:59:10 +0000393 _updateMonth : function () {
Akronc59f7322016-04-20 13:46:05 +0200394 if (this._showMonth === undefined || this._showMonth > 12)
Akrond5620d42017-12-06 15:30:28 +0100395 this._showMonth = 1;
Akronc59f7322016-04-20 13:46:05 +0200396
Nils Diewalda1228622015-04-25 01:59:10 +0000397 this._mElement.firstChild.data = loc.MONTH[this._showMonth-1];
Nils Diewald7148c6f2015-05-04 15:07:53 +0000398 this._selectMonth();
Nils Diewalda1228622015-04-25 01:59:10 +0000399 },
400
Nils Diewald7148c6f2015-05-04 15:07:53 +0000401
402 // Check if the viewed month is current
403 _selectMonth : function () {
Akronff78fef2020-10-13 12:00:03 +0200404 const t = this;
405 if (t._showYear === t.select()['year'] &&
406 t._showMonth === t.select()['month'])
407 t._mElement.classList.add('selected');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000408 else
Akronff78fef2020-10-13 12:00:03 +0200409 t._mElement.classList.remove('selected');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000410 },
411
412
413 // Create the day (calendar) helper element.
Nils Diewalda1228622015-04-25 01:59:10 +0000414 _dayHelper : function () {
Akronff78fef2020-10-13 12:00:03 +0200415 const table = d.createElement('table');
Nils Diewalda1228622015-04-25 01:59:10 +0000416
Nils Diewald7148c6f2015-05-04 15:07:53 +0000417 // Localized day view
Akronff78fef2020-10-13 12:00:03 +0200418 const tr = table.addE('thead').addE('tr');
419 for (let i = 0; i < 7; i++) {
Akron0b489ad2018-02-02 16:49:32 +0100420 tr.addE('th').addT(loc.WDAY[i]);
Nils Diewalda1228622015-04-25 01:59:10 +0000421 };
422
423 this._dBElement = this._dayBody();
424
425 table.appendChild(this._dBElement);
426 return table;
427 },
428
Akronff78fef2020-10-13 12:00:03 +0200429
430 // Create day body for calendar table
Nils Diewalda1228622015-04-25 01:59:10 +0000431 _dayBody : function () {
Akronff78fef2020-10-13 12:00:03 +0200432 const showDate = new Date(
Akrond5620d42017-12-06 15:30:28 +0100433 this._showYear,
434 this._showMonth - 1,
435 1,
436 0,
437 0,
438 0,
439 0
Nils Diewald7148c6f2015-05-04 15:07:53 +0000440 );
Akronff78fef2020-10-13 12:00:03 +0200441 const date = new Date(
Akrond5620d42017-12-06 15:30:28 +0100442 this._showYear,
443 this._showMonth - 1,
444 1,
445 0,
446 0,
447 0,
448 0
Nils Diewald7148c6f2015-05-04 15:07:53 +0000449 );
Akronff78fef2020-10-13 12:00:03 +0200450 const today = new Date();
451 const that = this;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000452
453 // What happens, in case someone clicks
454 // on a date
Akronff78fef2020-10-13 12:00:03 +0200455 const click = function () {
Akrond5620d42017-12-06 15:30:28 +0100456 that.set(
457 that._showYear,
458 that._showMonth,
459 parseInt(this.firstChild.data)
460 );
Nils Diewaldbdf79c52015-04-29 23:47:13 +0000461 };
Nils Diewalda1228622015-04-25 01:59:10 +0000462
463 // Skip back to the previous monday (may be in the last month)
464 date.setDate(date.getDate() - ((date.getDay() + 6) % 7));
465
Akronff78fef2020-10-13 12:00:03 +0200466 const tb = d.createElement('tbody');
Nils Diewalda1228622015-04-25 01:59:10 +0000467
Akronff78fef2020-10-13 12:00:03 +0200468 const s = this.select();
469
470 let tr, i, td;
Nils Diewalda1228622015-04-25 01:59:10 +0000471
472 // Iterate over all days of the table
473 while (1) {
474
Akrond5620d42017-12-06 15:30:28 +0100475 // Loop through the week
Akronff78fef2020-10-13 12:00:03 +0200476 tr = tb.addE('tr');
477 for (i = 0; i < 7; i++) {
478 td = tr.addE('td');
Akrond5620d42017-12-06 15:30:28 +0100479
480 // Not part of the current month
481 if (date.getMonth() !== showDate.getMonth()) {
482 td.classList.add('out');
483 }
484 else {
485 td.onclick = click;
486 };
487
488 // This is the current day
489 if (date.getDate() === today.getDate() &&
490 date.getMonth() === today.getMonth() &&
491 date.getFullYear() === today.getFullYear()) {
492 td.classList.add('today');
493 };
Nils Diewalda1228622015-04-25 01:59:10 +0000494
Akrond5620d42017-12-06 15:30:28 +0100495 // This is the day selected
496 if (s && s['day']) {
497 if (date.getDate() === s['day'] &&
498 date.getMonth() === s['month']-1 &&
499 date.getFullYear() === s['year']) {
500 td.classList.add('selected');
501 };
502 };
Akronff78fef2020-10-13 12:00:03 +0200503
Akrond5620d42017-12-06 15:30:28 +0100504 // Add the current day to the table
Akron0b489ad2018-02-02 16:49:32 +0100505 td.addT(date.getDate());
Akrond5620d42017-12-06 15:30:28 +0100506
507 // Next day
508 date.setDate(date.getDate() + 1);
509 };
Akronff78fef2020-10-13 12:00:03 +0200510
Akrond5620d42017-12-06 15:30:28 +0100511 if (date.getMonth() !== showDate.getMonth())
512 break;
Nils Diewalda1228622015-04-25 01:59:10 +0000513 };
514 return tb;
515 },
516
Nils Diewald7148c6f2015-05-04 15:07:53 +0000517 // Update the calendar view
Nils Diewalda1228622015-04-25 01:59:10 +0000518 _updateDay : function () {
Akronff78fef2020-10-13 12:00:03 +0200519 const newBody = this._dayBody();
Nils Diewalda1228622015-04-25 01:59:10 +0000520 this._dBElement.parentNode.replaceChild(
Akrond5620d42017-12-06 15:30:28 +0100521 newBody,
522 this._dBElement
Nils Diewalda1228622015-04-25 01:59:10 +0000523 );
524 this._dBElement = newBody;
Akron56ea7e32016-04-20 12:25:52 +0200525 },
526
Akronff78fef2020-10-13 12:00:03 +0200527
528 /**
529 * Parse date from string.
530 */
Akron56ea7e32016-04-20 12:25:52 +0200531 fromString : function (v) {
Akronc59f7322016-04-20 13:46:05 +0200532 if (v === undefined)
Akrond5620d42017-12-06 15:30:28 +0100533 return false;
Akron56ea7e32016-04-20 12:25:52 +0200534
Akronc59f7322016-04-20 13:46:05 +0200535 if (!KorAP._validDateRE.test(v))
Akrond5620d42017-12-06 15:30:28 +0100536 return false;
Akron56ea7e32016-04-20 12:25:52 +0200537
Akronff78fef2020-10-13 12:00:03 +0200538 const d = v.split('-', 3);
Akronc59f7322016-04-20 13:46:05 +0200539 d[0] = parseInt(d[0]);
540 if (d[1]) d[1] = parseInt(d[1]);
541 if (d[2]) d[2] = parseInt(d[2]);
542
543 // Select values
544 this.select(d[0], d[1], d[2]);
545 return true;
Nils Diewalda1228622015-04-25 01:59:10 +0000546 }
547 };
548});