DragScroll.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. o2.widget = o2.widget || {};
  2. o2.widget.DragScroll = new Class({
  3. // We'd like to use the Options Class Mixin
  4. Implements : [ Options, Events ],
  5. // Default options
  6. options : {
  7. friction : 5,
  8. axis : {
  9. x : true,
  10. y : true
  11. }
  12. },
  13. initialize : function(element, options) {
  14. element = this.element = document.id(element);
  15. this.content = element.getFirst();
  16. this.setOptions(options);
  17. // Drag speed
  18. var prevTime, prevScroll, scroll;
  19. //var timer;
  20. var speed=[];
  21. var timerFn = function() {
  22. var now = Date.now();
  23. // var s = element.getScroll();
  24. // scroll = [ s.x, s.y ];
  25. scroll = [ element.scrollLeft, element.scrollTop ];
  26. if (prevTime) {
  27. var dt = now - prevTime + 1;
  28. speed = [ 1000 * (scroll[0] - prevScroll[0]) / dt,
  29. 1000 * (scroll[1] - prevScroll[1]) / dt ];
  30. }
  31. prevScroll = scroll;
  32. prevTime = now;
  33. };
  34. // var timerFn1 = function() {
  35. // var now = Date.now();
  36. // // var s = element.getScroll();
  37. // // scroll = [ s.x, s.y ];
  38. // scroll = [ element.scrollLeft, element.scrollTop ];
  39. // if (prevTime) {
  40. //
  41. // var dt = now - prevTime + 1;
  42. // speed = [ 1000 * (scroll[0] - prevScroll[0]) / dt,
  43. // 1000 * (scroll[1] - prevScroll[1]) / dt ];
  44. // }
  45. // prevScroll = scroll;
  46. // prevTime = now;
  47. // };
  48. // Use Fx.Scroll for scrolling to the right position after the dragging
  49. var fx = this.fx = new Fx.Scroll(element, {
  50. transition : Fx.Transitions.Expo.easeOut
  51. });
  52. // Set initial scroll
  53. fx.set.apply(fx, this.limit(element.scrollLeft, element.scrollTop));
  54. var self = this;
  55. friction = this.options.friction, axis = this.options.axis;
  56. // Make the element draggable
  57. self.drag = this.drag = new Drag(element, {
  58. style : false,
  59. invert : true,
  60. modifiers : {
  61. x : axis.x && 'scrollLeft',
  62. y : axis.y && 'scrollTop'
  63. },
  64. onStart : function() {
  65. // Start the speed measuring
  66. self.fireEvent("dragStart");
  67. timerFn();
  68. // timer = setInterval(timerFn, 1000 / 60);
  69. // cancel any fx if they are still running
  70. fx.cancel();
  71. },
  72. onDrag: function(){
  73. self.fireEvent("drag");
  74. },
  75. onComplete : function() {
  76. // Stop the speed measuring
  77. // clearInterval(timer);
  78. timerFn();
  79. prevTime = false;
  80. // Scroll to the new location
  81. var tmpx = 0;
  82. var tmpy = 0;
  83. if (speed[0]) tmpx = speed[0];
  84. if (speed[1]) tmpy = speed[1];
  85. var l = self.limit(scroll[0] + tmpx/friction, scroll[1] + tmpy/friction);
  86. fx.start.apply(fx, l);
  87. self.fireEvent("dragComplete", l);
  88. }
  89. });
  90. },
  91. // Calculate the limits
  92. getLimit : function() {
  93. var limit = [ [ 0, 0 ], [ 0, 0 ] ], element = this.element;
  94. var styles = Object.values(
  95. this.content.getStyles('padding-left', 'border-left-width',
  96. 'margin-left', 'padding-top', 'border-top-width',
  97. 'margin-top', 'width', 'height')).invoke('toInt');
  98. var size = this.content.getComputedSize();
  99. if (!styles[7]) styles[7] = size.totalHeight;
  100. if (!styles[6]) styles[6] = size.totalWidth;
  101. limit[0][0] = this.sum(styles.slice(0, 3));
  102. limit[0][1] = styles[6] + limit[0][0] - element.clientWidth;
  103. limit[1][0] = this.sum(styles.slice(3, 6));
  104. limit[1][1] = styles[7] + limit[1][0] - element.clientHeight;
  105. return limit;
  106. },
  107. // Apply the limits to the x and y values
  108. limit : function(x, y) {
  109. var limit = this.getLimit();
  110. return [ x.limit(limit[0][0], limit[0][1]),
  111. y.limit(limit[1][0], limit[1][1]) ];
  112. },
  113. sum: function(array){
  114. var result = 0;
  115. for (var l = array.length; l--;) result += array[l];
  116. return result;
  117. }
  118. });