TimedMoveable.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. define("dojo/dnd/TimedMoveable", ["../main", "./Moveable"], function(dojo) {
  2. // module:
  3. // dojo/dnd/TimedMoveable
  4. // summary:
  5. // TODOC
  6. /*=====
  7. dojo.declare("dojo.dnd.__TimedMoveableArgs", [dojo.dnd.__MoveableArgs], {
  8. // timeout: Number
  9. // delay move by this number of ms,
  10. // accumulating position changes during the timeout
  11. timeout: 0
  12. });
  13. =====*/
  14. // precalculate long expressions
  15. var oldOnMove = dojo.dnd.Moveable.prototype.onMove;
  16. dojo.declare("dojo.dnd.TimedMoveable", dojo.dnd.Moveable, {
  17. // summary:
  18. // A specialized version of Moveable to support an FPS throttling.
  19. // This class puts an upper restriction on FPS, which may reduce
  20. // the CPU load. The additional parameter "timeout" regulates
  21. // the delay before actually moving the moveable object.
  22. // object attributes (for markup)
  23. timeout: 40, // in ms, 40ms corresponds to 25 fps
  24. constructor: function(node, params){
  25. // summary:
  26. // an object that makes a node moveable with a timer
  27. // node: Node||String
  28. // a node (or node's id) to be moved
  29. // params: dojo.dnd.__TimedMoveableArgs
  30. // object with additional parameters.
  31. // sanitize parameters
  32. if(!params){ params = {}; }
  33. if(params.timeout && typeof params.timeout == "number" && params.timeout >= 0){
  34. this.timeout = params.timeout;
  35. }
  36. },
  37. onMoveStop: function(/* dojo.dnd.Mover */ mover){
  38. if(mover._timer){
  39. // stop timer
  40. clearTimeout(mover._timer);
  41. // reflect the last received position
  42. oldOnMove.call(this, mover, mover._leftTop)
  43. }
  44. dojo.dnd.Moveable.prototype.onMoveStop.apply(this, arguments);
  45. },
  46. onMove: function(/* dojo.dnd.Mover */ mover, /* Object */ leftTop){
  47. mover._leftTop = leftTop;
  48. if(!mover._timer){
  49. var _t = this; // to avoid using dojo.hitch()
  50. mover._timer = setTimeout(function(){
  51. // we don't have any pending requests
  52. mover._timer = null;
  53. // reflect the last received position
  54. oldOnMove.call(_t, mover, mover._leftTop);
  55. }, this.timeout);
  56. }
  57. }
  58. });
  59. return dojo.dnd.TimedMoveable;
  60. });