-
Notifications
You must be signed in to change notification settings - Fork 13
/
paper-datetime-picker-item.html
165 lines (137 loc) · 5.03 KB
/
paper-datetime-picker-item.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
<!doctype html>
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout-classes.html">
<link rel="import" href="../paper-item/paper-icon-item.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="../paper-item/paper-item-body.html">
<link rel="import" href="../paper-ripple/paper-ripple.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="paper-date-picker-edit-dialog.html">
<link rel="import" href="paper-time-picker-edit-dialog.html">
<link rel="import" href="paper-date-picker-behaviors.html">
<!--
`paper-datetime-picker-item` provides a Material Design item that shows a date and a time and opens a date/time picker dialog on tap.
### Example
```html
<paper-datetime-picker-item icon="icons:today" date="{{date}}" date-format="ll" time-format="LT"></paper-datetime-picker-item>
```
@demo demo/index.html
-->
<dom-module id="paper-datetime-picker-item">
<template>
<style include="iron-flex">
:host {
@apply --layout-horizontal;
cursor: pointer;
}
:host([disabled]) .text{
color: var(--disabled-text-color);
}
.icon {
color: var(--disabled-text-color);
}
.item[has-no-date] .text {
color: var(--secondary-text-color);
}
</style>
<paper-icon-item on-tap="_editDate" class="flex item" has-no-date$="[[_hasNoDate]]">
<paper-ripple class="fit"></paper-ripple>
<iron-icon icon="[[icon]]" item-icon slot="item-icon" class="icon"></iron-icon>
<paper-item-body class="text">[[_formattedDate]]</paper-item-body>
</paper-icon-item>
<paper-item class="flex text" on-tap="_editTime" hidden="[[_hasNoDate]]">
<paper-ripple class="fit"></paper-ripple>
[[_formattedTime]]
</paper-item>
</template>
</dom-module>
<script>
(function() {
Polymer({
is: 'paper-datetime-picker-item',
properties: {
/**
* Icon shown left of the date
*/
icon: String,
_formattedDate: String,
_formattedTime: {
type: String,
value: '',
computed: '_getFormattedTime(date, locale)'
},
_hasNoDate: {
type: Boolean,
value: true,
computed: '_computeHasNoDate(date)'
}
},
behaviors: [
Polymer.PaperDatePickerItemBehavior
],
// Trigger calculation of _formattedDate also if date is undefined
observers: [
'_onChangedPlaceholder(placeholder, locale)',
'_onChangedDate(date, locale)'
],
// Private methods
_editDate: function() {
var dialog = document.createElement('paper-date-picker-edit-dialog');
// Wait until dialog is created before the properties can be set
// @see http://stackoverflow.com/a/31482376
this.async(function() {
// Initialize dialog with the current date (default to today to avoid exception)
dialog.date = this.date? moment(this.date).toDate() : new Date();
// Localize text
dialog.cancelButton = this.cancelButton;
dialog.okButton = this.okButton;
dialog.locale = this.locale;
dialog.minDate = this.minDate;
dialog.maxDate = this.maxDate;
// Capture date if the user saved the dialog
dialog.addEventListener('paper-date-picker-edit-dialog-save', function(e) {
var newDate = e.detail;
// If there is already a time picked, don't change it
// TODO: this piece is only necessary because of this issue with the date picker: https://github.com/bendavis78/paper-date-picker/issues/50
// Once issue is solved this method can be replaced by PaperDatePickerItemBehaviorImpl._edit
if (typeof this.date !== 'undefined' && this.date !== null) {
var hours = moment(this.date).format('HH');
var minutes = moment(this.date).format('mm');
newDate.setHours(hours, minutes);
} else if (this.defaultTime) {
// there is no old date, but we want to set the newly chosen one to the default time
var oldDate = new moment(this.defaultTime, 'HH:mm');
var hours = moment(oldDate).format('HH');
var minutes = moment(oldDate).format('mm');
newDate.setHours(hours, minutes);
}
this.date = newDate;
// Inform listener of 'change' events when the date was changed
this.fire('change', { value: this.date });
}.bind(this));
dialog.open();
}.bind(this));
// Return dialog for unit testing
return dialog;
},
_onChangedPlaceholder: function(placeholder, locale) {
this._formattedDate = this._getFormattedDate(this.date, placeholder, locale);
},
_onChangedDate: function(date, locale) {
this._formattedDate = this._getFormattedDate(date, this.placeholder, locale);
},
// Time related private methods
_editTime: function() {
return this._edit('paper-time-picker-edit-dialog');
},
_computeHasNoDate: function(date) {
return typeof date === 'undefined' || date === null;
}
});
})();
</script>