slider.js
5.56 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
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
(function($, undefined) {
var kendo = window.kendo,
ui = kendo.ui,
Widget = ui.Widget,
support = kendo.support,
proxy = $.proxy,
pow = Math.pow,
SLIDE = "slide",
CHANGE = "change",
ACTIVE_STATE = "km-state-active",
MARGINLEFT = "margin-left",
TRANSFORMSTYLE = support.transitions.css + "transform";
function round(value, precision) {
var power = pow(10, precision || 0);
return Math.round(value * power) / power;
}
function limitValue(value, minLimit, maxLimit) {
return Math.max( minLimit, Math.min( maxLimit, value));
}
var ColorSlider = Widget.extend({
init: function(element, options) {
var that = this;
Widget.fn.init.call(that, element, options);
that._wrapper();
that._drag();
that._background();
that._handle();
that.value(that.point);
that.bind([ CHANGE, SLIDE ], that.options);
},
events: [
CHANGE,
SLIDE
],
options: {
name: "ColorSlider",
value: 0,
max: 100,
precision: 0, // number of floating point digits
animateBackground: true
},
value: function (value) {
var that = this;
if (isNaN(value)) {
return that.point;
}
that._dimensions();
that.point = round(value, that.options.precision);
that._position(value * that.snapPoint * that.precisionFactor);
return that;
},
_move: function(e) {
var that = this, value;
e.preventDefault();
value = that._position(limitValue((e.touch.x.client - that.targetOffsetX) - (that.handleWidth / 2), 0, that.constrain));
if (value != that.point)
that.trigger(SLIDE, { value: value });
that.point = value;
},
_position: function(position) {
var that = this,
precision = that.options.precision;
that.position = position;
that.handle.css(TRANSFORMSTYLE, "translatex(" + position + "px)");
if (that.options.animateBackground) {
that.background.css(MARGINLEFT, that.origin + position);
}
return round(that.position / (that.snapPoint * that.precisionFactor), precision);
},
_start: function(e) {
var that = this;
that.targetOffsetX = e.touch.target.offset().left;
that.drag.capture();
that.handle.addClass(ACTIVE_STATE);
},
_stop: function(e) {
var that = this;
that.point = that._position(limitValue((e.touch.x.client - e.touch.target.offset().left) - (that.handleWidth / 2), 0, that.constrain));
that.handle.removeClass(ACTIVE_STATE);
that.trigger(CHANGE, { value: that.point });
},
_background: function() {
var that = this,
background = that.wrapper.children(".km-slider-wrapper").children(".km-slider-background");
if (!background[0]) {
background = $("<span class='km-slider-wrapper'><span class='km-slider-background'></span></span>")
.appendTo(that.wrapper)
.children(".km-slider-background");
}
that.origin = parseInt(background.css(MARGINLEFT), 10);
background.data("origin", that.origin);
that.background = background;
},
_dimensions: function() {
var that = this;
that.point = that.options.value;
that.constrain = that.wrapper.width();
that.handleWidth = that.handle.outerWidth(true);
that.precisionFactor = pow(10, that.options.precision);
that.snapPoint = that.constrain / (that.options.max * that.precisionFactor);
},
_handle: function() {
var that = this,
handle = that.wrapper.children(".km-slider-container").children(".km-slider-handle");
if (!handle[0]) {
handle = $("<span class='km-slider-container'><span class='km-slider-handle' /></span>")
.appendTo(that.wrapper)
.children(".km-slider-handle");
}
that.handle = handle;
},
_wrapper: function() {
var that = this,
element = that.element,
wrapper = element.parent("span.km-slider");
if (!wrapper[0]) {
wrapper = element.wrap('<span class="km-slider"/>').parent();
element.hide();
}
that.wrapper = wrapper;
},
_drag: function() {
var that = this;
if (!that.drag) {
that.drag = new kendo.UserEvents(that.wrapper, {
global: true,
tap: function(e) {
var touch = e.touch;
that.point = that._position(limitValue((touch.x.client - touch.target.offset().left) - (that.handleWidth / 2), 0, that.constrain));
that.trigger(CHANGE, { value: that.point });
},
start: proxy(that._start, that),
move: proxy(that._move, that),
end: proxy(that._stop, that)
});
}
}
});
ui.plugin(ColorSlider);
})(jQuery);