| 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 Popup Date Picker implementation. Pairs a goog.ui.DatePicker |
| 17 |
* with a goog.ui.Popup allowing the DatePicker to be attached to elements. |
| 18 |
* |
| 19 |
* @see ../demos/popupdatepicker.html |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.ui.PopupDatePicker'); |
| 23 |
|
| 24 |
goog.require('goog.events.EventType'); |
| 25 |
goog.require('goog.positioning.AnchoredPosition'); |
| 26 |
goog.require('goog.positioning.Corner'); |
| 27 |
goog.require('goog.positioning.Overflow'); |
| 28 |
goog.require('goog.style'); |
| 29 |
goog.require('goog.ui.Component'); |
| 30 |
goog.require('goog.ui.DatePicker'); |
| 31 |
goog.require('goog.ui.Popup'); |
| 32 |
goog.require('goog.ui.PopupBase'); |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Popup date picker widget. Fires goog.ui.PopupBase.EventType.SHOW or HIDE |
| 38 |
* events when its visibility changes. |
| 39 |
* |
| 40 |
* @param {goog.ui.DatePicker=} opt_datePicker Optional DatePicker. This |
| 41 |
* enables the use of a custom date-picker instance. |
| 42 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper. |
| 43 |
* @extends {goog.ui.Component} |
| 44 |
* @constructor |
| 45 |
*/ |
| 46 |
goog.ui.PopupDatePicker = function(opt_datePicker, opt_domHelper) { |
| 47 |
goog.ui.Component.call(this, opt_domHelper); |
| 48 |
|
| 49 |
this.datePicker_ = opt_datePicker || new goog.ui.DatePicker(); |
| 50 |
}; |
| 51 |
goog.inherits(goog.ui.PopupDatePicker, goog.ui.Component); |
| 52 |
goog.tagUnsealableClass(goog.ui.PopupDatePicker); |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Instance of a date picker control. |
| 57 |
* @type {goog.ui.DatePicker?} |
| 58 |
* @private |
| 59 |
*/ |
| 60 |
goog.ui.PopupDatePicker.prototype.datePicker_ = null; |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Instance of goog.ui.Popup used to manage the behavior of the date picker. |
| 65 |
* @type {goog.ui.Popup?} |
| 66 |
* @private |
| 67 |
*/ |
| 68 |
goog.ui.PopupDatePicker.prototype.popup_ = null; |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* Reference to the element that triggered the last popup. |
| 73 |
* @type {Element} |
| 74 |
* @private |
| 75 |
*/ |
| 76 |
goog.ui.PopupDatePicker.prototype.lastTarget_ = null; |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* Whether the date picker can move the focus to its key event target when it |
| 81 |
* is shown. The default is true. Setting to false can break keyboard |
| 82 |
* navigation, but this is needed for certain scenarios, for example the |
| 83 |
* toolbar menu in trogedit which can't have the selection changed. |
| 84 |
* @type {boolean} |
| 85 |
* @private |
| 86 |
*/ |
| 87 |
goog.ui.PopupDatePicker.prototype.allowAutoFocus_ = true; |
| 88 |
|
| 89 |
|
| 90 |
/** @override */ |
| 91 |
goog.ui.PopupDatePicker.prototype.createDom = function() { |
| 92 |
goog.ui.PopupDatePicker.superClass_.createDom.call(this); |
| 93 |
this.getElement().className = goog.getCssName('goog-popupdatepicker'); |
| 94 |
this.popup_ = new goog.ui.Popup(this.getElement()); |
| 95 |
this.popup_.setParentEventTarget(this); |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* @return {boolean} Whether the date picker is visible. |
| 101 |
*/ |
| 102 |
goog.ui.PopupDatePicker.prototype.isVisible = function() { |
| 103 |
return this.popup_ ? this.popup_.isVisible() : false; |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
/** @override */ |
| 108 |
goog.ui.PopupDatePicker.prototype.enterDocument = function() { |
| 109 |
goog.ui.PopupDatePicker.superClass_.enterDocument.call(this); |
| 110 |
// Create the DatePicker, if it isn't already. |
| 111 |
// Done here as DatePicker assumes that the element passed to it is attached |
| 112 |
// to a document. |
| 113 |
if (!this.datePicker_.isInDocument()) { |
| 114 |
var el = this.getElement(); |
| 115 |
// Make it initially invisible |
| 116 |
el.style.visibility = 'hidden'; |
| 117 |
goog.style.setElementShown(el, false); |
| 118 |
this.datePicker_.decorate(el); |
| 119 |
} |
| 120 |
this.getHandler() |
| 121 |
.listen( |
| 122 |
this.datePicker_, goog.ui.DatePicker.Events.CHANGE, |
| 123 |
this.onDateChanged_) |
| 124 |
.listen( |
| 125 |
this.datePicker_, goog.ui.DatePicker.Events.SELECT, |
| 126 |
this.onDateSelected_); |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
/** @override */ |
| 131 |
goog.ui.PopupDatePicker.prototype.disposeInternal = function() { |
| 132 |
goog.ui.PopupDatePicker.superClass_.disposeInternal.call(this); |
| 133 |
if (this.popup_) { |
| 134 |
this.popup_.dispose(); |
| 135 |
this.popup_ = null; |
| 136 |
} |
| 137 |
this.datePicker_.dispose(); |
| 138 |
this.datePicker_ = null; |
| 139 |
this.lastTarget_ = null; |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* DatePicker cannot be used to decorate pre-existing html, since they're |
| 145 |
* not based on Components. |
| 146 |
* @param {Element} element Element to decorate. |
| 147 |
* @return {boolean} Returns always false. |
| 148 |
* @override |
| 149 |
*/ |
| 150 |
goog.ui.PopupDatePicker.prototype.canDecorate = function(element) { |
| 151 |
return false; |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* @return {goog.ui.DatePicker} The date picker instance. |
| 157 |
*/ |
| 158 |
goog.ui.PopupDatePicker.prototype.getDatePicker = function() { |
| 159 |
return this.datePicker_; |
| 160 |
}; |
| 161 |
|
| 162 |
|
| 163 |
/** |
| 164 |
* @return {goog.date.Date?} The selected date, if any. See |
| 165 |
* goog.ui.DatePicker.getDate(). |
| 166 |
*/ |
| 167 |
goog.ui.PopupDatePicker.prototype.getDate = function() { |
| 168 |
return this.datePicker_.getDate(); |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
/** |
| 173 |
* Sets the selected date. See goog.ui.DatePicker.setDate(). |
| 174 |
* @param {goog.date.Date?} date The date to select. |
| 175 |
*/ |
| 176 |
goog.ui.PopupDatePicker.prototype.setDate = function(date) { |
| 177 |
this.datePicker_.setDate(date); |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* @return {Element} The last element that triggered the popup. |
| 183 |
*/ |
| 184 |
goog.ui.PopupDatePicker.prototype.getLastTarget = function() { |
| 185 |
return this.lastTarget_; |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
/** |
| 190 |
* Attaches the popup date picker to an element. |
| 191 |
* @param {Element} element The element to attach to. |
| 192 |
*/ |
| 193 |
goog.ui.PopupDatePicker.prototype.attach = function(element) { |
| 194 |
this.getHandler().listen( |
| 195 |
element, goog.events.EventType.MOUSEDOWN, this.showPopup_); |
| 196 |
}; |
| 197 |
|
| 198 |
|
| 199 |
/** |
| 200 |
* Detatches the popup date picker from an element. |
| 201 |
* @param {Element} element The element to detach from. |
| 202 |
*/ |
| 203 |
goog.ui.PopupDatePicker.prototype.detach = function(element) { |
| 204 |
this.getHandler().unlisten( |
| 205 |
element, goog.events.EventType.MOUSEDOWN, this.showPopup_); |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
/** |
| 210 |
* Sets whether the date picker can automatically move focus to its key event |
| 211 |
* target when it is set to visible. |
| 212 |
* @param {boolean} allow Whether to allow auto focus. |
| 213 |
*/ |
| 214 |
goog.ui.PopupDatePicker.prototype.setAllowAutoFocus = function(allow) { |
| 215 |
this.allowAutoFocus_ = allow; |
| 216 |
}; |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* @return {boolean} Whether the date picker can automatically move focus to |
| 221 |
* its key event target when it is set to visible. |
| 222 |
*/ |
| 223 |
goog.ui.PopupDatePicker.prototype.getAllowAutoFocus = function() { |
| 224 |
return this.allowAutoFocus_; |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
/** |
| 229 |
* Show the popup at the bottom-left corner of the specified element. |
| 230 |
* @param {Element} element Reference element for displaying the popup -- popup |
| 231 |
* will appear at the bottom-left corner of this element. |
| 232 |
* @param {boolean=} opt_keepDate Whether to keep the date picker's current |
| 233 |
* date. If false, the date is set to null. Defaults to false. |
| 234 |
*/ |
| 235 |
goog.ui.PopupDatePicker.prototype.showPopup = function(element, opt_keepDate) { |
| 236 |
this.lastTarget_ = element; |
| 237 |
this.popup_.setPosition( |
| 238 |
new goog.positioning.AnchoredPosition( |
| 239 |
element, goog.positioning.Corner.BOTTOM_START, |
| 240 |
(goog.positioning.Overflow.ADJUST_X_EXCEPT_OFFSCREEN | |
| 241 |
goog.positioning.Overflow.ADJUST_Y_EXCEPT_OFFSCREEN))); |
| 242 |
|
| 243 |
// Don't listen to date changes while we're setting up the popup so we don't |
| 244 |
// have to worry about change events when we call setDate(). |
| 245 |
this.getHandler() |
| 246 |
.unlisten( |
| 247 |
this.datePicker_, goog.ui.DatePicker.Events.CHANGE, |
| 248 |
this.onDateChanged_) |
| 249 |
.unlisten( |
| 250 |
this.datePicker_, goog.ui.DatePicker.Events.SELECT, |
| 251 |
this.onDateSelected_); |
| 252 |
|
| 253 |
var keepDate = !!opt_keepDate; |
| 254 |
if (!keepDate) { |
| 255 |
this.datePicker_.setDate(null); |
| 256 |
} |
| 257 |
|
| 258 |
// Forward the change event onto our listeners. Done before we start |
| 259 |
// listening to date changes again, so that listeners can change the date |
| 260 |
// without firing more events. |
| 261 |
this.dispatchEvent(goog.ui.PopupBase.EventType.SHOW); |
| 262 |
|
| 263 |
this.popup_.setVisible(true); |
| 264 |
if (this.allowAutoFocus_) { |
| 265 |
this.getElement().focus(); // Our element contains the date picker. |
| 266 |
} |
| 267 |
|
| 268 |
this.getHandler() |
| 269 |
.listen( |
| 270 |
this.datePicker_, goog.ui.DatePicker.Events.CHANGE, |
| 271 |
this.onDateChanged_) |
| 272 |
.listen( |
| 273 |
this.datePicker_, goog.ui.DatePicker.Events.SELECT, |
| 274 |
this.onDateSelected_); |
| 275 |
}; |
| 276 |
|
| 277 |
|
| 278 |
/** |
| 279 |
* Handles click events on the targets and shows the date picker. |
| 280 |
* @param {goog.events.Event} event The click event. |
| 281 |
* @private |
| 282 |
*/ |
| 283 |
goog.ui.PopupDatePicker.prototype.showPopup_ = function(event) { |
| 284 |
this.showPopup(/** @type {Element} */ (event.currentTarget)); |
| 285 |
}; |
| 286 |
|
| 287 |
|
| 288 |
/** |
| 289 |
* Hides this popup. |
| 290 |
*/ |
| 291 |
goog.ui.PopupDatePicker.prototype.hidePopup = function() { |
| 292 |
this.popup_.setVisible(false); |
| 293 |
if (this.allowAutoFocus_ && this.lastTarget_) { |
| 294 |
this.lastTarget_.focus(); |
| 295 |
} |
| 296 |
}; |
| 297 |
|
| 298 |
|
| 299 |
/** |
| 300 |
* Called when date selection is made. |
| 301 |
* |
| 302 |
* @param {!goog.events.Event} event The date change event. |
| 303 |
* @private |
| 304 |
*/ |
| 305 |
goog.ui.PopupDatePicker.prototype.onDateSelected_ = function(event) { |
| 306 |
this.hidePopup(); |
| 307 |
|
| 308 |
// Forward the change event onto our listeners. |
| 309 |
this.dispatchEvent(event); |
| 310 |
}; |
| 311 |
|
| 312 |
|
| 313 |
/** |
| 314 |
* Called when the date is changed. |
| 315 |
* |
| 316 |
* @param {!goog.events.Event} event The date change event. |
| 317 |
* @private |
| 318 |
*/ |
| 319 |
goog.ui.PopupDatePicker.prototype.onDateChanged_ = function(event) { |
| 320 |
// Forward the change event onto our listeners. |
| 321 |
this.dispatchEvent(event); |
| 322 |
}; |