summaryrefslogtreecommitdiff
path: root/js/dojo-1.7.2/dojox/geo/charting/Feature.js
blob: 4bfc0595bfff8424b9a53a3bf0df7e90972c08a9 (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
//>>built
define("dojox/geo/charting/Feature", ["dojo/_base/lang", "dojo/_base/declare","dojo/_base/array",
		"dojo/_base/html","dojo/dom","dojo/_base/event", "dojox/gfx/fx", "dojox/color"], 
  function(lang, declare,arr, html,dom, event, fx,color) {

return declare("dojox.geo.charting.Feature", null, {
	//	summary: 
	//		class to encapsulate a map element.
	//	tags:
	//		private

	_isZoomIn: false,
	isSelected: false,
	markerText:null,


	constructor: function(parent, name, shapeData){
		//	summary: 
		//		constructs a new Feature.
		//	tags:
		//		private
		this.id = name;
		this.shape = parent.mapObj.createGroup();
		this.parent = parent;
		this.mapObj = parent.mapObj;
		this._bbox = shapeData.bbox;
		this._center = shapeData.center;
		//TODO: fill color would be defined by charting data and legend
//		this._highlightFill = ["#FFCE52", "#CE6342", "#63A584"][Math.floor(Math.random() * 3)];
		this._defaultFill = parent.defaultColor;
		this._highlightFill = parent.highlightColor;
		this._defaultStroke = {
			width: this._normalizeStrokeWeight(.5),
			color: "white"
		};
		
		var shapes = (lang.isArray(shapeData.shape[0])) ? shapeData.shape : [shapeData.shape];
		arr.forEach(shapes, function(points){
			this.shape.createPolyline(points).setStroke(this._defaultStroke);
		}, this);
		this.unsetValue();
	},
	unsetValue:function(){
		//	summary: 
		//		clears the numeric value on this Feature object (removes color).
		
		this.value = null;
		this.unsetColor();
	},
	unsetColor:function(){
		this._defaultFill = this.parent.defaultColor;
		var col = new color.Color(this.parent.defaultColor).toHsl();
		col.l = 1.2 * col.l;
		this._highlightFill = color.fromHsl(col);
		this._setFillWith(this._defaultFill);
	},
	setValue:function(value){
		//	summary: 
		//		sets a numeric value on this Feature object (used together with series to apply a color).
		//	value:
		//		a number
		this.value = value;
		if (value == null) {
			this.unsetValue();
		} else {
			if (this.parent.series.length != 0) {
				for (var i = 0; i < this.parent.series.length; i++) {
					var range = this.parent.series[i];
					if ((value >= range.min) && (value < range.max)) {
						this._setFillWith(range.color);
						this._defaultFill = range.color;
						var col = new color.Color(range.color).toHsv();
						col.v = (col.v + 20);
						this._highlightFill = color.fromHsv(col);
						return;
					}
				}
				// did not found a range : unset color
				this.unsetColor();
			}
		}
	},
	_setFillWith: function(color){
		var borders = (lang.isArray(this.shape.children)) ? this.shape.children : [this.shape.children];
		arr.forEach(borders, lang.hitch(this,function(item){
			if(this.parent.colorAnimationDuration > 0){
				var anim1 = fx.animateFill({
					shape: item,
					color: {
						start: item.getFill(),
						end: color
					},
					duration: this.parent.colorAnimationDuration
				});
				anim1.play();
			}else{
				item.setFill(color);
			}
		}));
	},
	_setStrokeWith: function(stroke){
		var borders = (lang.isArray(this.shape.children)) ? this.shape.children : [this.shape.children];
		arr.forEach(borders, function(item){
			item.setStroke({
				color: stroke.color,
				width: stroke.width,
				join: "round"
			});
		});
	},
	_normalizeStrokeWeight: function(weight){
		var matrix = this.shape._getRealMatrix();
		return (dojox.gfx.renderer != "vml")?weight/(this.shape._getRealMatrix()||{xx:1}).xx:weight;
	},
	_onmouseoverHandler: function(evt){
		this.parent.onFeatureOver(this);
		this._setFillWith(this._highlightFill);
		this.mapObj.marker.show(this.id,evt);
	},
	_onmouseoutHandler: function(){
		this._setFillWith(this._defaultFill);
		this.mapObj.marker.hide();
		html.style("mapZoomCursor", "display", "none");
	},
	_onmousemoveHandler: function(evt){
		if(this.mapObj.marker._needTooltipRefresh){
			this.mapObj.marker.show(this.id,evt);
		}
		if(this.isSelected){
			if (this.parent.enableFeatureZoom) {
				evt = event.fix(evt || window.event);
				html.style("mapZoomCursor", "left", evt.pageX + 12 + "px");
				html.style("mapZoomCursor", "top", evt.pageY + "px");
				html.byId("mapZoomCursor").className = this._isZoomIn ? "mapZoomOut":"mapZoomIn";
				html.style("mapZoomCursor", "display", "block");
			}else{
				html.style("mapZoomCursor", "display", "none");
			}
		}
	},
	_onclickHandler: function(evt){
		this.parent.onFeatureClick(this);
		if(!this.isSelected){
			this.parent.deselectAll();
			this.select(true);
			this._onmousemoveHandler(evt);
		}else if(this.parent.enableFeatureZoom){
			if(this._isZoomIn){
				this._zoomOut();
			}else{
				this._zoomIn();
			}
		}
	},
	
	select: function(selected) {
		if(selected){
			this.shape.moveToFront();
			this._setStrokeWith({color:"black",width:this._normalizeStrokeWeight(2)});
			this._setFillWith(this._highlightFill);
			this.isSelected = true;
			this.parent.selectedFeature = this;
		}else{
			this._setStrokeWith(this._defaultStroke);
			this._setFillWith(this._defaultFill);
			this.isSelected = false;
			this._isZoomIn = false;
		}
	},
	
	_zoomIn: function(){
		var marker = this.mapObj.marker;
		marker.hide();
		this.parent.fitToMapArea(this._bbox, 15,true,lang.hitch(this,function(){
			this._setStrokeWith({color:"black",width:this._normalizeStrokeWeight(2)});
			marker._needTooltipRefresh = true;
			this.parent.onZoomEnd(this);
		}));
		this._isZoomIn = true;
		dom.byId("mapZoomCursor").className = "";
	},
	_zoomOut: function(){
		var marker = this.mapObj.marker;
		marker.hide();
		this.parent.fitToMapContents(3,true,lang.hitch(this,function(){
			this._setStrokeWith({color:"black",width:this._normalizeStrokeWeight(2)});
			marker._needTooltipRefresh = true;
			this.parent.onZoomEnd(this);
		}));
		this._isZoomIn = false;
		dom.byId("mapZoomCursor").className = "";
	},
	
	init: function(){
		this.shape.id = this.id;
		this.tooltip = null;
	}
});
});