This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 211
/
urth-timeline.html
235 lines (226 loc) · 10.3 KB
/
urth-timeline.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
<link rel="import" href="../polymer/polymer.html">
<dom-module id="urth-timeline">
<template>
<style>
#axis * {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.animating #window, .animating .handle-label, .animating .handle {
transition: all 1s ease;
}
.handle{
position:absolute;
display:inline-block;
background-color:#32698F;
height:30%;
bottom:0%;
width:1%;
z-index:10;
cursor:ew-resize;
}
#axis {
height:50px;
position:relative;
background-color:rgba(170,170,170,0.1);
border-right:solid 1px #aaa;
border-left:solid 1px #aaa;
margin-bottom: 30px;
}
#window{
display:inline-block;
position:absolute;
background-color:#3F84B5;
height:30%;
bottom:0%;
z-index:1;
opacity: 0.5;
cursor:move;
}
.handle-label {
position: absolute;
bottom: 100%;
left: -100%;
width: 200px;
font-size: 10px;
}
#axis-label-right {
right:0px;
}
.axis-label {
position: absolute;
top: 100%;
font-size: 10px;
}
</style>
<div id='axis'>
<span class='axis-label'>{{compute_label(startDate)}}</span>
<div id='left_handle' class='handle' on-track='track' style$='{{compute_left_handle_style(window_start)}}'>
<span class='handle-label'>{{compute_label(windowStartDate)}}</span>
</div>
<div id='window' class='window' on-track='track' style$='{{compute_window_style(window_start, window_end)}}'>
</div>
<div id='right_handle' on-track='track' class='handle' style$='{{compute_right_handle_style(window_end)}}'>
<span class='handle-label'>{{compute_label(windowEndDate)}}</span>
</div>
<span id='axis-label-right' class='axis-label'>{{compute_label(endDate)}}</span>
</div>
</template>
<script>
Polymer({
is: "urth-timeline",
compute_left_handle_style: function(window_start){
return 'left:' + window_start + '%;';
},
compute_right_handle_style: function(window_end){
return 'left:' + window_end + '%;';
},
compute_window_style: function(window_start, window_end){
var width = window_end - window_start;
return 'left:' + window_start + '%; width:' + width + '%;'
},
computeChange : function(mouse_delta){
var timeline_width = this.$.axis.getBoundingClientRect().width;
var timeline_change = (this.end - this.start) * (mouse_delta / timeline_width);
return Math.round(timeline_change * 100) / 100;
},
compute_label : function(date){
return date.toLocaleDateString();
},
computeWindowDates: function(){
var window = this.endDate.getTime() - this.startDate.getTime();
this.windowStartDate = new Date(this.startDate.getTime() + (window * (this.window_start/100)))
this.windowEndDate = new Date(this.startDate.getTime() + (window * (this.window_end/100)))
},
adjustWindow: function(){
this.$.axis.classList.add('animating')
if(this.window_end - this.window_start >= 75) {
var axis_length = (this.endDate.getTime() - this.startDate.getTime());
var additional_length = axis_length / 2;
var new_axis_length = axis_length * 2;
this.endDate = new Date(this.endDate.getTime() + additional_length);
this.startDate = new Date(this.startDate.getTime() - additional_length);
this.window_start = 100*((this.windowStartDate.getTime() - this.startDate.getTime()) / new_axis_length);
this.window_end = 100*((this.windowEndDate.getTime() - this.startDate.getTime()) / new_axis_length);
}
if(this.window_end - this.window_start <= 25) {
var axis_length = (this.windowEndDate.getTime() - this.windowStartDate.getTime());
var additional_length = axis_length;
var new_axis_length = axis_length*3;
this.endDate = new Date(this.windowEndDate.getTime() + additional_length);
this.startDate = new Date(this.windowStartDate.getTime() - additional_length);
this.window_start = 100*((this.windowStartDate.getTime() - this.startDate.getTime()) / new_axis_length);
this.window_end = 100*((this.windowEndDate.getTime() - this.startDate.getTime()) / new_axis_length);
}
if(this.window_start <= 5) {
var interval = this.endDate.getTime() - this.startDate.getTime();
var shift = interval * 0.25;
this.endDate = new Date(this.endDate.getTime() - shift);
this.startDate = new Date(this.startDate.getTime() - shift);
this.window_start += 25;
this.window_end += 25;
}
if(this.window_end >= 95) {
var interval = this.endDate.getTime() - this.startDate.getTime();
var shift = interval * 0.25;
this.endDate = new Date(this.endDate.getTime() + shift);
this.startDate = new Date(this.startDate.getTime() + shift);
this.window_start -= 25;
this.window_end -= 25;
}
setTimeout(function(){
this.$.axis.classList.remove('animating');
}.bind(this), 1500);
this.fire('window-changed', {window_start : this.windowStartDate, window_end :this.windowEndDate})
},
track: function(e) {
var change = this.computeChange(e.detail.ddx || 0);
if(e.target.track_condition(change)) {
e.target.track_effect(change)
}
this.computeWindowDates();
if(e.detail.state == 'end') {
this.adjustWindow();
}
this.computeWindowDates();
},
attached : function(){
if(!this.startDate || !this.endDate) {
this.endDate = new Date();
this.startDate = new Date(this.endDate.getTime() - 1000*60*60*24*30);
}
// Bind the effects of the track events for each element
this.$.left_handle.track_effect = function(change){
this.window_start = parseFloat(this.$.left_handle.style.left) + change;
}.bind(this);
this.$.right_handle.track_effect = function(change){
this.window_end = parseFloat(this.$.right_handle.style.left) + change;
}.bind(this);
this.$.window.track_effect = function(change){
this.window_start = parseFloat(this.$.left_handle.style.left) + change;
this.window_end = parseFloat(this.$.right_handle.style.left) + change;
}.bind(this);
// Bind conditions to elements to determine if the track change is valid
this.$.left_handle.track_condition = function(change) {
var new_start = parseFloat(this.$.left_handle.style.left) + change;
return new_start >= this.start && new_start < this.window_end
}.bind(this);
this.$.right_handle.track_condition = function(change) {
var new_end = parseFloat(this.$.right_handle.style.left) + change
return new_end <= this.end && new_end > this.window_start
}.bind(this);
this.$.window.track_condition = function(change) {
var new_start = parseFloat(this.$.left_handle.style.left) + change;
var new_end = parseFloat(this.$.right_handle.style.left) + change;
return new_start >= this.start && new_end <= this.end;
}.bind(this);
this.computeWindowDates();
this.fire('window-changed', {window_start : this.windowStartDate, window_end :this.windowEndDate})
},
properties: {
windowStartDate : {
type : Date,
notify: true
},
window_start : {
type : Number,
value : 25
},
window_end : {
type : Number,
value : 75
},
windowEndDate : {
type : Date,
notify: true
},
start : {
type : Number,
value : 0
},
startDate : {
type : Date,
notify: true,
value : function() {
return new Date(Date.now() - 1000*60*60*24*7);
}
},
end : {
type : Number,
value : 100
},
endDate : {
type : Date,
notify: true,
value : function(){
return new Date();
}
},
}
});
</script>
</dom-module>