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
| var Slider = new Class({
Implements: [Events, Options],
Binds: ['clickedElement', 'draggedKnob', 'syncOrder', 'scrolledElement'],
options: {/*
onTick: $empty(intPosition),
onChange: $empty(intStep),
onComplete: $empty(strStep),
onSetCollision: $empty(setKnob,[hitKnobs]),
onDragCollision: $empty(draggedKnob, hitKnob)*/
onTick: function(position){
if (this.options.snap) position = this.toPosition(this.step);
this.knob.setStyle(this.property, position);
},
snap: false,
offset: 0,
range: false,
wheel: false,
steps: 100,
mode: 'horizontal',
multiKnob: false,
measure: false
},
initialize: function(element, knob, options){
this.setOptions(options);
this.element = document.id(element);
this.knobSpacing = this.options.knobSpacing;
this.knob = document.id(knob).store('slider', this);
this.previousChange = this.previousEnd = this.step = -1;
var offset, limit = {}, modifiers = {'x': false, 'y': false};
switch (this.options.mode){
case 'vertical':
this.axis = 'y';
this.property = 'top';
offset = 'offsetHeight';
break;
case 'horizontal':
this.axis = 'x';
this.property = 'left';
offset = 'offsetWidth';
}
if(this.options.measure){
this.full = this.element.measure(function(){ this.half = this.knob[offset] / 2; return this.element[offset] - this.knob[offset] + (this.options.offset * 2); }.bind(this));
}
else{
this.full = this.element[offset] - this.knob[offset] + (this.options.offset * 2);
this.half = this.knob[offset] / 2;
}
this.min = $chk(this.options.range[0]) ? this.options.range[0] : 0;
this.max = $chk(this.options.range[1]) ? this.options.range[1] : this.options.steps;
this.range = this.max - this.min;
this.steps = this.options.steps || this.full;
this.stepSize = Math.abs(this.range) / this.steps;
this.stepWidth = this.stepSize * this.full / Math.abs(this.range) ;
//added either relative or absolute positioning to the element and absolutely positioned knobs so knobs do not clear their respective lefts and rights
var elementPosition = this.element.getStyle('position');
if(elementPosition != 'relative' || elementPosition != 'absolute'){ this.element.setStyle('position', 'relative'); }
this.knob.setStyle('position', (this.options.multiKnob) ? 'absolute' : 'relative').setStyle(this.property, - this.options.offset);
modifiers[this.axis] = this.property;
limit[this.axis] = [- this.options.offset, this.full - this.options.offset];
this.bound = {
clickedElement: this.clickedElement.bind(this),
scrolledElement: this.scrolledElement.bindWithEvent(this),
draggedKnob: this.draggedKnob.bind(this)
};
var dragOptions = {
snap: 0,
limit: limit,
modifiers: modifiers,
onDrag: this.bound.draggedKnob,
onStart: this.bound.draggedKnob,
onBeforeStart: (function(){
this.isDragging = true;
}).bind(this),
onComplete: function(){
this.isDragging = false;
this.draggedKnob();
this.knob.removeClass('collided');
this.drag.firstStep = true;
this.syncOrder();
this.end();
}.bind(this)
};
if (this.options.snap){
dragOptions.grid = Math.ceil(this.stepWidth);
dragOptions.limit[this.axis][1] = this.full;
}
this.drag = new Drag(this.knob, dragOptions), this.drag.firstStep = true;
this.attach();
},
attach: function(){
//click and mousewheel events are not added to multiknob sliders for obvious reasons
if(!this.options.multiKnob){
this.element.addEvent('mousedown', this.bound.clickedElement);
if (this.options.wheel) this.element.addEvent('mousewheel', this.bound.scrolledElement);
}
this.drag.attach();
return this;
},
detach: function(){
this.element.removeEvent('mousedown', this.bound.clickedElement);
this.element.removeEvent('mousewheel', this.bound.scrolledElement);
this.drag.detach();
return this;
},
set: function(step){
if (!((this.range > 0) ^ (step < this.min))) step = this.min;
if (!((this.range > 0) ^ (step > this.max))) step = this.max;
//added check to prevented knobs from being set to steps that violate the spacing of knobs
var instances = this.element.getChildren().retrieve('slider').erase(this).filter(function(e){ return e != null; }), hitKnobs = [];
instances.each(function(e, i){
var knobSpacing = Math.max(this.knobSpacing, e.knobSpacing);
if($defined(this.knobSpacing) && $defined(e.knobSpacing) && step > e.step - knobSpacing && step < e.step + knobSpacing){ hitKnobs.push(e.knob) };
}.bind(this));
if(hitKnobs.length > 0){ this.fireEvent('setCollision', [this.knob, hitKnobs]); return; }
else{
this.step = Math.round(step);
this.checkStep();
this.fireEvent('tick', this.toPosition(this.step));
this.syncOrder();
this.end();
return this;
}
},
clickedElement: function(event){
if (this.isDragging || event.target == this.knob) return;
var dir = this.range < 0 ? -1 : 1;
var position = event.page[this.axis] - this.element.getPosition()[this.axis] - this.half;
position = position.limit(-this.options.offset, this.full -this.options.offset);
this.step = Math.round(this.min + dir * this.toStep(position));
this.checkStep();
this.fireEvent('tick', position);
this.end();
},
scrolledElement: function(event){
var mode = (this.options.mode == 'horizontal') ? (event.wheel < 0) : (event.wheel > 0);
this.set(mode ? this.step - this.stepSize : this.step + this.stepSize);
event.stop();
},
draggedKnob: function(){
var dir = this.range < 0 ? -1 : 1;
var position = this.drag.value.now[this.axis];
var lastKnob = this.knob.getAllPrevious(), lastKnob = lastKnob.filter(function(e,i,a){ return e.retrieve('slider').knobSpacing != null; })[0];
var nextKnob = this.knob.getAllNext(), nextKnob = nextKnob.filter(function(e,i,a){ return e.retrieve('slider').knobSpacing != null; })[0];
var lastSlider = (lastKnob) ? lastKnob.retrieve('slider') : {}, nextSlider = (nextKnob) ? nextKnob.retrieve('slider') : {};
position = position.limit(-this.options.offset, this.full -this.options.offset);
//added check to prevented knobs from being dragged to steps that violate the spacing of knobs
if(this.step > this.toStep(position)){
var knobSpacing = Math.max(this.knobSpacing, lastSlider.knobSpacing);
if($defined(lastSlider.knobSpacing) && $defined(this.knobSpacing) && position < lastSlider.step * lastSlider.stepWidth + knobSpacing * lastSlider.stepWidth){
this.set(lastSlider.step + knobSpacing);
if(!this.knob.hasClass('collided') && !this.drag.firstStep){ this.knob.addClass('collided'); this.fireEvent('dragCollision', [this.knob, lastKnob]); }
this.drag.firstStep = false;
}
else{ this.knob.removeClass('collided'); this.step = Math.round(this.min + dir * this.toStep(position)); }
}
else{
var knobSpacing = Math.max(this.knobSpacing, nextSlider.knobSpacing);
if($defined(nextSlider.knobSpacing) && $defined(this.knobSpacing) && position > nextSlider.step * nextSlider.stepWidth - knobSpacing * nextSlider.stepWidth){
this.set(nextSlider.step - knobSpacing);
if(!this.knob.hasClass('collided') && !this.drag.firstStep){ this.knob.addClass('collided'); this.fireEvent('dragCollision', [this.knob, nextKnob]); }
this.drag.firstStep = false;
}
else{ this.knob.removeClass('collided'); this.step = Math.round(this.min + dir * this.toStep(position)); }
}
this.checkStep();
},
syncOrder:function(){
var pinhash = $H();
var sorted = this.element.getChildren().retrieve('slider').filter(function(e){ return e != null; }).map(function(e){ pinhash.include(e.step,e.knob); return e.step }).sort(function(a, b){ return a - b; });
pinhash.each(function(v,k,h){
var pin = h.get(sorted[0]);
if(pin){
pin.inject(this.element, 'bottom');
sorted.shift();
}
}.bind(this));
},
checkStep: function(){
if (this.previousChange != this.step){
this.previousChange = this.step;
this.fireEvent('change', this.step);
}
},
end: function(){
if (this.previousEnd !== this.step){
this.previousEnd = this.step;
this.fireEvent('complete', this.step + '');
}
},
toStep: function(position){
var step = (position + this.options.offset) * this.stepSize / this.full * this.steps;
return this.options.steps ? Math.round(step -= step % this.stepSize) : step;
},
toPosition: function(step){
return (this.full * Math.abs(this.min - step)) / (this.steps * this.stepSize) - this.options.offset;
}
});
//Here is an example usage, you basically call a Mootools slider instance up for each pin. You must pass in multiKnob: true
// - there is also an added feature bonus in that you can specify a knob spacing for each of the knobs you add to your slider,
// this lets you set the step-wise distance away that each pin will keep from one another.
$$('#search_filter_primaryWrap .slideknob').each(function(e,i){
var slidelabel_min = e.getParent().getPrevious(), slidelabel_max = e.getParent().getNext();
new Slider(e.getParent(), e, {
range: [0, 100],
steps: 100,
multiKnob: true,
knobSpacing: 5,
onChange: function(pos){
(e.hasClass('slideknob_min')) ? slidelabel_min.set('html', 'min<div>'+ pos +'%</div>') : slidelabel_max.set('html', 'max<div>'+ pos +'%</div>');
}
}).set((e.hasClass('slideknob_min')) ? 0 : 100);
}); |