[ Index ]
 

Code source de PRADO 3.0.6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/framework/Web/Javascripts/effects/ -> slider.js (source)

   1  // Copyright (c) 2005 Marty Haught, Thomas Fuchs 
   2  //
   3  // See http://script.aculo.us for more info
   4  // 
   5  // Permission is hereby granted, free of charge, to any person obtaining
   6  // a copy of this software and associated documentation files (the
   7  // "Software"), to deal in the Software without restriction, including
   8  // without limitation the rights to use, copy, modify, merge, publish,
   9  // distribute, sublicense, and/or sell copies of the Software, and to
  10  // permit persons to whom the Software is furnished to do so, subject to
  11  // the following conditions:
  12  // 
  13  // The above copyright notice and this permission notice shall be
  14  // included in all copies or substantial portions of the Software.
  15  //
  16  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23  
  24  if(!Control) var Control = {};
  25  Control.Slider = Class.create();
  26  
  27  // options:
  28  //  axis: 'vertical', or 'horizontal' (default)
  29  //
  30  // callbacks:
  31  //  onChange(value)
  32  //  onSlide(value)
  33  Control.Slider.prototype = {
  34    initialize: function(handle, track, options) {
  35      var slider = this;
  36      
  37      if(handle instanceof Array) {
  38        this.handles = handle.collect( function(e) { return $(e) });
  39      } else {
  40        this.handles = [$(handle)];
  41      }
  42      
  43      this.track   = $(track);
  44      this.options = options || {};
  45  
  46      this.axis      = this.options.axis || 'horizontal';
  47      this.increment = this.options.increment || 1;
  48      this.step      = parseInt(this.options.step || '1');
  49      this.range     = this.options.range || $R(0,1);
  50      
  51      this.value     = 0; // assure backwards compat
  52      this.values    = this.handles.map( function() { return 0 });
  53      this.spans     = this.options.spans ? this.options.spans.map(function(s){ return $(s) }) : false;
  54      this.options.startSpan = $(this.options.startSpan || null);
  55      this.options.endSpan   = $(this.options.endSpan || null);
  56  
  57      this.restricted = this.options.restricted || false;
  58  
  59      this.maximum   = this.options.maximum || this.range.end;
  60      this.minimum   = this.options.minimum || this.range.start;
  61  
  62      // Will be used to align the handle onto the track, if necessary
  63      this.alignX = parseInt(this.options.alignX || '0');
  64      this.alignY = parseInt(this.options.alignY || '0');
  65      
  66      this.trackLength = this.maximumOffset() - this.minimumOffset();
  67  
  68      this.handleLength = this.isVertical() ? 
  69        (this.handles[0].offsetHeight != 0 ? 
  70          this.handles[0].offsetHeight : this.handles[0].style.height.replace(/px$/,"")) : 
  71        (this.handles[0].offsetWidth != 0 ? this.handles[0].offsetWidth : 
  72          this.handles[0].style.width.replace(/px$/,""));
  73  
  74      this.active   = false;
  75      this.dragging = false;
  76      this.disabled = false;
  77  
  78      if(this.options.disabled) this.setDisabled();
  79  
  80      // Allowed values array
  81      this.allowedValues = this.options.values ? this.options.values.sortBy(Prototype.K) : false;
  82      if(this.allowedValues) {
  83        this.minimum = this.allowedValues.min();
  84        this.maximum = this.allowedValues.max();
  85      }
  86  
  87      this.eventMouseDown = this.startDrag.bindAsEventListener(this);
  88      this.eventMouseUp   = this.endDrag.bindAsEventListener(this);
  89      this.eventMouseMove = this.update.bindAsEventListener(this);
  90  
  91      // Initialize handles in reverse (make sure first handle is active)
  92      this.handles.each( function(h,i) {
  93        i = slider.handles.length-1-i;
  94        slider.setValue(parseFloat(
  95          (slider.options.sliderValue instanceof Array ? 
  96            slider.options.sliderValue[i] : slider.options.sliderValue) || 
  97           slider.range.start), i);
  98        Element.makePositioned(h); // fix IE
  99        Event.observe(h, "mousedown", slider.eventMouseDown);
 100      });
 101      
 102      Event.observe(this.track, "mousedown", this.eventMouseDown);
 103      Event.observe(document, "mouseup", this.eventMouseUp);
 104      Event.observe(document, "mousemove", this.eventMouseMove);
 105      
 106      this.initialized = true;
 107    },
 108    dispose: function() {
 109      var slider = this;    
 110      Event.stopObserving(this.track, "mousedown", this.eventMouseDown);
 111      Event.stopObserving(document, "mouseup", this.eventMouseUp);
 112      Event.stopObserving(document, "mousemove", this.eventMouseMove);
 113      this.handles.each( function(h) {
 114        Event.stopObserving(h, "mousedown", slider.eventMouseDown);
 115      });
 116    },
 117    setDisabled: function(){
 118      this.disabled = true;
 119    },
 120    setEnabled: function(){
 121      this.disabled = false;
 122    },  
 123    getNearestValue: function(value){
 124      if(this.allowedValues){
 125        if(value >= this.allowedValues.max()) return(this.allowedValues.max());
 126        if(value <= this.allowedValues.min()) return(this.allowedValues.min());
 127        
 128        var offset = Math.abs(this.allowedValues[0] - value);
 129        var newValue = this.allowedValues[0];
 130        this.allowedValues.each( function(v) {
 131          var currentOffset = Math.abs(v - value);
 132          if(currentOffset <= offset){
 133            newValue = v;
 134            offset = currentOffset;
 135          } 
 136        });
 137        return newValue;
 138      }
 139      if(value > this.range.end) return this.range.end;
 140      if(value < this.range.start) return this.range.start;
 141      return value;
 142    },
 143    setValue: function(sliderValue, handleIdx){
 144      if(!this.active) {
 145        this.activeHandleIdx = handleIdx || 0;
 146        this.activeHandle    = this.handles[this.activeHandleIdx];
 147        this.updateStyles();
 148      }
 149      handleIdx = handleIdx || this.activeHandleIdx || 0;
 150      if(this.initialized && this.restricted) {
 151        if((handleIdx>0) && (sliderValue<this.values[handleIdx-1]))
 152          sliderValue = this.values[handleIdx-1];
 153        if((handleIdx < (this.handles.length-1)) && (sliderValue>this.values[handleIdx+1]))
 154          sliderValue = this.values[handleIdx+1];
 155      }
 156      sliderValue = this.getNearestValue(sliderValue);
 157      this.values[handleIdx] = sliderValue;
 158      this.value = this.values[0]; // assure backwards compat
 159      
 160      this.handles[handleIdx].style[this.isVertical() ? 'top' : 'left'] = 
 161        this.translateToPx(sliderValue);
 162      
 163      this.drawSpans();
 164      if(!this.dragging || !this.event) this.updateFinished();
 165    },
 166    setValueBy: function(delta, handleIdx) {
 167      this.setValue(this.values[handleIdx || this.activeHandleIdx || 0] + delta, 
 168        handleIdx || this.activeHandleIdx || 0);
 169    },
 170    translateToPx: function(value) {
 171      return Math.round(
 172        ((this.trackLength-this.handleLength)/(this.range.end-this.range.start)) * 
 173        (value - this.range.start)) + "px";
 174    },
 175    translateToValue: function(offset) {
 176      return ((offset/(this.trackLength-this.handleLength) * 
 177        (this.range.end-this.range.start)) + this.range.start);
 178    },
 179    getRange: function(range) {
 180      var v = this.values.sortBy(Prototype.K); 
 181      range = range || 0;
 182      return $R(v[range],v[range+1]);
 183    },
 184    minimumOffset: function(){
 185      return(this.isVertical() ? this.alignY : this.alignX);
 186    },
 187    maximumOffset: function(){
 188      return(this.isVertical() ? 
 189        (this.track.offsetHeight != 0 ? this.track.offsetHeight :
 190          this.track.style.height.replace(/px$/,"")) - this.alignY : 
 191        (this.track.offsetWidth != 0 ? this.track.offsetWidth : 
 192          this.track.style.width.replace(/px$/,"")) - this.alignY);
 193    },  
 194    isVertical:  function(){
 195      return (this.axis == 'vertical');
 196    },
 197    drawSpans: function() {
 198      var slider = this;
 199      if(this.spans)
 200        $R(0, this.spans.length-1).each(function(r) { slider.setSpan(slider.spans[r], slider.getRange(r)) });
 201      if(this.options.startSpan)
 202        this.setSpan(this.options.startSpan,
 203          $R(0, this.values.length>1 ? this.getRange(0).min() : this.value ));
 204      if(this.options.endSpan)
 205        this.setSpan(this.options.endSpan, 
 206          $R(this.values.length>1 ? this.getRange(this.spans.length-1).max() : this.value, this.maximum));
 207    },
 208    setSpan: function(span, range) {
 209      if(this.isVertical()) {
 210        span.style.top = this.translateToPx(range.start);
 211        span.style.height = this.translateToPx(range.end - range.start + this.range.start);
 212      } else {
 213        span.style.left = this.translateToPx(range.start);
 214        span.style.width = this.translateToPx(range.end - range.start + this.range.start);
 215      }
 216    },
 217    updateStyles: function() {
 218      this.handles.each( function(h){ Element.removeClassName(h, 'selected') });
 219      Element.addClassName(this.activeHandle, 'selected');
 220    },
 221    startDrag: function(event) {
 222      if(Event.isLeftClick(event)) {
 223        if(!this.disabled){
 224          this.active = true;
 225          
 226          var handle = Event.element(event);
 227          var pointer  = [Event.pointerX(event), Event.pointerY(event)];
 228          var track = handle;
 229          if(track==this.track) {
 230            var offsets  = Position.cumulativeOffset(this.track); 
 231            this.event = event;
 232            this.setValue(this.translateToValue( 
 233             (this.isVertical() ? pointer[1]-offsets[1] : pointer[0]-offsets[0])-(this.handleLength/2)
 234            ));
 235            var offsets  = Position.cumulativeOffset(this.activeHandle);
 236            this.offsetX = (pointer[0] - offsets[0]);
 237            this.offsetY = (pointer[1] - offsets[1]);
 238          } else {
 239            // find the handle (prevents issues with Safari)
 240            while((this.handles.indexOf(handle) == -1) && handle.parentNode) 
 241              handle = handle.parentNode;
 242          
 243            this.activeHandle    = handle;
 244            this.activeHandleIdx = this.handles.indexOf(this.activeHandle);
 245            this.updateStyles();
 246          
 247            var offsets  = Position.cumulativeOffset(this.activeHandle);
 248            this.offsetX = (pointer[0] - offsets[0]);
 249            this.offsetY = (pointer[1] - offsets[1]);
 250          }
 251        }
 252        Event.stop(event);
 253      }
 254    },
 255    update: function(event) {
 256     if(this.active) {
 257        if(!this.dragging) this.dragging = true;
 258        this.draw(event);
 259        // fix AppleWebKit rendering
 260        if(navigator.appVersion.indexOf('AppleWebKit')>0) window.scrollBy(0,0);
 261        Event.stop(event);
 262     }
 263    },
 264    draw: function(event) {
 265      var pointer = [Event.pointerX(event), Event.pointerY(event)];
 266      var offsets = Position.cumulativeOffset(this.track);
 267      pointer[0] -= this.offsetX + offsets[0];
 268      pointer[1] -= this.offsetY + offsets[1];
 269      this.event = event;
 270      this.setValue(this.translateToValue( this.isVertical() ? pointer[1] : pointer[0] ));
 271      if(this.initialized && this.options.onSlide)
 272        this.options.onSlide(this.values.length>1 ? this.values : this.value, this);
 273    },
 274    endDrag: function(event) {
 275      if(this.active && this.dragging) {
 276        this.finishDrag(event, true);
 277        Event.stop(event);
 278      }
 279      this.active = false;
 280      this.dragging = false;
 281    },  
 282    finishDrag: function(event, success) {
 283      this.active = false;
 284      this.dragging = false;
 285      this.updateFinished();
 286    },
 287    updateFinished: function() {
 288      if(this.initialized && this.options.onChange) 
 289        this.options.onChange(this.values.length>1 ? this.values : this.value, this);
 290      this.event = null;
 291    }
 292  }


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7