forked from bendavis78/paper-date-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper-date-picker.html
343 lines (323 loc) · 10.4 KB
/
paper-date-picker.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-media-query/iron-media-query.html">
<link rel="import" href="../neon-animation/neon-animated-pages.html">
<link rel="import" href="../neon-animation/neon-animatable.html">
<link rel="import" href="../neon-animation/animations/fade-in-animation.html">
<link rel="import" href="../neon-animation/animations/fade-out-animation.html">
<link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html">
<link rel="import" href="../iron-selector/iron-selector.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../paper-styles/color.html">
<link rel="import" href="../paper-styles/default-theme.html">
<link rel="import" href="../paper-styles/shadow.html">
<link rel="import" href="../paper-styles/typography.html">
<link rel="import" href="../moment-element/moment-with-locales-import.html">
<link rel="import" href="paper-calendar.html">
<link rel="import" href="paper-date-picker-dialog-style.html">
<link rel="import" href="paper-year-list.html">
<!--
Material Design [Pickers](http://www.google.com/design/spec/components/pickers.html)
Provides a responsive date picker based on the material design spec.
## Examples:
Default picker:
<paper-date-picker></paper-date-picker>
Setting the initial date to April 20, 2015:
<paper-date-picker date="April 20, 2015"></paper-date-picker>
You may also specify a minimum and/or maximum date allowed in this picker using
the same date notation:
<paper-date-picker min-date="April 1, 2015" max-date="June 30, 2015"></paper-date-picker>
If you include this element as part of `paper-dialog`, use the class
`"paper-date-picker-dialog"` on the dialog element in order to give it proper
styling:
<paper-dialog id="dialog" class="paper-date-picker-dialog" modal
on-iron-overlay-closed="dismissDialog">
<paper-date-picker id="picker" date="[[date]]"></paper-date-picker>
<div class="buttons">
<paper-button dialog-dismiss>Cancel</paper-button>
<paper-button dialog-confirm>OK</paper-button>
</div>
</paper-dialog>
@element paper-date-picker
@blurb Provides a responsive date picker based on the material design spec.
@homepage http://bendavis78.github.io/paper-date-picker/
@demo demo/index.html
-->
<style is="custom-style" include="paper-date-picker-dialog-style">
/* includes dialog style at document-level for backward compatibility */
</style>
<dom-module id="paper-date-picker">
<template>
<style>
:host {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
display: inline-block;
color: var(--primary-text-color);
@apply --paper-font-body1;
@apply --paper-date-picker;
}
/** Landscape ******************/
#datePicker {
width: var(--date-picker-width, 512px);
height: var(--date-picker-height, 248px);
@apply --layout-horizontal;
}
#heading {
width: 168px;
@apply --paper-date-picker-heading;
}
/** Narrow *********************/
:host([narrow]) #datePicker {
width: var(--narrow-date-picker-width, 328px);
height: var(--narrow-date-picker-height, 428px);
@apply --layout-vertical;
}
:host([narrow]) #heading {
width: auto;
height: 96px;
padding: 16px 24px;
}
/** Heading ********************/
#heading {
padding: 16px;
box-sizing: border-box;
color: var(--text-primary-color);
background: var(--default-primary-color);
@apply --layout-vertical;
@apply --layout-around-justfied;
}
#heading .date,
#heading .year {
cursor: pointer;
}
#heading .date {
@apply --paper-font-display1;
font-weight: bold;
margin-top: 2px;
@apply --paper-date-picker-heading-date;
}
#heading div.date {
letter-spacing: 0.025em;
}
#heading .date span {
white-space: nowrap;
}
#heading .year {
@apply --paper-font-body2;
font-size: 16px;
@apply --paper-date-picker-heading-date;
}
#heading:not(.pg-chooseYear) .year,
#heading.pg-chooseYear .date {
color: var(--light-primary-color);
}
/** Calendar/Year picker ********/
:host([isTouch]) paper-year-list::-webkit-scrollbar {
width: 0 !important;
}
#pages {
@apply --layout-flex;
width: auto;
background: var(--default-background-color);
}
#calendar {
--paper-calendar: {
@apply --paper-date-picker-calendar;
}
}
</style>
<iron-media-query query="{{_getMediaQuery(forceNarrow, responsiveWidth)}}" query-matches="{{_queryMatches}}"></iron-media-query>
<div id="datePicker">
<template is="dom-if" if="{{!noHeading}}">
<div id="heading" class$="{{_getHeadingClass('heading-content', _selectedPage)}}">
<div class="year" on-tap="_tapHeadingYear">{{dateFormat(date, 'YYYY', locale)}}</div>
<div class="date" on-tap="_tapHeadingDate">
<template is="dom-repeat" items="{{_splitHeadingDate(date, headingFormat, locale)}}">
<span>{{item}}</span>
</template>
</div>
</div>
</template>
<neon-animated-pages id="pages" selected="{{_selectedPage}}" attr-for-selected="id"
entry-animation="fade-in-animation" exit-animation="fade-out-animation"
on-iron-select="_pageSelected">
<neon-animatable id="chooseDate">
<paper-calendar id="calendar" locale="{{locale}}" date="{{date}}"
min-date="{{minDate}}" max-date="{{maxDate}}">
</paper-calendar>
</neon-animatable>
<neon-animatable id="chooseYear">
<paper-year-list id="yearList" date="{{date}}" on-tap="_tapHeadingDate" min="[[_minYear]]" max="[[_maxYear]]"></paper-year-list>
</neon-animatable>
</neon-animated-pages>
</div>
</template>
<script>
Polymer({
is: 'paper-date-picker',
properties: {
/**
* The selected date (YYYY-MM-DD)
*/
date: {
type: Date,
notify: true
},
/**
* Maximum screen width at which the picker uses a vertical layout
*/
responsiveWidth: {
type: String,
value: '560px'
},
/**
* The current locale
*/
locale: {
type: String,
value: 'en'
},
/**
* Show/hide the heading
*/
noHeading: {
type: Boolean,
value: false
},
/**
* The format of the date displayed in the heading.
* See docuemntation for Moment.js for more info.
*/
headingFormat: {
type: String,
value: 'ddd, MMM D'
},
/**
* The minimum allowed date
*/
minDate: {
type: Date,
value: null
},
/**
* The maximum allowed date
*/
maxDate: {
type: Date,
value: null
},
/**
* Force narrow layout
*/
forceNarrow: {
type: Boolean,
value: false
},
narrow: {
type: Boolean,
reflectToAttribute: true,
notify: true,
computed: '_computeNarrow(forceNarrow, _queryMatches)'
},
isTouch: {
type: Boolean,
value: false,
readOnly: true,
reflectToAttribute: true
},
_queryMatches: {
type: Boolean,
value: false
},
headingBreak: {
type: String,
value: '[,]'
},
_selectedPage: String,
_maxYear: {
type: Number,
computed: '_getFullYear(maxDate)'
},
_minYear: {
type: Number,
computed: '_getFullYear(minDate)'
}
},
behaviors: [
Polymer.IronResizableBehavior
],
listeners: {
'iron-resize': '_resizeHandler'
},
ready: function() {
this.today = this.$.calendar.today;
this.isTouch = 'ontouchstart' in window;
this._selectPage('chooseDate');
},
dateFormat: function() {
return this.$.calendar.dateFormat.apply(this.$.calendar, arguments);
},
_tapHeadingDate: function() {
if (this.$.pages.selected !== 'chooseDate') {
this._selectPage('chooseDate');
} else {
// tapping the date header while already viewing months brings you back
// to the selected month
this.$.calendar.currentMonth = this.date.getMonth() + 1;
this.$.calendar.currentYear = this.date.getFullYear();
}
},
_tapHeadingYear: function() {
if (this.$.pages.selected !== 'chooseYear') {
this._selectPage('chooseYear');
this.$.yearList.centerSelected();
}
},
_selectPage: function(page) {
this.$.pages.selected = page;
},
_getMediaQuery: function(forceNarrow, responsiveWidth) {
return '(max-width: ' + (forceNarrow ? '' : responsiveWidth) + ')';
},
_getHeadingClass: function(pfx, selectedPage) {
return pfx + ' pg-' + selectedPage;
},
_getFullYear: function(date) {
return date ? date.getFullYear() : null;
},
_splitHeadingDate: function(date, format, locale) {
var re = new RegExp(this.headingBreak, 'g');
var text = this.dateFormat(date, format, locale);
var seps = text.match(re);
var value;
if (!seps) {
value = [text];
} else {
value = text.split(re).map(function(s, i) {
return s + (seps[i] || '');
});
}
return value;
},
_computeNarrow: function(queryMatches, forceNarrow) {
return queryMatches || forceNarrow;
},
_pageSelected: function() {
this._resizeHandler();
},
_resizeHandler: function() {
if (this._resizing) {
return;
}
this._resizing = true;
this.$.calendar.notifyResize();
this._resizing = false;
this.updateStyles();
}
});
</script>
</dom-module>