Skip to content

Commit

Permalink
dnd: removing dojo.marginBox() calls from all places in the DnD package,
Browse files Browse the repository at this point in the history
adding new TimedMoveable class, added a test for the new class.
Thx Douglas Hays for the idea of fps throttling on move, and for the
initial patch. Fixes #6132. Fixes #6133.

git-svn-id: http://svn.dojotoolkit.org/src/dojo/trunk@13002 560b804f-0ae3-0310-86f3-f6aa0a117693
  • Loading branch information
uhop committed Mar 10, 2008
1 parent ffd2133 commit 9daed80
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 19 deletions.
5 changes: 4 additions & 1 deletion dnd/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ dojo.declare("dojo.dnd.Manager", null, {
if(a){
//dojo.dnd.autoScrollNodes(e);
dojo.dnd.autoScroll(e);
dojo.marginBox(a.node, {l: e.pageX + this.OFFSET_X, t: e.pageY + this.OFFSET_Y});
//dojo.marginBox(a.node, {l: e.pageX + this.OFFSET_X, t: e.pageY + this.OFFSET_Y});
var s = a.node.style;
s.left = (e.pageX + this.OFFSET_X) + "px";
s.top = (e.pageY + this.OFFSET_Y) + "px";
var copy = Boolean(this.source.copyState(dojo.dnd.getCopyKeyState(e)));
if(this.copy != copy){
this._setCopyStatus(copy);
Expand Down
19 changes: 5 additions & 14 deletions dnd/Moveable.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ dojo.declare("dojo.dnd.Moveable", null, {
},
onMoveStop: function(/* dojo.dnd.Mover */ mover){
// summary: called after every move operation
if(mover.timer){
clearTimeout(mover.timer);
}
dojo.publish("/dnd/move/stop", [mover]);
dojo.removeClass(dojo.body(), "dojoMove");
dojo.removeClass(this.node, "dojoMoveItem");
Expand All @@ -108,17 +105,11 @@ dojo.declare("dojo.dnd.Moveable", null, {
onMove: function(/* dojo.dnd.Mover */ mover, /* Object */ leftTop){
// summary: called during every move notification,
// should actually move the node, can be overwritten.
var _this = this;
mover.leftTop = leftTop;
if(!mover.timer){
mover.timer = setTimeout(function(){
mover.timer = null;
var leftTop = mover.leftTop;
_this.onMoving(mover, leftTop);
dojo.marginBox(mover.node, leftTop);
_this.onMoved(mover, leftTop);
}, 0);
}
this.onMoving(mover, leftTop);
var s = mover.node.style;
s.left = leftTop.l + "px";
s.top = leftTop.t + "px";
this.onMoved(mover, leftTop);
},
onMoving: function(/* dojo.dnd.Mover */ mover, /* Object */ leftTop){
// summary: called before every incremental move,
Expand Down
62 changes: 62 additions & 0 deletions dnd/TimedMoveable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
dojo.provide("dojo.dnd.TimedMoveable");

dojo.require("dojo.dnd.Moveable");

(function(){
// precalculate long expressions
var oldOnMove = dojo.dnd.Moveable.prototype.onMove;

dojo.declare("dojo.dnd.TimedMoveable", dojo.dnd.Moveable, {
// summary:
// A specialized version of Moveable to support an FPS throttling.
// This class puts an upper restriction on FPS, which may reduce
// the CPU load. The additional parameter "timeout" regulates
// the delay before actually moving the moveable object.

// object attributes (for markup)
timeout: 40, // in ms, 40ms corresponds to 25 fps

constructor: function(node, params){
// summary: an object, which makes a node moveable with a timer
// node: Node: a node (or node's id) to be moved
// params: Object: an optional object with additional parameters.
// See dojo.dnd.Moveable for details on general parameters.
// Following parameters are specific for this class:
// timeout: Number: delay move by this number of ms
// accumulating position changes during the timeout

// sanitize parameters
if(!params){ params = {}; }
if(params.timeout && typeof params.timeout == "number" && params.timeout >= 0){
this.timeout = params.timeout;
}
},

// markup methods
markupFactory: function(params, node){
return new dojo.dnd.TimedMoveable(node, params);
},

onMoveStop: function(/* dojo.dnd.Mover */ mover){
if(mover._timer){
// stop timer
clearTimeout(mover._timer)
// reflect the last received position
oldOnMove.call(this, mover, mover._leftTop)
}
dojo.dnd.Moveable.prototype.onMoveStop.apply(this, arguments);
},
onMove: function(/* dojo.dnd.Mover */ mover, /* Object */ leftTop){
mover._leftTop = leftTop;
if(!mover._timer){
var _t = this; // to avoid using dojo.hitch()
mover._timer = setTimeout(function(){
// we don't have any pending requests
mover._timer = null;
// reflect the last received position
oldOnMove.call(_t, mover, mover._leftTop);
}, this.timeout);
}
}
});
})();
7 changes: 3 additions & 4 deletions dnd/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ dojo.declare("dojo.dnd.move.constrainedMoveable", dojo.dnd.Moveable, {
onMove: function(/* dojo.dnd.Mover */ mover, /* Object */ leftTop){
// summary: called during every move notification,
// should actually move the node, can be overwritten.
var c = this.constraintBox;
leftTop.l = leftTop.l < c.l ? c.l : c.r < leftTop.l ? c.r : leftTop.l;
leftTop.t = leftTop.t < c.t ? c.t : c.b < leftTop.t ? c.b : leftTop.t;
dojo.marginBox(mover.node, leftTop);
var c = this.constraintBox, s = mover.node.style;
s.left = (leftTop.l < c.l ? c.l : c.r < leftTop.l ? c.r : leftTop.l) + "px";
s.top = (leftTop.t < c.t ? c.t : c.b < leftTop.t ? c.b : leftTop.t) + "px";
}
});

Expand Down
112 changes: 112 additions & 0 deletions tests/dnd/test_timed_moveable.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dojo TimedMoveable test</title>
<style type="text/css">
@import "../../resources/dojo.css";
@import "dndDefault.css";

body {
padding: 1em;
color:#666;
background-color:#dedede;
}

#moveable1 {
background: #fff;
border: 1px solid black;
padding:8px;
}
#handle1 {
background: #333;
color: #fff;
font-weight:bold;
cursor: pointer;
border: 1px solid black;
}
#moveable2 {
background: #fff;
position: absolute;
width: 200px;
height: 200px;
left: 100px;
top: 100px;
padding: 10px 20px;
margin: 10px 20px;
border: 10px solid black;
cursor: pointer;
radius:8pt;
-moz-border-radius:8pt 8pt;
}
#moveable3 {
background: #fff;
position: absolute;
width: 200px;
height: 200px;
left: 350px;
top: 350px;
padding: 10px 20px;
margin: 10px 20px;
border: 10px solid green;
cursor: pointer;
radius:8pt;
-moz-border-radius:8pt 8pt;
}
</style>
<script type="text/javascript" src="../../dojo.js" djConfig="isDebug: true"></script>
<script type="text/javascript" src="../../dnd/Mover.js"></script>
<script type="text/javascript" src="../../dnd/Moveable.js"></script>
<script type="text/javascript" src="../../dnd/TimedMoveable.js"></script>
<script type="text/javascript" src="../../dnd/move.js"></script>
<script type="text/javascript">
dojo.require("dojo.dnd.TimedMoveable");
var m1, m2;
var init = function(){
m1 = new dojo.dnd.TimedMoveable("moveable1", {handle: "handle1"});
m2 = new dojo.dnd.TimedMoveable("moveable2");
m3 = new dojo.dnd.TimedMoveable("moveable3", {timeout: 500});

dojo.subscribe("/dnd/move/start", function(mover){
console.debug("Start move", mover);
});
dojo.subscribe("/dnd/move/stop", function(mover){
console.debug("Stop move", mover);
});

dojo.connect(m1, "onMoveStart", function(mover){
console.debug("Start moving m1", mover);
});
dojo.connect(m1, "onMoveStop", function(mover){
console.debug("Stop moving m1", mover);
});
};
dojo.addOnLoad(init);
</script>
</head>
<body>
<h1>Dojo TimedMoveable test</h1>
<p>One moveable (specially marked) is delayed by 500ms (2 fps). Other two are delayed by 40ms (25 fps) &mdash; this is the default value.</p>
<h2>1st run</h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent erat. In malesuada ultricies velit. Vestibulum tempor odio vitae diam. Morbi arcu lectus, laoreet eget, nonummy at, elementum a, quam. Pellentesque ac lacus. Cras quis est. Etiam suscipit, quam at eleifend nonummy, elit nunc sollicitudin tellus, at mattis est ligula interdum urna. Vivamus id augue sed mi consectetuer dictum. Suspendisse dapibus elit non urna. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam gravida dapibus ante. Nam viverra ligula in neque. Nullam at metus. Aenean ipsum.</p>
<p>Mauris vulputate elit a risus. Praesent pellentesque velit ac neque. Fusce ultrices augue vitae orci. Proin a ante. Nulla consectetuer arcu quis est. Suspendisse potenti. Aliquam erat volutpat. Morbi purus augue, eleifend eu, consectetuer sed, tristique ut, wisi. Cras ac tellus. Phasellus adipiscing, libero ac consequat volutpat, ligula purus mollis lectus, ac porttitor ipsum diam non urna. Donec lorem. Pellentesque diam tortor, posuere et, placerat vitae, iaculis et, sapien. Proin sodales vehicula purus. Quisque bibendum mi ac mauris. Quisque tellus. Morbi sagittis. Integer euismod rhoncus augue. Ut accumsan. Curabitur quis tellus sit amet odio ultricies tristique. Etiam malesuada.</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec est. Cras semper nunc ut metus. Pellentesque blandit pede at erat. Quisque nonummy leo id metus. Donec mi mi, viverra id, adipiscing vitae, consectetuer ut, elit. In lectus augue, porttitor quis, viverra id, dignissim id, leo. Maecenas sapien. Nam adipiscing sem. Aenean ligula. Etiam vel velit. In mollis cursus dolor. Suspendisse ac nibh id leo tempor posuere. Aliquam sapien tellus, elementum non, aliquam sed, luctus eu, augue. Aliquam elementum leo nec enim. Donec ornare sagittis magna. Mauris ac tellus.</p>
<p>Duis ac augue rhoncus neque adipiscing feugiat. Donec pulvinar sem vitae neque. Donec commodo metus at ipsum. Cras vel magna vehicula lorem varius consequat. Morbi at enim vitae lectus mollis sodales. Sed tincidunt quam ut mi varius hendrerit. Sed porta arcu non libero. Quisque et wisi. Pellentesque lobortis. Ut enim felis, varius vitae, ornare quis, auctor ut, risus. Ut porta lorem vel quam. Etiam nunc purus, consectetuer non, lobortis eu, fermentum eu, magna. Aenean ultrices ante. Aliquam erat volutpat. Morbi quis velit eu mi sollicitudin lacinia. Suspendisse potenti. Donec lectus.</p>
<p>Quisque egestas turpis. Sed id ipsum id libero euismod nonummy. Nam sed dolor. Mauris in turpis. Duis nec wisi eget ante ultrices varius. Ut eget neque. Suspendisse sagittis iaculis tellus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Phasellus at justo. Donec imperdiet, elit et commodo bibendum, leo augue pellentesque arcu, ac dapibus lorem nulla eget erat. In viverra, tellus eu luctus eleifend, urna nibh lobortis sapien, ac pulvinar massa enim vel turpis. Sed orci neque, sagittis eu, mattis vitae, rutrum condimentum, leo. Fusce wisi odio, convallis at, condimentum vel, imperdiet id, mi. Mauris semper, magna pretium consectetuer sollicitudin, eros enim vehicula risus, eu ultrices turpis quam at wisi. Nam mollis.</p>
<table id="moveable1">
<tr><td id="handle1" colspan="2">You can drag the table using this handle.</td></tr>
<tr><td>1</td><td>Lorem ipsum dolor sit amet...</td></tr>
<tr><td>2</td><td>Mauris vulputate elit a risus...</td></tr>
<tr><td>3</td><td>Pellentesque habitant morbi tristique senectus...</td></tr>
<tr><td>4</td><td>Duis ac augue rhoncus neque...</td></tr>
<tr><td>5</td><td>Quisque egestas turpis. Sed id...</td></tr>
</table>
<h2>2nd run</h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent erat. In malesuada ultricies velit. Vestibulum tempor odio vitae diam. Morbi arcu lectus, laoreet eget, nonummy at, elementum a, quam. Pellentesque ac lacus. Cras quis est. Etiam suscipit, quam at eleifend nonummy, elit nunc sollicitudin tellus, at mattis est ligula interdum urna. Vivamus id augue sed mi consectetuer dictum. Suspendisse dapibus elit non urna. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam gravida dapibus ante. Nam viverra ligula in neque. Nullam at metus. Aenean ipsum.</p>
<p>Mauris vulputate elit a risus. Praesent pellentesque velit ac neque. Fusce ultrices augue vitae orci. Proin a ante. Nulla consectetuer arcu quis est. Suspendisse potenti. Aliquam erat volutpat. Morbi purus augue, eleifend eu, consectetuer sed, tristique ut, wisi. Cras ac tellus. Phasellus adipiscing, libero ac consequat volutpat, ligula purus mollis lectus, ac porttitor ipsum diam non urna. Donec lorem. Pellentesque diam tortor, posuere et, placerat vitae, iaculis et, sapien. Proin sodales vehicula purus. Quisque bibendum mi ac mauris. Quisque tellus. Morbi sagittis. Integer euismod rhoncus augue. Ut accumsan. Curabitur quis tellus sit amet odio ultricies tristique. Etiam malesuada.</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec est. Cras semper nunc ut metus. Pellentesque blandit pede at erat. Quisque nonummy leo id metus. Donec mi mi, viverra id, adipiscing vitae, consectetuer ut, elit. In lectus augue, porttitor quis, viverra id, dignissim id, leo. Maecenas sapien. Nam adipiscing sem. Aenean ligula. Etiam vel velit. In mollis cursus dolor. Suspendisse ac nibh id leo tempor posuere. Aliquam sapien tellus, elementum non, aliquam sed, luctus eu, augue. Aliquam elementum leo nec enim. Donec ornare sagittis magna. Mauris ac tellus.</p>
<p>Duis ac augue rhoncus neque adipiscing feugiat. Donec pulvinar sem vitae neque. Donec commodo metus at ipsum. Cras vel magna vehicula lorem varius consequat. Morbi at enim vitae lectus mollis sodales. Sed tincidunt quam ut mi varius hendrerit. Sed porta arcu non libero. Quisque et wisi. Pellentesque lobortis. Ut enim felis, varius vitae, ornare quis, auctor ut, risus. Ut porta lorem vel quam. Etiam nunc purus, consectetuer non, lobortis eu, fermentum eu, magna. Aenean ultrices ante. Aliquam erat volutpat. Morbi quis velit eu mi sollicitudin lacinia. Suspendisse potenti. Donec lectus.</p>
<p>Quisque egestas turpis. Sed id ipsum id libero euismod nonummy. Nam sed dolor. Mauris in turpis. Duis nec wisi eget ante ultrices varius. Ut eget neque. Suspendisse sagittis iaculis tellus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Phasellus at justo. Donec imperdiet, elit et commodo bibendum, leo augue pellentesque arcu, ac dapibus lorem nulla eget erat. In viverra, tellus eu luctus eleifend, urna nibh lobortis sapien, ac pulvinar massa enim vel turpis. Sed orci neque, sagittis eu, mattis vitae, rutrum condimentum, leo. Fusce wisi odio, convallis at, condimentum vel, imperdiet id, mi. Mauris semper, magna pretium consectetuer sollicitudin, eros enim vehicula risus, eu ultrices turpis quam at wisi. Nam mollis.</p>
<div id="moveable2">You can drag this whole paragraph around.</div>
<div id="moveable3">My move is delayed by 500ms, which corresponds to 2 fps.</div>
</body>
</html>

0 comments on commit 9daed80

Please sign in to comment.