Permalink
Browse files

Use the "fid" of the featureContext instead of: fid || id.

  • Loading branch information...
1 parent 68e0997 commit 3292db369fb238991eb917aba409df73c5eca182 @jorix committed Dec 22, 2012
Showing with 85 additions and 103 deletions.
  1. +85 −103 lib/FeaturePopups.js
View
@@ -662,10 +662,6 @@ OpenLayers.Control.FeaturePopups = OpenLayers.Class(OpenLayers.Control, {
* combination of the layer.id+feature.id and can be used only as an
* html attribute.
*
- * It is convenient to use ${showPopup(fid)} instead of ${showPopup()}
- * when the layer features have fid, this ensures that the popups from
- * a list is still shown after zoom, even if a BBOX strategies is used.
- *
* Parameters:
* layer - {<OpenLayers.Layer.Vector>}
* options - {Object} Optional
@@ -676,13 +672,25 @@ OpenLayers.Control.FeaturePopups = OpenLayers.Class(OpenLayers.Control, {
* instead of values of context used by templates `list` and
* `hoverList`. If 'undefined' key exists their value will be
* used instead of text 'undefined'.
- * featureContext - {Object} Contains the keys with the values that were
- * used instead of values of feature properties (used by
- * templates: single, item, hover, hoverItem) If 'undefined' key exists
- * their value will be used instead of text 'undefined'.
+ * featureContext - {Object} Contains the keys with the values --could be a
+ * function or {string}--, the resulting value is used instead of values
+ * of feature property with the same name, if "fid" key is declared must
+ * be a function that returns unique values for each layer features.
+ * (used by templates: single, item, hover, hoverItem) If 'undefined'
+ * key exists their value will be used instead of text 'undefined'.
* eventListeners - {Object} This object will be registered with
* <OpenLayers.Events.on>, default scope is the control.
*
+ * *NOTE*: If the features of the layer may have an *"fid" duplicate* the key
+ * "fid" of "featureContext" *should be declared*, e.g. as
+ * (code)
+ * ... },
+ * featureContext: {
+ * fid: function(feature) { return feature.id; },
+ * ...
+ * }, ...
+ * (end)
+ *
* Valid templates:
* single - {String || Function} template used to show a single feature.
* list - {String || Function} template used to show selected
@@ -849,13 +857,12 @@ OpenLayers.Control.FeaturePopups = OpenLayers.Class(OpenLayers.Control, {
*
* Parameters:
* layerId - {String} id of the layer of selected feature.
- * featureId - {String} id of the feature.
- * idName - {String} Name of id: "id" or "fid".
+ * featureId - {String} id of the feature, see featureContext at <addLayer>.
*/
- showSingleFeatureById: function(layerId, featureId, idName) {
+ showSingleFeatureById: function(layerId, featureId) {
var layerObj = this.layerObjs[layerId];
if (layerObj) {
- layerObj.showSingleFeatureById(featureId, idName);
+ layerObj.showSingleFeatureById(featureId);
} else {
var popupObj = this.popupObjs.listItem;
popupObj && popupObj.clear();
@@ -1441,8 +1448,8 @@ OpenLayers.Control.FeaturePopups.Popup = OpenLayers.Class({
var elem = OpenLayers.Event.element(evt);
if (elem.id) {
var ids = elem.id.split('-');
- if (ids.length >= 3) {
- this.showSingleFeatureById(ids[1], ids[3], ids[2]);
+ if (ids.length >= 2) {
+ this.showSingleFeatureById(ids[1], ids[2]);
OpenLayers.Event.stop(evt);
}
}
@@ -1528,7 +1535,7 @@ OpenLayers.Control.FeaturePopups.Popup = OpenLayers.Class({
}
};
if (_relatedPopup.calculateRelativePosition) {
- _relatedPopup.calculateRelativePosition = function () {
+ _relatedPopup.calculateRelativePosition = function() {
return _relpopRelPosition;
};
}
@@ -1629,19 +1636,19 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
/**
* APIProperty: templates
- * {Object} Set of templates, see <FeaturePopups.AddLayer>
+ * {Object} Set of templates, see <FeaturePopups.addLayer>
*/
templates: null,
/**
* APIProperty: featureContext
- * {Object} See <FeaturePopups.AddLayer>
+ * {Object} See <FeaturePopups.addLayer>
*/
featureContext: null,
/**
* APIProperty: listContext
- * {Object} See <FeaturePopups.AddLayer>
+ * {Object} See <FeaturePopups.addLayer>
*/
listContext: null,
@@ -1745,7 +1752,7 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
* {RegEx} Used to activate events in the html elements to show individual
* popup.
*/
- regExpShow: /\$\{showPopup\((|id|fid)\)(\w*)\}/g,
+ regExpShow: /\$\{showPopup\(\w*\)\w*\}/g,
/**
* Property: regExpAttributes
@@ -1811,6 +1818,29 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
this.layerListeners['featuresremoved'] = this.onFeaturesremoved;
}
+ // fid by feature context
+ var getFid = this.featureContext && this.featureContext.fid;
+ if (getFid) {
+ this.getFeatureId = getFid;
+ } else {
+ /**
+ * Function: getFeatureId
+ * Internal use!, see "featureContext" at <FeaturePopups.addLayer>
+ * to know how to use "id" or "fid" of features.
+ *
+ * Parameters:
+ * feature - {OpenLayers.Feature.Vector}
+ *
+ * Returns:
+ * {String} A unique identifier of the feature within the layer.
+ */
+ this.getFeatureId = function(feature) {
+ return feature.fid || feature.id;
+ }
+ this.featureContext = OpenLayers.Util.extend(
+ this.featureContext, {fid: this.getFeatureId});
+ }
+
// Renderer of templates
// ---------------
// private vars
@@ -1895,15 +1925,14 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
*/
prepareTemplate: function(template) {
if (typeof template == 'string') {
- var subId = 0,
- layerId = this.layer.id;
+ var _subId = 0,
+ _layerId = this.layer.id;
template = template.replace(
this.regExpShow,
- function(a, idName) {
- subId++;
- idName = idName || 'id';
- return 'id="showPopup-' + layerId + '-' +
- idName + '-${' + idName + '}-' + subId + '"';
+ function(a) {
+ _subId++;
+ return 'id="showPopup-' + _layerId +
+ '-${fid}-' + _subId + '"';
}
);
template = template.replace(
@@ -2090,22 +2119,13 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
*/
storeAsSelected: function(feature) {
var layerId = feature.layer.id;
- var savedSF = this.selection,
- fid = feature.fid;
- if (fid) {
- savedSF[fid] = true;
- } else if (feature.cluster) {
+ var savedSF = this.selection;
+ if (feature.cluster) {
for (var i = 0 , len = feature.cluster.length; i < len; i++) {
- var cFeature = feature.cluster[i];
- var fidfid = cFeature.fid;
- if (fidfid) {
- savedSF[fidfid] = true;
- } else {
- savedSF[cFeature.id] = true;
- }
+ savedSF[this.getFeatureId(feature.cluster[i])] = true;
}
} else {
- savedSF[feature.id] = true;
+ savedSF[this.getFeatureId(feature)] = true;
}
},
@@ -2123,22 +2143,14 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
var savedSF = this.selection,
feature = evt.feature;
if (savedSF) {
- var fid = feature.fid;
- if (fid) {
- delete savedSF[fid];
- } else if (feature.cluster) {
+ if (feature.cluster) {
for (var i = 0, len = feature.cluster.length;
i < len; i++) {
- var cFeature = feature.cluster[i];
- var fidfid = cFeature.fid;
- if (fidfid) {
- delete savedSF[fidfid];
- } else {
- delete savedSF[cFeature.id];
- }
+ delete savedSF[
+ this.getFeatureId(feature.cluster[i])];
}
} else {
- delete savedSF[feature.id];
+ delete savedSF[this.getFeatureId(feature)];
}
}
}
@@ -2227,7 +2239,7 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
!_handlerFeature.lastFeature.layer) {
_replaceLastFeature = true;
}
- var select = function (feature) {
+ var select = function(feature) {
selectCtl.select(feature);
if (_replaceLastFeature) {
_handlerFeature.lastFeature = feature;
@@ -2238,23 +2250,15 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
this.updatingSelection = true;
for (var i = 0 , len = features.length; i < len; i++) {
var feature = features[i];
- if (feature.fid && savedSF[feature.fid]) {
- select(feature);
- } else if (feature.cluster) {
+ if (feature.cluster) {
for (var ii = 0, lenlen = feature.cluster.length;
ii < lenlen; ii++) {
- var cFeature = feature.cluster[ii];
- if (cFeature.fid) {
- if (savedSF[cFeature.fid]) {
- select(feature);
- break;
- }
- } else if (savedSF[cFeature.id]) {
+ if (savedSF[this.getFeatureId(feature.cluster[ii])]) {
select(feature);
break;
}
}
- } else if (savedSF[feature.id]) {
+ } else if (savedSF[this.getFeatureId(feature)]) {
select(feature);
}
}
@@ -2275,15 +2279,9 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
layerFeatures = this.getSingleFeatures(layer.features);
// get hash
if (layer.getVisibility() && layer.map) {
- var feature,
- ids = [];
+ var ids = [];
for (var i = 0, len = layerFeatures.length; i < len; ++i) {
- feature = layerFeatures[i];
- if (feature.fid) {
- ids.push(feature.fid);
- } else {
- ids.push(feature.id);
- }
+ ids.push(this.getFeatureId(layerFeatures[i]));
}
featuresHash = ids.sort().join('\t');
} else {
@@ -2330,11 +2328,7 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
for (var ii = 0, llen = clusterFeatures.length;
ii < llen; ii++) {
var cFeature = clusterFeatures[ii];
- if (cFeature.fid) {
- if (savedSF[cFeature.fid]) {
- features.push(cFeature);
- }
- } else if (savedSF[cFeature.id]) {
+ if (savedSF[this.getFeatureId(cFeature)]) {
features.push(cFeature);
}
}
@@ -2479,23 +2473,15 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
for (var i = 0 , len = features.length; i < len; i++) {
var feature = features[i],
selected = false;
- if (feature.fid && savedSF[feature.fid]) {
- selected = true;
- } else if (feature.cluster) {
+ if (feature.cluster) {
for (var ii = 0, lenlen = feature.cluster.length;
ii < lenlen; ii++) {
- var cFeature = feature.cluster[ii];
- if (cFeature.fid) {
- if (savedSF[cFeature.fid]) {
- selected = true;
- break;
- }
- } else if (savedSF[cFeature.id]) {
+ if (savedSF[this.getFeatureId(feature.cluster[ii])]) {
selected = true;
break;
}
}
- } else if (savedSF[feature.id]) {
+ } else if (savedSF[this.getFeatureId(feature)]) {
selected = true;
}
if (selected) {
@@ -2555,23 +2541,15 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
for (var i = features.length - 1; i >= 0; i--) {
var feature = features[i],
selected = false;
- if (feature.fid && savedSF[feature.fid]) {
- selected = true;
- } else if (feature.cluster) {
+ if (feature.cluster) {
for (var ii = 0, lenlen = feature.cluster.length;
ii < lenlen; ii++) {
- var cFeature = feature.cluster[ii];
- if (cFeature.fid) {
- if (savedSF[cFeature.fid]) {
- selected = true;
- break;
- }
- } else if (savedSF[cFeature.id]) {
+ if (savedSF[this.getFeatureId(feature.cluster[ii])]) {
selected = true;
break;
}
}
- } else if (savedSF[feature.id]) {
+ } else if (savedSF[this.getFeatureId(feature)]) {
selected = true;
}
if (!selected) {
@@ -2590,19 +2568,20 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
/**
* APIMethod: showSingleFeatureById
+ * See featureContext at <FeaturePopups.addLayer> to know how to use "id" or
+ * "fid" of features.
*
* Parameters:
* featureId - {String} id of the feature.
- * idName - {String} Name of id: "id" or "fid".
*/
- showSingleFeatureById: function(featureId, idName) {
+ showSingleFeatureById: function(featureId) {
var popupObj = this.control.popupObjs.listItem;
if (!popupObj) { return; }
- idName = idName || 'id';
var clearPopup = true;
if (featureId) {
var i, len, feature,
+ found = false,
layer = this.layer,
features = layer.features;
for (i = 0, len = features.length; i < len; i++) {
@@ -2613,14 +2592,17 @@ OpenLayers.Control.FeaturePopups.Layer = OpenLayers.Class({
for (ii = 0, len2 = cFeature.cluster.length;
ii < len2; ii++) {
feature = cFeature.cluster[ii];
- if (feature[idName] == featureId) {
+ if (this.getFeatureId(feature) === featureId) {
+ found = true;
break;
}
}
+ } else {
+ found = this.getFeatureId(feature) === featureId;
}
// Don't try to show a cluster as a single feature,
// templates.single does not support it.
- if (feature[idName] == featureId && !feature.cluster) {
+ if (found && !feature.cluster) {
popupObj.clear();
var template = this.templates.single;
if (template) {

0 comments on commit 3292db3

Please sign in to comment.