dom/tests/mochitest/ajax/scriptaculous/src/slider.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 // script.aculo.us slider.js v1.7.1_beta2, Tue May 15 15:15:45 EDT 2007
michael@0 2
michael@0 3 // Copyright (c) 2005-2007 Marty Haught, Thomas Fuchs
michael@0 4 //
michael@0 5 // script.aculo.us is freely distributable under the terms of an MIT-style license.
michael@0 6 // For details, see the script.aculo.us web site: http://script.aculo.us/
michael@0 7
michael@0 8 if(!Control) var Control = {};
michael@0 9 Control.Slider = Class.create();
michael@0 10
michael@0 11 // options:
michael@0 12 // axis: 'vertical', or 'horizontal' (default)
michael@0 13 //
michael@0 14 // callbacks:
michael@0 15 // onChange(value)
michael@0 16 // onSlide(value)
michael@0 17 Control.Slider.prototype = {
michael@0 18 initialize: function(handle, track, options) {
michael@0 19 var slider = this;
michael@0 20
michael@0 21 if(handle instanceof Array) {
michael@0 22 this.handles = handle.collect( function(e) { return $(e) });
michael@0 23 } else {
michael@0 24 this.handles = [$(handle)];
michael@0 25 }
michael@0 26
michael@0 27 this.track = $(track);
michael@0 28 this.options = options || {};
michael@0 29
michael@0 30 this.axis = this.options.axis || 'horizontal';
michael@0 31 this.increment = this.options.increment || 1;
michael@0 32 this.step = parseInt(this.options.step || '1');
michael@0 33 this.range = this.options.range || $R(0,1);
michael@0 34
michael@0 35 this.value = 0; // assure backwards compat
michael@0 36 this.values = this.handles.map( function() { return 0 });
michael@0 37 this.spans = this.options.spans ? this.options.spans.map(function(s){ return $(s) }) : false;
michael@0 38 this.options.startSpan = $(this.options.startSpan || null);
michael@0 39 this.options.endSpan = $(this.options.endSpan || null);
michael@0 40
michael@0 41 this.restricted = this.options.restricted || false;
michael@0 42
michael@0 43 this.maximum = this.options.maximum || this.range.end;
michael@0 44 this.minimum = this.options.minimum || this.range.start;
michael@0 45
michael@0 46 // Will be used to align the handle onto the track, if necessary
michael@0 47 this.alignX = parseInt(this.options.alignX || '0');
michael@0 48 this.alignY = parseInt(this.options.alignY || '0');
michael@0 49
michael@0 50 this.trackLength = this.maximumOffset() - this.minimumOffset();
michael@0 51
michael@0 52 this.handleLength = this.isVertical() ?
michael@0 53 (this.handles[0].offsetHeight != 0 ?
michael@0 54 this.handles[0].offsetHeight : this.handles[0].style.height.replace(/px$/,"")) :
michael@0 55 (this.handles[0].offsetWidth != 0 ? this.handles[0].offsetWidth :
michael@0 56 this.handles[0].style.width.replace(/px$/,""));
michael@0 57
michael@0 58 this.active = false;
michael@0 59 this.dragging = false;
michael@0 60 this.disabled = false;
michael@0 61
michael@0 62 if(this.options.disabled) this.setDisabled();
michael@0 63
michael@0 64 // Allowed values array
michael@0 65 this.allowedValues = this.options.values ? this.options.values.sortBy(Prototype.K) : false;
michael@0 66 if(this.allowedValues) {
michael@0 67 this.minimum = this.allowedValues.min();
michael@0 68 this.maximum = this.allowedValues.max();
michael@0 69 }
michael@0 70
michael@0 71 this.eventMouseDown = this.startDrag.bindAsEventListener(this);
michael@0 72 this.eventMouseUp = this.endDrag.bindAsEventListener(this);
michael@0 73 this.eventMouseMove = this.update.bindAsEventListener(this);
michael@0 74
michael@0 75 // Initialize handles in reverse (make sure first handle is active)
michael@0 76 this.handles.each( function(h,i) {
michael@0 77 i = slider.handles.length-1-i;
michael@0 78 slider.setValue(parseFloat(
michael@0 79 (slider.options.sliderValue instanceof Array ?
michael@0 80 slider.options.sliderValue[i] : slider.options.sliderValue) ||
michael@0 81 slider.range.start), i);
michael@0 82 Element.makePositioned(h); // fix IE
michael@0 83 Event.observe(h, "mousedown", slider.eventMouseDown);
michael@0 84 });
michael@0 85
michael@0 86 Event.observe(this.track, "mousedown", this.eventMouseDown);
michael@0 87 Event.observe(document, "mouseup", this.eventMouseUp);
michael@0 88 Event.observe(document, "mousemove", this.eventMouseMove);
michael@0 89
michael@0 90 this.initialized = true;
michael@0 91 },
michael@0 92 dispose: function() {
michael@0 93 var slider = this;
michael@0 94 Event.stopObserving(this.track, "mousedown", this.eventMouseDown);
michael@0 95 Event.stopObserving(document, "mouseup", this.eventMouseUp);
michael@0 96 Event.stopObserving(document, "mousemove", this.eventMouseMove);
michael@0 97 this.handles.each( function(h) {
michael@0 98 Event.stopObserving(h, "mousedown", slider.eventMouseDown);
michael@0 99 });
michael@0 100 },
michael@0 101 setDisabled: function(){
michael@0 102 this.disabled = true;
michael@0 103 },
michael@0 104 setEnabled: function(){
michael@0 105 this.disabled = false;
michael@0 106 },
michael@0 107 getNearestValue: function(value){
michael@0 108 if(this.allowedValues){
michael@0 109 if(value >= this.allowedValues.max()) return(this.allowedValues.max());
michael@0 110 if(value <= this.allowedValues.min()) return(this.allowedValues.min());
michael@0 111
michael@0 112 var offset = Math.abs(this.allowedValues[0] - value);
michael@0 113 var newValue = this.allowedValues[0];
michael@0 114 this.allowedValues.each( function(v) {
michael@0 115 var currentOffset = Math.abs(v - value);
michael@0 116 if(currentOffset <= offset){
michael@0 117 newValue = v;
michael@0 118 offset = currentOffset;
michael@0 119 }
michael@0 120 });
michael@0 121 return newValue;
michael@0 122 }
michael@0 123 if(value > this.range.end) return this.range.end;
michael@0 124 if(value < this.range.start) return this.range.start;
michael@0 125 return value;
michael@0 126 },
michael@0 127 setValue: function(sliderValue, handleIdx){
michael@0 128 if(!this.active) {
michael@0 129 this.activeHandleIdx = handleIdx || 0;
michael@0 130 this.activeHandle = this.handles[this.activeHandleIdx];
michael@0 131 this.updateStyles();
michael@0 132 }
michael@0 133 handleIdx = handleIdx || this.activeHandleIdx || 0;
michael@0 134 if(this.initialized && this.restricted) {
michael@0 135 if((handleIdx>0) && (sliderValue<this.values[handleIdx-1]))
michael@0 136 sliderValue = this.values[handleIdx-1];
michael@0 137 if((handleIdx < (this.handles.length-1)) && (sliderValue>this.values[handleIdx+1]))
michael@0 138 sliderValue = this.values[handleIdx+1];
michael@0 139 }
michael@0 140 sliderValue = this.getNearestValue(sliderValue);
michael@0 141 this.values[handleIdx] = sliderValue;
michael@0 142 this.value = this.values[0]; // assure backwards compat
michael@0 143
michael@0 144 this.handles[handleIdx].style[this.isVertical() ? 'top' : 'left'] =
michael@0 145 this.translateToPx(sliderValue);
michael@0 146
michael@0 147 this.drawSpans();
michael@0 148 if(!this.dragging || !this.event) this.updateFinished();
michael@0 149 },
michael@0 150 setValueBy: function(delta, handleIdx) {
michael@0 151 this.setValue(this.values[handleIdx || this.activeHandleIdx || 0] + delta,
michael@0 152 handleIdx || this.activeHandleIdx || 0);
michael@0 153 },
michael@0 154 translateToPx: function(value) {
michael@0 155 return Math.round(
michael@0 156 ((this.trackLength-this.handleLength)/(this.range.end-this.range.start)) *
michael@0 157 (value - this.range.start)) + "px";
michael@0 158 },
michael@0 159 translateToValue: function(offset) {
michael@0 160 return ((offset/(this.trackLength-this.handleLength) *
michael@0 161 (this.range.end-this.range.start)) + this.range.start);
michael@0 162 },
michael@0 163 getRange: function(range) {
michael@0 164 var v = this.values.sortBy(Prototype.K);
michael@0 165 range = range || 0;
michael@0 166 return $R(v[range],v[range+1]);
michael@0 167 },
michael@0 168 minimumOffset: function(){
michael@0 169 return(this.isVertical() ? this.alignY : this.alignX);
michael@0 170 },
michael@0 171 maximumOffset: function(){
michael@0 172 return(this.isVertical() ?
michael@0 173 (this.track.offsetHeight != 0 ? this.track.offsetHeight :
michael@0 174 this.track.style.height.replace(/px$/,"")) - this.alignY :
michael@0 175 (this.track.offsetWidth != 0 ? this.track.offsetWidth :
michael@0 176 this.track.style.width.replace(/px$/,"")) - this.alignY);
michael@0 177 },
michael@0 178 isVertical: function(){
michael@0 179 return (this.axis == 'vertical');
michael@0 180 },
michael@0 181 drawSpans: function() {
michael@0 182 var slider = this;
michael@0 183 if(this.spans)
michael@0 184 $R(0, this.spans.length-1).each(function(r) { slider.setSpan(slider.spans[r], slider.getRange(r)) });
michael@0 185 if(this.options.startSpan)
michael@0 186 this.setSpan(this.options.startSpan,
michael@0 187 $R(0, this.values.length>1 ? this.getRange(0).min() : this.value ));
michael@0 188 if(this.options.endSpan)
michael@0 189 this.setSpan(this.options.endSpan,
michael@0 190 $R(this.values.length>1 ? this.getRange(this.spans.length-1).max() : this.value, this.maximum));
michael@0 191 },
michael@0 192 setSpan: function(span, range) {
michael@0 193 if(this.isVertical()) {
michael@0 194 span.style.top = this.translateToPx(range.start);
michael@0 195 span.style.height = this.translateToPx(range.end - range.start + this.range.start);
michael@0 196 } else {
michael@0 197 span.style.left = this.translateToPx(range.start);
michael@0 198 span.style.width = this.translateToPx(range.end - range.start + this.range.start);
michael@0 199 }
michael@0 200 },
michael@0 201 updateStyles: function() {
michael@0 202 this.handles.each( function(h){ Element.removeClassName(h, 'selected') });
michael@0 203 Element.addClassName(this.activeHandle, 'selected');
michael@0 204 },
michael@0 205 startDrag: function(event) {
michael@0 206 if(Event.isLeftClick(event)) {
michael@0 207 if(!this.disabled){
michael@0 208 this.active = true;
michael@0 209
michael@0 210 var handle = Event.element(event);
michael@0 211 var pointer = [Event.pointerX(event), Event.pointerY(event)];
michael@0 212 var track = handle;
michael@0 213 if(track==this.track) {
michael@0 214 var offsets = Position.cumulativeOffset(this.track);
michael@0 215 this.event = event;
michael@0 216 this.setValue(this.translateToValue(
michael@0 217 (this.isVertical() ? pointer[1]-offsets[1] : pointer[0]-offsets[0])-(this.handleLength/2)
michael@0 218 ));
michael@0 219 var offsets = Position.cumulativeOffset(this.activeHandle);
michael@0 220 this.offsetX = (pointer[0] - offsets[0]);
michael@0 221 this.offsetY = (pointer[1] - offsets[1]);
michael@0 222 } else {
michael@0 223 // find the handle (prevents issues with Safari)
michael@0 224 while((this.handles.indexOf(handle) == -1) && handle.parentNode)
michael@0 225 handle = handle.parentNode;
michael@0 226
michael@0 227 if(this.handles.indexOf(handle)!=-1) {
michael@0 228 this.activeHandle = handle;
michael@0 229 this.activeHandleIdx = this.handles.indexOf(this.activeHandle);
michael@0 230 this.updateStyles();
michael@0 231
michael@0 232 var offsets = Position.cumulativeOffset(this.activeHandle);
michael@0 233 this.offsetX = (pointer[0] - offsets[0]);
michael@0 234 this.offsetY = (pointer[1] - offsets[1]);
michael@0 235 }
michael@0 236 }
michael@0 237 }
michael@0 238 Event.stop(event);
michael@0 239 }
michael@0 240 },
michael@0 241 update: function(event) {
michael@0 242 if(this.active) {
michael@0 243 if(!this.dragging) this.dragging = true;
michael@0 244 this.draw(event);
michael@0 245 if(Prototype.Browser.WebKit) window.scrollBy(0,0);
michael@0 246 Event.stop(event);
michael@0 247 }
michael@0 248 },
michael@0 249 draw: function(event) {
michael@0 250 var pointer = [Event.pointerX(event), Event.pointerY(event)];
michael@0 251 var offsets = Position.cumulativeOffset(this.track);
michael@0 252 pointer[0] -= this.offsetX + offsets[0];
michael@0 253 pointer[1] -= this.offsetY + offsets[1];
michael@0 254 this.event = event;
michael@0 255 this.setValue(this.translateToValue( this.isVertical() ? pointer[1] : pointer[0] ));
michael@0 256 if(this.initialized && this.options.onSlide)
michael@0 257 this.options.onSlide(this.values.length>1 ? this.values : this.value, this);
michael@0 258 },
michael@0 259 endDrag: function(event) {
michael@0 260 if(this.active && this.dragging) {
michael@0 261 this.finishDrag(event, true);
michael@0 262 Event.stop(event);
michael@0 263 }
michael@0 264 this.active = false;
michael@0 265 this.dragging = false;
michael@0 266 },
michael@0 267 finishDrag: function(event, success) {
michael@0 268 this.active = false;
michael@0 269 this.dragging = false;
michael@0 270 this.updateFinished();
michael@0 271 },
michael@0 272 updateFinished: function() {
michael@0 273 if(this.initialized && this.options.onChange)
michael@0 274 this.options.onChange(this.values.length>1 ? this.values : this.value, this);
michael@0 275 this.event = null;
michael@0 276 }
michael@0 277 }

mercurial