Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // ----- start component -------
  3.  
  4. function Slider(slider,min,max,resulation) {
  5.     this.slider =slider;
  6.     this.track = slider.find('.track');
  7.     this.thumb = slider.find('.thumb');
  8.     this.document = $(document);
  9.  
  10.     this.min = min;
  11.     this.max = max;
  12.     this.resolution = resulation;
  13.     this.position = 0;
  14.     this.dx = 0;
  15.  
  16. }
  17.  
  18. Slider.prototype.init = function() {
  19.     this.slider.on('mousedown.slider',$.proxy(this.onMouseDown, this));
  20.     var sliderInterface = {
  21.         getValue: this.getValue,
  22.         setValue: this.setValue
  23.     };
  24.     $('.slider').data('interface', sliderInterface);
  25.  
  26.  
  27. };
  28.  
  29. Slider.prototype.onMouseDown = function(e)
  30. {
  31.  
  32.     if(e.target === this.thumb[0]) { //thumb clicked?
  33.         this.dx = e.pageX - this.thumb.offset().left;
  34.  
  35.         this.slider.addClass('active');
  36.         this.document.on('mousemove.slider', $.proxy(this.onMouseMove, this));
  37.         this.document.one('mouseup.slider', $.proxy(this.onMouseUp, this));
  38.     }
  39.     else{
  40.         this.position = (e.pageX - this.slider.offset().left - this.dx);
  41.         this.thumb.css('left', this.position);
  42.     }
  43.  
  44.  
  45.     //track clicked? position thumb
  46.  
  47.  
  48.     e.preventDefault();
  49. }
  50.  
  51. Slider.prototype.onMouseUp = function(e)
  52. {
  53.     //$document.off('mousemove', onMouseMove);
  54.     this.document.off('mousemove.slider', $.proxy(this.onMouseMove, this));
  55.     this.slider.removeClass('active');
  56.     //$document.off('mouseup');
  57.     e.preventDefault();
  58.  
  59.     console.log(this.getValue());
  60. }
  61.  
  62. Slider.prototype.onMouseMove = function(e)
  63. {
  64.  
  65.     this.position = (e.pageX - this.slider.offset().left - this.dx);
  66.  
  67.     this.position = Math.max(0, this.position);
  68.     this.position = Math.min(this.position, this.track.width() - this.thumb.width());
  69.  
  70.     this.thumb.css('left', this.position);
  71. }
  72.  
  73.  
  74.  
  75. Slider.prototype.getValue = function()
  76. {
  77.  
  78.     return this.positionToValue(this.position);
  79.  
  80.  
  81. };
  82.  
  83.  
  84. Slider.prototype.setValue = function(value)
  85. {
  86.  
  87.  
  88.     this.position = this.valueToPosition(value);
  89.  
  90.  
  91.  
  92.     this.thumb.css('left', this.position);
  93. };
  94.  
  95.  
  96. Slider.prototype.positionToValue = function(pos)
  97. {
  98.     var range = this.getRange();
  99.  
  100.         return pos/((this.track.width() - this.thumb.width()) / range) + this.min;
  101.     /*else
  102.         return pos/((this.track.width() - this.thumb.width()) / range)  ;
  103.     */
  104. };
  105.  
  106.  
  107. Slider.prototype.valueToPosition = function (val) {
  108.  
  109.     var range = this.getRange();
  110.  
  111.     var pos = (this.track.width() - this.thumb.width()) * (val/range);
  112.  
  113.     pos = Math.max(0, pos);
  114.     pos = Math.min(pos, this.track.width() - this.thumb.width());
  115.  
  116.     return pos;
  117.  
  118. };
  119.  
  120. Slider.prototype.getRange = function(){
  121.  
  122.     if(this.min < 0)
  123.         return this.max + (this.min * (-1))
  124.     else
  125.         return this.max - this.min;
  126. };
  127.  
  128.  
  129.  
  130.  
  131. (function() {
  132.  
  133.         var test = new Slider($('.slider').first(),0,100,1);
  134.         test.init();
  135.         test.setValue(33);
  136.         console.log(test.getValue());
  137.  
  138.         var test1 = new Slider($('.slider').last(),-1,1,0.5);
  139.         test1.init();
  140.         test1.setValue(0);
  141.         console.log(test1.getValue());
  142.  
  143.  
  144.  
  145.  
  146. })();
  147.  
  148. // ----- end component -------
  149.  
  150. // ----- testing only -----
  151.  
  152. (function() {
  153.  
  154.     /*
  155.     $('.shortcuts > a').on('click', function () {
  156.         var sliderId = '#slider' + $(this).parent().attr('id').replace('shortcuts', '');
  157.         $(sliderId).data('interface').setValue(parseFloat($(this).text()));
  158.     });
  159.  
  160.     setInterval(function() {
  161.         console.log('slider values: ');
  162.         $('.slider').each(function() {
  163.             console.log('value: ' + $(this).data('interface').getValue()); //todo output in html
  164.         });
  165.     }, 3000);
  166.     */
  167. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement