-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathFeatureTip.js
102 lines (92 loc) · 2.68 KB
/
FeatureTip.js
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
/**
* Copyright (c) 2008-2012 The Open Source Geospatial Foundation
*
* Published under the BSD license.
* See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text
* of the license.
*/
/** api: (define)
* module = GeoExt
* class = FeatureTip
* base_link = `Ext.Tip <http://dev.sencha.com/deploy/dev/docs/?class=Ext.Tip>`_
*/
Ext.namespace("GeoExt");
/** api: example
* Sample code to create a feature tip:
*
* .. code-block:: javascript
*
* var bogusMarkup = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
* var featureTip = new GeoExt.FeatureTip({
* location: feature,
* width: 100,
* map: mapPanel.map,
* html: bogusMarkup
* });
* featureTip.show();
*/
/** api: constructor
* .. class:: FeatureTip(config)
*
* Create a tip displaying at the location of a feature.
*/
GeoExt.FeatureTip = Ext.extend(Ext.Tip, {
/** api: config[map]
* ``OpenLayers.Map``
*/
map: null,
/** api: config[location]
* ``OpenLayers.Feature.Vector``
*/
location: null,
/** private: method[initComponent]
* Initializes the feature tip.
*/
initComponent: function() {
var centroid = this.location.geometry.getCentroid();
this.location = new OpenLayers.LonLat(centroid.x, centroid.y);
this.map.events.on({
"move" : this.show,
scope : this
});
GeoExt.FeatureTip.superclass.initComponent.call(this);
},
/** private: method[beforeDestroy]
* Cleanup events before destroying the feature tip.
*/
beforeDestroy: function() {
this.map.events.un({
"move" : this.show,
scope : this
});
GeoExt.FeatureTip.superclass.beforeDestroy.call(this);
},
/** private: method[getPosition]
* Get the position of the feature in pixel space.
*
* :returns: ``Array`` The position of the feature in pixel space or
* null if the feature is not visible in the map.
*/
getPosition: function() {
if (this.map.getExtent().containsLonLat(this.location)) {
var locationPx = this.map.getPixelFromLonLat(this.location),
mapBox = Ext.fly(this.map.div).getBox(true),
y = locationPx.y + mapBox.y,
x = locationPx.x + mapBox.x;
return [x, y];
} else {
return null;
}
},
/** api: method[show]
* Show the feature tip.
*/
show: function() {
var position = this.getPosition();
if (position !== null) {
this.showAt(position);
} else {
this.hide();
}
}
});