-
Notifications
You must be signed in to change notification settings - Fork 15
/
Slider.js
136 lines (114 loc) · 4.35 KB
/
Slider.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Owner: [email protected]
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
define(function(require, exports, module) {
var Surface = require('famous/core/Surface');
var CanvasSurface = require('famous/surfaces/CanvasSurface');
var Transform = require('famous/core/Transform');
var EventHandler = require('famous/core/EventHandler');
var Utilities = require('famous/math/Utilities');
var OptionsManager = require('famous/core/OptionsManager');
var MouseSync = require('famous/inputs/MouseSync');
var TouchSync = require('famous/inputs/TouchSync');
var GenericSync = require('famous/inputs/GenericSync');
GenericSync.register({
mouse : MouseSync,
touch : TouchSync
});
/** @constructor */
function Slider(options) {
this.options = Object.create(Slider.DEFAULT_OPTIONS);
this.optionsManager = new OptionsManager(this.options);
if (options) this.setOptions(options);
this.indicator = new CanvasSurface({
size: this.options.indicatorSize,
classes : ['slider-back']
});
this.label = new Surface({
size: this.options.labelSize,
content: this.options.label,
properties : {pointerEvents : 'none'},
classes: ['slider-label']
});
this.eventOutput = new EventHandler();
this.eventInput = new EventHandler();
EventHandler.setInputHandler(this, this.eventInput);
EventHandler.setOutputHandler(this, this.eventOutput);
var scale = (this.options.range[1] - this.options.range[0]) / this.options.indicatorSize[0];
this.sync = new GenericSync(
['mouse', 'touch'],
{
scale : scale,
direction : GenericSync.DIRECTION_X
}
);
this.indicator.pipe(this.sync);
this.sync.pipe(this);
this.eventInput.on('update', function(data) {
this.set(data.position);
}.bind(this));
this._drawPos = 0;
_updateLabel.call(this);
}
Slider.DEFAULT_OPTIONS = {
size: [200, 60],
indicatorSize: [200, 30],
labelSize: [200, 30],
range: [0, 1],
precision: 2,
value: 0,
label: '',
fillColor: 'rgba(170, 170, 170, 1)'
};
function _updateLabel() {
this.label.setContent(this.options.label + '<span style="float: right">' + this.get().toFixed(this.options.precision) + '</span>');
}
Slider.prototype.setOptions = function setOptions(options) {
return this.optionsManager.setOptions(options);
};
Slider.prototype.get = function get() {
return this.options.value;
};
Slider.prototype.set = function set(value) {
if (value === this.options.value) return;
this.options.value = Utilities.clamp(value, this.options.range);
_updateLabel.call(this);
this.eventOutput.emit('change', {value: value});
};
Slider.prototype.getSize = function getSize() {
return this.options.size;
};
Slider.prototype.render = function render() {
var range = this.options.range;
var fillSize = Math.floor(((this.get() - range[0]) / (range[1] - range[0])) * this.options.indicatorSize[0]);
if (fillSize < this._drawPos) {
this.indicator.getContext('2d').clearRect(fillSize, 0, this._drawPos - fillSize + 1, this.options.indicatorSize[1]);
}
else if (fillSize > this._drawPos) {
var ctx = this.indicator.getContext('2d');
ctx.fillStyle = this.options.fillColor;
ctx.fillRect(this._drawPos-1, 0, fillSize - this._drawPos+1, this.options.indicatorSize[1]);
}
this._drawPos = fillSize;
return {
size: this.options.size,
target: [
{
origin: [0, 0],
target: this.indicator.render()
},
{
transform: Transform.translate(0, 0, 1),
origin: [0, 0],
target: this.label.render()
}
]
};
};
module.exports = Slider;
});