[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/jquery_ui/jquery.ui.old/ui/ -> ui.slider.js (source)

   1  /*

   2   * jQuery UI Slider 1.6

   3   *

   4   * Copyright (c) 2008 AUTHORS.txt (http://ui.jquery.com/about)

   5   * Dual licensed under the MIT (MIT-LICENSE.txt)

   6   * and GPL (GPL-LICENSE.txt) licenses.

   7   *

   8   * http://docs.jquery.com/UI/Slider

   9   *

  10   * Depends:

  11   *    ui.core.js

  12   */
  13  (function($) {
  14  
  15  $.fn.unwrap = $.fn.unwrap || function(expr) {
  16    return this.each(function(){
  17       $(this).parents(expr).eq(0).after(this).remove();
  18    });
  19  };
  20  
  21  $.widget("ui.slider", {
  22  
  23      _init: function() {
  24  
  25          var self = this;
  26          this.element.addClass("ui-slider");
  27          this._initBoundaries();
  28  
  29          // Initialize mouse and key events for interaction

  30          this.handle = $(this.options.handle, this.element);
  31          if (!this.handle.length) {
  32              self.handle = self.generated = $(self.options.handles || [0]).map(function() {
  33                  var handle = $("<div/>").addClass("ui-slider-handle").appendTo(self.element);
  34                  if (this.id)
  35                      handle.attr("id", this.id);
  36                  return handle[0];
  37              });
  38          }
  39  
  40          var handleclass = function(el) {
  41              this.element = $(el);
  42              this.element.data("mouse", this);
  43              this.options = self.options;
  44  
  45              this.element.bind("mousedown", function() {
  46                  if(self.currentHandle) this.blur(self.currentHandle);
  47                  self._focus(this, true);
  48              });
  49  
  50              this._mouseInit();
  51          };
  52  
  53          $.extend(handleclass.prototype, $.ui.mouse, {
  54              _mouseCapture: function() { return true; },
  55              _mouseStart: function(event) { return self._start.call(self, event, this.element[0]); },
  56              _mouseDrag: function(event) { return self._drag.call(self, event, this.element[0]); },
  57              _mouseStop: function(event) { return self._stop.call(self, event, this.element[0]); },
  58              trigger: function(event) { this._mouseDown(event); }
  59          });
  60  
  61          $(this.handle)
  62              .each(function() {
  63                  new handleclass(this);
  64              })
  65              .wrap('<a href="#" style="outline:none;border:none;"></a>')
  66              .parent()
  67                  .bind('click', function() { return false; })
  68                  .bind('focus', function(event) { self._focus(this.firstChild); })
  69                  .bind('blur', function(event) { self._blur(this.firstChild); })
  70                  .bind('keydown', function(event) { if(!self.options.noKeyboard) return self._keydown(event.keyCode, this.firstChild); })
  71          ;
  72  
  73          // Bind the click to the slider itself

  74          this.element.bind('mousedown.slider', function(event) {
  75  
  76              if($(event.target).is('.ui-slider-handle')) return;
  77  
  78              //Go to the actual clicked posiion, apply a click

  79              self._click.apply(self, [event]);
  80  
  81              //initiate a handle drag, so we can click+drag somewhere

  82               self.currentHandle.data("mouse").trigger(event);
  83  
  84               //This is for always triggering the change event

  85              self.firstValue = self.firstValue + 1;
  86  
  87          });
  88  
  89          // Move the first handle to the startValue

  90          $.each(this.options.handles || [], function(index, handle) {
  91              self.moveTo(handle.start, index, true);
  92          });
  93          if (!isNaN(this.options.startValue))
  94              this.moveTo(this.options.startValue, 0, true);
  95  
  96          this.previousHandle = $(this.handle[0]); //set the previous handle to the first to allow clicking before selecting the handle

  97          if(this.handle.length == 2 && this.options.range) this._createRange();
  98  
  99      },
 100  
 101      destroy: function() {
 102  
 103          this.element
 104              .removeClass("ui-slider ui-slider-disabled")
 105              .removeData("slider")
 106              .unbind(".slider");
 107  
 108          if(this.handle && this.handle.length) {
 109              this.handle
 110                  .unwrap("a");
 111              this.handle.each(function() {
 112                  var mouse = $(this).data("mouse");
 113                  mouse && mouse._mouseDestroy();
 114              });
 115          }
 116  
 117          this.generated && this.generated.remove();
 118  
 119      },
 120  
 121      _start: function(event, handle) {
 122  
 123          var o = this.options;
 124          if(o.disabled) return false;
 125  
 126          // Prepare the outer size

 127          this.actualSize = { width: this.element.outerWidth() , height: this.element.outerHeight() };
 128  
 129          // This is a especially ugly fix for strange blur events happening on mousemove events

 130          if (!this.currentHandle)
 131              this._focus(this.previousHandle, true);
 132  
 133          this.offset = this.element.offset();
 134  
 135          this.handleOffset = this.currentHandle.offset();
 136          this.clickOffset = { top: event.pageY - this.handleOffset.top, left: event.pageX - this.handleOffset.left };
 137  
 138          this.firstValue = this.value();
 139  
 140          this._propagate('start', event);
 141          this._drag(event, handle);
 142          return true;
 143  
 144      },
 145  
 146      _drag: function(event, handle) {
 147  
 148          var o = this.options;
 149  
 150          var position = { top: event.pageY - this.offset.top - this.clickOffset.top, left: event.pageX - this.offset.left - this.clickOffset.left};
 151          if(!this.currentHandle) this._focus(this.previousHandle, true); //This is a especially ugly fix for strange blur events happening on mousemove events

 152  
 153          position.left = this._translateLimits(position.left, "x");
 154          position.top = this._translateLimits(position.top, "y");
 155  
 156          if (o.stepping.x) {
 157              var value = this._convertValue(position.left, "x");
 158              value = this._round(value / o.stepping.x) * o.stepping.x;
 159              position.left = this._translateValue(value, "x");
 160          }
 161          if (o.stepping.y) {
 162              var value = this._convertValue(position.top, "y");
 163              value = this._round(value / o.stepping.y) * o.stepping.y;
 164              position.top = this._translateValue(value, "y");
 165          }
 166  
 167          position.left = this._translateRange(position.left, "x");
 168          position.top = this._translateRange(position.top, "y");
 169  
 170          if(o.axis != "vertical") this.currentHandle.css({ left: position.left });
 171          if(o.axis != "horizontal") this.currentHandle.css({ top: position.top });
 172  
 173          //Store the slider's value

 174          this.currentHandle.data("mouse").sliderValue = {
 175              x: this._round(this._convertValue(position.left, "x")) || 0,
 176              y: this._round(this._convertValue(position.top, "y")) || 0
 177          };
 178  
 179          if (this.rangeElement)
 180              this._updateRange();
 181          this._propagate('slide', event);
 182          return false;
 183  
 184      },
 185  
 186      _stop: function(event) {
 187  
 188          this._propagate('stop', event);
 189  
 190          if (this.firstValue != this.value())
 191              this._propagate('change', event);
 192  
 193          // This is a especially ugly fix for strange blur events happening on mousemove events

 194          this._focus(this.currentHandle, true);
 195  
 196          return false;
 197  
 198      },
 199  
 200      _round: function(value) {
 201  
 202          return this.options.round ? parseInt(value,10) : parseFloat(value);
 203  
 204      },
 205  
 206      _setData: function(key, value) {
 207  
 208          $.widget.prototype._setData.apply(this, arguments);
 209  
 210          if (/min|max|steps/.test(key)) {
 211              this._initBoundaries();
 212          }
 213  
 214          if(key == "range") {
 215              value ? this.handle.length == 2 && this._createRange() : this._removeRange();
 216          }
 217  
 218      },
 219  
 220      _initBoundaries: function() {
 221  
 222          var element = this.element[0], o = this.options;
 223          this.actualSize = { width: this.element.outerWidth() , height: this.element.outerHeight() };
 224  
 225          $.extend(o, {
 226              axis: o.axis || (element.offsetWidth < element.offsetHeight ? 'vertical' : 'horizontal'),
 227              max: !isNaN(parseInt(o.max,10)) ? { x: parseInt(o.max, 10), y: parseInt(o.max, 10) } : ({ x: o.max && o.max.x || 100, y: o.max && o.max.y || 100 }),
 228              min: !isNaN(parseInt(o.min,10)) ? { x: parseInt(o.min, 10), y: parseInt(o.min, 10) } : ({ x: o.min && o.min.x || 0, y: o.min && o.min.y || 0 })
 229          });
 230          //Prepare the real maxValue

 231          o.realMax = {
 232              x: o.max.x - o.min.x,
 233              y: o.max.y - o.min.y
 234          };
 235          //Calculate stepping based on steps

 236          o.stepping = {
 237              x: o.stepping && o.stepping.x || parseInt(o.stepping, 10) || (o.steps ? o.realMax.x/(o.steps.x || parseInt(o.steps, 10) || o.realMax.x) : 0),
 238              y: o.stepping && o.stepping.y || parseInt(o.stepping, 10) || (o.steps ? o.realMax.y/(o.steps.y || parseInt(o.steps, 10) || o.realMax.y) : 0)
 239          };
 240  
 241      },
 242  
 243      _keydown: function(keyCode, handle) {
 244  
 245          if (this.options.disabled)
 246              return;
 247  
 248          var k = keyCode;
 249          if(/(33|34|35|36|37|38|39|40)/.test(k)) {
 250              var o = this.options, xpos, ypos;
 251              if (/(35|36)/.test(k)) {
 252                  xpos = (k == 35) ? o.max.x : o.min.x;
 253                  ypos = (k == 35) ? o.max.y : o.min.y;
 254              } else {
 255                  var oper = /(34|37|40)/.test(k) ? "-=" : "+=";
 256                  var step = /(37|38|39|40)/.test(k) ? "_oneStep" : "_pageStep";
 257                  xpos = oper + this[step]("x");
 258                  ypos = oper + this[step]("y");
 259              }
 260              this.moveTo({
 261                  x: xpos,
 262                  y: ypos
 263              }, handle);
 264              return false;
 265          }
 266          return true;
 267  
 268      },
 269  
 270      _focus: function(handle,hard) {
 271  
 272          this.currentHandle = $(handle).addClass('ui-slider-handle-active');
 273  
 274          if (hard)
 275              this.currentHandle.parent()[0].focus();
 276  
 277      },
 278  
 279      _blur: function(handle) {
 280  
 281          $(handle).removeClass('ui-slider-handle-active');
 282  
 283          if(this.currentHandle && this.currentHandle[0] == handle) {
 284              this.previousHandle = this.currentHandle;
 285              this.currentHandle = null;
 286          };
 287  
 288      },
 289  
 290      _click: function(event) {
 291  
 292          // This method is only used if:

 293          // - The user didn't click a handle

 294          // - The Slider is not disabled

 295          // - There is a current, or previous selected handle (otherwise we wouldn't know which one to move)

 296  
 297          var pointer = [event.pageX, event.pageY];
 298  
 299          var clickedHandle = false;
 300          this.handle.each(function() {
 301              if(this == event.target)
 302                  clickedHandle = true;
 303          });
 304          if (clickedHandle || this.options.disabled || !(this.currentHandle || this.previousHandle))
 305              return;
 306  
 307          // If a previous handle was focussed, focus it again

 308          if (!this.currentHandle && this.previousHandle)
 309              this._focus(this.previousHandle, true);
 310  
 311          // propagate only for distance > 0, otherwise propagation is done my drag

 312          this.offset = this.element.offset();
 313  
 314          this.moveTo({
 315              y: this._convertValue(event.pageY - this.offset.top - this.currentHandle[0].offsetHeight/2, "y"),
 316              x: this._convertValue(event.pageX - this.offset.left - this.currentHandle[0].offsetWidth/2, "x")
 317          }, null, !this.options.distance);
 318  
 319      },
 320  
 321      _createRange: function() {
 322  
 323          if(this.rangeElement) return;
 324          this.rangeElement = $('<div></div>')
 325              .addClass('ui-slider-range')
 326              .css({ position: 'absolute' })
 327              .appendTo(this.element);
 328          this._updateRange();
 329  
 330      },
 331  
 332      _removeRange: function() {
 333  
 334          this.rangeElement.remove();
 335          this.rangeElement = null;
 336  
 337      },
 338  
 339      _updateRange: function() {
 340  
 341          var prop = this.options.axis == "vertical" ? "top" : "left";
 342          var size = this.options.axis == "vertical" ? "height" : "width";
 343  
 344          this.rangeElement.css(prop, (this._round($(this.handle[0]).css(prop)) || 0) + this._handleSize(0, this.options.axis == "vertical" ? "y" : "x")/2);
 345          this.rangeElement.css(size, (this._round($(this.handle[1]).css(prop)) || 0) - (this._round($(this.handle[0]).css(prop)) || 0));
 346  
 347      },
 348  
 349      _getRange: function() {
 350  
 351          return this.rangeElement ? this._convertValue(this._round(this.rangeElement.css(this.options.axis == "vertical" ? "height" : "width")), this.options.axis == "vertical" ? "y" : "x") : null;
 352  
 353      },
 354  
 355      _handleIndex: function() {
 356  
 357          return this.handle.index(this.currentHandle[0]);
 358  
 359      },
 360  
 361      value: function(handle, axis) {
 362  
 363          if(this.handle.length == 1) this.currentHandle = this.handle;
 364          if(!axis) axis = this.options.axis == "vertical" ? "y" : "x";
 365  
 366          var curHandle = $(handle != undefined && handle !== null ? this.handle[handle] || handle : this.currentHandle);
 367  
 368          if(curHandle.data("mouse").sliderValue) {
 369              return this._round(curHandle.data("mouse").sliderValue[axis]);
 370          } else {
 371              return this._round(((this._round(curHandle.css(axis == "x" ? "left" : "top")) / (this.actualSize[axis == "x" ? "width" : "height"] - this._handleSize(handle,axis))) * this.options.realMax[axis]) + this.options.min[axis]);
 372          }
 373  
 374      },
 375  
 376      _convertValue: function(value,axis) {
 377  
 378          return this.options.min[axis] + (value / (this.actualSize[axis == "x" ? "width" : "height"] - this._handleSize(null,axis))) * this.options.realMax[axis];
 379  
 380      },
 381  
 382      _translateValue: function(value,axis) {
 383  
 384          return ((value - this.options.min[axis]) / this.options.realMax[axis]) * (this.actualSize[axis == "x" ? "width" : "height"] - this._handleSize(null,axis));
 385  
 386      },
 387  
 388      _translateRange: function(value,axis) {
 389  
 390          if (this.rangeElement) {
 391              if (this.currentHandle[0] == this.handle[0] && value >= this._translateValue(this.value(1),axis))
 392                  value = this._translateValue(this.value(1,axis) - this._oneStep(axis), axis);
 393              if (this.currentHandle[0] == this.handle[1] && value <= this._translateValue(this.value(0),axis))
 394                  value = this._translateValue(this.value(0,axis) + this._oneStep(axis), axis);
 395          }
 396  
 397          if (this.options.handles) {
 398              var handle = this.options.handles[this._handleIndex()];
 399              if (value < this._translateValue(handle.min,axis)) {
 400                  value = this._translateValue(handle.min,axis);
 401              } else if (value > this._translateValue(handle.max,axis)) {
 402                  value = this._translateValue(handle.max,axis);
 403              }
 404          }
 405  
 406          return value;
 407  
 408      },
 409  
 410      _translateLimits: function(value,axis) {
 411  
 412          if (value >= this.actualSize[axis == "x" ? "width" : "height"] - this._handleSize(null,axis))
 413              value = this.actualSize[axis == "x" ? "width" : "height"] - this._handleSize(null,axis);
 414  
 415          if (value <= 0)
 416              value = 0;
 417  
 418          return value;
 419  
 420      },
 421  
 422      _handleSize: function(handle,axis) {
 423  
 424          return $(handle != undefined && handle !== null ? this.handle[handle] : this.currentHandle)[0]["offset"+(axis == "x" ? "Width" : "Height")];
 425  
 426      },
 427  
 428      _oneStep: function(axis) {
 429  
 430          return this.options.stepping[axis] || 1;
 431  
 432      },
 433  
 434      _pageStep: function(axis) {
 435  
 436          return /* this.options.paging[axis] ||*/ 10;
 437  
 438      },
 439  
 440      moveTo: function(value, handle, noPropagation) {
 441  
 442          var o = this.options;
 443  
 444          // Prepare the outer size

 445          this.actualSize = { width: this.element.outerWidth() , height: this.element.outerHeight() };
 446  
 447          //If no handle has been passed, no current handle is available and we have multiple handles, return false

 448          if (handle == undefined && !this.currentHandle && this.handle.length != 1)
 449              return false;
 450  
 451          //If only one handle is available, use it

 452          if (handle == undefined && !this.currentHandle)
 453              handle = 0;
 454  
 455          if (handle != undefined)
 456              this.currentHandle = this.previousHandle = $(this.handle[handle] || handle);
 457  
 458          if(value.x !== undefined && value.y !== undefined) {
 459              var x = value.x, y = value.y;
 460          } else {
 461              var x = value, y = value;
 462          }
 463  
 464          if(x !== undefined && x.constructor != Number) {
 465              var me = /^\-\=/.test(x), pe = /^\+\=/.test(x);
 466              if(me || pe) {
 467                  x = this.value(null, "x") + this._round(x.replace(me ? '=' : '+=', ''));
 468              } else {
 469                  x = isNaN(this._round(x)) ? undefined : this._round(x);
 470              }
 471          }
 472  
 473          if(y !== undefined && y.constructor != Number) {
 474              var me = /^\-\=/.test(y), pe = /^\+\=/.test(y);
 475              if(me || pe) {
 476                  y = this.value(null, "y") + this._round(y.replace(me ? '=' : '+=', ''));
 477              } else {
 478                  y = isNaN(this._round(y)) ? undefined : this._round(y);
 479              }
 480          }
 481  
 482          if(o.axis != "vertical" && x !== undefined) {
 483              if(o.stepping.x) x = this._round(x / o.stepping.x) * o.stepping.x;
 484              x = this._translateValue(x, "x");
 485              x = this._translateLimits(x, "x");
 486              x = this._translateRange(x, "x");
 487  
 488              o.animate ? this.currentHandle.stop().animate({ left: x }, (Math.abs(parseInt(this.currentHandle.css("left"),10) - x)) * (!isNaN(parseInt(o.animate,10)) ? o.animate : 5)) : this.currentHandle.css({ left: x });
 489          }
 490  
 491          if(o.axis != "horizontal" && y !== undefined) {
 492              if(o.stepping.y) y = this._round(y / o.stepping.y) * o.stepping.y;
 493              y = this._translateValue(y, "y");
 494              y = this._translateLimits(y, "y");
 495              y = this._translateRange(y, "y");
 496              o.animate ? this.currentHandle.stop().animate({ top: y }, (Math.abs(parseInt(this.currentHandle.css("top"),10) - y)) * (!isNaN(parseInt(o.animate,10)) ? o.animate : 5)) : this.currentHandle.css({ top: y });
 497          }
 498  
 499          if (this.rangeElement)
 500              this._updateRange();
 501  
 502          //Store the slider's value

 503          this.currentHandle.data("mouse").sliderValue = {
 504              x: this._round(this._convertValue(x, "x")) || 0,
 505              y: this._round(this._convertValue(y, "y")) || 0
 506          };
 507  
 508          if (!noPropagation) {
 509              this._propagate('start', null);
 510              this._propagate("slide", null);
 511              this._propagate('stop', null);
 512              this._propagate('change', null);
 513          }
 514  
 515      },
 516  
 517      _propagate: function(n, event) {
 518  
 519          $.ui.plugin.call(this, n, [event, this.ui()]);
 520          this.element.triggerHandler(n == "slide" ? n : "slide"+n, [event, this.ui()], this.options[n]);
 521  
 522      },
 523  
 524      plugins: {},
 525  
 526      ui: function(event) {
 527          return {
 528              options: this.options,
 529              handle: this.currentHandle,
 530              value: this.options.axis != "both" || !this.options.axis ?
 531                  this._round(this.value(null, this.options.axis == "vertical" ? "y" : "x")) :
 532                  {
 533                      x: this._round(this.value(null, "x")),
 534                      y: this._round(this.value(null, "y"))
 535                  },
 536              range: this._getRange()
 537          };
 538      }
 539  
 540  });
 541  
 542  $.extend($.ui.slider, {
 543      getter: "value",
 544      version: "1.6",
 545      defaults: {
 546          animate: false,
 547          distance: 1,
 548          handle: ".ui-slider-handle",
 549          round: true
 550      }
 551  });
 552  
 553  })(jQuery);


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7