1 | define( |
---|
2 | ["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 | |
---|
28 | constructor : function(params){ |
---|
29 | // summary: |
---|
30 | // Constructs a new `dojox.geo.openlayers.WidgetFeature` |
---|
31 | // params: Object |
---|
32 | // The parameters describing the widget. |
---|
33 | this._params = params; |
---|
34 | }, |
---|
35 | |
---|
36 | setParameters : function(params){ |
---|
37 | // summary: |
---|
38 | // Sets the parameters describing the widget. |
---|
39 | // params: Object |
---|
40 | // The parameters describing the widget. |
---|
41 | this._params = params; |
---|
42 | }, |
---|
43 | |
---|
44 | getParameters : function(){ |
---|
45 | // summary: |
---|
46 | // Retreives the parameters describing the widget. |
---|
47 | // returns: Object |
---|
48 | // The parameters describing the widget. |
---|
49 | return this._params; |
---|
50 | }, |
---|
51 | |
---|
52 | _getWidget : function(){ |
---|
53 | // summary: |
---|
54 | // Creates, if necessary the widget and returns it; |
---|
55 | // tags: |
---|
56 | // private |
---|
57 | var params = this._params; |
---|
58 | |
---|
59 | if ((this._widget == null) && (params != null)) { |
---|
60 | var w = null; |
---|
61 | |
---|
62 | if (typeof (params.createWidget) == "function") { |
---|
63 | w = params.createWidget.call(this); |
---|
64 | } else if (params.dojoType) { |
---|
65 | dojo["require"](params.dojoType); |
---|
66 | var c = lang.getObject(params.dojoType); |
---|
67 | w = new c(params); |
---|
68 | } else if (params.dijitId) { |
---|
69 | w = dijit.byId(params.dijitId); |
---|
70 | } else if (params.widget) { |
---|
71 | w = params.widget; |
---|
72 | } |
---|
73 | |
---|
74 | if (w != null) { |
---|
75 | this._widget = w; |
---|
76 | if (typeof (w.startup) == "function") |
---|
77 | w.startup(); |
---|
78 | var n = w.domNode; |
---|
79 | if (n != null) |
---|
80 | html.style(n, { |
---|
81 | position : "absolute" |
---|
82 | }); |
---|
83 | } |
---|
84 | this._widget = w; |
---|
85 | } |
---|
86 | return this._widget; |
---|
87 | }, |
---|
88 | |
---|
89 | _getWidgetWidth : function(){ |
---|
90 | // summary: |
---|
91 | // gets the widget width |
---|
92 | // tags: |
---|
93 | // private |
---|
94 | var p = this._params; |
---|
95 | if (p.width) |
---|
96 | return p.width; |
---|
97 | var w = this._getWidget(); |
---|
98 | if (w) |
---|
99 | return html.style(w.domNode, "width"); |
---|
100 | return 10; |
---|
101 | }, |
---|
102 | |
---|
103 | _getWidgetHeight : function(){ |
---|
104 | // summary: |
---|
105 | // gets the widget height |
---|
106 | // tags: |
---|
107 | // private |
---|
108 | var p = this._params; |
---|
109 | if (p.height) |
---|
110 | return p.height; |
---|
111 | var w = this._getWidget(); |
---|
112 | if (w) |
---|
113 | return html.style(w.domNode, "height"); |
---|
114 | return 10; |
---|
115 | }, |
---|
116 | |
---|
117 | render : function(){ |
---|
118 | // summary: |
---|
119 | // renders the widget. |
---|
120 | // descrption: |
---|
121 | // Places the widget accordingly to longitude and latitude defined in parameters. |
---|
122 | // This function is called when the center of the maps or zoom factor changes. |
---|
123 | var layer = this.getLayer(); |
---|
124 | |
---|
125 | var widget = this._getWidget(); |
---|
126 | if (widget == null) |
---|
127 | return; |
---|
128 | var params = this._params; |
---|
129 | var lon = params.longitude; |
---|
130 | var lat = params.latitude; |
---|
131 | var from = this.getCoordinateSystem(); |
---|
132 | var map = layer.getDojoMap(); |
---|
133 | var p = map.transformXY(lon, lat, from); |
---|
134 | var a = this._getLocalXY(p); |
---|
135 | |
---|
136 | var width = this._getWidgetWidth(); |
---|
137 | var height = this._getWidgetHeight(); |
---|
138 | |
---|
139 | var x = a[0] - width / 2; |
---|
140 | var y = a[1] - height / 2; |
---|
141 | var dom = widget.domNode; |
---|
142 | |
---|
143 | var pa = layer.olLayer.div; |
---|
144 | if (dom.parentNode != pa) { |
---|
145 | if (dom.parentNode) |
---|
146 | dom.parentNode.removeChild(dom); |
---|
147 | pa.appendChild(dom); |
---|
148 | } |
---|
149 | this._updateWidgetPosition({ |
---|
150 | x : x, |
---|
151 | y : y, |
---|
152 | width : width, |
---|
153 | height : height |
---|
154 | }); |
---|
155 | }, |
---|
156 | |
---|
157 | _updateWidgetPosition : function(box){ |
---|
158 | // summary: |
---|
159 | // Places the widget with the computed x and y values |
---|
160 | // tags: |
---|
161 | // private |
---|
162 | // var box = this._params; |
---|
163 | |
---|
164 | var w = this._widget; |
---|
165 | var dom = w.domNode; |
---|
166 | |
---|
167 | html.style(dom, { |
---|
168 | position : "absolute", |
---|
169 | left : box.x + "px", |
---|
170 | top : box.y + "px", |
---|
171 | width : box.width + "px", |
---|
172 | height : box.height + "px" |
---|
173 | }); |
---|
174 | |
---|
175 | if (w.srcNodeRef) { |
---|
176 | html.style(w.srcNodeRef, { |
---|
177 | position : "absolute", |
---|
178 | left : box.x + "px", |
---|
179 | top : box.y + "px", |
---|
180 | width : box.width + "px", |
---|
181 | height : box.height + "px" |
---|
182 | }); |
---|
183 | } |
---|
184 | |
---|
185 | if (lang.isFunction(w.resize)) |
---|
186 | w.resize({ |
---|
187 | w : box.width, |
---|
188 | h : box.height |
---|
189 | }); |
---|
190 | }, |
---|
191 | |
---|
192 | remove : function(){ |
---|
193 | // summary: |
---|
194 | // removes this feature. |
---|
195 | // description: |
---|
196 | // Remove this feature by disconnecting the widget from the dom. |
---|
197 | var w = this.getWidget(); |
---|
198 | if (!w) |
---|
199 | return; |
---|
200 | var dom = w.domNode; |
---|
201 | if (dom.parentNode) |
---|
202 | dom.parentNode.removeChild(dom); |
---|
203 | } |
---|
204 | }); |
---|
205 | }); |
---|