forked from noflo/noflo-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-scrubber.html
95 lines (83 loc) · 2.71 KB
/
number-scrubber.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
<polymer-element name="number-scrubber" attributes="value min max mod step distance" touch-action="none" on-trackstart="{{onTrackStart}}" on-track="{{onTrack}}" on-trackend="{{onTrackEnd}}">
<template>
<style>
:host {
-webkit-user-select: none; /* disable cut copy paste */
-webkit-touch-callout: none; /* disable callout, image save panel */
user-select: none;
touch-callout: none;
cursor: ew-resize;
display: inline-block;
text-align: center;
}
</style>
</template>
<script>
Polymer('number-scrubber', {
value: undefined,
startValue: 0,
min: -Infinity,
max: Infinity,
mod: 0,
step: 1,
distance: 5,
precision: 1000000,
scrubstart: null,
scrubend: null,
onTrackStart: function (event) {
// Don't select
document.body.style.webkitUserSelect = 'none';
document.body.style.MozUserSelect = 'none';
document.body.style.msUserSelect = 'none';
document.body.style.userSelect = 'none';
document.body.style.cursor = 'ew-resize';
this.fire("scrubstart");
if (this.value === undefined) { return; }
this.value = parseFloat(this.value);
this.startValue = this.value;
},
onTrack: function (event) {
if (this.value === undefined) { return; }
var lastValue = this.value;
var delta = event.dx;
if (this.distance > 1) {
delta = Math.round( delta / this.distance );
}
if (this.step !== 1) {
if (this.step > 1) {
delta = Math.round( delta / this.step ) * this.step;
} else if (this.step > 0) {
delta *= this.step;
}
}
var newValue = this.startValue + delta;
if (this.mod !== 0) {
newValue %= this.mod;
}
if (isFinite(this.min)) {
newValue = Math.max(newValue, this.min);
}
if (isFinite(this.max)) {
newValue = Math.min(newValue, this.max);
}
// Stupid JS numbers
if (this.precision > 1) {
newValue = Math.round( newValue * this.precision ) / this.precision;
}
if (this.value !== newValue) {
this.value = newValue;
this.fire("scrub", this.value);
}
},
onTrackEnd: function (event) {
// Reset select
document.body.style.webkitUserSelect = 'auto';
document.body.style.MozUserSelect = 'auto';
document.body.style.msUserSelect = 'auto';
document.body.style.userSelect = 'auto';
document.body.style.cursor = 'auto';
this.fire("scrubend");
}
});
</script>
</polymer-element>