|
1 | | -<!-- |
2 | | -@license |
3 | | -Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
4 | | -This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt |
5 | | -The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
6 | | -The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt |
7 | | -Code distributed by Google as part of the polymer project is also |
8 | | -subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt |
9 | | ---> |
10 | | - |
11 | 1 | <link rel="import" href="../polymer/polymer.html"> |
12 | 2 |
|
13 | 3 | <link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html"> |
|
20 | 10 | <link rel="import" href="../paper-dialog/paper-dialog.html"> |
21 | 11 | <link rel="import" href="../paper-date-picker/paper-date-picker.html"> |
22 | 12 |
|
23 | | -<script src="../moment/min/moment-with-locales.min.js"></script> |
24 | | - |
| 13 | +<link rel="import" href="./moment-with-locales-import.html"> |
25 | 14 | <!-- |
26 | 15 | An element providing a solution to no problem in particular. |
27 | 16 |
|
|
51 | 40 | text-align: left; |
52 | 41 | } |
53 | 42 |
|
| 43 | + :host paper-dialog paper-date-picker { |
| 44 | + margin: 0px; |
| 45 | + padding: 0px; |
| 46 | + } |
| 47 | + |
54 | 48 | :host > paper-button::shadow > .content.paper-button { |
55 | 49 | padding: 0px; |
56 | 50 | } |
|
63 | 57 | paper-button.btn-clear:hover { |
64 | 58 | background: var(--paper-red-50); |
65 | 59 | } |
| 60 | + |
| 61 | + @media (max-width: 600px) { |
| 62 | + :host paper-dialog { |
| 63 | + margin: 0px |
| 64 | + } |
| 65 | + } |
| 66 | + |
66 | 67 | </style> |
67 | 68 |
|
68 | | - <paper-button on-tap="showDialog" |
69 | | - focus-target="[[_dropdownContent]]"> |
| 69 | + <paper-button on-tap="_showDialog" |
| 70 | + focus-target="[[_dropdownContent]]" |
| 71 | + tabindex="-1"> |
70 | 72 | <paper-input id="input" |
71 | 73 | disabled="[[disabled]]" |
72 | | - value="[[dateInputText]]" |
| 74 | + value="[[_computeInputText(value)]]" |
73 | 75 | invalid="{{invalid}}" |
74 | 76 | prevent-invalid-input="[[preventInvalidInput]]" |
75 | 77 | allowed-pattern="[[allowedPattern]]" |
|
97 | 99 | autosave="[[autosave]]" |
98 | 100 | results="[[results]]" |
99 | 101 | error-message="[[errorMessage]]" |
100 | | - label="[[label]]"> |
| 102 | + label="[[label]]" |
| 103 | + on-blur="_onInputBlur" |
| 104 | + on-keydown="_onInputKeyDown" |
| 105 | + on-keyup="_onInputKeyUp" |
| 106 | + tabindex="-1" |
| 107 | + readonly> |
101 | 108 | <iron-icon icon="[[prefixIcon]]" hidden="[[noPrefixIcon]]" prefix></iron-icon> |
102 | 109 | <iron-icon icon="arrow-drop-down" suffix></iron-icon> |
103 | 110 | </paper-input> |
104 | 111 | </paper-button> |
105 | | - <paper-dialog id="dialog" class="paper-date-picker-dialog" modal="[[modal]]" on-iron-overlay-closed="dismissDialog"> |
| 112 | + <paper-dialog id="dialog" class="paper-date-picker-dialog" modal="[[modal]]" on-iron-overlay-closed="_dismissDialog"> |
106 | 113 | <paper-date-picker id="picker" locale="{{locale}}"></paper-date-picker> |
107 | 114 | <div class="buttons"> |
108 | | - <paper-button dialog-confirm on-tap="onTapClearValue" class="btn-clear">Clear</paper-button> |
| 115 | + <paper-button dialog-dismiss on-tap="_onTapClearValue" class="btn-clear">Clear</paper-button> |
109 | 116 | <paper-button dialog-dismiss>Cancel</paper-button> |
110 | 117 | <paper-button dialog-confirm>OK</paper-button> |
111 | 118 | </div> |
|
121 | 128 | is: 'paper-input-date-picker', |
122 | 129 |
|
123 | 130 | behaviors: [ |
124 | | - Polymer.IronFormElementBehavior, |
125 | | - Polymer.PaperInputBehavior, |
126 | | - Polymer.IronControlState, |
127 | | - Polymer.IronButtonState, |
128 | | - Polymer.IronA11yKeysBehavior |
| 131 | + Polymer.IronButtonState |
129 | 132 | ], |
130 | 133 |
|
| 134 | + hostAttributes: { |
| 135 | + tabindex: '0' |
| 136 | + }, |
| 137 | + |
131 | 138 | properties: { |
| 139 | + |
| 140 | + /** |
| 141 | + * Turns the datepicker in a modal dialog . |
| 142 | + */ |
| 143 | + modal: { |
| 144 | + type: Boolean |
| 145 | + }, |
132 | 146 |
|
133 | 147 | /** |
134 | 148 | * The label for the dropdown. |
|
175 | 189 | type: Boolean, |
176 | 190 | value: false |
177 | 191 | }, |
178 | | - |
179 | | - /** |
180 | | - * Format date to be displayed on input. |
181 | | - */ |
182 | | - dateFormat: { |
183 | | - type: String, |
184 | | - }, |
185 | 192 |
|
186 | 193 | /** |
187 | 194 | * For localization formatting. |
188 | 195 | */ |
189 | 196 | locale: { |
190 | 197 | type: String |
191 | 198 | }, |
192 | | - |
193 | | - /** |
194 | | - * The derived "label" of the currently selected item. This value |
195 | | - * is the `label` property on the selected item if set, or else the |
196 | | - * trimmed text content of the selected item. |
197 | | - */ |
198 | | - dateInputText: { |
199 | | - type: String, |
200 | | - notify: true, |
201 | | - computed: '_computeInputText(value)' |
202 | | - }, |
203 | 199 |
|
204 | 200 | keyEventTarget: { |
205 | 201 | type: Object, |
206 | 202 | value: function() { |
207 | 203 | return this; |
208 | 204 | } |
209 | 205 | }, |
| 206 | + |
| 207 | + value: { |
| 208 | + type: Date, |
| 209 | + notify: true |
| 210 | + } |
210 | 211 | }, |
211 | 212 |
|
212 | | - // Element Lifecycle |
| 213 | + get $picker() { |
| 214 | + return this.$.picker; |
| 215 | + }, |
213 | 216 |
|
214 | | - ready: function() { |
215 | | - // `ready` is called after all elements have been configured, but |
216 | | - // propagates bottom-up. This element's children are ready, but parents |
217 | | - // are not. |
218 | | - // |
219 | | - // This is the point where you should make modifications to the DOM (when |
220 | | - // necessary), or kick off any processes the element wants to perform. |
221 | | - var $this = this; |
222 | | - |
223 | | - this.locale = window.navigator.userLanguage || window.navigator.language; |
224 | | - |
225 | | - this.addEventListener("focus", function () { |
226 | | - |
227 | | - $this.$.input.readonly = true; |
228 | | - $this.$.input.focus(); |
229 | | - }, true); |
230 | | - |
231 | | - this.addEventListener("blur", function () { |
| 217 | + get _inputElement() { |
| 218 | + return this.$.input; |
| 219 | + }, |
232 | 220 |
|
233 | | - $this.$.input.readonly = false; |
234 | | - }, true); |
235 | | - |
236 | | - this._inputElement = this.$.input; |
237 | | - |
238 | | - this.dateFormat = this.$.picker.dateFormat; |
| 221 | + listeners: { |
| 222 | + 'focus': '_onFocus' |
239 | 223 | }, |
240 | 224 |
|
241 | | - attached: function() { |
242 | | - // `attached` fires once the element and its parents have been inserted |
243 | | - // into a document. |
244 | | - // |
245 | | - // This is a good place to perform any work related to your element's |
246 | | - // visual state or active behavior (measuring sizes, beginning animations, |
247 | | - // loading resources, etc). |
| 225 | + _onFocus: function (e) { |
| 226 | + this._inputElement.focus(); |
| 227 | + this.setAttribute('tabindex','-1'); |
248 | 228 | }, |
249 | 229 |
|
250 | | - detached: function() { |
251 | | - // The analog to `attached`, `detached` fires when the element has been |
252 | | - // removed from a document. |
253 | | - // |
254 | | - // Use this to clean up anything you did in `attached`. |
| 230 | + _onInputBlur: function (e) { |
| 231 | + this.setAttribute('tabindex','0'); |
| 232 | + e.stopPropagation(); |
| 233 | + }, |
| 234 | + |
| 235 | + _onInputKeyDown: function (e) { |
| 236 | + if (e.keyCode === 13) { |
| 237 | + this._showDialog(); |
| 238 | + e.stopPropagation(); |
| 239 | + } |
| 240 | + }, |
| 241 | + |
| 242 | + _onInputKeyUp: function (e) { |
| 243 | + if (e.keyCode === 46) { |
| 244 | + this._clearValue(); |
| 245 | + } |
| 246 | + e.stopPropagation(); |
| 247 | + }, |
| 248 | + |
| 249 | + _onTapClearValue: function () { |
| 250 | + this.value = null; |
| 251 | + }, |
| 252 | + |
| 253 | + // Element Lifecycle |
| 254 | + |
| 255 | + ready: function() { |
| 256 | + if (!this.locale) { |
| 257 | + this.locale = window.navigator.userLanguage || window.navigator.language; |
| 258 | + } |
255 | 259 | }, |
256 | 260 |
|
257 | 261 | // Element Behavior |
258 | 262 | keyBindings: { |
259 | 263 | 'del': '_delPressed' |
260 | 264 | }, |
261 | 265 |
|
262 | | - _delPressed: function(event) { |
263 | | - |
| 266 | + _delPressed: function() { |
264 | 267 | this._clearValue(); |
265 | 268 | }, |
266 | 269 |
|
267 | 270 | _clearValue: function() { |
268 | | - |
269 | | - this.value = ''; |
| 271 | + this.value = null; |
270 | 272 | }, |
271 | 273 |
|
272 | 274 | /** |
|
279 | 281 | return ''; |
280 | 282 | } |
281 | 283 |
|
282 | | - return this.dateFormat(value, 'LL'); |
| 284 | + return this.$picker.dateFormat(value, 'LL'); |
283 | 285 | }, |
284 | 286 |
|
285 | | - dismissDialog: function(event) { |
286 | | - |
287 | | - var $this = this; |
288 | | - |
289 | | - if (event.detail.confirmed) { |
290 | | - |
291 | | - this.value = this.$.picker.date; |
292 | | - } |
293 | | - |
| 287 | + _dismissDialog: function(event) { |
| 288 | + var $this = this; |
| 289 | + if ((!event.detail.canceled) && (event.detail.confirmed)) { |
| 290 | + this.value = this.$picker.date; |
| 291 | + } |
294 | 292 | this.focus(); |
295 | 293 | }, |
296 | 294 |
|
297 | | - showDialog: function() { |
298 | | - |
299 | | - this.focus(); |
300 | | - this.$.picker.date = this.value; |
| 295 | + _showDialog: function() { |
| 296 | + this.$picker.date = (this.value) ? this.value : new Date(); |
301 | 297 | this.$.dialog.toggle(); |
302 | 298 | }, |
303 | | - |
304 | | - onTapClearValue: function () { |
305 | | - |
306 | | - this.$.picker.date = null; |
307 | | - }, |
308 | 299 |
|
309 | 300 | }); |
310 | 301 |
|
|
0 commit comments