1 | define(["dojo/_base/lang", "dojo/_base/declare","dojo/_base/array", |
---|
2 | "dojo/_base/html","dojo/dom","dojo/_base/event", "dojox/gfx/fx", "dojox/color"], |
---|
3 | function(lang, declare,arr, html,dom, event, fx,color) { |
---|
4 | |
---|
5 | return declare("dojox.geo.charting.Feature", null, { |
---|
6 | // summary: |
---|
7 | // class to encapsulate a map element. |
---|
8 | // tags: |
---|
9 | // private |
---|
10 | |
---|
11 | _isZoomIn: false, |
---|
12 | isSelected: false, |
---|
13 | markerText:null, |
---|
14 | |
---|
15 | |
---|
16 | constructor: function(parent, name, shapeData){ |
---|
17 | // summary: |
---|
18 | // constructs a new Feature. |
---|
19 | // tags: |
---|
20 | // private |
---|
21 | this.id = name; |
---|
22 | this.shape = parent.mapObj.createGroup(); |
---|
23 | this.parent = parent; |
---|
24 | this.mapObj = parent.mapObj; |
---|
25 | this._bbox = shapeData.bbox; |
---|
26 | this._center = shapeData.center; |
---|
27 | //TODO: fill color would be defined by charting data and legend |
---|
28 | // this._highlightFill = ["#FFCE52", "#CE6342", "#63A584"][Math.floor(Math.random() * 3)]; |
---|
29 | this._defaultFill = parent.defaultColor; |
---|
30 | this._highlightFill = parent.highlightColor; |
---|
31 | this._defaultStroke = { |
---|
32 | width: this._normalizeStrokeWeight(.5), |
---|
33 | color: "white" |
---|
34 | }; |
---|
35 | |
---|
36 | var shapes = (lang.isArray(shapeData.shape[0])) ? shapeData.shape : [shapeData.shape]; |
---|
37 | arr.forEach(shapes, function(points){ |
---|
38 | this.shape.createPolyline(points).setStroke(this._defaultStroke); |
---|
39 | }, this); |
---|
40 | this.unsetValue(); |
---|
41 | }, |
---|
42 | unsetValue:function(){ |
---|
43 | // summary: |
---|
44 | // clears the numeric value on this Feature object (removes color). |
---|
45 | |
---|
46 | this.value = null; |
---|
47 | this.unsetColor(); |
---|
48 | }, |
---|
49 | unsetColor:function(){ |
---|
50 | this._defaultFill = this.parent.defaultColor; |
---|
51 | var col = new color.Color(this.parent.defaultColor).toHsl(); |
---|
52 | col.l = 1.2 * col.l; |
---|
53 | this._highlightFill = color.fromHsl(col); |
---|
54 | this._setFillWith(this._defaultFill); |
---|
55 | }, |
---|
56 | setValue:function(value){ |
---|
57 | // summary: |
---|
58 | // sets a numeric value on this Feature object (used together with series to apply a color). |
---|
59 | // value: |
---|
60 | // a number |
---|
61 | this.value = value; |
---|
62 | if (value == null) { |
---|
63 | this.unsetValue(); |
---|
64 | } else { |
---|
65 | if (this.parent.series.length != 0) { |
---|
66 | for (var i = 0; i < this.parent.series.length; i++) { |
---|
67 | var range = this.parent.series[i]; |
---|
68 | if ((value >= range.min) && (value < range.max)) { |
---|
69 | this._setFillWith(range.color); |
---|
70 | this._defaultFill = range.color; |
---|
71 | var col = new color.Color(range.color).toHsv(); |
---|
72 | col.v = (col.v + 20); |
---|
73 | this._highlightFill = color.fromHsv(col); |
---|
74 | return; |
---|
75 | } |
---|
76 | } |
---|
77 | // did not found a range : unset color |
---|
78 | this.unsetColor(); |
---|
79 | } |
---|
80 | } |
---|
81 | }, |
---|
82 | _setFillWith: function(color){ |
---|
83 | var borders = (lang.isArray(this.shape.children)) ? this.shape.children : [this.shape.children]; |
---|
84 | arr.forEach(borders, lang.hitch(this,function(item){ |
---|
85 | if(this.parent.colorAnimationDuration > 0){ |
---|
86 | var anim1 = fx.animateFill({ |
---|
87 | shape: item, |
---|
88 | color: { |
---|
89 | start: item.getFill(), |
---|
90 | end: color |
---|
91 | }, |
---|
92 | duration: this.parent.colorAnimationDuration |
---|
93 | }); |
---|
94 | anim1.play(); |
---|
95 | }else{ |
---|
96 | item.setFill(color); |
---|
97 | } |
---|
98 | })); |
---|
99 | }, |
---|
100 | _setStrokeWith: function(stroke){ |
---|
101 | var borders = (lang.isArray(this.shape.children)) ? this.shape.children : [this.shape.children]; |
---|
102 | arr.forEach(borders, function(item){ |
---|
103 | item.setStroke({ |
---|
104 | color: stroke.color, |
---|
105 | width: stroke.width, |
---|
106 | join: "round" |
---|
107 | }); |
---|
108 | }); |
---|
109 | }, |
---|
110 | _normalizeStrokeWeight: function(weight){ |
---|
111 | var matrix = this.shape._getRealMatrix(); |
---|
112 | return (dojox.gfx.renderer != "vml")?weight/(this.shape._getRealMatrix()||{xx:1}).xx:weight; |
---|
113 | }, |
---|
114 | _onmouseoverHandler: function(evt){ |
---|
115 | this.parent.onFeatureOver(this); |
---|
116 | this._setFillWith(this._highlightFill); |
---|
117 | this.mapObj.marker.show(this.id,evt); |
---|
118 | }, |
---|
119 | _onmouseoutHandler: function(){ |
---|
120 | this._setFillWith(this._defaultFill); |
---|
121 | this.mapObj.marker.hide(); |
---|
122 | html.style("mapZoomCursor", "display", "none"); |
---|
123 | }, |
---|
124 | _onmousemoveHandler: function(evt){ |
---|
125 | if(this.mapObj.marker._needTooltipRefresh){ |
---|
126 | this.mapObj.marker.show(this.id,evt); |
---|
127 | } |
---|
128 | if(this.isSelected){ |
---|
129 | if (this.parent.enableFeatureZoom) { |
---|
130 | evt = event.fix(evt || window.event); |
---|
131 | html.style("mapZoomCursor", "left", evt.pageX + 12 + "px"); |
---|
132 | html.style("mapZoomCursor", "top", evt.pageY + "px"); |
---|
133 | html.byId("mapZoomCursor").className = this._isZoomIn ? "mapZoomOut":"mapZoomIn"; |
---|
134 | html.style("mapZoomCursor", "display", "block"); |
---|
135 | }else{ |
---|
136 | html.style("mapZoomCursor", "display", "none"); |
---|
137 | } |
---|
138 | } |
---|
139 | }, |
---|
140 | _onclickHandler: function(evt){ |
---|
141 | this.parent.onFeatureClick(this); |
---|
142 | if(!this.isSelected){ |
---|
143 | this.parent.deselectAll(); |
---|
144 | this.select(true); |
---|
145 | this._onmousemoveHandler(evt); |
---|
146 | }else if(this.parent.enableFeatureZoom){ |
---|
147 | if(this._isZoomIn){ |
---|
148 | this._zoomOut(); |
---|
149 | }else{ |
---|
150 | this._zoomIn(); |
---|
151 | } |
---|
152 | } |
---|
153 | }, |
---|
154 | |
---|
155 | select: function(selected) { |
---|
156 | if(selected){ |
---|
157 | this.shape.moveToFront(); |
---|
158 | this._setStrokeWith({color:"black",width:this._normalizeStrokeWeight(2)}); |
---|
159 | this._setFillWith(this._highlightFill); |
---|
160 | this.isSelected = true; |
---|
161 | this.parent.selectedFeature = this; |
---|
162 | }else{ |
---|
163 | this._setStrokeWith(this._defaultStroke); |
---|
164 | this._setFillWith(this._defaultFill); |
---|
165 | this.isSelected = false; |
---|
166 | this._isZoomIn = false; |
---|
167 | } |
---|
168 | }, |
---|
169 | |
---|
170 | _zoomIn: function(){ |
---|
171 | var marker = this.mapObj.marker; |
---|
172 | marker.hide(); |
---|
173 | this.parent.fitToMapArea(this._bbox, 15,true,lang.hitch(this,function(){ |
---|
174 | this._setStrokeWith({color:"black",width:this._normalizeStrokeWeight(2)}); |
---|
175 | marker._needTooltipRefresh = true; |
---|
176 | this.parent.onZoomEnd(this); |
---|
177 | })); |
---|
178 | this._isZoomIn = true; |
---|
179 | dom.byId("mapZoomCursor").className = ""; |
---|
180 | }, |
---|
181 | _zoomOut: function(){ |
---|
182 | var marker = this.mapObj.marker; |
---|
183 | marker.hide(); |
---|
184 | this.parent.fitToMapContents(3,true,lang.hitch(this,function(){ |
---|
185 | this._setStrokeWith({color:"black",width:this._normalizeStrokeWeight(2)}); |
---|
186 | marker._needTooltipRefresh = true; |
---|
187 | this.parent.onZoomEnd(this); |
---|
188 | })); |
---|
189 | this._isZoomIn = false; |
---|
190 | dom.byId("mapZoomCursor").className = ""; |
---|
191 | }, |
---|
192 | |
---|
193 | init: function(){ |
---|
194 | this.shape.id = this.id; |
---|
195 | this.tooltip = null; |
---|
196 | } |
---|
197 | }); |
---|
198 | }); |
---|