-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsliderWithLabel.js
138 lines (131 loc) · 4.29 KB
/
sliderWithLabel.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
$.fn.slideWithLabel = function(options) {
var self=this;
var fromSliderFunction=options['fromSlider'];
var getValue;
if(fromSliderFunction){
options.min=0; options.max=1;
getValue=function(){
var sliderValue=self['slider']('value');
return fromSliderFunction.call(self['slider'],sliderValue);
};
}else{
getValue=function(){
return self['slider']('value');
};
}
var toSliderFunction=options['toSlider'];
var convertToSliderValue;
if(toSliderFunction){
convertToSliderValue=function(value){
return toSliderFunction.call(self['slider'],value);
};
options.value=convertToSliderValue(options.value);
}else{
convertToSliderValue=function(value){
return value;
};
}
var convertToDisplayValue=function(value){
var display=options['display'];
if(!display) return ''+value;
return display.call(self['slider'],value);
};
var slider = this['slider'](options);
var label = slider.next();
label.css('display','none');
label.css('position','absolute');
label.css('padding-top','1em');
var spacing = $('<div style="display: inline-block; height: 0;"></div>');
spacing.insertAfter(slider);
var updateLabel=function() {
var handle = $(slider.children('.ui-slider-handle'));
$('.slider-value', label).text(convertToDisplayValue(getValue()));
//offset
var top = handle.offset().top + handle.outerHeight(true);
var left = handle.offset().left - (label.width() - handle.width())/ 2;
var sliderOffset = slider.offset();
left = Math.max(sliderOffset.left, left);
left = Math.min(sliderOffset.left+slider.outerWidth()-label.outerWidth(), left);
label.css('top', top);
label.css('left', left);
//label.animate({"left" : left}, 100);
spacing.css('height',label.outerHeight(true));
label.css('display','inline-block');
};
var lastValue=options.value;
var fireChangedIfRequired=function(){
var newValue=slider['slider']('value');
if(lastValue!=newValue){
lastValue=newValue;
if(options['changed']){
options['changed']();
}
}
};
slider.bind('change', function () {
setTimeout(function(){
updateLabel();
fireChangedIfRequired();
},1);
});
slider.bind('slide', function () {
setTimeout(function(){
updateLabel();
fireChangedIfRequired();
},1);
});
updateLabel();
setTimeout(function() { updateLabel(); },1);
$(window).resize(function() {
updateLabel();
});
return function(){
if(arguments.length==2 && arguments[0]=='value'){
var fromValue=arguments[1];
var toValue=convertToSliderValue(fromValue);
var ret=slider['slider']('value',toValue);
slider.trigger('change');
return ret;
}
if(arguments.length==1 && arguments[0]=='object'){
return self;
}
if(arguments.length==1 && arguments[0]=='value'){
return getValue();
}
if(arguments.length==2){
if(arguments[0]=='trigger'){
slider.trigger(arguments[1]);
return;
}
}
if(arguments.length==3){
if(arguments[0]=='option'){
var value,newValue;
if(arguments[1]=='min'){
var newMin=arguments[2];
value=getValue();
if(newMin>value){
newValue=convertToSliderValue(newMin);
slider['slider']('value',newValue);
}
slider['slider']('option', 'min', newMin);
slider.trigger('change');
return;
}
if(arguments[1]=='max'){
var newMax=arguments[2];
value=getValue();
if(newMax<value){
newValue=convertToSliderValue(newMax);
slider['slider']('value',newValue);
}
slider['slider']('option', 'max', newMax);
slider.trigger('change');
return;
}
}
}
return slider['slider'].apply(slider,arguments);
};
};