TimedMoveable.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojo.dnd.TimedMoveable"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.dnd.TimedMoveable"] = true;
  8. dojo.provide("dojo.dnd.TimedMoveable");
  9. dojo.require("dojo.dnd.Moveable");
  10. /*=====
  11. dojo.declare("dojo.dnd.__TimedMoveableArgs", [dojo.dnd.__MoveableArgs], {
  12. // timeout: Number
  13. // delay move by this number of ms,
  14. // accumulating position changes during the timeout
  15. timeout: 0
  16. });
  17. =====*/
  18. (function(){
  19. // precalculate long expressions
  20. var oldOnMove = dojo.dnd.Moveable.prototype.onMove;
  21. dojo.declare("dojo.dnd.TimedMoveable", dojo.dnd.Moveable, {
  22. // summary:
  23. // A specialized version of Moveable to support an FPS throttling.
  24. // This class puts an upper restriction on FPS, which may reduce
  25. // the CPU load. The additional parameter "timeout" regulates
  26. // the delay before actually moving the moveable object.
  27. // object attributes (for markup)
  28. timeout: 40, // in ms, 40ms corresponds to 25 fps
  29. constructor: function(node, params){
  30. // summary:
  31. // an object that makes a node moveable with a timer
  32. // node: Node||String
  33. // a node (or node's id) to be moved
  34. // params: dojo.dnd.__TimedMoveableArgs
  35. // object with additional parameters.
  36. // sanitize parameters
  37. if(!params){ params = {}; }
  38. if(params.timeout && typeof params.timeout == "number" && params.timeout >= 0){
  39. this.timeout = params.timeout;
  40. }
  41. },
  42. // markup methods
  43. markupFactory: function(params, node){
  44. return new dojo.dnd.TimedMoveable(node, params);
  45. },
  46. onMoveStop: function(/* dojo.dnd.Mover */ mover){
  47. if(mover._timer){
  48. // stop timer
  49. clearTimeout(mover._timer)
  50. // reflect the last received position
  51. oldOnMove.call(this, mover, mover._leftTop)
  52. }
  53. dojo.dnd.Moveable.prototype.onMoveStop.apply(this, arguments);
  54. },
  55. onMove: function(/* dojo.dnd.Mover */ mover, /* Object */ leftTop){
  56. mover._leftTop = leftTop;
  57. if(!mover._timer){
  58. var _t = this; // to avoid using dojo.hitch()
  59. mover._timer = setTimeout(function(){
  60. // we don't have any pending requests
  61. mover._timer = null;
  62. // reflect the last received position
  63. oldOnMove.call(_t, mover, mover._leftTop);
  64. }, this.timeout);
  65. }
  66. }
  67. });
  68. })();
  69. }