Debian Codegraph @

dojox/dgauges/RectangularGauge.js

raw symbols SCIP
1 define(["dojo/_base/declare", "./GaugeBase", "dojox/gfx/matrix"], function(declare, GaugeBase, matrix){
2 return declare("dojox.dgauges.RectangularGauge", GaugeBase, {
3 // summary:
4 // The base class for rectangular gauges.
5 // You can create custom horizontal or vertical gauges by extending this class.
6 // See dojox/dgauges/components/default/HorinzontalLinearGauge.js for an example of rectangular gauge.
7
8 // orientation: "horizontal"|"vertical"
9 // The orientation of the gauge. Default is "horizontal".
10 orientation: "horizontal",
11
12 // leading, middle and trailing graphical parts
13 _middleParts: null,
14 _leadingParts: null,
15 _trailingParts: null,
16 _baseParts: null,
17 _classParts: null,
18 _layoutInfos: {},
19 constructor: function(){
20
21 this.orientation = "horizontal";
22
23 this._middleParts = [];
24 this._leadingParts = [];
25 this._trailingParts = [];
26 this._baseParts = [];
27 this._classParts = [];
28
29 this._layoutInfos = {
30 leading: {
31 x: 0,
32 y: 0,
33 w: 0,
34 h: 0
35 },
36 middle: {
37 x: 0,
38 y: 0,
39 w: 0,
40 h: 0
41 },
42 trailing: {
43 x: 0,
44 y: 0,
45 w: 0,
46 h: 0
47 }
48 };
49 this.addInvalidatingProperties(["orientation"]);
50
51 },
52
53 addElement: function(name, element, location){
54 // summary:
55 // Adds a element to the gauge.
56 // name: String
57 // The name of the element to be added.
58 // element: Object
59 // This parameter can be:
60 // - A function which takes on argument of type GFX Group and return null or a
61 // GFX element retrievable using the getElementRenderer() method.
62 // - A Scale instance, i.e. CircularScale or RectangularScale.
63 // - A TextIndicator instance.
64 // location: String
65 // The area to place the element. Valid values are "leading"|"middle"|"trailing". Leading and trailing areas are fixed size. The
66 // middle area use the remaining size. If not specified, the element's refreshRendering
67 // is called with the whole gauge size as argument.
68
69 this.inherited(arguments);
70
71 var obj = this._elements[this._elements.length - 1];
72
73 if(location == "middle"){
74 this._middleParts.push(obj);
75 }else if(location == "leading"){
76 this._leadingParts.push(obj);
77 }else if(location == "trailing"){
78 this._trailingParts.push(obj);
79 }else{
80 if(obj._isGFX){
81 this._baseParts.push(obj);
82 }else{
83 this._classParts.push(obj);
84 }
85 }
86 },
87
88 removeElement: function(name){
89 // summary:
90 // Remove the element defined by name from the gauge.
91 // name: String
92 // The name of the element as defined using addElement.
93 // returns: Object
94 // A reference to the removed element.
95 var obj = this.getElement(name);
96 if(obj){
97 if(this._middleParts && this._middleParts.indexOf(obj) >= 0){
98 this._middleParts.splice(this._middleParts.indexOf(obj), 1);
99 }else if(this._leadingParts && this._leadingParts.indexOf(obj) >= 0){
100 this._leadingParts.splice(this._leadingParts.indexOf(obj), 1);
101 }else if(this._trailingParts && this._trailingParts.indexOf(obj) >= 0){
102 this._trailingParts.splice(this._trailingParts.indexOf(obj), 1);
103 }else if(this._baseParts && this._baseParts.indexOf(obj) >= 0){
104 this._baseParts.splice(this._baseParts.indexOf(obj), 1);
105 }else if(this._classParts && this._classParts.indexOf(obj) >= 0){
106 this._classParts.splice(this._classParts.indexOf(obj), 1);
107 }
108 }
109
110 this.inherited(arguments);
111 },
112
113 _computeArrayBoundingBox: function(elements){
114 // summary:
115 // Internal method.
116 // tags:
117 // private
118 if(elements.length == 0){
119 return {x: 0, y: 0, w: 0, h: 0};
120 }
121 var bbox = null;
122 var minX, minY, maxX, maxY;
123 minX = minY = +Infinity;
124 maxX = maxY = -Infinity;
125 for(var i = 0; i < elements.length; i++){
126 bbox = this._computeBoundingBox(elements[i]._gfxGroup);
127 if(minX > bbox.x){
128 minX = bbox.x;
129 }
130 if(minY > bbox.y){
131 minY = bbox.y;
132 }
133 if(maxX < bbox.x + bbox.width){
134 maxX = bbox.x + bbox.width;
135 }
136 if(maxY < bbox.y + bbox.height){
137 maxY = bbox.y + bbox.height;
138 }
139 }
140 return {x: minX, y:minY, w: maxX-minX, h: maxY-minY};
141 },
142
143 refreshRendering: function(){
144 if(this._widgetBox.w <= 0 || this._widgetBox.h <= 0){
145 return;
146 }
147 var i;
148 if(this._baseParts){
149 for(i = 0; i < this._baseParts.length; i++){
150 this._baseParts[i].width = this._widgetBox.w;
151 this._baseParts[i].height = this._widgetBox.h;
152 this._elementsRenderers[this._baseParts[i]._name] = this._baseParts[i].refreshRendering();
153 }
154 }
155
156 if(this._leadingParts){
157 for(i = 0; i < this._leadingParts.length; i++){
158 this._elementsRenderers[this._leadingParts[i]._name] = this._leadingParts[i].refreshRendering();
159 }
160 }
161
162 if(this._trailingParts){
163 for(i = 0; i < this._trailingParts.length; i++){
164 this._elementsRenderers[this._trailingParts[i]._name] = this._trailingParts[i].refreshRendering();
165 }
166 }
167
168 var leadingBoundingBox = this._computeArrayBoundingBox(this._leadingParts);
169 var trailingBoundingBox = this._computeArrayBoundingBox(this._trailingParts);
170 var middleBoundingBox = {};
171
172 if(this.orientation == "horizontal"){
173 middleBoundingBox.x = leadingBoundingBox.x + leadingBoundingBox.w;
174 middleBoundingBox.y = 0;
175 middleBoundingBox.w = this._widgetBox.w - leadingBoundingBox.w - trailingBoundingBox.w;
176 middleBoundingBox.h = this._widgetBox.h;
177 }else{
178 middleBoundingBox.x = 0;
179 middleBoundingBox.y = leadingBoundingBox.y + leadingBoundingBox.h;
180 middleBoundingBox.w = this._widgetBox.w;
181 middleBoundingBox.h = this._widgetBox.h - leadingBoundingBox.h - trailingBoundingBox.h;
182 }
183
184 this._layoutInfos = {
185 leading: leadingBoundingBox,
186 middle: middleBoundingBox,
187 trailing: trailingBoundingBox
188 };
189
190 // translates middle part
191 for(i = 0; i < this._middleParts.length; i++){
192 this._middleParts[i]._gfxGroup.setTransform([matrix.translate(middleBoundingBox.x, middleBoundingBox.y)]);
193 }
194
195 // translates trailing part
196 if(this._trailingParts){
197 for(i = 0; i < this._trailingParts.length; i++){
198 this._trailingParts[i]._gfxGroup.setTransform(matrix.translate(this._widgetBox.w - trailingBoundingBox.w, 0));
199 }
200 }
201
202 // Render remaining elements (scales, ...)
203 for(i = 0; i < this._classParts.length; i++){
204 this._elementsRenderers[this._classParts[i]._name] = this._classParts[i].refreshRendering();
205 }
206 }
207 })
208 });