forked from tommoor/tinyrange-jquery-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.range.js
273 lines (214 loc) · 7.02 KB
/
jquery.range.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* jQuery.range - A tiny, easily styleable range selector
* Tom Moor, http://tommoor.com
* Copyright (c) 2011 Tom Moor
* MIT Licensed
* @version 1.0
*/
(function($){
var TinyRange = function(){
// locals
var options;
var $input;
var $rail;
var $handle;
var $handle2;
var $selection;
var $dragging;
var $original;
var jump;
var size;
var defaults = {
orientation: 'horizontal', // todo
range: true,
values: false,
snap: false,
change: function(){},
blur: function(){}
};
var jumpHandle = function(ev) {
ev.pageX = ev.pageX - $input.offset().left;
// get closest handle
var x1 = $handle.position().left;
var dist = ev.pageX - x1;
if($handle2){
var x2 = $handle2.position().left;
var dist2 = ev.pageX - x2;
}
// move towards click
if(!$handle2 || Math.abs(dist) < Math.abs(dist2) ){
if(dist > 0) moveHandle($handle, valueToPx(jump)+x1);
if(dist < 0) moveHandle($handle, -valueToPx(jump)+x1);
} else {
if(dist2 > 0) moveHandle($handle2, valueToPx(jump)+x2);
if(dist2 < 0) moveHandle($handle2, -valueToPx(jump)+x2);
}
}
var moveHandle = function($h, p, update){
var boundR = $input.width()-size;
var boundL = 0;
if(options.range){
if($h[0] === $handle[0]){
boundR = $handle2.position().left;
} else {
boundL = $handle.position().left;
}
}
if(p >= boundR){
p = boundR;
} else if(p <= boundL){
p = boundL;
}
if(options.snap && p !== boundR){
var snapPx = valueToPx(options.snap);
p = Math.round(p/snapPx) * snapPx;
}
$h.css({'left': p, 'position': 'absolute'});
if(options.range) updateSelection();
if(update !== false) updateValues();
}
var dragStart = function(ev){
ev.stopPropagation();
ev.preventDefault();
$dragging = $(this);
};
var drag = function(ev){
if($dragging){
ev.preventDefault();
var pos = ev.pageX - $input.offset().left;
moveHandle($dragging, pos);
}
};
var updateSelection = function(){
var p = $handle.position().left;
var w = $handle2.position().left-p;
$selection.css({
'left': p,
'width': w,
'position': 'absolute'
});
};
var dragEnd = function(ev){
if($dragging){
$dragging = null;
options.blur(options.values);
}
};
var updateValues = function(){
var prev;
if(options.range){
prev = options.values.slice(); // clone
options.values[0] = pxToValue($handle);
options.values[1] = pxToValue($handle2);
// set value on original element
$original.val(options.values[0] +','+options.values[1]);
} else {
prev = options.values;
options.values = pxToValue($handle);
// set value on original element
$original.val(options.values);
}
if(options.values !== prev) options.change(options.values);
};
var updateHandles = function(){
if (options.values){
if (options.range){
moveHandle($handle2, valueToPx(options.values[1]), false);
moveHandle($handle, valueToPx(options.values[0]), false);
} else {
moveHandle($handle, valueToPx(options.values), false);
}
}
updateValues();
};
var pxToValue = function($h){
var w = $input.width()-size;
var p = $h.position().left;
var v = (p/(w/(options.max-options.min)))+options.min;
if(options.snap) return Math.floor(v/options.snap) * options.snap;
return Math.round(v);
};
var valueToPx = function(val){
var w = $input.width();
var v = (val*(w/(options.max-options.min)))-options.min;
return v;
};
var bound = function(input){
return Math.max(Math.min(input, options.max), options.min);
};
var methods = {
init : function(o){
// element already replaced
if($(this).data('TinyRange')) return this;
// options
defaults.min = parseFloat($(this).attr('min'));
defaults.max = parseFloat($(this).attr('max'));
defaults.snap = parseFloat($(this).attr('step'));
// options passed into plugin override input attributes
options = $.extend(defaults, o);
if(options.values){
//
} else if(options.range){
options.values = [0, options.max];
} else {
options.values = parseFloat($(this).attr('value'));
}
// how far do handles jump on click, default to step value
jump = options.snap ? options.snap : options.max/10;
// create dom elements
$input = $('<div/>', {'class': 'range-input'}).mousedown(jumpHandle);
$rail = $('<div/>', {'class': 'range-rail'}).appendTo($input);
if(options.range) $selection = $('<div/>', {'class': 'range-selection'}).appendTo($input);
$handle = $('<a/>', {'class': 'range-handle'}).appendTo($input).mousedown(dragStart);
if(options.range) $handle2 = $handle.clone(true).appendTo($input);
// replace dom element
$(this).after($input);
$(this).hide();
$original = $(this);
// attach events
$(document).bind('mouseup', dragEnd);
$(document).bind('mousemove', drag);
// position handles
size = $handle.width();
updateHandles();
return this;
},
set: function(input){
if(typeof input === 'string'){
options.values = bound(input);
} else if(typeof input === 'object' && input.length === 2){
options.values[0] = bound(input[0]);
options.values[1] = bound(input[1]);
}
updateHandles();
},
destroy : function(){
$input.remove();
$(this).show().data('TinyRange', false);
$(document).unbind('mouseup', dragEnd);
$(document).unbind('mousemove', drag);
return this;
}
};
return methods;
};
$.fn.range = function(method) {
// so that arguments are accessible within each closure
var args = arguments;
return this.each(function(){
var state = $(this).data('TinyRange');
// Method calling logic
if (state && state[method] ) {
state[ method ].apply( this, Array.prototype.slice.call( args, 1 ));
} else if ( typeof method === 'object' || ! method ) {
// create new tinyrange
var tr = (new TinyRange(this));
tr.init.apply( this, args );
// save state in jquery data
$(this).data('TinyRange', tr);
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.range' );
}
});
};
})(jQuery);