Debian Codegraph @

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

raw
1 // Copyright 2007 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 Input Date Picker implementation. Pairs a
17 * goog.ui.PopupDatePicker with an input element and handles the input from
18 * either.
19 *
20 * @see ../demos/inputdatepicker.html
21 */
22
23 goog.provide('goog.ui.InputDatePicker');
24
25 goog.require('goog.date.DateTime');
26 goog.require('goog.dom');
27 goog.require('goog.dom.InputType');
28 goog.require('goog.dom.TagName');
29 goog.require('goog.i18n.DateTimeParse');
30 goog.require('goog.string');
31 goog.require('goog.ui.Component');
32 goog.require('goog.ui.DatePicker');
33 /** @suppress {extraRequire} */
34 goog.require('goog.ui.LabelInput');
35 goog.require('goog.ui.PopupBase');
36 goog.require('goog.ui.PopupDatePicker');
37
38
39
40 /**
41 * Input date picker widget.
42 *
43 * @param {goog.i18n.DateTimeFormat} dateTimeFormatter A formatter instance
44 * used to format the date picker's date for display in the input element.
45 * @param {goog.i18n.DateTimeParse} dateTimeParser A parser instance used to
46 * parse the input element's string as a date to set the picker.
47 * @param {goog.ui.DatePicker=} opt_datePicker Optional DatePicker. This
48 * enables the use of a custom date-picker instance.
49 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
50 * @extends {goog.ui.Component}
51 * @constructor
52 */
53 goog.ui.InputDatePicker = function(
54 dateTimeFormatter, dateTimeParser, opt_datePicker, opt_domHelper) {
55 goog.ui.Component.call(this, opt_domHelper);
56
57 this.dateTimeFormatter_ = dateTimeFormatter;
58 this.dateTimeParser_ = dateTimeParser;
59
60 this.popupDatePicker_ =
61 new goog.ui.PopupDatePicker(opt_datePicker, opt_domHelper);
62 this.addChild(this.popupDatePicker_);
63 this.popupDatePicker_.setAllowAutoFocus(false);
64 };
65 goog.inherits(goog.ui.InputDatePicker, goog.ui.Component);
66 goog.tagUnsealableClass(goog.ui.InputDatePicker);
67
68
69 /**
70 * Used to format the date picker's date for display in the input element.
71 * @type {goog.i18n.DateTimeFormat}
72 * @private
73 */
74 goog.ui.InputDatePicker.prototype.dateTimeFormatter_ = null;
75
76
77 /**
78 * Used to parse the input element's string as a date to set the picker.
79 * @type {goog.i18n.DateTimeParse}
80 * @private
81 */
82 goog.ui.InputDatePicker.prototype.dateTimeParser_ = null;
83
84
85 /**
86 * The instance of goog.ui.PopupDatePicker used to pop up and select the date.
87 * @type {goog.ui.PopupDatePicker}
88 * @private
89 */
90 goog.ui.InputDatePicker.prototype.popupDatePicker_ = null;
91
92
93 /**
94 * The element that the PopupDatePicker should be parented to. Defaults to the
95 * body element of the page.
96 * @type {Element}
97 * @private
98 */
99 goog.ui.InputDatePicker.prototype.popupParentElement_ = null;
100
101
102 /**
103 * Returns the PopupDatePicker's internal DatePicker instance. This can be
104 * used to customize the date picker's styling.
105 *
106 * @return {goog.ui.DatePicker} The internal DatePicker instance.
107 */
108 goog.ui.InputDatePicker.prototype.getDatePicker = function() {
109 return this.popupDatePicker_.getDatePicker();
110 };
111
112
113 /**
114 * Returns the PopupDatePicker instance.
115 *
116 * @return {goog.ui.PopupDatePicker} Popup instance.
117 */
118 goog.ui.InputDatePicker.prototype.getPopupDatePicker = function() {
119 return this.popupDatePicker_;
120 };
121
122
123 /**
124 * Returns the selected date, if any. Compares the dates from the date picker
125 * and the input field, causing them to be synced if different.
126 * @return {goog.date.DateTime} The selected date, if any.
127 */
128 goog.ui.InputDatePicker.prototype.getDate = function() {
129
130 // The user expectation is that the date be whatever the input shows.
131 // This method biases towards the input value to conform to that expectation.
132
133 var inputDate = this.getInputValueAsDate_();
134 var pickerDate = this.popupDatePicker_.getDate();
135
136 if (inputDate && pickerDate) {
137 if (!inputDate.equals(pickerDate)) {
138 this.popupDatePicker_.setDate(inputDate);
139 }
140 } else {
141 this.popupDatePicker_.setDate(null);
142 }
143
144 return inputDate;
145 };
146
147
148 /**
149 * Sets the selected date. See goog.ui.PopupDatePicker.setDate().
150 * @param {goog.date.Date} date The date to set.
151 */
152 goog.ui.InputDatePicker.prototype.setDate = function(date) {
153 this.popupDatePicker_.setDate(date);
154 };
155
156
157 /**
158 * Sets the value of the input element. This can be overridden to support
159 * alternative types of input setting.
160 *
161 * @param {string} value The value to set.
162 */
163 goog.ui.InputDatePicker.prototype.setInputValue = function(value) {
164 var el = this.getElement();
165 if (el.labelInput_) {
166 var labelInput = /** @type {goog.ui.LabelInput} */ (el.labelInput_);
167 labelInput.setValue(value);
168 } else {
169 el.value = value;
170 }
171 };
172
173
174 /**
175 * Returns the value of the input element. This can be overridden to support
176 * alternative types of input getting.
177 *
178 * @return {string} The input value.
179 */
180 goog.ui.InputDatePicker.prototype.getInputValue = function() {
181 var el = this.getElement();
182 if (el.labelInput_) {
183 var labelInput = /** @type {goog.ui.LabelInput} */ (el.labelInput_);
184 return labelInput.getValue();
185 } else {
186 return el.value;
187 }
188 };
189
190
191 /**
192 * Sets the value of the input element from date object.
193 *
194 * @param {?goog.date.Date} date The value to set.
195 * @private
196 */
197 goog.ui.InputDatePicker.prototype.setInputValueAsDate_ = function(date) {
198 this.setInputValue(date ? this.dateTimeFormatter_.format(date) : '');
199 };
200
201
202 /**
203 * Gets the input element value and attempts to parse it as a date.
204 *
205 * @return {goog.date.DateTime} The date object is returned if the parse
206 * is successful, null is returned on failure.
207 * @private
208 */
209 goog.ui.InputDatePicker.prototype.getInputValueAsDate_ = function() {
210 var value = goog.string.trim(this.getInputValue());
211 if (value) {
212 var date = new goog.date.DateTime();
213 // DateTime needed as parse assumes it can call getHours(), getMinutes(),
214 // etc, on the date if hours and minutes aren't defined.
215 if (this.dateTimeParser_.strictParse(value, date) > 0) {
216 // Parser with YYYY format string will interpret 1 as year 1 A.D.
217 // However, datepicker.setDate() method will change it into 1901.
218 // Same is true for any other pattern when number entered by user is
219 // different from number of digits in the pattern. (YY and 1 will be 1AD).
220 // See i18n/datetimeparse.js
221 // Conversion happens in goog.date.Date/DateTime constructor
222 // when it calls new Date(year...). See ui/datepicker.js.
223 return date;
224 }
225 }
226
227 return null;
228 };
229
230
231 /**
232 * Creates an input element for use with the popup date picker.
233 * @override
234 */
235 goog.ui.InputDatePicker.prototype.createDom = function() {
236 this.setElementInternal(
237 this.getDomHelper().createDom(
238 goog.dom.TagName.INPUT, {'type': goog.dom.InputType.TEXT}));
239 this.popupDatePicker_.createDom();
240 };
241
242
243 /**
244 * Sets the element that the PopupDatePicker should be parented to. If not set,
245 * defaults to the body element of the page.
246 * @param {Element} el The element that the PopupDatePicker should be parented
247 * to.
248 */
249 goog.ui.InputDatePicker.prototype.setPopupParentElement = function(el) {
250 this.popupParentElement_ = el;
251 };
252
253
254 /** @override */
255 goog.ui.InputDatePicker.prototype.enterDocument = function() {
256 // this.popupDatePicker_ has been added as a child even though it isn't really
257 // a child (since its root element is not within InputDatePicker's DOM tree).
258 // The PopupDatePicker will have its enterDocument method called as a result
259 // of calling the superClass's enterDocument method. The PopupDatePicker needs
260 // to be attached to the document *before* calling enterDocument so that when
261 // PopupDatePicker decorates its element as a DatePicker, the element will be
262 // in the document and enterDocument will be called for the DatePicker. Having
263 // the PopupDatePicker's element in the document before calling enterDocument
264 // will ensure that the event handlers for DatePicker are attached.
265 //
266 // An alternative could be to stop adding popupDatePicker_ as a child and
267 // instead keep a reference to it and sync some event handlers, etc. but
268 // appending the element to the document before calling enterDocument is a
269 // less intrusive option.
270 //
271 // See cl/100837907 for more context and the discussion around this decision.
272 (this.popupParentElement_ || this.getDomHelper().getDocument().body)
273 .appendChild(this.popupDatePicker_.getElement());
274
275 goog.ui.InputDatePicker.superClass_.enterDocument.call(this);
276 var el = this.getElement();
277
278 this.popupDatePicker_.attach(el);
279
280 // Set the date picker to have the input's initial value, if any.
281 this.popupDatePicker_.setDate(this.getInputValueAsDate_());
282
283 var handler = this.getHandler();
284 handler.listen(
285 this.popupDatePicker_, goog.ui.DatePicker.Events.CHANGE,
286 this.onDateChanged_);
287 handler.listen(
288 this.popupDatePicker_, goog.ui.PopupBase.EventType.SHOW, this.onPopup_);
289 };
290
291
292 /** @override */
293 goog.ui.InputDatePicker.prototype.exitDocument = function() {
294 goog.ui.InputDatePicker.superClass_.exitDocument.call(this);
295 var el = this.getElement();
296
297 this.popupDatePicker_.detach(el);
298 this.popupDatePicker_.exitDocument();
299 goog.dom.removeNode(this.popupDatePicker_.getElement());
300 };
301
302
303 /** @override */
304 goog.ui.InputDatePicker.prototype.decorateInternal = function(element) {
305 goog.ui.InputDatePicker.superClass_.decorateInternal.call(this, element);
306
307 this.popupDatePicker_.createDom();
308 };
309
310
311 /** @override */
312 goog.ui.InputDatePicker.prototype.disposeInternal = function() {
313 goog.ui.InputDatePicker.superClass_.disposeInternal.call(this);
314 this.popupDatePicker_.dispose();
315 this.popupDatePicker_ = null;
316 this.popupParentElement_ = null;
317 };
318
319
320 /**
321 * See goog.ui.PopupDatePicker.showPopup().
322 * @param {Element} element Reference element for displaying the popup -- popup
323 * will appear at the bottom-left corner of this element.
324 */
325 goog.ui.InputDatePicker.prototype.showForElement = function(element) {
326 this.popupDatePicker_.showPopup(element);
327 };
328
329
330 /**
331 * See goog.ui.PopupDatePicker.hidePopup().
332 */
333 goog.ui.InputDatePicker.prototype.hidePopup = function() {
334 this.popupDatePicker_.hidePopup();
335 };
336
337
338 /**
339 * Event handler for popup date picker popup events.
340 *
341 * @param {goog.events.Event} e popup event.
342 * @private
343 */
344 goog.ui.InputDatePicker.prototype.onPopup_ = function(e) {
345 var inputValueAsDate = this.getInputValueAsDate_();
346 this.setDate(inputValueAsDate);
347 // don't overwrite the input value with empty date if input is not valid
348 if (inputValueAsDate) {
349 this.setInputValueAsDate_(this.getDatePicker().getDate());
350 }
351 };
352
353
354 /**
355 * Event handler for date change events. Called when the date changes.
356 *
357 * @param {goog.ui.DatePickerEvent} e Date change event.
358 * @private
359 */
360 goog.ui.InputDatePicker.prototype.onDateChanged_ = function(e) {
361 this.setInputValueAsDate_(e.date);
362 };