1 | define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/sniff", "dojo/_base/array", "dojo/on", "dojox/gfx", "./IndicatorBase"], |
---|
2 | function(lang, declare, has, array, on, gfx, IndicatorBase){ |
---|
3 | return declare("dojox.dgauges.TextIndicator", IndicatorBase, { |
---|
4 | // summary: |
---|
5 | // This type of indicator is used to render text. |
---|
6 | // To render an arbitrary text, set the value property. |
---|
7 | // To render the value of a value indicator or a range indicator, set the indicator property. |
---|
8 | // Setting the indicator property takes precedence on setting the value property. |
---|
9 | // When the indicator property is set, the text is automatically updated on value changes. |
---|
10 | |
---|
11 | // font: Object |
---|
12 | // Font used by this element. |
---|
13 | font: null, |
---|
14 | // x: Number |
---|
15 | // The text anchor x-position. Default is 0. |
---|
16 | x: 0, |
---|
17 | // y: Number |
---|
18 | // The text anchor y-position. Default is 0. |
---|
19 | y: 0, |
---|
20 | // align: String |
---|
21 | // An alignment of a text in regards to the anchor position: |
---|
22 | // |
---|
23 | // - "start": A text's baseline starts at the anchor. |
---|
24 | // This is the default value of the align attribute. |
---|
25 | // - "middle": A text's baseline is centered on the anchor point. |
---|
26 | // - "end": A text's baseline ends at the anchor point. |
---|
27 | align: "middle", |
---|
28 | // color: Object |
---|
29 | // The color of the text. |
---|
30 | color: "black", |
---|
31 | // indicator: dojox/dgauges/IndicatorBase |
---|
32 | // If this property is set, the value of the indicator is automatically |
---|
33 | // rendered by this text element. |
---|
34 | indicator: null, |
---|
35 | // labelFunc: Object |
---|
36 | // If set, this method allows to format the value of this text indicator. |
---|
37 | // A label function takes the text to render as argument and returns a String. |
---|
38 | labelFunc: null, |
---|
39 | |
---|
40 | constructor: function(){ |
---|
41 | this.addInvalidatingProperties(["indicator"]); |
---|
42 | |
---|
43 | var resetProps = ["x", "y", "font", "align", "color", "labelFunc"]; |
---|
44 | array.forEach(resetProps, lang.hitch(this, function(entry){ |
---|
45 | this.watch(entry, this._resetText); |
---|
46 | })); |
---|
47 | |
---|
48 | this.watch("indicator", lang.hitch(this, this._indicatorChanged)); |
---|
49 | }, |
---|
50 | |
---|
51 | postscript: function(mixin){ |
---|
52 | // summary: |
---|
53 | // Internal method |
---|
54 | // tags: |
---|
55 | // private |
---|
56 | this.inherited(arguments); |
---|
57 | if(mixin && mixin.indicator){ |
---|
58 | this._indicatorChanged("indicator", null, mixin.indicator); |
---|
59 | } |
---|
60 | }, |
---|
61 | |
---|
62 | _resetText: function(){ |
---|
63 | // summary: |
---|
64 | // Internal method. |
---|
65 | // tags: |
---|
66 | // private |
---|
67 | this._textCreated = false; |
---|
68 | this.invalidateRendering(); |
---|
69 | }, |
---|
70 | |
---|
71 | _valueWatcher: null, |
---|
72 | |
---|
73 | _indicatorChanged: function(name, oldValue, newValue){ |
---|
74 | // summary: |
---|
75 | // Internal method. |
---|
76 | // tags: |
---|
77 | // private |
---|
78 | if(this._valueWatcher){ |
---|
79 | this._valueWatcher.unwatch(); |
---|
80 | } |
---|
81 | this._valueWatcher = newValue.watch("value", lang.hitch(this, this.refreshRendering)); |
---|
82 | }, |
---|
83 | |
---|
84 | _getFont: function(){ |
---|
85 | // summary: |
---|
86 | // Internal method. |
---|
87 | // tags: |
---|
88 | // private |
---|
89 | var font = this.font; |
---|
90 | if(!font && this._gauge){ |
---|
91 | font = this._gauge.font; |
---|
92 | } |
---|
93 | if(!font){ |
---|
94 | font = gfx.defaultFont; |
---|
95 | } |
---|
96 | return font; |
---|
97 | }, |
---|
98 | |
---|
99 | _textCreated: false, |
---|
100 | _textInstance: null, |
---|
101 | |
---|
102 | _createText: function(group, font, color, text, x, y, align){ |
---|
103 | // summary: |
---|
104 | // Internal method. |
---|
105 | // tags: |
---|
106 | // private |
---|
107 | var gfxText = group.createText({ |
---|
108 | x: x, |
---|
109 | y: y, |
---|
110 | text: text, |
---|
111 | align: align |
---|
112 | }).setFont(font).setFill(color); |
---|
113 | return gfxText; |
---|
114 | }, |
---|
115 | |
---|
116 | refreshRendering: function(){ |
---|
117 | if(this._gfxGroup == null){ |
---|
118 | return; |
---|
119 | } |
---|
120 | var text; |
---|
121 | if(this.indicator){ |
---|
122 | text = this.indicator.value; |
---|
123 | }else{ |
---|
124 | text = this.value; |
---|
125 | } |
---|
126 | if(this.labelFunc){ |
---|
127 | text = this.labelFunc(text); |
---|
128 | } |
---|
129 | var iOsVersion = has("iphone"); |
---|
130 | // Workaround for a bug on iOS version < 5.0: Recreate the text every times |
---|
131 | if(!this._textCreated || (iOsVersion != undefined && iOsVersion < 5)){ |
---|
132 | this._gfxGroup.clear(); |
---|
133 | var font = this._getFont(); |
---|
134 | this._textInstance = this._createText(this._gfxGroup, font, font.color ? font.color : this.color, "", this.x, this.y, this.align); |
---|
135 | this._textCreated = true; |
---|
136 | } |
---|
137 | this._textInstance.setShape({ |
---|
138 | text: text |
---|
139 | }); |
---|
140 | |
---|
141 | return this._textInstance; |
---|
142 | } |
---|
143 | }) |
---|
144 | }); |
---|