Debian Codegraph @

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

raw
1 // Copyright 2013 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 The default renderer for {@link goog.ui.DatePicker}.
17 *
18 * @see ../demos/datepicker.html
19 */
20
21 goog.provide('goog.ui.DefaultDatePickerRenderer');
22
23 goog.require('goog.dom');
24 goog.require('goog.dom.TagName');
25 /** @suppress {extraRequire} Interface. */
26 goog.require('goog.ui.DatePickerRenderer');
27
28
29
30 /**
31 * Default renderer for {@link goog.ui.DatePicker}. Renders the date picker's
32 * navigation header and footer.
33 *
34 * @param {string} baseCssClass Name of base CSS class of the date picker.
35 * @param {goog.dom.DomHelper=} opt_domHelper DOM helper.
36 * @constructor
37 * @implements {goog.ui.DatePickerRenderer}
38 */
39 goog.ui.DefaultDatePickerRenderer = function(baseCssClass, opt_domHelper) {
40 /**
41 * Name of base CSS class of datepicker
42 * @type {string}
43 * @private
44 */
45 this.baseCssClass_ = baseCssClass;
46
47 /**
48 * @type {!goog.dom.DomHelper}
49 * @private
50 */
51 this.dom_ = opt_domHelper || goog.dom.getDomHelper();
52 };
53
54
55 /**
56 * Returns the dom helper that is being used on this component.
57 * @return {!goog.dom.DomHelper} The dom helper used on this component.
58 */
59 goog.ui.DefaultDatePickerRenderer.prototype.getDomHelper = function() {
60 return this.dom_;
61 };
62
63
64 /**
65 * Returns base CSS class. This getter is used to get base CSS class part.
66 * All CSS class names in component are created as:
67 * goog.getCssName(this.getBaseCssClass(), 'CLASS_NAME')
68 * @return {string} Base CSS class.
69 */
70 goog.ui.DefaultDatePickerRenderer.prototype.getBaseCssClass = function() {
71 return this.baseCssClass_;
72 };
73
74
75 /**
76 * Render the navigation row (navigating months and maybe years).
77 *
78 * @param {!Element} row The parent element to render the component into.
79 * @param {boolean} simpleNavigation Whether the picker should render a simple
80 * navigation menu that only contains controls for navigating to the next
81 * and previous month. The default navigation menu contains controls for
82 * navigating to the next/previous month, next/previous year, and menus for
83 * jumping to specific months and years.
84 * @param {boolean} showWeekNum Whether week numbers should be shown.
85 * @param {string} fullDateFormat The full date format.
86 * {@see goog.i18n.DateTimeSymbols}.
87 * @override
88 */
89 goog.ui.DefaultDatePickerRenderer.prototype.renderNavigationRow = function(
90 row, simpleNavigation, showWeekNum, fullDateFormat) {
91 // Populate the navigation row according to the configured navigation mode.
92 var cell, monthCell, yearCell;
93
94 if (simpleNavigation) {
95 cell = this.getDomHelper().createElement(goog.dom.TagName.TD);
96 cell.colSpan = showWeekNum ? 1 : 2;
97 this.createButton_(
98 cell, '\u00AB',
99 goog.getCssName(this.getBaseCssClass(), 'previousMonth')); // <<
100 row.appendChild(cell);
101
102 cell = this.getDomHelper().createElement(goog.dom.TagName.TD);
103 cell.colSpan = showWeekNum ? 6 : 5;
104 cell.className = goog.getCssName(this.getBaseCssClass(), 'monthyear');
105 row.appendChild(cell);
106
107 cell = this.getDomHelper().createElement(goog.dom.TagName.TD);
108 this.createButton_(
109 cell, '\u00BB',
110 goog.getCssName(this.getBaseCssClass(), 'nextMonth')); // >>
111 row.appendChild(cell);
112
113 } else {
114 monthCell = this.getDomHelper().createElement(goog.dom.TagName.TD);
115 monthCell.colSpan = 5;
116 this.createButton_(
117 monthCell, '\u00AB',
118 goog.getCssName(this.getBaseCssClass(), 'previousMonth')); // <<
119 this.createButton_(
120 monthCell, '', goog.getCssName(this.getBaseCssClass(), 'month'));
121 this.createButton_(
122 monthCell, '\u00BB',
123 goog.getCssName(this.getBaseCssClass(), 'nextMonth')); // >>
124
125 yearCell = this.getDomHelper().createElement(goog.dom.TagName.TD);
126 yearCell.colSpan = 3;
127 this.createButton_(
128 yearCell, '\u00AB',
129 goog.getCssName(this.getBaseCssClass(), 'previousYear')); // <<
130 this.createButton_(
131 yearCell, '', goog.getCssName(this.getBaseCssClass(), 'year'));
132 this.createButton_(
133 yearCell, '\u00BB',
134 goog.getCssName(this.getBaseCssClass(), 'nextYear')); // <<
135
136 // If the date format has year ('y') appearing first before month ('m'),
137 // show the year on the left hand side of the datepicker popup. Otherwise,
138 // show the month on the left side. This check assumes the data to be
139 // valid, and that all date formats contain month and year.
140 if (fullDateFormat.indexOf('y') < fullDateFormat.indexOf('m')) {
141 row.appendChild(yearCell);
142 row.appendChild(monthCell);
143 } else {
144 row.appendChild(monthCell);
145 row.appendChild(yearCell);
146 }
147 }
148 };
149
150
151 /**
152 * Render the footer row (with select buttons).
153 *
154 * @param {!Element} row The parent element to render the component into.
155 * @param {boolean} showWeekNum Whether week numbers should be shown.
156 * @override
157 */
158 goog.ui.DefaultDatePickerRenderer.prototype.renderFooterRow = function(
159 row, showWeekNum) {
160 // Populate the footer row with buttons for Today and None.
161 var cell = this.getDomHelper().createElement(goog.dom.TagName.TD);
162 cell.colSpan = showWeekNum ? 2 : 3;
163 cell.className = goog.getCssName(this.getBaseCssClass(), 'today-cont');
164
165 /** @desc Label for button that selects the current date. */
166 var MSG_DATEPICKER_TODAY_BUTTON_LABEL = goog.getMsg('Today');
167 this.createButton_(
168 cell, MSG_DATEPICKER_TODAY_BUTTON_LABEL,
169 goog.getCssName(this.getBaseCssClass(), 'today-btn'));
170 row.appendChild(cell);
171
172 cell = this.getDomHelper().createElement(goog.dom.TagName.TD);
173 cell.colSpan = showWeekNum ? 4 : 3;
174 row.appendChild(cell);
175
176 cell = this.getDomHelper().createElement(goog.dom.TagName.TD);
177 cell.colSpan = 2;
178 cell.className = goog.getCssName(this.getBaseCssClass(), 'none-cont');
179
180 /** @desc Label for button that clears the selection. */
181 var MSG_DATEPICKER_NONE = goog.getMsg('None');
182 this.createButton_(
183 cell, MSG_DATEPICKER_NONE,
184 goog.getCssName(this.getBaseCssClass(), 'none-btn'));
185 row.appendChild(cell);
186 };
187
188
189 /**
190 * Support function for button creation.
191 *
192 * @param {Element} parentNode Container the button should be added to.
193 * @param {string} label Button label.
194 * @param {string=} opt_className Class name for button, which will be used
195 * in addition to "goog-date-picker-btn".
196 * @private
197 * @return {!Element} The created button element.
198 */
199 goog.ui.DefaultDatePickerRenderer.prototype.createButton_ = function(
200 parentNode, label, opt_className) {
201 var classes = [goog.getCssName(this.getBaseCssClass(), 'btn')];
202 if (opt_className) {
203 classes.push(opt_className);
204 }
205 var el = this.getDomHelper().createElement(goog.dom.TagName.BUTTON);
206 el.className = classes.join(' ');
207 el.appendChild(this.getDomHelper().createTextNode(label));
208 parentNode.appendChild(el);
209 return el;
210 };