-
Notifications
You must be signed in to change notification settings - Fork 65
/
scrollbar.js
100 lines (77 loc) · 2.83 KB
/
scrollbar.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
function Scrollbar(params) {
this.parentEl = params.parentEl;
this.x = params.x;
this.y = params.y;
this.width = params.width || 20;
this.height = params.height || 200;
this.value = params.initValue || 0;
this.rowsHeight = params.rowsHeight || 200;
this.viewportHeight = params.viewportHeight;
if(this.rowsHeight < this.viewportHeight)
this.thumbHeight = this.height; // Nothing to scroll
else
this.thumbHeight = this.height - this.height*(this.rowsHeight - this.viewportHeight) / this.rowsHeight; // Test
// Ideally we should have some listeners
this.listeners = [];
this.min = params.min;
this.max = params.max;
// In case we want some ticks on the scrollbar
this.ordinal_axis = d3.scale.ordinal().domain(d3.range(this.min, this.max+1)).rangePoints([0, this.height]);
this.linear_axis = d3.scale.linear().domain([this.min, this.max]).range([0, this.height]);
this.scale = d3.scale.linear().domain([0, this.viewportHeight-this.rowsHeight]).range([0, this.height*(this.rowsHeight - this.viewportHeight) / this.rowsHeight]);
var that = this;
var dragScrollbar = d3.behavior.drag()
.origin(Object)
.on("dragstart", dragstartScrollbar)
.on("drag", dragScrollbar)
.on("dragend", dragendScrollbar);
var backgroundScrollbar = this.parentEl.append("rect").attr({
width: 20,
height: params.height,
fill: 'lightgray',
class: "scrollbar-background",
x: params.x,
y: params.y
});
this.parentEl.selectAll(".scrollbar-thumb").remove()
this.gThumb = this.parentEl.selectAll(".scrollbar-thumb")
.data([{value: this.value, dx:0, x:0, y:0}])
.enter()
.append("g")
.attr("class", "scrollbar-thumb")
.on("mouseover", function(d){
d3.select(this).style("cursor", "pointer")
})
.call(dragScrollbar);
this.gThumb.append("rect")
.attr("width", 20)
.attr("height", this.thumbHeight)
.attr("x", params.x)
.attr("y", params.y)
.attr("rx", 20)
.attr("ry", 10)
.attr("fill", "gray")
function dragstartScrollbar(d) {
d.dx = 0;
}
function dragendScrollbar(d) {
}
function dragScrollbar(d) {
d.y += d3.event.dy;
d.y = Math.max(0, Math.min(d.y, that.height-that.thumbHeight))
that.gThumb.attr("transform", "translate("+[0, d.y]+")");
d3.select(".gRows").attr('transform', 'translate(0, ' + that.scale.invert(d.y) + ')');
// Subset background should stick to his place
d3.select(".background-subsets").attr('transform', function (d, i) {
return 'translate(' + [ 0, d.y ] + ')'
});
}
}
Scrollbar.prototype.setValue = function(value) {
this.value = value;
var new_y = this.scale(value);
this.gThumb.attr("transform", function(d) {
d.y = Math.min(0, new_y);
return "translate("+[0, -d.y]+")";
})
}