-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathd-calendar.html
447 lines (371 loc) · 12.7 KB
/
d-calendar.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../font-roboto/roboto.html"/>
<link rel="import" href="../core-meta/core-meta.html"/>
<link rel="import" href="../core-style/core-style.html"/>
<link rel="import" href="../core-media-query/core-media-query.html"/>
<link rel="import" href="../core-animated-pages/core-animated-pages.html"/>
<link rel="import" href="../core-animated-pages/transitions/slide-up.html"/>
<link rel="import" href="../paper-icon-button/paper-icon-button.html"/>
<link rel="import" href="d-calendar-theme.html"/>
<link rel="import" href="d-calendar-weeks.html"/>
<link rel="import" href="d-calendar-years.html"/>
<link rel="import" href="d-date-utils.html"/>
<!--
Element providing an interactive calendar and datepicker.
Example
<d-calendar selectedDate="4/4/16"></d-calendar>
@element d-calendar
@blurb Element providing an interactive calendar and datepicker.
@status alpha
@homepage https://github.com/subpopular/d-calendar
-->
<!--
Fired when an item element is tapped.
@event core-activate
@param {Object} detail
@param {Object} detail.item the item element
-->
<polymer-element name="d-calendar" attributes="selectedDate viewingDate date language header" touch-action="auto">
<template id="template">
<core-style ref="d-calendar-theme"></core-style>
<core-media-query query="max-height: 360px" queryMatches="{{landscape}}"></core-media-query>
<d-date-utils id="dateUtils"></d-date-utils>
<div id="wrapper" layout horizontal?="{{landscape}}" vertical?="{{!landscape}}">
<div id="calendar-selection" hidden?="{{ !header }}">
<div id="selectedWeekdayHeader" layout horizontal>
<div relative flex id="selectedWeekday">
<div class="selectedWeekday slideup animate">{{selectedWeekday}}</div>
</div>
</div>
<div id="selectedDateHeader" layout vertical center center-justified>
<div id="selectedMonth">
<div class="selectedMonth slideup animate" style="text-transform: uppercase">{{selectedMonth}}</div>
</div>
<div id="selectedDay">
<div class="selectedDay slideup animate" view="dateView" on-tap="{{viewSelectAction}}">{{selectedDay}}</div>
</div>
<div id="selectedYear">
<div class="selectedYear slideup animate" view="yearView" on-tap="{{viewSelectAction}}">{{selectedYear}}</div>
</div>
</div>
</div>
<div id="calendar" layout vertical on-trackx="{{trackx}}" on-trackstart="{{trackstart}}" on-trackend="{{trackend}}">
<core-animated-pages id="views" selected="{{selectedView}}" transitions="slide-up hero-transition cross-fade-delayed cross-fade">
<section name="dateView">
<div cross-fade>
<div id="header" layout horizontal>
<paper-icon-button icon="chevron-left" on-tap="{{prevAction}}" noink></paper-icon-button>
<div flex relative id="viewingMonthYear">
<h4 class="viewingMonthYear slideleft animate">{{viewingMonth}} <span view="yearView" on-tap="{{viewSelectAction}}" hero hero-id="poo">{{viewingYear}}</span></h4>
</div>
<paper-icon-button icon="chevron-right" on-tap="{{nextAction}}" noink></paper-icon-button>
</div>
<div class="weekday" layout horizontal center>
<template repeat="{{weekday in weekdays}}">
<div class="weekday-name">{{weekday}}</div>
</template>
</div>
<d-calendar-weeks
date="{{viewingDate}}"
selectedDate="{{selectedDate}}"
on-d-calendar-date-selected="{{dateSelectAction}}">
</d-calendar-weeks>
</div>
</section>
<section name="yearView">
<div cross-fade>
<d-calendar-years
on-d-calendar-year-selected="{{yearSelectAction}}"
viewingYear="{{viewingYear}}">
</d-calendar-years>
</div>
</section>
</core-animated-pages>
</div>
</div>
</template>
<script>
(function(){
var prototype = {
/**
* The `selectedDate` attribute sets an initial selectedDate
*
* @attribute selectedDate
* @type string
* @default {current date}
*/
selectedDate: null,
/**
* The `selectedView` attribute gets/sets the calendar view
* to one of `dateView` or `yearView`.
*
* @attribute selectedView
* @type string
* @default 'dateView'
*/
selectedView: 'dateView',
/**
* The `language` attribute sets the language for localized day and month names
*
* @attribute language
* @type string
* @default 'en'
*/
language: 'en',
locale: {
'en': {
monthNames: [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
],
shortMonthNames: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
],
dayNames: [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
],
shortDayNames: [
'S',
'M',
'T',
'W',
'T',
'F',
'S'
],
},
'es': {
monthNames: [
'Enero',
'Febrero',
'Marzo',
'Abril',
'Mayo',
'Junio',
'Julio',
'Agosto',
'Septiembre',
'Octubre',
'Noviembre',
'Diciembre'
],
shortMonthNames: [
'Ene',
'Feb',
'Mar',
'Abr',
'May',
'Jun',
'Jul',
'Ago',
'Sep',
'Oct',
'Nov',
'Dic'
],
dayNames: [
'Domingo',
'Lunes',
'Martes',
'Miércoles',
'Jueves',
'Viernes',
'Sábado'
],
shortDayNames: [
'D',
'L',
'M',
'X',
'J',
'V',
'S'
],
},
},
getFullMonth: function(d) {
return this.locale[this.language].monthNames[d.getMonth()];
},
getShortMonth: function(d) {
return this.locale[this.language].shortMonthNames[d.getMonth()];
},
getDayOfWeek: function(d) {
return this.locale[this.language].dayNames[d.getDay()];
},
getShortDayNames: function() {
return this.locale[this.language].shortDayNames;
},
ready: function() {
this.dateUtils = this.$.dateUtils;
if (!this.selectedDate)
this.selectedDate = new Date();
else
this.selectedDate = new Date(this.selectedDate);
this.weekdays = this.getShortDayNames();
this.viewingDate = this.viewingDate || new Date(this.selectedDate);
this.viewingDate.setDate(1);
this.nowDate = new Date();
this.addEventListener('keyup', this.keyup.bind(this));
},
selectedDateChanged: function(olddate, newdate) {
if (olddate && !olddate.getTime) olddate = new Date(olddate);
if (newdate && !newdate.getTime) {
newdate = new Date(newdate);
if (!newdate.getTime) return;
this.selectedDate = newdate;
}
this.reverseAnimation = olddate && olddate.getTime() > newdate.getTime();
this.async(this.updateSelectedDateProps);
this.date = this.selectedDate;
this.focus();
this.fire('d-calendar-date-selected', {date: this.selectedDate})
},
viewingDateChanged: function(olddate, newdate) {
this.reverseAnimation = olddate && olddate.getTime() > newdate.getTime();
this.async(this.updateViewingDateProps)
},
dateChanged: function(olddate, newdate) {
},
/**
* The `updateSelectedDateProps` is none of your business.
*
* @method updateSelectedDateProps
* @param {String} greeting Pass in a specific greeting.
* @return {String} Returns a string greeting.
*/
updateSelectedDateProps: function() {
this.selectedWeekday = this.getDayOfWeek(this.selectedDate);
this.selectedMonth = this.getShortMonth(this.selectedDate);
this.selectedDay = this.selectedDate.getDate();
this.selectedYear = this.selectedDate.getFullYear();
},
updateViewingDateProps: function() {
this.viewingMonth = this.getFullMonth(this.viewingDate);
this.viewingYear = this.viewingDate.getFullYear();
this.viewingMonthYear = this.viewingMonth + ' ' + this.viewingYear;
},
nextAction: function() {
this.viewingDate = this.dateUtils.addMonths(this.viewingDate, 1);
},
prevAction: function() {
this.viewingDate = this.dateUtils.addMonths(this.viewingDate, -1);
},
viewSelectAction: function(ev, detail, sender) {
this.selectedView = (sender.getAttribute('view'));
},
dateSelectAction: function(ev, detail, sender) {
this.selectedDate = new Date(ev.detail.date);
},
yearSelectAction: function(ev, detail, sender) {
var year = ev.detail.year;
this.disablePropertyAnimations = true;
this.viewingDate = new Date(year, this.viewingDate.getMonth(), 1);
this.async(function(){
this.$.views.selected = 'dateView';
this.disablePropertyAnimations = false;
}, null, 280);
},
renderNode: function(element) {
if (this.disablePropertyAnimations) return;
var self = this;
var delayRemovalTime = 400;
var el = element;
var elClone = el.cloneNode(true);
el._originalTransition = element.style.transition;
el.style.transition = 'none';
el.parentElement.appendChild(elClone);
if (!this.reverseAnimation) {
el.classList.remove('in') // back to start position immediately
} else {
el.classList.add('out') // go to end position immediately
}
this.async(function(){
el.style.transition = el._originalTransition;
if (!this.reverseAnimation) {
elClone.classList.add('out'); // animate to end position
} else {
elClone.classList.remove('in'); // animate to start position
el.classList.remove('out'); // animate to start position
}
el.classList.add('in');
});
this.async(function(){
try {
el.parentNode.removeChild(elClone);
} catch (err) {
}
}, null, delayRemovalTime);
},
keyup: function(e){
var month = this.selectedDate.getMonth();
switch (e.keyCode) {
case 37:
this.prevAction()
break;
case 39:
this.nextAction()
break;
}
},
isXSwipe: function(start, end) {
var totalTouchTime = end.timeStamp - start.timeStamp;
var dx = end.dx;
return totalTouchTime < 500 && Math.abs(dx) > 40
},
trackx: function(e) {
},
trackstart: function(e) {
this.trackStartEvent = e;
},
trackend: function(e) {
if (this.isXSwipe(this.trackStartEvent, e)) {
e.dx < 0 ? this.nextAction() : this.prevAction();
}
}
};
// Set up the values we want to watch and assign the change callback,
// so that we can animate the nodes bound to them.
var fields = [
'selectedWeekday',
'selectedDay',
'selectedMonth',
'selectedYear',
'viewingMonthYear'
]
fields.forEach(function(field){
prototype[field + 'Changed'] = function(oldval, newval) {
var el = this.shadowRoot.querySelector('.' + field);
if (el) this.renderNode(el);
}
})
Polymer('d-calendar', prototype);
})()
</script>
</polymer-element>