summaryrefslogtreecommitdiff
path: root/js/dojo/dojox/drawing/annotations/Label.js
blob: 22b9d323a1aadd5c0bf6d5eabdd35ee5e7845278 (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
//>>built
// wrapped by build app
define("dojox/drawing/annotations/Label", ["dijit","dojo","dojox","dojo/require!dojox/drawing/stencil/Text"], function(dijit,dojo,dojox){
dojo.provide("dojox.drawing.annotations.Label");
dojo.require("dojox.drawing.stencil.Text");

dojox.drawing.annotations.Label = dojox.drawing.util.oo.declare(
	// summary:
	// 	An annotation called internally to label an Stencil.
	// description:
	//	Annotation is positioned with dojox.drawing.util.positioning.label
	//	That method should be overwritten for custom placement. Or,
	//	add a 'setLabelCustom' method to the Stencil and it will be used.
	//
	dojox.drawing.stencil.Text,
	function(/*Object*/options){
		// arguments:
		//	options: Object
		//		One key value: the stencil that called this.
		//
		this.master = options.stencil;
		this.labelPosition = options.labelPosition || "BR"; // TL, TR, BR, BL, or function
		if(dojo.isFunction(this.labelPosition)){
			this.setLabel = this.setLabelCustom;
		}
		this.setLabel(options.text || "");
		this.connect(this.master, "onTransform", this, "setLabel");
		this.connect(this.master, "destroy", this, "destroy");
		
		if(this.style.labelSameColor){
			this.connect(this.master, "attr", this, "beforeAttr");
		}
	},{
		_align:"start",
		drawingType:"label",
		
		setLabelCustom: function(/* ? String */text){
			// summary:
			//	Attaches to custom positioning within a Stencil
			//
			var d = dojo.hitch(this.master, this.labelPosition)();
			this.setData({
				x:d.x,
				y:d.y,
				width:d.w || this.style.text.minWidth,
				height:d.h || this._lineHeight
			});
			
			// is an event, not text, so keep the old label:
			if(text && !text.split){ text = this.getText(); }
			
			this.render(this.typesetter(text));
		},
		
		setLabel: function(/* String */text){
			// summary:
			//	Sets the text of the label. Not called directly. Should
			//	be called within Stencil. See stencil._Base
			//
			// onTransform will pass an object here
			var x, y, box = this.master.getBounds();
			
			if(/B/.test(this.labelPosition)){
				y = box.y2 - this._lineHeight;
			}else{
				y = box.y1;
			}
			
			if(/R/.test(this.labelPosition)){
				x = box.x2;
			}else{
				y = box.y1;
				this._align = "end";
			}
			
			if(!this.labelWidth || (text && text.split && text != this.getText())){
				this.setData({
					x:x,
					y:y,
					height:this._lineHeight,
					width:this.style.text.minWidth
				});
				
				this.labelWidth = this.style.text.minWidth;
				this.render(this.typesetter(text));
				
			}else{
				
				this.setData({
					x:x,
					y:y,
					height:this.data.height,
					width:this.data.width
				});
				
				this.render();
			}
			
		},
		beforeAttr: function(key, value){
			if(value!==undefined){
				// make it an object
				var k = key; key = {}; key[k] = value;
			}
			delete key.x;
			delete key.y;
			delete key.width;
			delete key.height;
			this.attr(key);
			 // FIXME: this.created should already be set, shouldn't it?
			!this.created && this.render();
		}
	}

);
});