WidgetFeature.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. define(
  2. "dojox/geo/openlayers/WidgetFeature", ["dojo/_base/kernel", "dojo/_base/declare", "dojo/_base/html", "dojo/_base/lang", "dojox/geo/openlayers/Feature"],
  3. function(dojo, declare, html, lang, Feature){
  4. /*=====
  5. var Feature = dojox.geo.openlayers.Feature;
  6. =====*/
  7. return declare("dojox.geo.openlayers.WidgetFeature", Feature, {
  8. // summary:
  9. // Wraps a Dojo widget, provide geolocalisation of the widget and interface
  10. // to Layer class.
  11. // description:
  12. // This class allows to add a widget in a `dojox.geo.openlayers.Layer`.
  13. // Parameters are passed to the constructor. These parameters describe the widget
  14. // and provide geo-localisation of this widget.
  15. // parameters can be:
  16. // * _createWidget_: Function for widget creation. Must return a `dijit._Widget`.
  17. // * _dojoType_: The class of a widget to create;
  18. // * _dijitId_: The digitId of an existing widget.
  19. // * _widget_: An already created widget.
  20. // * _width_: The width of the widget.
  21. // * _height_: The height of the widget.
  22. // * _longitude_: The longitude, in decimal degrees where to place the widget.
  23. // * _latitude_: The latitude, in decimal degrees where to place the widget.
  24. // You must define a least one widget retrieval parameter and the geo-localization parameters.
  25. _widget : null,
  26. _bbox : null,
  27. constructor : function(params){
  28. // summary:
  29. // Constructs a new `dojox.geo.openlayers.WidgetFeature`
  30. // params: Object
  31. // The parameters describing the widget.
  32. this._params = params;
  33. },
  34. setParameters : function(params){
  35. // summary:
  36. // Sets the parameters describing the widget.
  37. // params: Object
  38. // The parameters describing the widget.
  39. this._params = params;
  40. },
  41. getParameters : function(){
  42. // summary:
  43. // Retreives the parameters describing the widget.
  44. // returns: Object
  45. // The parameters describing the widget.
  46. return this._params;
  47. },
  48. _getWidget : function(){
  49. // summary:
  50. // Creates, if necessary the widget and returns it;
  51. // tags:
  52. // private
  53. var params = this._params;
  54. if ((this._widget == null) && (params != null)) {
  55. var w = null;
  56. if (typeof (params.createWidget) == "function") {
  57. w = params.createWidget.call(this);
  58. } else if (params.dojoType) {
  59. dojo["require"](params.dojoType);
  60. var c = lang.getObject(params.dojoType);
  61. w = new c(params);
  62. } else if (params.dijitId) {
  63. w = dijit.byId(params.dijitId);
  64. } else if (params.widget) {
  65. w = params.widget;
  66. }
  67. if (w != null) {
  68. this._widget = w;
  69. if (typeof (w.startup) == "function")
  70. w.startup();
  71. var n = w.domNode;
  72. if (n != null)
  73. html.style(n, {
  74. position : "absolute"
  75. });
  76. }
  77. this._widget = w;
  78. }
  79. return this._widget;
  80. },
  81. _getWidgetWidth : function(){
  82. // summary:
  83. // gets the widget width
  84. // tags:
  85. // private
  86. var p = this._params;
  87. if (p.width)
  88. return p.width;
  89. var w = this._getWidget();
  90. if (w)
  91. return html.style(w.domNode, "width");
  92. return 10;
  93. },
  94. _getWidgetHeight : function(){
  95. // summary:
  96. // gets the widget height
  97. // tags:
  98. // private
  99. var p = this._params;
  100. if (p.height)
  101. return p.height;
  102. var w = this._getWidget();
  103. if (w)
  104. return html.style(w.domNode, "height");
  105. return 10;
  106. },
  107. render : function(){
  108. // summary:
  109. // renders the widget.
  110. // descrption:
  111. // Places the widget accordingly to longitude and latitude defined in parameters.
  112. // This function is called when the center of the maps or zoom factor changes.
  113. var layer = this.getLayer();
  114. var widget = this._getWidget();
  115. if (widget == null)
  116. return;
  117. var params = this._params;
  118. var lon = params.longitude;
  119. var lat = params.latitude;
  120. var from = this.getCoordinateSystem();
  121. var map = layer.getDojoMap();
  122. var p = map.transformXY(lon, lat, from);
  123. var a = this._getLocalXY(p);
  124. var width = this._getWidgetWidth();
  125. var height = this._getWidgetHeight();
  126. var x = a[0] - width / 2;
  127. var y = a[1] - height / 2;
  128. var dom = widget.domNode;
  129. var pa = layer.olLayer.div;
  130. if (dom.parentNode != pa) {
  131. if (dom.parentNode)
  132. dom.parentNode.removeChild(dom);
  133. pa.appendChild(dom);
  134. }
  135. this._updateWidgetPosition({
  136. x : x,
  137. y : y,
  138. width : width,
  139. height : height
  140. });
  141. },
  142. _updateWidgetPosition : function(box){
  143. // summary:
  144. // Places the widget with the computed x and y values
  145. // tags:
  146. // private
  147. // var box = this._params;
  148. var w = this._widget;
  149. var dom = w.domNode;
  150. html.style(dom, {
  151. position : "absolute",
  152. left : box.x + "px",
  153. top : box.y + "px",
  154. width : box.width + "px",
  155. height : box.height + "px"
  156. });
  157. if (w.srcNodeRef) {
  158. html.style(w.srcNodeRef, {
  159. position : "absolute",
  160. left : box.x + "px",
  161. top : box.y + "px",
  162. width : box.width + "px",
  163. height : box.height + "px"
  164. });
  165. }
  166. if (lang.isFunction(w.resize))
  167. w.resize({
  168. w : box.width,
  169. h : box.height
  170. });
  171. },
  172. remove : function(){
  173. // summary:
  174. // removes this feature.
  175. // description:
  176. // Remove this feature by disconnecting the widget from the dom.
  177. var w = this.getWidget();
  178. if (!w)
  179. return;
  180. var dom = w.domNode;
  181. if (dom.parentNode)
  182. dom.parentNode.removeChild(dom);
  183. }
  184. });
  185. });