/
feature-popups-plus.js
131 lines (128 loc) · 5.32 KB
/
feature-popups-plus.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
// Create control and add some layers
// ----------------------------------
// ** advanced use **
var framedCloudScrolable = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
displayClass: "olScrollable olPopup"
});
var fpControl = new OpenLayers.Control.FeaturePopups({
boxSelectionOptions: {},
// ** Options for the SelectFeature control to select **
selectOptions: {clickout: true},
// ** Close the window when displaying the detail of an item **
popupListItemOptions: {relatedToClear: ['list', 'single']},
// ** Don't use close box on popups
mode: OpenLayers.Control.FeaturePopups.DEFAULT &
~OpenLayers.Control.FeaturePopups.CLOSE_BOX,
// ** Allow to zoom with the scroll wheel when the mouse is in the single popup active area, but like all drugs can have side effects ;-) **
popupSingleOptions: {popupClass: framedCloudScrolable},
// ** Overwrites html of the list popups adding a vacuum <li> before each item **
popupListOptions: {eventListeners: {
"beforepopupdisplayed": function(e){
var html = [],
feature0 = null,
layerObj0 = null,
count = 0;
for (var i = 0, iLen = e.selection.length; i < iLen; i++) {
var sel = e.selection[i],
htmlAux = [],
layerObj = sel.layerObj;
for (var ii = 0, iiLen = sel.features.length; ii < iiLen; ii++) {
if (ii > 0 || iiLen === 1) { // remove the first feature to simulate a filter.
feature0 = sel.features[ii];
layerObj0 = layerObj;
count++;
htmlAux.push(
layerObj.renderTemplate(layerObj.templates.item, feature0)
);
}
}
html.push(layerObj.renderTemplate(
layerObj.templates.list, {
layer: sel.layer,
count: sel.features.length,
html: htmlAux.join('\n')
}
));
}
if (count === 1) {
layerObj0.control.popupObjs.single.showPopup({
layerObj: layerObj0,
layer: layerObj.layer,
feature: feature0
},
feature0.geometry.getBounds().getCenterLonLat(),
layerObj0.getSingleHtml(feature0).html,
true
);
layerObj0 = null;
feature0 = null;
return false;
}
e.html = html.join('\n');
}
}},
// ** Overwrites html of the hover list popups adding a vacuum <hr> before each item **
popupHoverListOptions: {eventListeners: {
"beforepopupdisplayed": function(e){
var htmlAux = [];
var sel = e.selection,
layerObj = sel.layerObj;
for (var ii = 0, iiLen = sel.features.length; ii < iiLen; ii++) {
if (true) {
htmlAux.push(
"<hr>" +
layerObj.renderTemplate(layerObj.templates.hoverItem, sel.features[ii])
);
}
}
e.html = layerObj.renderTemplate(
layerObj.templates.hoverList, {
layer: sel.layer,
count: sel.features.length,
html: htmlAux.join('\n')
}
);
}
}},
layers: [
[
// Uses: Templates for hover & select and safe selection
sundialsLayer, {templates: {
// hover: single & list
hover: '${.name}',
hoverList: '<b>${count}</b><br>${html}',
hoverItem: '${.name}<br>',
// select: single & list
single: '<div><h2>${.name}</h2>${.description}</div>',
item: '<li><a href=\"#\" ${showPopup()}>${.name}</a></li>'
}}], [
// Uses: Internationalized templates.
sprintersLayer, {templates: {
hover: '${.Name}',
single: '${i18n(\"Name\")}: ${.Name}<br>' +
'${i18n(\"Country\")}: ${.Country}<br>' +
'${i18n(\"City\")}: ${.City}<br>',
item: '<li><a href=\"#\" ${showPopup()}>${.Name}</a></li>'
}}], [
// Uses: Templates as functions (only from hover-single and select-list)
tasmaniaRoadsLayer, {templates: {
hover: function(feature) {
return 'Length: ' + Math.round(feature.geometry.getLength() / 10) / 100 + ' km';
},
item: function(feature) {
return '<li>' + Math.round(feature.geometry.getLength() / 10) / 100 + ' km</li>';
}
}}]
]
});
map.addControl(fpControl);
// Add a layer to the control using addLayer
// -----------------------------------------
fpControl.addLayer(
// Uses: Safe selection by "fid"
poisLayer, {templates: {
hover: '${.title}',
single: '<h2>${.title}</h2>${.description}',
item: '<li><a href=\"#\" ${showPopup(fid)}>${.title}</a></li>'
}}
);