Feature.js 5.7 KB

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