Skip to content

Commit

Permalink
Allow registering identify exporters and attribute calculators via API
Browse files Browse the repository at this point in the history
  • Loading branch information
manisandro committed Apr 18, 2024
1 parent 6965b95 commit 1fa94de
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
49 changes: 37 additions & 12 deletions components/IdentifyViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class IdentifyViewer extends React.Component {
this.export({[layer]: [result]});
};
export = (json, clipboard = false) => {
const exporter = [...BuiltinExporters, ...this.props.customExporters].find(entry => entry.id === this.state.exportFormat);
const exporter = this.getExporters().find(entry => entry.id === this.state.exportFormat);
if (exporter) {
if (!this.props.exportGeometry) {
json = Object.entries(json).reduce((res, [layerId, features]) => (
Expand Down Expand Up @@ -422,9 +422,7 @@ class IdentifyViewer extends React.Component {
);
});
}
if (this.props.attributeCalculator) {
rows = rows.concat(this.props.attributeCalculator(layer, result));
}
rows.push(...this.computeExtraAttributes(layer, result));
if (featureReportTemplate) {
rows = rows.concat(
<tr key="__featurereport">
Expand All @@ -450,7 +448,7 @@ class IdentifyViewer extends React.Component {
extraattribs = (
<div className="identify-result-box">
<table className="attribute-list"><tbody>
{this.props.attributeCalculator ? this.props.attributeCalculator(layer, result) : null}
{this.computeExtraAttributes(layer, result)}
{featureReportTemplate ? (
<tr>
<td className={"identify-attr-title " + this.props.longAttributesDisplay}>
Expand Down Expand Up @@ -528,7 +526,8 @@ class IdentifyViewer extends React.Component {
);
}
// "el.style.background='inherit'": HACK to trigger an additional repaint, since Safari/Chrome on iOS render the element cut off the first time
const clipboardExportDisabled = ([...BuiltinExporters, ...this.props.customExporters].find(entry => entry.id === this.state.exportFormat) || {}).allowClipboard !== true;
const exporters = this.getExporters();
const clipboardExportDisabled = exporters.find(entry => entry.id === this.state.exportFormat)?.allowClipboard !== true;
return (
<div className="identify-body" ref={el => { if (el) el.style.background = 'inherit'; } }>
{body}
Expand All @@ -537,12 +536,7 @@ class IdentifyViewer extends React.Component {
<span className="identify-buttonbox-spacer" />
<span>{LocaleUtils.tr("identify.exportformat")}&nbsp;</span>
<select className="combo identify-export-format" onChange={ev => this.setState({exportFormat: ev.target.value})} value={this.state.exportFormat}>
{Object.values(BuiltinExporters).filter(entry => {
return !Array.isArray(this.props.enableExport) || this.props.enableExport.includes(entry.id);
}).map(entry => (
<option key={entry.id} value={entry.id}>{entry.title ?? LocaleUtils.tr(entry.titleMsgId)}</option>
))}
{Object.values(this.props.customExporters).filter(entry => {
{exporters.filter(entry => {
return !Array.isArray(this.props.enableExport) || this.props.enableExport.includes(entry.id);
}).map(entry => (
<option key={entry.id} value={entry.id}>{entry.title ?? LocaleUtils.tr(entry.titleMsgId)}</option>
Expand All @@ -555,6 +549,37 @@ class IdentifyViewer extends React.Component {
</div>
);
}
computeExtraAttributes = (layer, result) => {
const rows = [];
Object.values(window.qwc2.__attributeCalculators || {}).forEach((calc, idx) => {
const row = calc(layer, result);
if (row.length === 2) {
rows.push((
<tr key={"custom-attr-" + idx}>
<td className="identify-attr-title"><i>{row[0]}</i></td>
<td className="identify-attr-value">{row[1]}</td>
</tr>
));
} else if (row.length === 1) {
rows.push((
<tr key={"custom-attr-" + idx}>
<td colSpan="2">{row[0]}</td>
</tr>
));
}
});
if (this.props.attributeCalculator) {
rows.push(...this.props.attributeCalculator(layer, result));
}
return rows;
};
getExporters = () => {
return [
...BuiltinExporters,
...this.props.customExporters,
...Object.values(window.qwc2.__identifyExportes || [])
];
};
setIframeContent = (iframe, html) => {
if (iframe.getAttribute("identify-content-set")) {
return;
Expand Down
2 changes: 1 addition & 1 deletion components/PluginsContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PluginsContainer extends React.Component {
renderPlugins = (pluginsConfig) => {
const allPlugins = {
...this.props.plugins,
...(window.qwc2?.customPlugins ?? {})
...(window.qwc2?.__customPlugins ?? {})
};
return pluginsConfig.map((pluginConf, idx) => {
const Plugin = allPlugins[pluginConf.name + "Plugin"];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qwc2",
"version": "2024.04.16-master",
"version": "2024.04.18-master",
"description": "QGIS Web Client 2 core",
"author": "Sourcepole AG",
"license": "BSD-2-Clause",
Expand Down
25 changes: 22 additions & 3 deletions plugins/API.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,11 @@ import VectorLayerUtils from '../utils/VectorLayerUtils';
*/
class API extends React.Component {
componentDidMount() {
window.qwc2 = {};
window.qwc2.customPlugins = {};
window.qwc2 = {
__customPlugins: {},
__attributeCalculators: {},
__identifyExportes: {}
};
// Auto-binded functions
for (const prop of Object.keys(this.props)) {
window.qwc2[prop] = this.props[prop];
Expand All @@ -155,6 +158,10 @@ class API extends React.Component {
window.qwc2.LayerRole = LayerRole;
window.qwc2.addPlugin = this.addPlugin;
window.qwc2.removePlugin = this.removePlugin;
window.qwc2.addIdentifyAttributeCalculator = this.addIdentifyAttributeCalculator;
window.qwc2.removeIdentifyAttributeCalculator = this.removeIdentifyAttributeCalculator;
window.qwc2.addIdentifyExporter = this.addIdentifyExporter;
window.qwc2.removeIdentifyExporter = this.removeIdentifyExporter;
window.qwc2.addExternalLayer = this.addExternalLayer;
window.qwc2.drawScratch = this.drawScratch;
window.qwc2.drawGeometry = this.drawGeometry;
Expand Down Expand Up @@ -251,7 +258,19 @@ class API extends React.Component {
};
removePlugin = (name) => {
this.props.unregisterCustomPlugin(name);
delete window.qwc2.customPlugins[name];
delete window.qwc2.__customPlugins[name];
};
addIdentifyAttributeCalculator = (name, calcFunc) => {
window.qwc2.__attributeCalculators[name] = calcFunc;
};
removeIdentifyAttributeCalculator = (name) => {
delete window.qwc2.__attributeCalculators[name];
};
addIdentifyExporter = (name, exporterConfig) => {
window.qwc2.__identifyExportes[name] = exporterConfig;
};
removeIdentifyExporter = (name) => {
delete window.qwc2.__identifyExportes[name];
};
addExternalLayer = (resource, beforeLayerName = null, sublayers = true) => {
const params = LayerUtils.splitLayerUrlParam(resource);
Expand Down

0 comments on commit 1fa94de

Please sign in to comment.