-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable-react.js
More file actions
82 lines (76 loc) · 2.85 KB
/
variable-react.js
File metadata and controls
82 lines (76 loc) · 2.85 KB
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
/** @jsx React.DOM */
var average = function (myArray) {
var sum = 0;
for (var i = 0; i < myArray.length; i++) {
sum += myArray[i];
};
return sum / myArray.length;
};
var saturate = function (value,limits) {
var min = Math.min(limits[0],limits[1]);
var max = Math.max(limits[0],limits[1]);
if (value < min) {
return min;
} else if (value > max) {
return max;
};
return value;
};
var Variable = React.createClass({
/*
Required Properties:
value
limits (array with two element: [minimum value and maximum value]
callback (calls this function on every state change, with current value as argument / use this callback to set the value property of this component)
Optional Properties:
sensitivity (the greater the sensitivity, the quicker the mouse moves through the range of provided values)
format (a function to prepare numerical value for display - e.g. date/time formatting, currency)
transformation (function to transform the value itself, rather than just its display. Use for rounding, primarily)
Notes:
1. this component produces a <span> element with a class called "variable" (use this for styling)
*/
getInitialState: function () {
return {dragging: false};
},
componentDidMount: function () {
var t = this;
var currentValue = this.props.value;
$(document).mousemove(function (e) {
if (t.state.dragging) {
var range = t.props.limits[1] - t.props.limits[0],
dragRange = 100 / (t.props.sensitivity || 1),
mouseY = e.clientY || e.pageY;
currentValue = saturate(t.state.startVal + (range)*(-(mouseY - t.state.mouseY0)/dragRange), t.props.limits);
};
});
var f = t.props.callback;
var transform = this.props.transformation || function (x) {return x};
setInterval(function () {
if (t.state.dragging) {
f(transform(currentValue));
}
}, 10);
},
handleMouseDown: function (e) {
var component = this;
this.setState({dragging: true,
startVal: this.props.value,
mouseY0: (e.clientY || e.pageY)});
$("body").css({cursor: "ns-resize"});
$(document).one("mouseup", function () {
component.setState({dragging: false});
$("body").css({cursor: ""});
});
},
handleMouseOver: function (e) {
$(e.target).css({cursor:"ns-resize"});
},
render: function () {
var format = this.props.format || function (x) {return x};
return (
<span className="variable" onMouseDown={this.handleMouseDown} onMouseOver={this.handleMouseOver}>
{format(this.props.value)}
</span>
);
}
});