Skip to content

Commit

Permalink
feature: featureinfo templates with alias and API functionality (orig…
Browse files Browse the repository at this point in the history
…o-map#1682)

* feature: aliases with featureinfo templates

Fixes origo-map#1681
Configured like:
  "attributeAlias": {
    "name":"Namn"
  }
in the config. Can be tested by removing the attribute setting for the origo cities layer in the standard map.

* Added API functionality

* Update map.js

mapConfig assigned with switch statement
  • Loading branch information
jokd authored Mar 7, 2023
1 parent 4252658 commit b8053b3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/featureinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import getFeatureInfo from './getfeatureinfo';
import replacer from './utils/replacer';
import SelectedItem from './models/SelectedItem';
import attachmentclient from './utils/attachmentclient';
import getAttributes, { getContent } from './getattributes';
import getAttributes, { getContent, featureinfotemplates } from './getattributes';
import relatedtables from './utils/relatedtables';

const styleTypes = StyleTypes();
Expand Down Expand Up @@ -694,7 +694,8 @@ const Featureinfo = function Featureinfo(options = {}) {
},
render,
showInfo,
showFeatureInfo
showFeatureInfo,
featureinfotemplates
});
};

Expand Down
26 changes: 23 additions & 3 deletions src/featureinfotemplates.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import defaultTemplate from './templates/featureinfotemplate';
import templateHelpers from './utils/templatehelpers';

const templates = {};
templates.default = defaultTemplate;

function featureinfotemplates(template, attributes) {
return templates[template](attributes);
function addFeatureinfotemplate(name, fn) {
templates[name] = fn;
return true;
}
export default featureinfotemplates;

function renameKeys(obj, newKeys) {
const keyValues = Object.keys(obj).map(key => {
const newKey = newKeys[key] || key;
return { [newKey]: obj[key] };
});
return Object.assign({}, ...keyValues);
}

function getFromTemplate(template, featureAttributes, attributeAlias) {
const attributes = featureAttributes;
if (attributes.url) {
attributes.url = `<a href="${attributes.url}" target="_blank">${attributes.url}</a>`;
}
const renamedObj = renameKeys(attributes, attributeAlias);
return templates[template](renamedObj);
}

export default { getFromTemplate, addFeatureinfotemplate, templateHelpers };
11 changes: 5 additions & 6 deletions src/getattributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ function getAttributes(feature, layer, map) {
featureinfoElement.appendChild(ulList);
const attributes = feature.getProperties();
const geometryName = feature.getGeometryName();
const attributeAlias = map.get('mapConfig').attributeAlias || [];
delete attributes[geometryName];
let content;
let attribute;
Expand All @@ -207,7 +208,7 @@ function getAttributes(feature, layer, map) {
// If attributes is string then use template named with the string
if (typeof layerAttributes === 'string') {
// Use attributes with the template
const li = featureinfotemplates(layerAttributes, attributes);
const li = featureinfotemplates.getFromTemplate(layerAttributes, attributes, attributeAlias);
const templateList = document.createElement('ul');
featureinfoElement.appendChild(templateList);
templateList.innerHTML = li;
Expand All @@ -216,7 +217,7 @@ function getAttributes(feature, layer, map) {
attribute = layer.get('attributes')[i];
val = '';
if (attribute.template) {
const li = featureinfotemplates(attribute.template, attributes);
const li = featureinfotemplates.getFromTemplate(attribute.template, attributes, attributeAlias);
const templateList = document.createElement('ul');
featureinfoElement.appendChild(templateList);
templateList.innerHTML = li;
Expand Down Expand Up @@ -244,7 +245,7 @@ function getAttributes(feature, layer, map) {
}
} else {
// Use attributes with the template
const li = featureinfotemplates('default', attributes);
const li = featureinfotemplates.getFromTemplate('default', attributes, attributeAlias);
const templateList = document.createElement('ul');
featureinfoElement.appendChild(templateList);
templateList.innerHTML = li;
Expand All @@ -253,7 +254,5 @@ function getAttributes(feature, layer, map) {
return content;
}

// export { getAttributes as default, getContent };

export default getAttributes;
export { getContent };
export { getContent, featureinfotemplates };
18 changes: 17 additions & 1 deletion src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,29 @@ import mapInteractions from './mapinteractions';

const Map = (options = {}) => {
const interactions = mapInteractions({ target: options.target, mapInteractions: options.pageSettings && options.pageSettings.mapInteractions ? options.pageSettings.mapInteractions : {} });
const mapConfig = {};
const keys = Object.keys(options);
keys.forEach((key) => {
switch (key) {
case 'controls':
case 'defaultControls':
case 'groups':
case 'layers':
case 'source':
case 'styles':
break;
default:
mapConfig[key] = options[key];
}
});

const mapOptions = Object.assign(options, { interactions });
delete mapOptions.layers;
mapOptions.controls = [];

const view = new OlView(options);
const map = new OlMap(Object.assign(mapOptions, { view }));

map.set('mapConfig', mapConfig);
return map;
};

Expand Down

0 comments on commit b8053b3

Please sign in to comment.