forked from jacarma/SmartPopup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartPopup-Id.js
211 lines (190 loc) · 5.48 KB
/
SmartPopup-Id.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the Clear BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Control.js
* @requires OpenLayers/Control/SelectFeature.js
* @requires OpenLayers/Popup/FramedCloud.js
*/
/**
* Class: OpenLayers.Control.SmartPopup
*
* Inherits from:
* - <OpenLayers.Control>
*/
OpenLayers.Control.SmartPopup = OpenLayers.Class(OpenLayers.Control, {
/**
* APIProperty: autoActivate
* {Boolean} Activate the control when it is added to a map. Default is
* true.
*/
autoActivate: true,
/**
* Property: popup
*/
popup: null,
/**
* Property: selectControl
* {<OpenLayers.Control.SelectFeature>}
*/
selectControl: null,
/**
* Property: templates
* {Object} stores templates of this control's layers.
*/
templates: null,
/**
* Property: layers
* {Array(<OpenLayers.Layer.Vector>)} The layers this control will work on.
*/
layers: null,
/**
* Constructor: OpenLayers.Control.SmartPopup
*/
initialize: function() {
OpenLayers.Control.prototype.initialize.apply(this, []);
this.selectControl = new OpenLayers.Control.SelectFeature([], {
onSelect: this.selectFeature,
onUnselect: this.unselectFeature,
scope: this
});
this.templates = {};
this.layers = [];
},
/**
* APIMethod: destroy
*/
destroy: function() {
this.deactivate();
this.templates = null;
this.layers = null;
OpenLayers.Control.prototype.destroy.apply(this, arguments);
},
/**
* APIMethod: activate
*/
activate: function() {
if (OpenLayers.Control.prototype.activate.apply(this, arguments)) {
this.map.addControl(this.selectControl);
this.selectControl.activate();
this.map.events.on({
scope: this,
"zoomend": this.onZoomEnd,
"addlayer": this.onAddLayer,
"removelayer": this.onRemoveLayer
});
for (var i = 0, len = this.map.layers.length; i < len; i++) {
this.addLayer(this.map.layers[i]);
}
return true;
} else {
return false;
}
},
/**
* APIMethod: deactivate
*/
deactivate: function() {
if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) {
this.map.events.un({
scope: this,
"zoomend": this.onZoomEnd,
"addlayer": this.onAddLayer,
"removelayer": this.onRemoveLayer
});
this.destroyPopup();
return true;
} else {
return false;
}
},
/**
* Method: onAddLayer
*/
onAddLayer: function (evt) {
this.addLayer(evt.layer);
},
/**
* Method: onAddLayer
*/
onRemoveLayer: function (evt) {
this.removeLayer(evt.layer);
},
/**
* APIMethod: addLayer
*
* Parameters:
* layer - {<OpenLayers.Layer.Vector>}
* templateId - {String} If the layer has informed the property templateId
* this argument is optional.
*
* Returns:
* {Boolean} True if the layer was successfully added.
*/
addLayer: function (layer, templateId) {
templateId = templateId || layer.templateId;
if (templateId && !this.templates[layer.id] && layer instanceof OpenLayers.Layer.Vector) {
this.templates[layer.id] = document.getElementById(templateId).innerHTML;
this.layers.push(layer);
this.selectControl.setLayer(this.layers);
return true;
} else {
return false;
}
},
/**
* APIMethod: removeLayer
*/
removeLayer: function () {
// TODO
},
/**
* Method: selectFeature
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} the selected feature.
*/
selectFeature: function(feature) {
var featureAttributes = feature.attributes;
var html = this.templates[feature.layer.id].replace(
/%([^%]*)%/g,
function(a,b,c,d){return (featureAttributes[b])}
);
this.popup = new OpenLayers.Popup.FramedCloud(null,
feature.geometry.getBounds().getCenterLonLat(), null,
html,
null, false, null);
this.popup.maxSize=new OpenLayers.Size(300,500);
this.map.addPopup(this.popup);
},
/**
* Method: unselectFeature
* Called when the select feature control unselects a feature.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The unselected feature.
*/
unselectFeature: function(feature) {
this.destroyPopup();
},
/**
* Method: onZoomEnd
*/
onZoomEnd: function(feature) {
this.destroyPopup();
},
/**
* Method: destroyPopup
*/
destroyPopup: function () {
if (this.popup){
if (this.map) {
this.map.removePopup(this.popup);
}
this.popup.destroy();
this.popup = null;
}
},
CLASS_NAME: "OpenLayers.Control.SmartPopup"
});