Debian Codegraph @

dojox/drawing/plugins/tools/Pan.js

raw symbols SCIP
1 define(["dojo/_base/lang", "../../util/oo", "../_Plugin", "../../manager/_registry"],
2 function(lang, oo, Plugin, registry){
3
4 var Pan = oo.declare(
5 Plugin,
6 function(options){
7 this.domNode = options.node;
8 var _scrollTimeout;
9 this.toolbar = options.scope;
10 this.connect(this.toolbar, "onToolClick", this, function(){
11 this.onSetPan(false)
12 });
13 this.connect(this.keys, "onKeyUp", this, "onKeyUp");
14 this.connect(this.keys, "onKeyDown", this, "onKeyDown");
15 this.connect(this.keys, "onArrow", this, "onArrow");
16 this.connect(this.anchors, "onAnchorUp", this, "checkBounds");
17 this.connect(this.stencils, "register", this, "checkBounds");
18 this.connect(this.canvas, "resize", this, "checkBounds");
19 this.connect(this.canvas, "setZoom", this, "checkBounds");
20 this.connect(this.canvas, "onScroll", this, function(){
21 if(this._blockScroll){
22 this._blockScroll = false;
23 return;
24 }
25 _scrollTimeout && clearTimeout(_scrollTimeout);
26 _scrollTimeout = setTimeout(lang.hitch(this, "checkBounds"), 200);
27 });
28 this._mouseHandle = this.mouse.register(this);
29 // This HAS to be called after setting initial objects or things get screwy.
30 //this.checkBounds();
31
32 },{
33 // summary:
34 // A plugin that allows for a scrolling canvas. An action
35 // tool is added to the toolbar that allows for panning. Holding
36 // the space bar is a shortcut to that action. The canvas will
37 // only pan and scroll if there are objects out of the viewable
38 // area.
39 // example:
40 // | <div dojoType="dojox.drawing.Toolbar" drawingId="drawingNode" class="drawingToolbar vertical">
41 // | <div tool="dojox.drawing.tools.Line" selected="true">Line</div>
42 // | <div plugin="dojox.drawing.plugins.tools.Pan" options="{}">Pan</div>
43 // | </div>
44
45 selected:false,
46 keyScroll:false,
47 type:"dojox.drawing.plugins.tools.Pan",
48
49 onPanUp: function(obj){
50 if(obj.id == this.button.id){
51 this.onSetPan(false);
52 }
53 },
54
55 onKeyUp: function(evt){
56 switch(evt.keyCode){
57 case 32:
58 this.onSetPan(false);
59 break;
60 case 39: case 37: case 38: case 40:
61 clearInterval(this._timer);
62 break;
63 }
64 },
65
66 onKeyDown: function(evt){
67 if(evt.keyCode == 32){
68 this.onSetPan(true);
69 }
70 },
71
72 interval: 20,
73
74 onArrow: function(evt){
75 if(this._timer){ clearInterval(this._timer); }
76 this._timer = setInterval(lang.hitch(this,function(evt){
77 this.canvas.domNode.parentNode.scrollLeft += evt.x*10;
78 this.canvas.domNode.parentNode.scrollTop += evt.y*10;
79 },evt), this.interval);
80 },
81
82 onSetPan: function(/*Boolean|Event*/ bool){
83 if(bool === true || bool === false){
84 this.selected = !bool;
85 }
86 console.log('ON SET PAN:', this.selected)
87 if(this.selected){
88 this.selected = false;
89 this.button.deselect();
90 }else{
91 this.selected = true;
92 this.button.select();
93 }
94 this.mouse.setEventMode(this.selected ? "pan" : "");
95 },
96
97 onPanDrag: function(obj){
98 var x = obj.x - obj.last.x;
99 var y = obj.y - obj.last.y;
100 this.canvas.domNode.parentNode.scrollTop -= obj.move.y;
101 this.canvas.domNode.parentNode.scrollLeft -= obj.move.x;
102 this.canvas.onScroll();
103 },
104
105 onUp: function(obj){
106 if(obj.withinCanvas){
107 this.keyScroll = true;
108 }else{
109 this.keyScroll = false;
110 }
111 },
112
113 onStencilUp: function(obj){
114 // this gets called even on click-off because of the
115 // issues with TextBlock deselection
116 this.checkBounds();
117 },
118 onStencilDrag: function(obj){
119 // this gets called even on click-off because of the
120 // issues with TextBlock deselection
121 //this.checkBounds();
122 },
123
124 checkBounds: function(){
125
126 //watch("CHECK BOUNDS DISABLED", true); return;
127
128
129 // summary:
130 // Scans all items on the canvas and checks if they are out of
131 // bounds. If so, a scroll bar (in Canvas) is shown. If the position
132 // is left or top, the canvas is scrolled all items are relocated
133 // the distance of the scroll. Ideally, it should look as if the
134 // items do not move.
135
136 // logging stuff here so it can be turned on and off. This method is
137 // very high maintenance.
138 var log = function(){
139 //console.log.apply(console, arguments);
140 }
141 var warn = function(){
142 //console.warn.apply(console, arguments);
143 }
144 //console.clear();
145 //console.time("check bounds");
146
147 // initialize a shot-tin of vars
148 var t=Infinity, r=-Infinity, b=-10000, l=10000,
149 sx=0, sy=0, dy=0, dx=0,
150 mx = this.stencils.group ? this.stencils.group.getTransform() : {dx:0, dy:0},
151 sc = this.mouse.scrollOffset(),
152 // scY, scX: the scrollbar creates the need for extra dimension
153 scY = sc.left ? 10 : 0,
154 scX = sc.top ? 10 : 0,
155 // ch, cw: the current size of the canvas
156 ch = this.canvas.height,
157 cw = this.canvas.width,
158 z = this.canvas.zoom,
159 // pch, pcw: the normal size of the canvas (not scrolled)
160 // these could change if the container resizes.
161 pch = this.canvas.parentHeight,
162 pcw = this.canvas.parentWidth;
163
164
165 this.stencils.withSelected(function(m){
166 var o = m.getBounds();
167 warn("SEL BOUNDS:", o);
168 t = Math.min(o.y1 + mx.dy, t);
169 r = Math.max(o.x2 + mx.dx, r);
170 b = Math.max(o.y2 + mx.dy, b);
171 l = Math.min(o.x1 + mx.dx, l);
172 });
173
174 this.stencils.withUnselected(function(m){
175 var o = m.getBounds();
176 warn("UN BOUNDS:", o);
177 t = Math.min(o.y1, t);
178 r = Math.max(o.x2, r);
179 b = Math.max(o.y2, b);
180 l = Math.min(o.x1, l);
181 log("----------- B:", b, o.y2)
182 });
183
184 b *= z;
185 var xscroll = 0, yscroll = 0;
186 log("Bottom test", "b:", b, "z:", z, "ch:", ch, "pch:", pch, "top:", sc.top, "sy:", sy, "mx.dy:", mx.dy);
187 if(b > pch || sc.top ){
188 log("*bottom scroll*");
189 // item off bottom
190 ch = Math.max(b, pch + sc.top);
191 sy = sc.top;
192 xscroll += this.canvas.getScrollWidth();
193 }else if(!sy && ch>pch){
194 log("*bottom remove*");
195 // item moved from bottom
196 ch = pch;
197 }
198
199 r *= z;
200 if(r > pcw || sc.left){
201 //log("*right scroll*");
202 // item off right
203 cw = Math.max(r, pcw + sc.left);
204 sx = sc.left;
205 yscroll += this.canvas.getScrollWidth();
206 }else if(!sx && cw>pcw){
207 //log("*right remove*");
208 // item moved from right
209 cw = pcw;
210 }
211
212 // add extra space for scrollbars
213 // double it to give some breathing room
214 cw += xscroll*2;
215 ch += yscroll*2;
216
217 this._blockScroll = true;
218
219 // selected items are not transformed. The selection itself is
220 // and the items are on de-select
221 this.stencils.group && this.stencils.group.applyTransform({dx:dx, dy:dy});
222
223 // non-selected items are transformed
224 this.stencils.withUnselected(function(m){
225 m.transformPoints({dx:dx, dy:dy});
226 });
227
228 this.canvas.setDimensions(cw, ch, sx, sy);
229
230 //console.timeEnd("check bounds");
231 }
232 }
233 );
234
235 Pan.setup = {
236 name:"dojox.drawing.plugins.tools.Pan",
237 tooltip:"Pan Tool",
238 iconClass:"iconPan",
239 button:false
240 };
241
242 lang.setObject("dojox.drawing.plugins.tools.Pan", Pan);
243 registry.register(Pan.setup, "plugin");
244 return Pan;
245
246 });