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