summaryrefslogtreecommitdiff
path: root/js/dojo/dojox/drawing/manager/Stencil.js
blob: 17e7a82240db3b2a85d9d8dcec1bcb8c9d751a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//>>built
// wrapped by build app
define("dojox/drawing/manager/Stencil", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
dojo.provide("dojox.drawing.manager.Stencil");

(function(){
	var surface, surfaceNode;
	dojox.drawing.manager.Stencil = dojox.drawing.util.oo.declare(
		// summary:
		//		The main class for tracking Stencils that are cretaed, added,
		//		selected, or deleted. Also handles selections, multiple
		//		selections, adding and removing from selections, and dragging
		//		selections. It's this class that triggers the anchors to
		//		appear on a Stencil and whther there are anchor on a multiple
		//		select or not (currently not)
		//
		function(options){
			//
			// TODO: mixin props
			//
			surface = options.surface;
			this.canvas = options.canvas;
			
			this.defaults = dojox.drawing.defaults.copy();
			this.undo = options.undo;
			this.mouse = options.mouse;
			this.keys = options.keys;
			this.anchors = options.anchors;
			this.stencils = {};
			this.selectedStencils = {};
			this._mouseHandle = this.mouse.register(this);
			
			dojo.connect(this.keys, "onArrow", this, "onArrow");
			dojo.connect(this.keys, "onEsc", this, "deselect");
			dojo.connect(this.keys, "onDelete", this, "onDelete");
			
		},
		{
			_dragBegun: false,
			_wasDragged:false,
			_secondClick:false,
			_isBusy:false,
			
			setRecentStencil: function(stencil){
				// summary:
				//		Keeps track of the most recent stencil interacted
				//		with, whether created or selected.
				this.recent = stencil;
			},
			
			getRecentStencil: function(){
				// summary:
				//		Returns the stencil most recently interacted
				//		with whether it's last created or last selected
				return this.recent;
			},
			
			register: function(/*Object*/stencil){
				// summary:
				//		Key method for adding Stencils. Stencils
				//		can be added to the canvas without adding
				//		them to this, but they won't have selection
				//		or drag ability.
				//
				console.log("Selection.register ::::::", stencil.id);
				if(stencil.isText && !stencil.editMode && stencil.deleteEmptyCreate && !stencil.getText()){
					// created empty text field
					// defaults say to delete
					console.warn("EMPTY CREATE DELETE", stencil);
					stencil.destroy();
					return false;
				}
				
				this.stencils[stencil.id] = stencil;
				this.setRecentStencil(stencil);
				
				if(stencil.execText){
					if(stencil._text && !stencil.editMode){
						console.log("select text");
						this.selectItem(stencil);
					}
					stencil.connect("execText", this, function(){
						if(stencil.isText && stencil.deleteEmptyModify && !stencil.getText()){
							console.warn("EMPTY MOD DELETE", stencil);
							// text deleted
							// defaults say to delete
							this.deleteItem(stencil);
						}else if(stencil.selectOnExec){
							this.selectItem(stencil);
						}
					});
				}
				
				stencil.connect("deselect", this, function(){
					if(!this._isBusy && this.isSelected(stencil)){
						// called from within stencil. do action.
						this.deselectItem(stencil);
					}
				});
				
				stencil.connect("select", this, function(){
					if(!this._isBusy && !this.isSelected(stencil)){
						// called from within stencil. do action.
						this.selectItem(stencil);
					}
				});
				
				return stencil;
			},
			unregister: function(/*Object*/stencil){
				// summary:
				//		Method for removing Stencils from the manager.
				//		This doesn't delete them, only removes them from
				// 		the list.
				//
				console.log("Selection.unregister ::::::", stencil.id, "sel:", stencil.selected);
				if(stencil){
					stencil.selected && this.onDeselect(stencil);
					delete this.stencils[stencil.id];
				}
			},
			
			onArrow: function(/*Key Event*/evt){
				// summary:
				// 		Moves selection based on keyboard arrow keys
				//
				// FIXME: Check constraints
				if(this.hasSelected()){
					this.saveThrottledState();
					this.group.applyTransform({dx:evt.x, dy: evt.y});
				}
			},
			
			_throttleVrl:null,
			_throttle: false,
			throttleTime:400,
			_lastmxx:-1,
			_lastmxy:-1,
			saveMoveState: function(){
				// summary:
				//		Internal. Used for the prototype undo stack.
				// 		Saves selection position.
				//
				var mx = this.group.getTransform();
				if(mx.dx == this._lastmxx && mx.dy == this._lastmxy){ return; }
				this._lastmxx = mx.dx;
				this._lastmxy = mx.dy;
				//console.warn("SAVE MOVE!", mx.dx, mx.dy);
				this.undo.add({
					before:dojo.hitch(this.group, "setTransform", mx)
				});
			},
			
			saveThrottledState: function(){
				// summary:
				//		Internal. Used for the prototype undo stack.
				//		Prevents an undo point on every mouse move.
				//		Only does a point when the mouse hesitates.
				//
				clearTimeout(this._throttleVrl);
				clearInterval(this._throttleVrl);
				this._throttleVrl = setTimeout(dojo.hitch(this, function(){
					this._throttle = false;
					this.saveMoveState();
				}), this.throttleTime);
				if(this._throttle){ return; }
				this._throttle = true;
				
				this.saveMoveState();
				
			},
			unDelete: function(/*Array*/stencils){
				// summary:
				//		Undeletes a stencil. Used in undo stack.
				//
				console.log("unDelete:", stencils);
				for(var s in stencils){
					stencils[s].render();
					this.onSelect(stencils[s]);
				}
			},
			onDelete: function(/*Boolean*/noundo){
				// summary:
				//		Event fired on deletion of a stencil
				//
				console.log("Stencil onDelete", noundo);
				if(noundo!==true){
					this.undo.add({
						before:dojo.hitch(this, "unDelete", this.selectedStencils),
						after:dojo.hitch(this, "onDelete", true)
					});
				}
				this.withSelected(function(m){
					this.anchors.remove(m);
					var id = m.id;
					console.log("delete:", m);
					m.destroy();
					delete this.stencils[id];
				});
				this.selectedStencils = {};
			},
			
			deleteItem: function(/*Object*/stencil){
				// summary:
				//		Deletes a stencil.
				//		NOTE: supports limited undo.
				//
				// manipulating the selection to fire onDelete properly
				if(this.hasSelected()){
					// there is a selection
					var sids = [];
					for(var m in this.selectedStencils){
						if(this.selectedStencils.id == stencil.id){
							if(this.hasSelected()==1){
								// the deleting stencil is the only one selected
								this.onDelete();
								return;
							}
						}else{
							sids.push(this.selectedStencils.id);
						}
					}
					// remove selection, delete, restore selection
					this.deselect();
					this.selectItem(stencil);
					this.onDelete();
					dojo.forEach(sids, function(id){
						this.selectItem(id);
					}, this);
				}else{
					// there is not a selection. select it, delete it
					this.selectItem(stencil);
					// now delete selection
					this.onDelete();
				}
			},
			
			removeAll: function(){
				// summary:
				//		Deletes all Stencils on the canvas.
				
				this.selectAll();
				this._isBusy = true;
				this.onDelete();
				this.stencils = {};
				this._isBusy = false;
			},
			
			setSelectionGroup: function(){
				// summary:
				//		Internal. Creates a new selection group
				//		used to hold selected stencils.
				//
				this.withSelected(function(m){
					this.onDeselect(m, true);
				});
				
				if(this.group){
					surface.remove(this.group);
					this.group.removeShape();
				}
				this.group = surface.createGroup();
				this.group.setTransform({dx:0, dy: 0});
				
				this.withSelected(function(m){
					this.group.add(m.container);
					m.select();
				});
			},
			
			setConstraint: function(){
				// summary:
				//		Internal. Gets all selected stencils' coordinates
				//		and determines how far left and up the selection
				//		can go without going below zero
				//
				var t = Infinity, l = Infinity;
				this.withSelected(function(m){
					var o = m.getBounds();
					t = Math.min(o.y1, t);
					l = Math.min(o.x1, l);
				});
				this.constrain = {l:-l, t:-t};
			},
			
			
			
			onDeselect: function(stencil, keepObject){
				// summary:
				//		Event fired on deselection of a stencil
				//
				if(!keepObject){
					delete this.selectedStencils[stencil.id];
				}
				//console.log('onDeselect, keep:', keepObject, "stencil:", stencil.type)
				
				this.anchors.remove(stencil);
				
				surface.add(stencil.container);
				stencil.selected && stencil.deselect();
				stencil.applyTransform(this.group.getTransform());
			},
			
			deselectItem: function(/*Object*/stencil){
				// summary:
				//		Deselect passed stencil
				//
				// note: just keeping with standardized methods
				this.onDeselect(stencil);
			},
			
			deselect: function(){ // all stencils
				// summary:
				//		Deselect all stencils
				//
				this.withSelected(function(m){
					this.onDeselect(m);
				});
				this._dragBegun = false;
				this._wasDragged = false;
			},
			
			onSelect: function(/*Object*/stencil){
				// summary:
				//		Event fired on selection of a stencil
				//
				//console.log("stencil.onSelect", stencil);
				if(!stencil){
					console.error("null stencil is not selected:", this.stencils)
				}
				if(this.selectedStencils[stencil.id]){ return; }
				this.selectedStencils[stencil.id] = stencil;
				this.group.add(stencil.container);
				stencil.select();
				if(this.hasSelected()==1){
					this.anchors.add(stencil, this.group);
				}
			},
			
			selectAll: function(){
				// summary:
				//		Selects all items
				this._isBusy = true;
				for(var m in this.stencils){
					//if(!this.stencils[m].selected){
						this.selectItem(m);
					//}
				}
				this._isBusy = false;
			},
			
			selectItem: function(/*String|Object*/ idOrItem){
				// summary:
				//		Method used to select a stencil.
				//
				var id = typeof(idOrItem)=="string" ? idOrItem : idOrItem.id;
				var stencil = this.stencils[id];
				this.setSelectionGroup();
				this.onSelect(stencil);
				this.group.moveToFront();
				this.setConstraint();
			},
			
			onLabelDoubleClick: function(/*EventObject*/obj){
				// summary:
				//		Event to connect a textbox to
				//		for label edits
				console.info("mgr.onLabelDoubleClick:", obj);
				if(this.selectedStencils[obj.id]){
					this.deselect();
				}
			},
			
			onStencilDoubleClick: function(/*EventObject*/obj){
				// summary:
				//		Event fired on the double-click of a stencil
				//
				console.info("mgr.onStencilDoubleClick:", obj);
				if(this.selectedStencils[obj.id]){
					if(this.selectedStencils[obj.id].edit){
						console.info("Mgr Stencil Edit -> ", this.selectedStencils[obj.id]);
						var m = this.selectedStencils[obj.id];
						// deselect must happen first to set the transform
						// then edit knows where to set the text box
						m.editMode = true;
						this.deselect();
						m.edit();
					}
				}
				
			},
			
			onAnchorUp: function(){
				// summary:
				//		Event fire on mouseup off of an anchor point
				this.setConstraint();
			},
			
			onStencilDown: function(/*EventObject*/obj, evt){
				// summary:
				//		Event fired on mousedown on a stencil
				//
				console.info(" >>> onStencilDown:", obj.id, this.keys.meta);
				if(!this.stencils[obj.id]){ return; }
				this.setRecentStencil(this.stencils[obj.id]);
				this._isBusy = true;
				
				
				if(this.selectedStencils[obj.id] && this.keys.meta){
					if(dojo.isMac && this.keys.cmmd){
						// block context menu
						
					}
					console.log("    shift remove");
					this.onDeselect(this.selectedStencils[obj.id]);
					if(this.hasSelected()==1){
						this.withSelected(function(m){
							this.anchors.add(m, this.group);
						});
					}
					this.group.moveToFront();
					this.setConstraint();
					return;
				
				}else if(this.selectedStencils[obj.id]){
					console.log("    clicked on selected");
					// clicking on same selected item(s)
					// RESET OFFSETS
					var mx = this.group.getTransform();
					this._offx = obj.x - mx.dx;
					this._offy = obj.y - mx.dy;
					return;
				
				}else if(!this.keys.meta){
					
					console.log("    deselect all");
					this.deselect();
				
				}else{
					// meta-key add
					//console.log("reset sel and add stencil")
				}
				console.log("    add stencil to selection");
				// add a stencil
				this.selectItem(obj.id);
				
				mx = this.group.getTransform();
				this._offx = obj.x - mx.dx;
				this._offy = obj.y - mx.dx;
				
				this.orgx = obj.x;
				this.orgy = obj.y;
				
				this._isBusy = false;
				
				// TODO:
				//  dojo.style(surfaceNode, "cursor", "pointer");
				
				// TODO:
				this.undo.add({
					before:function(){
						
					},
					after: function(){
						
					}
				});
			},
			
			onLabelDown: function(/*EventObject*/obj, evt){
				// summary:
				//		Event fired on mousedown of a stencil's label
				//		Because it's an annotation the id will be the
				//		master stencil.
				//console.info("===============>>>Label click: ",obj, " evt: ",evt);
				this.onStencilDown(obj,evt);
			},
			
			onStencilUp: function(/*EventObject*/obj){
				// summary:
				//		Event fired on mouseup off of a stencil
				//
			},
			
			onLabelUp: function(/*EventObject*/obj){
				this.onStencilUp(obj);
			},
			
			onStencilDrag: function(/*EventObject*/obj){
				// summary:
				//		Event fired on every mousemove of a stencil drag
				//
				if(!this._dragBegun){
					// bug, in FF anyway - first mouse move shows x=0
					// the 'else' fixes it
					this.onBeginDrag(obj);
					this._dragBegun = true;
				}else{
					this.saveThrottledState();
					
					var x = obj.x - obj.last.x,
						y = obj.y - obj.last.y,
						c = this.constrain,
						mz = this.defaults.anchors.marginZero;
					
					
					x = obj.x - this._offx;
					y = obj.y - this._offy;
					
					if(x < c.l + mz){
						x = c.l + mz;
					}
					if(y < c.t + mz){
						y = c.t + mz;
					}
					
					this.group.setTransform({
						dx: x,
						dy: y
					});
					
					
				}
			},
			
			onLabelDrag: function(/*EventObject*/obj){
				this.onStencilDrag(obj);
			},
			
			onDragEnd: function(/*EventObject*/obj){
				// summary:
				//		Event fired at the end of a stencil drag
				//
				this._dragBegun = false;
			},
			onBeginDrag: function(/*EventObject*/obj){
				// summary:
				//		Event fired at the beginning of a stencil drag
				//
				this._wasDragged = true;
			},
			
			onDown: function(/*EventObject*/obj){
				// summary:
				//		Event fired on mousedown on the canvas
				//
				this.deselect();
			},
						
			
			onStencilOver: function(obj){
				// summary:
				//		This changes the cursor when hovering over
				//		a selectable stencil.
				//console.log("OVER")
				dojo.style(obj.id, "cursor", "move");
			},

			onStencilOut: function(obj){
				// summary:
				//		This restores the cursor.
				//console.log("OUT")
				dojo.style(obj.id, "cursor", "crosshair");
			},
			
			exporter: function(){
				// summary:
				//		Collects all Stencil data and returns an
				//		Array of objects.
				var items = [];
				for(var m in this.stencils){
					this.stencils[m].enabled && items.push(this.stencils[m].exporter());
				}
				return items; // Array
			},
			
			listStencils: function(){
				return this.stencils;
			},
			
			toSelected: function(/*String*/func){
				// summary:
				//		Convenience function calls function *within*
				//		all selected stencils
				var args = Array.prototype.slice.call(arguments).splice(1);
				for(var m in this.selectedStencils){
					var item = this.selectedStencils[m];
					item[func].apply(item, args);
				}
			},
			
			withSelected: function(/*Function*/func){
				// summary:
				//		Convenience function calls function on
				//		all selected stencils
				var f = dojo.hitch(this, func);
				for(var m in this.selectedStencils){
					f(this.selectedStencils[m]);
				}
			},
			
			withUnselected: function(/*Function*/func){
				// summary:
				//		Convenience function calls function on
				//		all stencils that are not selected
				var f = dojo.hitch(this, func);
				for(var m in this.stencils){
					!this.stencils[m].selected && f(this.stencils[m]);
				}
			},
			
			withStencils: function(/*Function*/func){
				// summary:
				//		Convenience function calls function on
				//		all stencils
				var f = dojo.hitch(this, func);
				for(var m in this.stencils){
					f(this.stencils[m]);
				}
			},
			
			hasSelected: function(){
				// summary:
				// 		Returns number of selected (generally used
				//		as truthy or falsey)
				//
				// FIXME: should be areSelected?
				var ln = 0;
				for(var m in this.selectedStencils){ ln++; }
				return ln; // Number
			},
			
			isSelected: function(/*Object*/stencil){
				// summary:
				//		Returns if passed stencil is selected or not
				//		based on internal collection, not on stencil
				//		boolean
				return !!this.selectedStencils[stencil.id]; // Boolean
			}
		}
		
	);
})();

});