-
Notifications
You must be signed in to change notification settings - Fork 0
/
zdkelt-time-picker.js
138 lines (131 loc) · 2.79 KB
/
zdkelt-time-picker.js
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
Polymer({
is : 'zdkelt-time-picker',
properties: {
/**
* The value formated as 'HH:MM'
*
* The time is given in 24-hours time
*/
value : {
type : String,
value : '00:00',
reflectToAttribute: true,
notify : true,
observer : '_valueChanged'
},
/**
* Define the view
*
* available value are `hours` (default) or `minutes`
*/
view : {
type : String,
value : 'hours',
observer: '_setView'
},
/**
* not yet implemented
* Boolean flag indicates to draw our clock with 12 hours
*/
hour12 : {
type : Boolean,
value : false,
notify: true
},
/**
* The hours value
*/
hours : {
type : String,
value: '00'
},
/**
* The minutes value
*/
minutes: {
type : String,
value: '00'
}
},
attached: function () {
this.refresh();
},
/**
* Force the redraw of the clock
*/
refresh: function () {
this.view = 'minutes';
setTimeout((function () {
this.view = 'hours';
}).bind(this), 0);
},
/**
* set the view, depending of the `view`property
*/
_setView: function () {
this.$.hours.classList.remove('selected');
this.$.minutes.classList.remove('selected');
switch (this.view) {
case 'hours':
this.$.hours.classList.toggle('selected');
this.$.clock.minutes = false;
this.$.clock.value = parseInt(this.hours, 10);
break;
case 'minutes':
this.$.minutes.classList.toggle('selected');
this.$.clock.minutes = true;
this.$.clock.value = parseInt(this.minutes, 10);
break;
}
},
/**
* toggle the view
*/
_toggleView: function (evt) {
var select = this.$.header.querySelector('.selected');
if (evt && evt.target === select) {
return;
}
this.view = select.id === 'hours' ? 'minutes' : 'hours';
},
_valueChanged: function (newValue) {
if (!newValue) {
this.value = this.hours + ':' + this.minutes;
return;
}
var tmp = newValue.match(/(\d+):(\d+)/);
if (tmp) {
this.set('hours', tmp[1]);
this.set('minutes', tmp[2]);
if (this.$.clock.minutes) {
this.$.clock.value = parseInt(tmp[2], 10);
} else {
this.$.clock.value = parseInt(tmp[1], 10);
}
}
},
/**
* intercept the `change`event of the zdkelt-clock component.
*
* if the the view was the `hours` view, it's then automatically changed
* to the `minutes` view.
*/
_clockChange: function (evt) {
evt.stopPropagation();
if (this.view === 'hours') {
this._toggleView();
}
},
/**
* The `update` event is emitted when the mouse is dragged
*
* @event update
*/
_clockUpdate: function (evt) {
evt.stopPropagation();
var tmp = parseInt(evt.detail, 10);
this.set(this.view, (tmp < 10 ? '0' : '') + tmp);
this.value = this.hours + ':' + this.minutes;
this.fire('update', this.value);
}
});