Debian Codegraph @

closure-library/closure/goog/ui/datepicker.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Date picker implementation.
17 *
18 * @author [email protected] (Emil A Eklund)
19 * @see ../demos/datepicker.html
20 */
21
22 goog.provide('goog.ui.DatePicker');
23 goog.provide('goog.ui.DatePicker.Events');
24 goog.provide('goog.ui.DatePickerEvent');
25
26 goog.require('goog.a11y.aria');
27 goog.require('goog.asserts');
28 goog.require('goog.date.Date');
29 goog.require('goog.date.DateRange');
30 goog.require('goog.date.Interval');
31 goog.require('goog.dom');
32 goog.require('goog.dom.NodeType');
33 goog.require('goog.dom.TagName');
34 goog.require('goog.dom.classlist');
35 goog.require('goog.events.Event');
36 goog.require('goog.events.EventType');
37 goog.require('goog.events.KeyHandler');
38 goog.require('goog.i18n.DateTimeFormat');
39 goog.require('goog.i18n.DateTimePatterns');
40 goog.require('goog.i18n.DateTimeSymbols');
41 goog.require('goog.style');
42 goog.require('goog.ui.Component');
43 goog.require('goog.ui.DefaultDatePickerRenderer');
44 goog.require('goog.ui.IdGenerator');
45
46
47
48 /**
49 * DatePicker widget. Allows a single date to be selected from a calendar like
50 * view.
51 *
52 * @param {goog.date.Date|Date=} opt_date Date to initialize the date picker
53 * with, defaults to the current date.
54 * @param {Object=} opt_dateTimeSymbols Date and time symbols to use.
55 * Defaults to goog.i18n.DateTimeSymbols if not set.
56 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
57 * @param {goog.ui.DatePickerRenderer=} opt_renderer Optional Date picker
58 * renderer.
59 * @constructor
60 * @extends {goog.ui.Component}
61 */
62 goog.ui.DatePicker = function(
63 opt_date, opt_dateTimeSymbols, opt_domHelper, opt_renderer) {
64 goog.ui.Component.call(this, opt_domHelper);
65
66 /**
67 * Date and time symbols to use.
68 * @type {!goog.i18n.DateTimeSymbolsType}
69 * @private
70 */
71 this.symbols_ = /** @type {!goog.i18n.DateTimeSymbolsType} */ (
72 opt_dateTimeSymbols || goog.i18n.DateTimeSymbols);
73
74 this.wdayNames_ = this.symbols_.STANDALONESHORTWEEKDAYS;
75
76 // Formatters for the various areas of the picker
77 this.i18nDateFormatterDay_ = new goog.i18n.DateTimeFormat('d', this.symbols_);
78 this.i18nDateFormatterDay2_ =
79 new goog.i18n.DateTimeFormat('dd', this.symbols_);
80 this.i18nDateFormatterWeek_ =
81 new goog.i18n.DateTimeFormat('w', this.symbols_);
82 // Formatter for day grid aria label. This should always have the day first so
83 // that a screenreader user can rapidly navigate within a month without always
84 // hearing the month. It should read the month name instead of number to avoid
85 // confusing people who are used to different orders.
86 this.i18nDateFormatterDayAriaLabel_ =
87 new goog.i18n.DateTimeFormat('d MMM', this.symbols_);
88
89 // Previous implementation did not use goog.i18n.DateTimePatterns,
90 // so it is likely most developers did not set it.
91 // This is why the fallback to a hard-coded string (just in case).
92 var patYear = goog.i18n.DateTimePatterns.YEAR_FULL || 'y';
93 this.i18nDateFormatterYear_ =
94 new goog.i18n.DateTimeFormat(patYear, this.symbols_);
95 var patMMMMy = goog.i18n.DateTimePatterns.YEAR_MONTH_FULL || 'MMMM y';
96 this.i18nDateFormatterMonthYear_ =
97 new goog.i18n.DateTimeFormat(patMMMMy, this.symbols_);
98
99 /**
100 * @type {!goog.ui.DatePickerRenderer}
101 * @private
102 */
103 this.renderer_ = opt_renderer ||
104 new goog.ui.DefaultDatePickerRenderer(
105 this.getBaseCssClass(), this.getDomHelper());
106
107 /**
108 * Selected date.
109 * @type {goog.date.Date}
110 * @private
111 */
112 this.date_ = new goog.date.Date(opt_date);
113 this.date_.setFirstWeekCutOffDay(this.symbols_.FIRSTWEEKCUTOFFDAY);
114 this.date_.setFirstDayOfWeek(this.symbols_.FIRSTDAYOFWEEK);
115
116 /**
117 * Active month.
118 * @type {goog.date.Date}
119 * @private
120 */
121 this.activeMonth_ = this.date_.clone();
122 this.activeMonth_.setDate(1);
123
124 /**
125 * Class names to apply to the weekday columns.
126 * @type {Array<string>}
127 * @private
128 */
129 this.wdayStyles_ = ['', '', '', '', '', '', ''];
130 this.wdayStyles_[this.symbols_.WEEKENDRANGE[0]] =
131 goog.getCssName(this.getBaseCssClass(), 'wkend-start');
132 this.wdayStyles_[this.symbols_.WEEKENDRANGE[1]] =
133 goog.getCssName(this.getBaseCssClass(), 'wkend-end');
134
135 /**
136 * Object that is being used to cache key handlers.
137 * @type {Object}
138 * @private
139 */
140 this.keyHandlers_ = {};
141
142 /**
143 * Collection of dates that make up the date picker.
144 * @type {!Array<!Array<!goog.date.Date>>}
145 * @private
146 */
147 this.grid_ = [];
148
149 /** @private {Array<!Array<Element>>} */
150 this.elTable_;
151
152 /**
153 * TODO(tbreisacher): Remove external references to this field,
154 * and make it private.
155 * @type {Element}
156 */
157 this.tableBody_;
158
159 /** @private {Element} */
160 this.tableFoot_;
161
162 /** @private {Element} */
163 this.elYear_;
164
165 /** @private {Element} */
166 this.elMonth_;
167
168 /** @private {Element} */
169 this.elToday_;
170
171 /** @private {Element} */
172 this.elNone_;
173
174 /** @private {Element} */
175 this.menu_;
176
177 /** @private {Element} */
178 this.menuSelected_;
179
180 /** @private {?Element} */
181 this.selectedCell_;
182
183 /** @private {function(Element)} */
184 this.menuCallback_;
185 };
186 goog.inherits(goog.ui.DatePicker, goog.ui.Component);
187 goog.tagUnsealableClass(goog.ui.DatePicker);
188
189
190 /**
191 * Flag indicating if the number of weeks shown should be fixed.
192 * @type {boolean}
193 * @private
194 */
195 goog.ui.DatePicker.prototype.showFixedNumWeeks_ = true;
196
197
198 /**
199 * Flag indicating if days from other months should be shown.
200 * @type {boolean}
201 * @private
202 */
203 goog.ui.DatePicker.prototype.showOtherMonths_ = true;
204
205
206 /**
207 * Range of dates which are selectable by the user.
208 * @type {!goog.date.DateRange}
209 * @private
210 */
211 goog.ui.DatePicker.prototype.userSelectableDateRange_ =
212 goog.date.DateRange.allTime();
213
214
215 /**
216 * Flag indicating if extra week(s) always should be added at the end. If not
217 * set the extra week is added at the beginning if the number of days shown
218 * from the previous month is less than the number from the next month.
219 * @type {boolean}
220 * @private
221 */
222 goog.ui.DatePicker.prototype.extraWeekAtEnd_ = true;
223
224
225 /**
226 * Flag indicating if week numbers should be shown.
227 * @type {boolean}
228 * @private
229 */
230 goog.ui.DatePicker.prototype.showWeekNum_ = true;
231
232
233 /**
234 * Flag indicating if weekday names should be shown.
235 * @type {boolean}
236 * @private
237 */
238 goog.ui.DatePicker.prototype.showWeekdays_ = true;
239
240
241 /**
242 * Flag indicating if none is a valid selection. Also controls if the none
243 * button should be shown or not.
244 * @type {boolean}
245 * @private
246 */
247 goog.ui.DatePicker.prototype.allowNone_ = true;
248
249
250 /**
251 * Flag indicating if the today button should be shown.
252 * @type {boolean}
253 * @private
254 */
255 goog.ui.DatePicker.prototype.showToday_ = true;
256
257
258 /**
259 * Flag indicating if the picker should use a simple navigation menu that only
260 * contains controls for navigating to the next and previous month. The default
261 * navigation menu contains controls for navigating to the next/previous month,
262 * next/previous year, and menus for jumping to specific months and years.
263 * @type {boolean}
264 * @private
265 */
266 goog.ui.DatePicker.prototype.simpleNavigation_ = false;
267
268
269 /**
270 * Custom decorator function. Takes a goog.date.Date object, returns a String
271 * representing a CSS class or null if no special styling applies
272 * @type {Function}
273 * @private
274 */
275 goog.ui.DatePicker.prototype.decoratorFunction_ = null;
276
277
278 /**
279 * Flag indicating if the dates should be printed as a two charater date.
280 * @type {boolean}
281 * @private
282 */
283 goog.ui.DatePicker.prototype.longDateFormat_ = false;
284
285
286 /**
287 * Element for navigation row on a datepicker.
288 * @type {Element}
289 * @private
290 */
291 goog.ui.DatePicker.prototype.elNavRow_ = null;
292
293
294 /**
295 * Element for the month/year in the navigation row.
296 * @type {Element}
297 * @private
298 */
299 goog.ui.DatePicker.prototype.elMonthYear_ = null;
300
301
302 /**
303 * Element for footer row on a datepicker.
304 * @type {Element}
305 * @private
306 */
307 goog.ui.DatePicker.prototype.elFootRow_ = null;
308
309
310 /**
311 * Generator for unique table cell IDs.
312 * @type {goog.ui.IdGenerator}
313 * @private
314 */
315 goog.ui.DatePicker.prototype.cellIdGenerator_ =
316 goog.ui.IdGenerator.getInstance();
317
318
319 /**
320 * Name of base CSS class of datepicker.
321 * @type {string}
322 * @private
323 */
324 goog.ui.DatePicker.BASE_CSS_CLASS_ = goog.getCssName('goog-date-picker');
325
326
327 /**
328 * The numbers of years to show before and after the current one in the
329 * year pull-down menu. A total of YEAR_MENU_RANGE * 2 + 1 will be shown.
330 * Example: for range = 2 and year 2013 => [2011, 2012, 2013, 2014, 2015]
331 * @const {number}
332 * @private
333 */
334 goog.ui.DatePicker.YEAR_MENU_RANGE_ = 5;
335
336
337 /**
338 * Constants for event names
339 *
340 * @const
341 */
342 goog.ui.DatePicker.Events = {
343 CHANGE: 'change',
344 CHANGE_ACTIVE_MONTH: 'changeActiveMonth',
345 SELECT: 'select'
346 };
347
348
349 /**
350 * @deprecated Use isInDocument.
351 */
352 goog.ui.DatePicker.prototype.isCreated =
353 goog.ui.DatePicker.prototype.isInDocument;
354
355
356 /**
357 * @return {number} The first day of week, 0 = Monday, 6 = Sunday.
358 */
359 goog.ui.DatePicker.prototype.getFirstWeekday = function() {
360 return this.activeMonth_.getFirstDayOfWeek();
361 };
362
363
364 /**
365 * Returns the class name associated with specified weekday.
366 * @param {number} wday The week day number to get the class name for.
367 * @return {string} The class name associated with specified weekday.
368 */
369 goog.ui.DatePicker.prototype.getWeekdayClass = function(wday) {
370 return this.wdayStyles_[wday];
371 };
372
373
374 /**
375 * @return {boolean} Whether a fixed number of weeks should be showed. If not
376 * only weeks for the current month will be shown.
377 */
378 goog.ui.DatePicker.prototype.getShowFixedNumWeeks = function() {
379 return this.showFixedNumWeeks_;
380 };
381
382
383 /**
384 * @return {boolean} Whether a days from the previous and/or next month should
385 * be shown.
386 */
387 goog.ui.DatePicker.prototype.getShowOtherMonths = function() {
388 return this.showOtherMonths_;
389 };
390
391
392 /**
393 * @return {boolean} Whether a the extra week(s) added always should be at the
394 * end. Only applicable if a fixed number of weeks are shown.
395 */
396 goog.ui.DatePicker.prototype.getExtraWeekAtEnd = function() {
397 return this.extraWeekAtEnd_;
398 };
399
400
401 /**
402 * @return {boolean} Whether week numbers should be shown.
403 */
404 goog.ui.DatePicker.prototype.getShowWeekNum = function() {
405 return this.showWeekNum_;
406 };
407
408
409 /**
410 * @return {boolean} Whether weekday names should be shown.
411 */
412 goog.ui.DatePicker.prototype.getShowWeekdayNames = function() {
413 return this.showWeekdays_;
414 };
415
416
417 /**
418 * @return {boolean} Whether none is a valid selection.
419 */
420 goog.ui.DatePicker.prototype.getAllowNone = function() {
421 return this.allowNone_;
422 };
423
424
425 /**
426 * @return {boolean} Whether the today button should be shown.
427 */
428 goog.ui.DatePicker.prototype.getShowToday = function() {
429 return this.showToday_;
430 };
431
432
433 /**
434 * Returns base CSS class. This getter is used to get base CSS class part.
435 * All CSS class names in component are created as:
436 * goog.getCssName(this.getBaseCssClass(), 'CLASS_NAME')
437 * @return {string} Base CSS class.
438 */
439 goog.ui.DatePicker.prototype.getBaseCssClass = function() {
440 return goog.ui.DatePicker.BASE_CSS_CLASS_;
441 };
442
443
444 /**
445 * Sets the first day of week
446 *
447 * @param {number} wday Week day, 0 = Monday, 6 = Sunday.
448 */
449 goog.ui.DatePicker.prototype.setFirstWeekday = function(wday) {
450 this.activeMonth_.setFirstDayOfWeek(wday);
451 this.updateCalendarGrid_();
452 this.redrawWeekdays_();
453 };
454
455
456 /**
457 * Sets class name associated with specified weekday.
458 *
459 * @param {number} wday Week day, 0 = Monday, 6 = Sunday.
460 * @param {string} className Class name.
461 */
462 goog.ui.DatePicker.prototype.setWeekdayClass = function(wday, className) {
463 this.wdayStyles_[wday] = className;
464 this.redrawCalendarGrid_();
465 };
466
467
468 /**
469 * Sets whether a fixed number of weeks should be showed. If not only weeks
470 * for the current month will be showed.
471 *
472 * @param {boolean} b Whether a fixed number of weeks should be showed.
473 */
474 goog.ui.DatePicker.prototype.setShowFixedNumWeeks = function(b) {
475 this.showFixedNumWeeks_ = b;
476 this.updateCalendarGrid_();
477 };
478
479
480 /**
481 * Sets whether a days from the previous and/or next month should be shown.
482 *
483 * @param {boolean} b Whether a days from the previous and/or next month should
484 * be shown.
485 */
486 goog.ui.DatePicker.prototype.setShowOtherMonths = function(b) {
487 this.showOtherMonths_ = b;
488 this.redrawCalendarGrid_();
489 };
490
491
492 /**
493 * Sets the range of dates which may be selected by the user.
494 *
495 * @param {!goog.date.DateRange} dateRange The range of selectable dates.
496 */
497 goog.ui.DatePicker.prototype.setUserSelectableDateRange = function(dateRange) {
498 this.userSelectableDateRange_ = dateRange;
499 };
500
501
502 /**
503 * Gets the range of dates which may be selected by the user.
504 *
505 * @return {!goog.date.DateRange} The range of selectable dates.
506 */
507 goog.ui.DatePicker.prototype.getUserSelectableDateRange = function() {
508 return this.userSelectableDateRange_;
509 };
510
511
512 /**
513 * Determine if a date may be selected by the user.
514 *
515 * @param {!goog.date.Date} date The date to be tested.
516 * @return {boolean} Whether the user may select this date.
517 * @private
518 */
519 goog.ui.DatePicker.prototype.isUserSelectableDate_ = function(date) {
520 return this.userSelectableDateRange_.contains(date);
521 };
522
523
524 /**
525 * Sets whether the picker should use a simple navigation menu that only
526 * contains controls for navigating to the next and previous month. The default
527 * navigation menu contains controls for navigating to the next/previous month,
528 * next/previous year, and menus for jumping to specific months and years.
529 *
530 * @param {boolean} b Whether to use a simple navigation menu.
531 */
532 goog.ui.DatePicker.prototype.setUseSimpleNavigationMenu = function(b) {
533 this.simpleNavigation_ = b;
534 this.updateNavigationRow_();
535 this.updateCalendarGrid_();
536 };
537
538
539 /**
540 * Sets whether a the extra week(s) added always should be at the end. Only
541 * applicable if a fixed number of weeks are shown.
542 *
543 * @param {boolean} b Whether a the extra week(s) added always should be at the
544 * end.
545 */
546 goog.ui.DatePicker.prototype.setExtraWeekAtEnd = function(b) {
547 this.extraWeekAtEnd_ = b;
548 this.updateCalendarGrid_();
549 };
550
551
552 /**
553 * Sets whether week numbers should be shown.
554 *
555 * @param {boolean} b Whether week numbers should be shown.
556 */
557 goog.ui.DatePicker.prototype.setShowWeekNum = function(b) {
558 this.showWeekNum_ = b;
559 // The navigation and footer rows may rely on the number of visible columns,
560 // so we update them when adding/removing the weeknum column.
561 this.updateNavigationRow_();
562 this.updateFooterRow_();
563 this.updateCalendarGrid_();
564 };
565
566
567 /**
568 * Sets whether weekday names should be shown.
569 *
570 * @param {boolean} b Whether weekday names should be shown.
571 */
572 goog.ui.DatePicker.prototype.setShowWeekdayNames = function(b) {
573 this.showWeekdays_ = b;
574 this.redrawWeekdays_();
575 this.redrawCalendarGrid_();
576 };
577
578
579 /**
580 * Sets whether the picker uses narrow weekday names ('M', 'T', 'W', ...).
581 *
582 * The default behavior is to use short names ('Mon', 'Tue', 'Wed', ...).
583 *
584 * @param {boolean} b Whether to use narrow weekday names.
585 */
586 goog.ui.DatePicker.prototype.setUseNarrowWeekdayNames = function(b) {
587 this.wdayNames_ = b ? this.symbols_.STANDALONENARROWWEEKDAYS :
588 this.symbols_.STANDALONESHORTWEEKDAYS;
589 this.redrawWeekdays_();
590 };
591
592
593 /**
594 * Sets whether none is a valid selection.
595 *
596 * @param {boolean} b Whether none is a valid selection.
597 */
598 goog.ui.DatePicker.prototype.setAllowNone = function(b) {
599 this.allowNone_ = b;
600 if (this.elNone_) {
601 this.updateTodayAndNone_();
602 }
603 };
604
605
606 /**
607 * Sets whether the today button should be shown.
608 *
609 * @param {boolean} b Whether the today button should be shown.
610 */
611 goog.ui.DatePicker.prototype.setShowToday = function(b) {
612 this.showToday_ = b;
613 if (this.elToday_) {
614 this.updateTodayAndNone_();
615 }
616 };
617
618
619 /**
620 * Updates the display style of the None and Today buttons as well as hides the
621 * table foot if both are hidden.
622 * @private
623 */
624 goog.ui.DatePicker.prototype.updateTodayAndNone_ = function() {
625 goog.style.setElementShown(this.elToday_, this.showToday_);
626 goog.style.setElementShown(this.elNone_, this.allowNone_);
627 goog.style.setElementShown(
628 this.tableFoot_, this.showToday_ || this.allowNone_);
629 };
630
631
632 /**
633 * Sets the decorator function. The function should have the interface of
634 * {string} f({goog.date.Date});
635 * and return a String representing a CSS class to decorate the cell
636 * corresponding to the date specified.
637 *
638 * @param {Function} f The decorator function.
639 */
640 goog.ui.DatePicker.prototype.setDecorator = function(f) {
641 this.decoratorFunction_ = f;
642 };
643
644
645 /**
646 * Sets whether the date will be printed in long format. In long format, dates
647 * such as '1' will be printed as '01'.
648 *
649 * @param {boolean} b Whethere dates should be printed in long format.
650 */
651 goog.ui.DatePicker.prototype.setLongDateFormat = function(b) {
652 this.longDateFormat_ = b;
653 this.redrawCalendarGrid_();
654 };
655
656
657 /**
658 * Changes the active month to the previous one.
659 */
660 goog.ui.DatePicker.prototype.previousMonth = function() {
661 this.activeMonth_.add(new goog.date.Interval(goog.date.Interval.MONTHS, -1));
662 this.updateCalendarGrid_();
663 this.fireChangeActiveMonthEvent_();
664 };
665
666
667 /**
668 * Changes the active month to the next one.
669 */
670 goog.ui.DatePicker.prototype.nextMonth = function() {
671 this.activeMonth_.add(new goog.date.Interval(goog.date.Interval.MONTHS, 1));
672 this.updateCalendarGrid_();
673 this.fireChangeActiveMonthEvent_();
674 };
675
676
677 /**
678 * Changes the active year to the previous one.
679 */
680 goog.ui.DatePicker.prototype.previousYear = function() {
681 this.activeMonth_.add(new goog.date.Interval(goog.date.Interval.YEARS, -1));
682 this.updateCalendarGrid_();
683 this.fireChangeActiveMonthEvent_();
684 };
685
686
687 /**
688 * Changes the active year to the next one.
689 */
690 goog.ui.DatePicker.prototype.nextYear = function() {
691 this.activeMonth_.add(new goog.date.Interval(goog.date.Interval.YEARS, 1));
692 this.updateCalendarGrid_();
693 this.fireChangeActiveMonthEvent_();
694 };
695
696
697 /**
698 * Selects the current date.
699 */
700 goog.ui.DatePicker.prototype.selectToday = function() {
701 this.setDate(new goog.date.Date());
702 };
703
704
705 /**
706 * Clears the selection.
707 */
708 goog.ui.DatePicker.prototype.selectNone = function() {
709 if (this.allowNone_) {
710 this.setDate(null);
711 }
712 };
713
714
715 /**
716 * @return {!goog.date.Date} The active month displayed.
717 */
718 goog.ui.DatePicker.prototype.getActiveMonth = function() {
719 return this.activeMonth_.clone();
720 };
721
722
723 /**
724 * @return {goog.date.Date} The selected date or null if nothing is selected.
725 */
726 goog.ui.DatePicker.prototype.getDate = function() {
727 return this.date_ && this.date_.clone();
728 };
729
730
731 /**
732 * @param {number} row The row in the grid.
733 * @param {number} col The column in the grid.
734 * @return {goog.date.Date} The date in the grid or null if there is none.
735 */
736 goog.ui.DatePicker.prototype.getDateAt = function(row, col) {
737 return this.grid_[row] ?
738 this.grid_[row][col] ? this.grid_[row][col].clone() : null :
739 null;
740 };
741
742
743 /**
744 * Returns a date element given a row and column. In elTable_, the elements that
745 * represent dates are 1 indexed because of other elements such as headers.
746 * This corrects for the offset and makes the API 0 indexed.
747 *
748 * @param {number} row The row in the element table.
749 * @param {number} col The column in the element table.
750 * @return {Element} The element in the grid or null if there is none.
751 * @protected
752 */
753 goog.ui.DatePicker.prototype.getDateElementAt = function(row, col) {
754 if (row < 0 || col < 0) {
755 return null;
756 }
757 var adjustedRow = row + 1;
758 return this.elTable_[adjustedRow] ?
759 this.elTable_[adjustedRow][col + 1] || null :
760 null;
761 };
762
763
764 /**
765 * Sets the selected date. Will always fire the SELECT event.
766 *
767 * @param {goog.date.Date|Date} date Date to select or null to select nothing.
768 */
769 goog.ui.DatePicker.prototype.setDate = function(date) {
770 this.setDate_(date, true);
771 };
772
773
774 /**
775 * Sets the selected date, and optionally fires the SELECT event based on param.
776 *
777 * @param {goog.date.Date|Date} date Date to select or null to select nothing.
778 * @param {boolean} fireSelection Whether to fire the selection event.
779 * @private
780 */
781 goog.ui.DatePicker.prototype.setDate_ = function(date, fireSelection) {
782 // Check if the month has been changed.
783 var sameMonth = date == this.date_ ||
784 date && this.date_ && date.getFullYear() == this.date_.getFullYear() &&
785 date.getMonth() == this.date_.getMonth();
786
787 // Check if the date has been changed.
788 var sameDate =
789 date == this.date_ || sameMonth && date.getDate() == this.date_.getDate();
790
791 // Set current date to clone of supplied goog.date.Date or Date.
792 this.date_ = date && new goog.date.Date(date);
793
794 // Set current month
795 if (date) {
796 this.activeMonth_.set(this.date_);
797 // Set years with two digits to their full year, not 19XX.
798 this.activeMonth_.setFullYear(this.date_.getFullYear());
799 this.activeMonth_.setDate(1);
800 }
801
802 // Update calendar grid even if the date has not changed as even if today is
803 // selected another month can be displayed.
804 this.updateCalendarGrid_();
805
806 if (fireSelection) {
807 // TODO(eae): Standardize selection and change events with other components.
808 // Fire select event.
809 var selectEvent = new goog.ui.DatePickerEvent(
810 goog.ui.DatePicker.Events.SELECT, this, this.date_);
811 this.dispatchEvent(selectEvent);
812 }
813
814 // Fire change event.
815 if (!sameDate) {
816 var changeEvent = new goog.ui.DatePickerEvent(
817 goog.ui.DatePicker.Events.CHANGE, this, this.date_);
818 this.dispatchEvent(changeEvent);
819 }
820
821 // Fire change active month event.
822 if (!sameMonth) {
823 this.fireChangeActiveMonthEvent_();
824 }
825 };
826
827
828 /**
829 * Updates the navigation row (navigating months and maybe years) in the navRow_
830 * element of a created picker.
831 * @private
832 */
833 goog.ui.DatePicker.prototype.updateNavigationRow_ = function() {
834 if (!this.elNavRow_) {
835 return;
836 }
837 var row = this.elNavRow_;
838
839 // Clear the navigation row.
840 while (row.firstChild) {
841 row.removeChild(row.firstChild);
842 }
843
844 var fullDateFormat =
845 this.symbols_.DATEFORMATS[goog.i18n.DateTimeFormat.Format.FULL_DATE]
846 .toLowerCase();
847 this.renderer_.renderNavigationRow(
848 row, this.simpleNavigation_, this.showWeekNum_, fullDateFormat);
849
850 if (this.simpleNavigation_) {
851 this.addPreventDefaultClickHandler_(
852 row, goog.getCssName(this.getBaseCssClass(), 'previousMonth'),
853 this.previousMonth);
854 var previousMonthElement = goog.dom.getElementByClass(
855 goog.getCssName(this.getBaseCssClass(), 'previousMonth'), row);
856 if (previousMonthElement) {
857 // Note: we're hiding the next and previous month buttons from screen
858 // readers because keyboard navigation doesn't currently work correctly
859 // with them. If that is fixed, we can show the buttons again.
860 goog.a11y.aria.setState(
861 previousMonthElement, goog.a11y.aria.State.HIDDEN, true);
862 previousMonthElement.tabIndex = -1;
863 }
864
865 this.addPreventDefaultClickHandler_(
866 row, goog.getCssName(this.getBaseCssClass(), 'nextMonth'),
867 this.nextMonth);
868 var nextMonthElement = goog.dom.getElementByClass(
869 goog.getCssName(this.getBaseCssClass(), 'nextMonth'), row);
870 if (nextMonthElement) {
871 goog.a11y.aria.setState(
872 nextMonthElement, goog.a11y.aria.State.HIDDEN, true);
873 nextMonthElement.tabIndex = -1;
874 }
875
876 this.elMonthYear_ = goog.dom.getElementByClass(
877 goog.getCssName(this.getBaseCssClass(), 'monthyear'), row);
878 } else {
879 this.addPreventDefaultClickHandler_(
880 row, goog.getCssName(this.getBaseCssClass(), 'previousMonth'),
881 this.previousMonth);
882 this.addPreventDefaultClickHandler_(
883 row, goog.getCssName(this.getBaseCssClass(), 'nextMonth'),
884 this.nextMonth);
885 this.addPreventDefaultClickHandler_(
886 row, goog.getCssName(this.getBaseCssClass(), 'month'),
887 this.showMonthMenu_);
888
889 this.addPreventDefaultClickHandler_(
890 row, goog.getCssName(this.getBaseCssClass(), 'previousYear'),
891 this.previousYear);
892 this.addPreventDefaultClickHandler_(
893 row, goog.getCssName(this.getBaseCssClass(), 'nextYear'),
894 this.nextYear);
895 this.addPreventDefaultClickHandler_(
896 row, goog.getCssName(this.getBaseCssClass(), 'year'),
897 this.showYearMenu_);
898
899 this.elMonth_ = goog.dom.getElementByClass(
900 goog.getCssName(this.getBaseCssClass(), 'month'), row);
901 this.elYear_ = goog.dom.getDomHelper().getElementByClass(
902 goog.getCssName(this.getBaseCssClass(), 'year'), row);
903 }
904 };
905
906
907 /**
908 * Setup click handler with prevent default.
909 *
910 * @param {!Element} parentElement The parent element of the element. This is
911 * needed because the element in question might not be in the dom yet.
912 * @param {string} cssName The CSS class name of the element to attach a click
913 * handler.
914 * @param {Function} handlerFunction The click handler function.
915 * @private
916 */
917 goog.ui.DatePicker.prototype.addPreventDefaultClickHandler_ = function(
918 parentElement, cssName, handlerFunction) {
919 var element = goog.dom.getElementByClass(cssName, parentElement);
920 this.getHandler().listen(element, goog.events.EventType.CLICK, function(e) {
921 e.preventDefault();
922 handlerFunction.call(this, e);
923 });
924 };
925
926
927 /**
928 * Updates the footer row (with select buttons) in the footRow_ element of a
929 * created picker.
930 * @private
931 */
932 goog.ui.DatePicker.prototype.updateFooterRow_ = function() {
933 if (!this.elFootRow_) {
934 return;
935 }
936
937 var row = this.elFootRow_;
938
939 // Clear the footer row.
940 goog.dom.removeChildren(row);
941
942 this.renderer_.renderFooterRow(row, this.showWeekNum_);
943
944 this.addPreventDefaultClickHandler_(
945 row, goog.getCssName(this.getBaseCssClass(), 'today-btn'),
946 this.selectToday);
947 this.addPreventDefaultClickHandler_(
948 row, goog.getCssName(this.getBaseCssClass(), 'none-btn'),
949 this.selectNone);
950
951 this.elToday_ = goog.dom.getElementByClass(
952 goog.getCssName(this.getBaseCssClass(), 'today-btn'), row);
953 this.elNone_ = goog.dom.getElementByClass(
954 goog.getCssName(this.getBaseCssClass(), 'none-btn'), row);
955
956 this.updateTodayAndNone_();
957 };
958
959
960 /** @override */
961 goog.ui.DatePicker.prototype.decorateInternal = function(el) {
962 goog.ui.DatePicker.superClass_.decorateInternal.call(this, el);
963 goog.asserts.assert(el);
964 goog.dom.classlist.add(el, this.getBaseCssClass());
965
966 var table =
967 this.dom_.createDom(goog.dom.TagName.TABLE, {'role': 'presentation'});
968 var thead = this.dom_.createDom(goog.dom.TagName.THEAD);
969 var tbody = this.dom_.createDom(goog.dom.TagName.TBODY, {'role': 'grid'});
970 var tfoot = this.dom_.createDom(goog.dom.TagName.TFOOT);
971
972 tbody.tabIndex = 0;
973
974 // As per comment in colorpicker: table.tBodies and table.tFoot should not be
975 // used because of a bug in Safari, hence using an instance variable
976 this.tableBody_ = tbody;
977 this.tableFoot_ = tfoot;
978
979 var row = this.dom_.createDom(goog.dom.TagName.TR, {'role': 'row'});
980 row.className = goog.getCssName(this.getBaseCssClass(), 'head');
981 this.elNavRow_ = row;
982 this.updateNavigationRow_();
983
984 thead.appendChild(row);
985
986 var cell;
987 this.elTable_ = [];
988 for (var i = 0; i < 7; i++) {
989 row = this.dom_.createElement(goog.dom.TagName.TR);
990 this.elTable_[i] = [];
991 for (var j = 0; j < 8; j++) {
992 cell = this.dom_.createElement(j == 0 || i == 0 ? 'th' : 'td');
993 if ((j == 0 || i == 0) && j != i) {
994 cell.className = (j == 0) ?
995 goog.getCssName(this.getBaseCssClass(), 'week') :
996 goog.getCssName(this.getBaseCssClass(), 'wday');
997 goog.a11y.aria.setRole(cell, j == 0 ? 'rowheader' : 'columnheader');
998 } else if (i !== 0 && j !== 0) {
999 goog.a11y.aria.setRole(cell, 'gridcell');
1000 // Make the cells programmatically-focusable (see focus() call below).
1001 cell.setAttribute('tabindex', '-1');
1002 }
1003 row.appendChild(cell);
1004 this.elTable_[i][j] = cell;
1005 }
1006 tbody.appendChild(row);
1007 }
1008
1009 row = this.dom_.createElement(goog.dom.TagName.TR);
1010 row.className = goog.getCssName(this.getBaseCssClass(), 'foot');
1011 this.elFootRow_ = row;
1012 this.updateFooterRow_();
1013 tfoot.appendChild(row);
1014
1015
1016 table.cellSpacing = '0';
1017 table.cellPadding = '0';
1018 table.appendChild(thead);
1019 table.appendChild(tbody);
1020 table.appendChild(tfoot);
1021 el.appendChild(table);
1022
1023 this.redrawWeekdays_();
1024 this.updateCalendarGrid_();
1025
1026 el.tabIndex = 0;
1027 };
1028
1029
1030 /** @override */
1031 goog.ui.DatePicker.prototype.createDom = function() {
1032 goog.ui.DatePicker.superClass_.createDom.call(this);
1033 this.decorateInternal(this.getElement());
1034 };
1035
1036
1037 /** @override */
1038 goog.ui.DatePicker.prototype.enterDocument = function() {
1039 goog.ui.DatePicker.superClass_.enterDocument.call(this);
1040
1041 var eh = this.getHandler();
1042 eh.listen(
1043 this.tableBody_, goog.events.EventType.CLICK, this.handleGridClick_);
1044 eh.listen(
1045 this.getKeyHandlerForElement_(this.getElement()),
1046 goog.events.KeyHandler.EventType.KEY, this.handleGridKeyPress_);
1047 };
1048
1049
1050 /** @override */
1051 goog.ui.DatePicker.prototype.exitDocument = function() {
1052 goog.ui.DatePicker.superClass_.exitDocument.call(this);
1053 this.destroyMenu_();
1054 for (var uid in this.keyHandlers_) {
1055 this.keyHandlers_[uid].dispose();
1056 }
1057 this.keyHandlers_ = {};
1058 };
1059
1060
1061 /**
1062 * @deprecated Use decorate instead.
1063 */
1064 goog.ui.DatePicker.prototype.create = goog.ui.DatePicker.prototype.decorate;
1065
1066
1067 /** @override */
1068 goog.ui.DatePicker.prototype.disposeInternal = function() {
1069 goog.ui.DatePicker.superClass_.disposeInternal.call(this);
1070
1071 this.elTable_ = null;
1072 this.tableBody_ = null;
1073 this.tableFoot_ = null;
1074 this.elNavRow_ = null;
1075 this.elFootRow_ = null;
1076 this.elMonth_ = null;
1077 this.elMonthYear_ = null;
1078 this.elYear_ = null;
1079 this.elToday_ = null;
1080 this.elNone_ = null;
1081 };
1082
1083
1084 /**
1085 * Click handler for date grid.
1086 *
1087 * @param {goog.events.BrowserEvent} event Click event.
1088 * @private
1089 */
1090 goog.ui.DatePicker.prototype.handleGridClick_ = function(event) {
1091 if (event.target.tagName == goog.dom.TagName.TD) {
1092 // colIndex/rowIndex is broken in Safari, find position by looping
1093 var el, x = -2, y = -2; // first col/row is for weekday/weeknum
1094 for (el = event.target; el; el = el.previousSibling, x++) {
1095 }
1096 for (el = event.target.parentNode; el; el = el.previousSibling, y++) {
1097 }
1098 var obj = this.grid_[y][x];
1099 if (this.isUserSelectableDate_(obj)) {
1100 this.setDate(obj.clone());
1101 }
1102 }
1103 };
1104
1105
1106 /**
1107 * Keypress handler for date grid.
1108 *
1109 * @param {goog.events.BrowserEvent} event Keypress event.
1110 * @private
1111 */
1112 goog.ui.DatePicker.prototype.handleGridKeyPress_ = function(event) {
1113 var months, days;
1114 switch (event.keyCode) {
1115 case 33: // Page up
1116 event.preventDefault();
1117 months = -1;
1118 break;
1119 case 34: // Page down
1120 event.preventDefault();
1121 months = 1;
1122 break;
1123 case 37: // Left
1124 event.preventDefault();
1125 days = -1;
1126 break;
1127 case 39: // Right
1128 event.preventDefault();
1129 days = 1;
1130 break;
1131 case 38: // Down
1132 event.preventDefault();
1133 days = -7;
1134 break;
1135 case 40: // Up
1136 event.preventDefault();
1137 days = 7;
1138 break;
1139 case 36: // Home
1140 event.preventDefault();
1141 this.selectToday();
1142 case 46: // Delete
1143 event.preventDefault();
1144 this.selectNone();
1145 break;
1146 case 13: // Enter
1147 case 32: // Space
1148 event.preventDefault();
1149 this.setDate_(this.date_, true /* fireSelection */);
1150 default:
1151 return;
1152 }
1153 var date;
1154 if (this.date_) {
1155 date = this.date_.clone();
1156 date.add(new goog.date.Interval(0, months, days));
1157 } else {
1158 date = this.activeMonth_.clone();
1159 date.setDate(1);
1160 }
1161 if (this.isUserSelectableDate_(date)) {
1162 this.setDate_(date, false /* fireSelection */);
1163 // Focus the currently-selected cell to surface its aria-label for assistive
1164 // tech, (eg) allowing unsighted users to see what the arrow keys are doing.
1165 this.selectedCell_.focus();
1166 }
1167 };
1168
1169
1170 /**
1171 * Click handler for month button. Opens month selection menu.
1172 *
1173 * @param {goog.events.BrowserEvent} event Click event.
1174 * @private
1175 */
1176 goog.ui.DatePicker.prototype.showMonthMenu_ = function(event) {
1177 event.stopPropagation();
1178
1179 var list = [];
1180 for (var i = 0; i < 12; i++) {
1181 list.push(this.symbols_.STANDALONEMONTHS[i]);
1182 }
1183 this.createMenu_(
1184 this.elMonth_, list, this.handleMonthMenuClick_,
1185 this.symbols_.STANDALONEMONTHS[this.activeMonth_.getMonth()]);
1186 };
1187
1188
1189 /**
1190 * Click handler for year button. Opens year selection menu.
1191 *
1192 * @param {goog.events.BrowserEvent} event Click event.
1193 * @private
1194 */
1195 goog.ui.DatePicker.prototype.showYearMenu_ = function(event) {
1196 event.stopPropagation();
1197
1198 var list = [];
1199 var year = this.activeMonth_.getFullYear();
1200 var loopDate = this.activeMonth_.clone();
1201 for (var i = -goog.ui.DatePicker.YEAR_MENU_RANGE_;
1202 i <= goog.ui.DatePicker.YEAR_MENU_RANGE_; i++) {
1203 loopDate.setFullYear(year + i);
1204 list.push(this.i18nDateFormatterYear_.format(loopDate));
1205 }
1206 this.createMenu_(
1207 this.elYear_, list, this.handleYearMenuClick_,
1208 this.i18nDateFormatterYear_.format(this.activeMonth_));
1209 };
1210
1211
1212 /**
1213 * Call back function for month menu.
1214 *
1215 * @param {Element} target Selected item.
1216 * @private
1217 */
1218 goog.ui.DatePicker.prototype.handleMonthMenuClick_ = function(target) {
1219 var itemIndex = Number(target.getAttribute('itemIndex'));
1220 this.activeMonth_.setMonth(itemIndex);
1221 this.updateCalendarGrid_();
1222
1223 if (this.elMonth_.focus) {
1224 this.elMonth_.focus();
1225 }
1226 };
1227
1228
1229 /**
1230 * Call back function for year menu.
1231 *
1232 * @param {Element} target Selected item.
1233 * @private
1234 */
1235 goog.ui.DatePicker.prototype.handleYearMenuClick_ = function(target) {
1236 if (target.firstChild.nodeType == goog.dom.NodeType.TEXT) {
1237 // We use the same technique used for months to get the position of the
1238 // item in the menu, as the year is not necessarily numeric.
1239 var itemIndex = Number(target.getAttribute('itemIndex'));
1240 var year = this.activeMonth_.getFullYear();
1241 this.activeMonth_.setFullYear(
1242 year + itemIndex - goog.ui.DatePicker.YEAR_MENU_RANGE_);
1243 this.updateCalendarGrid_();
1244 }
1245
1246 this.elYear_.focus();
1247 };
1248
1249
1250 /**
1251 * Support function for menu creation.
1252 *
1253 * @param {Element} srcEl Button to create menu for.
1254 * @param {Array<string>} items List of items to populate menu with.
1255 * @param {function(Element)} method Call back method.
1256 * @param {string} selected Item to mark as selected in menu.
1257 * @private
1258 */
1259 goog.ui.DatePicker.prototype.createMenu_ = function(
1260 srcEl, items, method, selected) {
1261 this.destroyMenu_();
1262
1263 var el = this.dom_.createElement(goog.dom.TagName.DIV);
1264 el.className = goog.getCssName(this.getBaseCssClass(), 'menu');
1265
1266 this.menuSelected_ = null;
1267
1268 var ul = this.dom_.createElement(goog.dom.TagName.UL);
1269 for (var i = 0; i < items.length; i++) {
1270 var li = this.dom_.createDom(goog.dom.TagName.LI, null, items[i]);
1271 li.setAttribute('itemIndex', i);
1272 if (items[i] == selected) {
1273 this.menuSelected_ = li;
1274 }
1275 ul.appendChild(li);
1276 }
1277 el.appendChild(ul);
1278 srcEl = /** @type {!HTMLElement} */ (srcEl);
1279 el.style.left = srcEl.offsetLeft + srcEl.parentNode.offsetLeft + 'px';
1280 el.style.top = srcEl.offsetTop + 'px';
1281 el.style.width = srcEl.clientWidth + 'px';
1282 this.elMonth_.parentNode.appendChild(el);
1283
1284 this.menu_ = el;
1285 if (!this.menuSelected_) {
1286 this.menuSelected_ = /** @type {Element} */ (ul.firstChild);
1287 }
1288 this.menuSelected_.className =
1289 goog.getCssName(this.getBaseCssClass(), 'menu-selected');
1290 this.menuCallback_ = method;
1291
1292 var eh = this.getHandler();
1293 eh.listen(this.menu_, goog.events.EventType.CLICK, this.handleMenuClick_);
1294 eh.listen(
1295 this.getKeyHandlerForElement_(this.menu_),
1296 goog.events.KeyHandler.EventType.KEY, this.handleMenuKeyPress_);
1297 eh.listen(
1298 this.dom_.getDocument(), goog.events.EventType.CLICK, this.destroyMenu_);
1299 el.tabIndex = 0;
1300 el.focus();
1301 };
1302
1303
1304 /**
1305 * Click handler for menu.
1306 *
1307 * @param {goog.events.BrowserEvent} event Click event.
1308 * @private
1309 */
1310 goog.ui.DatePicker.prototype.handleMenuClick_ = function(event) {
1311 event.stopPropagation();
1312
1313 this.destroyMenu_();
1314 if (this.menuCallback_) {
1315 this.menuCallback_(/** @type {Element} */ (event.target));
1316 }
1317 };
1318
1319
1320 /**
1321 * Keypress handler for menu.
1322 *
1323 * @param {goog.events.BrowserEvent} event Keypress event.
1324 * @private
1325 */
1326 goog.ui.DatePicker.prototype.handleMenuKeyPress_ = function(event) {
1327 // Prevent the grid keypress handler from catching the keypress event.
1328 event.stopPropagation();
1329
1330 var el;
1331 var menuSelected = this.menuSelected_;
1332 switch (event.keyCode) {
1333 case 35: // End
1334 event.preventDefault();
1335 el = menuSelected.parentNode.lastChild;
1336 break;
1337 case 36: // Home
1338 event.preventDefault();
1339 el = menuSelected.parentNode.firstChild;
1340 break;
1341 case 38: // Up
1342 event.preventDefault();
1343 el = menuSelected.previousSibling;
1344 break;
1345 case 40: // Down
1346 event.preventDefault();
1347 el = menuSelected.nextSibling;
1348 break;
1349 case 13: // Enter
1350 case 9: // Tab
1351 case 0: // Space
1352 event.preventDefault();
1353 this.destroyMenu_();
1354 this.menuCallback_(menuSelected);
1355 break;
1356 }
1357 if (el && el != menuSelected) {
1358 menuSelected.className = '';
1359 el.className = goog.getCssName(this.getBaseCssClass(), 'menu-selected');
1360 this.menuSelected_ = /** @type {!Element} */ (el);
1361 }
1362 };
1363
1364
1365 /**
1366 * Support function for menu destruction.
1367 * @private
1368 */
1369 goog.ui.DatePicker.prototype.destroyMenu_ = function() {
1370 if (this.menu_) {
1371 var eh = this.getHandler();
1372 eh.unlisten(this.menu_, goog.events.EventType.CLICK, this.handleMenuClick_);
1373 eh.unlisten(
1374 this.getKeyHandlerForElement_(this.menu_),
1375 goog.events.KeyHandler.EventType.KEY, this.handleMenuKeyPress_);
1376 eh.unlisten(
1377 this.dom_.getDocument(), goog.events.EventType.CLICK,
1378 this.destroyMenu_);
1379 goog.dom.removeNode(this.menu_);
1380 delete this.menu_;
1381 }
1382 };
1383
1384
1385 /**
1386 * Determines the dates/weekdays for the current month and builds an in memory
1387 * representation of the calendar.
1388 *
1389 * @private
1390 */
1391 goog.ui.DatePicker.prototype.updateCalendarGrid_ = function() {
1392 if (!this.getElement()) {
1393 return;
1394 }
1395
1396 var date = this.activeMonth_.clone();
1397 date.setDate(1);
1398
1399 // Show year name of select month
1400 if (this.elMonthYear_) {
1401 goog.dom.setTextContent(
1402 this.elMonthYear_, this.i18nDateFormatterMonthYear_.format(date));
1403 }
1404 if (this.elMonth_) {
1405 goog.dom.setTextContent(
1406 this.elMonth_, this.symbols_.STANDALONEMONTHS[date.getMonth()]);
1407 }
1408 if (this.elYear_) {
1409 goog.dom.setTextContent(
1410 this.elYear_, this.i18nDateFormatterYear_.format(date));
1411 }
1412
1413 var wday = date.getWeekday();
1414 var days = date.getNumberOfDaysInMonth();
1415
1416 // Determine how many days to show for previous month
1417 date.add(new goog.date.Interval(goog.date.Interval.MONTHS, -1));
1418 date.setDate(date.getNumberOfDaysInMonth() - (wday - 1));
1419
1420 if (this.showFixedNumWeeks_ && !this.extraWeekAtEnd_ && days + wday < 33) {
1421 date.add(new goog.date.Interval(goog.date.Interval.DAYS, -7));
1422 }
1423
1424 // Create weekday/day grid
1425 var dayInterval = new goog.date.Interval(goog.date.Interval.DAYS, 1);
1426 this.grid_ = [];
1427 for (var y = 0; y < 6; y++) { // Weeks
1428 this.grid_[y] = [];
1429 for (var x = 0; x < 7; x++) { // Weekdays
1430 this.grid_[y][x] = date.clone();
1431 // Date.add breaks dates before year 100 by adding 1900 to the year
1432 // value. As a workaround we store the year before the add and reapply it
1433 // after (with special handling for January 1st).
1434 var year = date.getFullYear();
1435 date.add(dayInterval);
1436 if (date.getMonth() == 0 && date.getDate() == 1) {
1437 // Increase year on January 1st.
1438 year++;
1439 }
1440 date.setFullYear(year);
1441 }
1442 }
1443
1444 this.redrawCalendarGrid_();
1445 };
1446
1447
1448 /**
1449 * Draws calendar view from in memory representation and applies class names
1450 * depending on the selection, weekday and whatever the day belongs to the
1451 * active month or not.
1452 * @private
1453 */
1454 goog.ui.DatePicker.prototype.redrawCalendarGrid_ = function() {
1455 if (!this.getElement()) {
1456 return;
1457 }
1458
1459 var month = this.activeMonth_.getMonth();
1460 var today = new goog.date.Date();
1461 var todayYear = today.getFullYear();
1462 var todayMonth = today.getMonth();
1463 var todayDate = today.getDate();
1464
1465 // Draw calendar week by week, a worst case month has six weeks.
1466 for (var y = 0; y < 6; y++) {
1467 // Draw week number, if enabled
1468 if (this.showWeekNum_) {
1469 goog.dom.setTextContent(
1470 this.elTable_[y + 1][0],
1471 this.i18nDateFormatterWeek_.format(this.grid_[y][0]));
1472 goog.dom.classlist.set(
1473 this.elTable_[y + 1][0],
1474 goog.getCssName(this.getBaseCssClass(), 'week'));
1475 } else {
1476 goog.dom.setTextContent(this.elTable_[y + 1][0], '');
1477 goog.dom.classlist.set(this.elTable_[y + 1][0], '');
1478 }
1479
1480 for (var x = 0; x < 7; x++) {
1481 var o = this.grid_[y][x];
1482 var el = this.elTable_[y + 1][x + 1];
1483
1484 // Assign a unique element id (required for setting the active descendant
1485 // ARIA role) unless already set.
1486 if (!el.id) {
1487 el.id = this.cellIdGenerator_.getNextUniqueId();
1488 }
1489 goog.asserts.assert(el, 'The table DOM element cannot be null.');
1490 goog.a11y.aria.setRole(el, 'gridcell');
1491 // Set the aria label of the grid cell to the month plus the day.
1492 goog.a11y.aria.setLabel(
1493 el, this.i18nDateFormatterDayAriaLabel_.format(o));
1494
1495 var classes = [goog.getCssName(this.getBaseCssClass(), 'date')];
1496 if (!this.isUserSelectableDate_(o)) {
1497 classes.push(
1498 goog.getCssName(this.getBaseCssClass(), 'unavailable-date'));
1499 }
1500 if (this.showOtherMonths_ || o.getMonth() == month) {
1501 // Date belongs to previous or next month
1502 if (o.getMonth() != month) {
1503 classes.push(goog.getCssName(this.getBaseCssClass(), 'other-month'));
1504 }
1505
1506 // Apply styles set by setWeekdayClass
1507 var wday = (x + this.activeMonth_.getFirstDayOfWeek() + 7) % 7;
1508 if (this.wdayStyles_[wday]) {
1509 classes.push(this.wdayStyles_[wday]);
1510 }
1511
1512 // Current date
1513 if (o.getDate() == todayDate && o.getMonth() == todayMonth &&
1514 o.getFullYear() == todayYear) {
1515 classes.push(goog.getCssName(this.getBaseCssClass(), 'today'));
1516 }
1517
1518 // Selected date
1519 if (this.date_ && o.getDate() == this.date_.getDate() &&
1520 o.getMonth() == this.date_.getMonth() &&
1521 o.getFullYear() == this.date_.getFullYear()) {
1522 classes.push(goog.getCssName(this.getBaseCssClass(), 'selected'));
1523 goog.asserts.assert(
1524 this.tableBody_, 'The table body DOM element cannot be null');
1525 this.selectedCell_ = el;
1526 }
1527
1528 // Custom decorator
1529 if (this.decoratorFunction_) {
1530 var customClass = this.decoratorFunction_(o);
1531 if (customClass) {
1532 classes.push(customClass);
1533 }
1534 }
1535
1536 // Set cell text to the date and apply classes.
1537 var formattedDate = this.longDateFormat_ ?
1538 this.i18nDateFormatterDay2_.format(o) :
1539 this.i18nDateFormatterDay_.format(o);
1540 goog.dom.setTextContent(el, formattedDate);
1541 // Date belongs to previous or next month and showOtherMonths is false,
1542 // clear text and classes.
1543 } else {
1544 goog.dom.setTextContent(el, '');
1545 }
1546 goog.dom.classlist.set(el, classes.join(' '));
1547 }
1548
1549 // Hide the either the last one or last two weeks if they contain no days
1550 // from the active month and the showFixedNumWeeks is false. The first four
1551 // weeks are always shown as no month has less than 28 days).
1552 if (y >= 4) {
1553 var parentEl = /** @type {Element} */ (
1554 this.elTable_[y + 1][0].parentElement ||
1555 this.elTable_[y + 1][0].parentNode);
1556 goog.style.setElementShown(
1557 parentEl,
1558 this.grid_[y][0].getMonth() == month || this.showFixedNumWeeks_);
1559 }
1560 }
1561 };
1562
1563
1564 /**
1565 * Fires the CHANGE_ACTIVE_MONTH event.
1566 * @private
1567 */
1568 goog.ui.DatePicker.prototype.fireChangeActiveMonthEvent_ = function() {
1569 var changeMonthEvent = new goog.ui.DatePickerEvent(
1570 goog.ui.DatePicker.Events.CHANGE_ACTIVE_MONTH, this,
1571 this.getActiveMonth());
1572 this.dispatchEvent(changeMonthEvent);
1573 };
1574
1575
1576 /**
1577 * Draw weekday names, if enabled. Start with whatever day has been set as the
1578 * first day of week.
1579 * @private
1580 */
1581 goog.ui.DatePicker.prototype.redrawWeekdays_ = function() {
1582 if (!this.getElement()) {
1583 return;
1584 }
1585 if (this.showWeekdays_) {
1586 for (var x = 0; x < 7; x++) {
1587 var el = this.elTable_[0][x + 1];
1588 var wday = (x + this.activeMonth_.getFirstDayOfWeek() + 7) % 7;
1589 goog.dom.setTextContent(el, this.wdayNames_[(wday + 1) % 7]);
1590 }
1591 }
1592 var parentEl = /** @type {Element} */ (
1593 this.elTable_[0][0].parentElement || this.elTable_[0][0].parentNode);
1594 goog.style.setElementShown(parentEl, this.showWeekdays_);
1595 };
1596
1597
1598 /**
1599 * Returns the key handler for an element and caches it so that it can be
1600 * retrieved at a later point.
1601 * @param {Element} el The element to get the key handler for.
1602 * @return {goog.events.KeyHandler} The key handler for the element.
1603 * @private
1604 */
1605 goog.ui.DatePicker.prototype.getKeyHandlerForElement_ = function(el) {
1606 var uid = goog.getUid(el);
1607 if (!(uid in this.keyHandlers_)) {
1608 this.keyHandlers_[uid] = new goog.events.KeyHandler(el);
1609 }
1610 return this.keyHandlers_[uid];
1611 };
1612
1613
1614
1615 /**
1616 * Object representing a date picker event.
1617 *
1618 * @param {string} type Event type.
1619 * @param {goog.ui.DatePicker} target Date picker initiating event.
1620 * @param {goog.date.Date} date Selected date.
1621 * @constructor
1622 * @extends {goog.events.Event}
1623 * @final
1624 */
1625 goog.ui.DatePickerEvent = function(type, target, date) {
1626 goog.events.Event.call(this, type, target);
1627
1628 /**
1629 * The selected date
1630 * @type {goog.date.Date}
1631 */
1632 this.date = date;
1633 };
1634 goog.inherits(goog.ui.DatePickerEvent, goog.events.Event);