/
jquery.limitslider.js
238 lines (198 loc) · 6.26 KB
/
jquery.limitslider.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
/*jslint devel: true, bitwise: true, regexp: true, browser: true, confusion: true, unparam: true, eqeq: true, white: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */
/*globals jQuery */
/*!
* LimitSlider
*
* Copyright (c) 2011-2016 Martijn W. van der Lee
* Licensed under the MIT.
*/
/* Slider extension with forced limits and gaps.
* Optional ranges, titles and labels.
*/
;(function ($, undefined) {
"use strict";
$.widget('vanderlee.limitslider', $.ui.slider, {
options: $.extend({
'classEven': 'ui-slider-handle-even',
'classOdd': 'ui-slider-handle-odd',
'gap': undefined,
'left': undefined,
'right': undefined,
'limit': undefined,
'limits': undefined,
'ranges': [],
'title': false,
'label': false
}, $.ui.slider.prototype.options),
_create: function() {
if (!this.options.values) {
this.options.values = [this.options.value];
}
$.ui.slider.prototype._create.call(this);
$(this.element).addClass('ui-limitslider');
this._renderRanges();
this._renderLabels();
this._renderTitles();
},
_renderTitle: function(index) {
if (this.options.title) {
var value = this.options.values[index];
$(this.handles[index])
.attr('title', $.isFunction(this.options.title) ? this.options.title(value, index) : value)
.addClass(this.options[index % 2 ? 'classEven' : 'classOdd']);
}
},
_renderTitles: function(index) {
if (this.options.title) {
var that = this;
$.each(this.options.values, function(v) {
that._renderTitle(v);
});
}
},
_renderLabel: function(index, init) {
if (this.options.label) {
var value = this.options.values[index],
text = $.isFunction(this.options.label) ? this.options.label(value, index) : value;
if (init) {
var html = $('<div>').css({
'text-align': 'center'
, 'font-size': '75%'
, 'display': 'table-cell'
, 'vertical-align': 'middle'
}).html(text);
$(this.handles[index]).html(html).css({
'text-decoration': 'none'
, 'display': 'table'
});
} else {
$('> div', this.handles[index]).html(text);
}
}
},
_renderLabels: function() {
if (this.options.label) {
var that = this;
$.each(this.options.values, function(v) {
that._renderLabel(v, true);
});
}
},
_renderRanges: function() {
var options = this.options,
values = options.values,
scale = function(value) {
return (value - options.min) * 100 / (options.max - options.min);
},
index,
left,
right,
range;
$('.ui-slider-range', this.element).remove();
for (index = 0; index <= values.length; ++index) {
var range = options.ranges[index],
sliderRange;
if (range) {
left = scale(index == 0? options.min : values[index - 1]);
right = scale(index < values.length? values[index] : options.max);
sliderRange = $('<div/>')
.addClass('ui-slider-range ui-widget-header')
.css('width', (right - left) + '%');
if (range.styleClass) {
sliderRange.addClass(range.styleClass);
}
if (left == 0) {
sliderRange.addClass('ui-slider-range-min');
} else if (right == 100) {
sliderRange.addClass('ui-slider-range-max');
} else {
sliderRange.css('left', left+'%');
}
$(this.element).prepend(sliderRange);
// sliderRange.prependTo(this.element);
}
}
},
_slide: function(event, index, newVal) {
// Left limit
if (this.options.left) {
newVal = Math.max(newVal, this.options.left);
}
// Right limit
if (this.options.right) {
newVal = Math.min(newVal, this.options.right);
}
// Limit
if (this.options.limit) {
newVal = Math.max(newVal, this.options.limit[0]);
newVal = Math.min(newVal, this.options.limit[1]);
}
// Per-slider limit
if (this.options.limits && this.options.limits[index]) {
newVal = Math.max(newVal, this.options.limits[index][0]);
newVal = Math.min(newVal, this.options.limits[index][1]);
}
if (this.options.gap || this.options.gap === 0) {
// Gap to previous
if (index > 0) {
newVal = Math.max(newVal, this.options.values[index - 1] + this.options.gap);
}
// Gap to next
if (index < this.options.values.length - 1) {
newVal = Math.min(newVal, this.options.values[index + 1] - this.options.gap);
}
}
// Call parent
$.ui.slider.prototype._slide.call(this, event, index, newVal);
},
_change: function(event, index) {
// Call parent
$.ui.slider.prototype._change.call(this, event, index);
// Apply visuals
this._renderRanges();
this._renderLabel(index);
this._renderTitle(index);
},
insert: function(index, value, range, limit) {
var max = this.options.values.length,
prev,
next;
index = (index === null || typeof index === 'undefined')
? max
: Math.max(0, Math.min(index, max));
if (typeof value === 'undefined') {
prev = index <= 0 ? this.options.min : this.options.values[index - 1],
next = index >= max ? this.options.max : this.options.values[index];
value = Math.round((prev + next) * .5);
}
this.options.values.splice(index, 0, value);
if (this.options.ranges) {
this.options.ranges.splice(index, 0, range || false);
}
if (this.options.limits) {
this.options.limits.splice(index, 0, range || undefined);
}
this._create();
this.element.trigger('slide', [index, value]);
return this;
},
remove: function(index, length) {
var max = this.options.values.length - 1;
length = Math.max(1, length || 1);
if (max > length - 1) {
index = (index === null || typeof index === 'undefined')
? max + 1 - length
: Math.max(0, Math.min(index, max));
this.options.values.splice(index, length);
if (this.options.ranges) {
this.options.ranges.splice(index, length);
}
if (this.options.limits) {
this.options.limits.splice(index, length);
}
this._create();
}
return this;
}
});
}(jQuery));