-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…0352) --------- Co-authored-by: Lorenzo Natali <lorenzo.natali@geosolutionsgroup.com>
- Loading branch information
1 parent
0774b09
commit 79f5729
Showing
17 changed files
with
471 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2024, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, { useState, useEffect } from 'react'; | ||
import trimEnd from 'lodash/trimEnd'; | ||
import max from 'lodash/max'; | ||
import axios from '../../../libs/ajax'; | ||
import Message from '../../../components/I18N/Message'; | ||
import Loader from '../../../components/misc/Loader'; | ||
import { getLayerIds } from '../../../utils/ArcGISUtils'; | ||
|
||
/** | ||
* ArcGISLegend renders legend from a MapServer or ImageServer service | ||
* @prop {object} node layer node options | ||
*/ | ||
function ArcGISLegend({ | ||
node = {} | ||
}) { | ||
const [legendData, setLegendData] = useState(null); | ||
const [error, setError] = useState(false); | ||
const legendUrl = node.url ? `${trimEnd(node.url, '/')}/legend` : ''; | ||
useEffect(() => { | ||
if (legendUrl) { | ||
axios.get(legendUrl, { | ||
params: { | ||
f: 'json' | ||
} | ||
}) | ||
.then(({ data }) => setLegendData(data)) | ||
.catch(() => setError(true)); | ||
} | ||
}, [legendUrl]); | ||
|
||
const supportedLayerIds = node.name !== undefined ? getLayerIds(node.name, node?.options?.layers || []) : []; | ||
|
||
const legendLayers = (legendData?.layers || []) | ||
.filter(({ layerId }) => node.name === undefined ? true : supportedLayerIds.includes(`${layerId}`)); | ||
const loading = !legendData && !error; | ||
return ( | ||
<div className="ms-arcgis-legend"> | ||
{legendLayers.map(({ legendGroups, legend, layerName }) => { | ||
const legendItems = legendGroups | ||
? legendGroups.map(legendGroup => legend.filter(item => item.groupId === legendGroup.id)).flat() | ||
: legend; | ||
const maxWidth = max(legendItems.map(item => item.width)); | ||
return (<> | ||
{legendLayers.length > 1 && <div className="ms-legend-title">{layerName}</div>} | ||
<ul className="ms-legend"> | ||
{legendItems.map((item, idx) => { | ||
return (<li key={idx} className="ms-legend-rule"> | ||
<div className="ms-legend-icon" style={{ minWidth: maxWidth }}> | ||
<img | ||
src={`data:${item.contentType};base64,${item.imageData}`} | ||
width={item.width} | ||
height={item.height} | ||
/> | ||
</div> | ||
{item.label} | ||
</li>); | ||
})} | ||
</ul> | ||
</>); | ||
})} | ||
{loading && <Loader size={12} style={{display: 'inline-block'}}/>} | ||
{error && <Message msgId="layerProperties.legenderror" />} | ||
</div> | ||
); | ||
} | ||
|
||
export default ArcGISLegend; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
web/client/plugins/TOC/components/__tests__/ArcGISLegend-test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2024, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import React from 'react'; | ||
|
||
import ReactDOM from 'react-dom'; | ||
import ArcGISLegend from '../ArcGISLegend'; | ||
import expect from 'expect'; | ||
import { act } from 'react-dom/test-utils'; | ||
import axios from '../../../../libs/ajax'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import { waitFor } from '@testing-library/react'; | ||
|
||
describe('ArcGISLegend', () => { | ||
let mockAxios; | ||
beforeEach((done) => { | ||
mockAxios = new MockAdapter(axios); | ||
document.body.innerHTML = '<div id="container"></div>'; | ||
setTimeout(done); | ||
}); | ||
|
||
afterEach((done) => { | ||
mockAxios.restore(); | ||
ReactDOM.unmountComponentAtNode(document.getElementById("container")); | ||
document.body.innerHTML = ''; | ||
setTimeout(done); | ||
}); | ||
it('should render with defaults', () => { | ||
act(() => { | ||
ReactDOM.render(<ArcGISLegend/>, document.getElementById("container")); | ||
}); | ||
expect(document.querySelector('.ms-arcgis-legend')).toBeTruthy(); | ||
}); | ||
it('should show the legend container when the legend request succeed', (done) => { | ||
mockAxios.onGet().reply(200, { layers: [{ layerId: 1, legend: [{ contentType: 'image/png', imageData: 'imageData', label: 'Label', width: 30, height: 20 }] }] }); | ||
act(() => { | ||
ReactDOM.render(<ArcGISLegend node={{ name: 1, url: '/rest/MapServer' }}/>, document.getElementById("container")); | ||
}); | ||
waitFor(() => expect(document.querySelector('.mapstore-small-size-loader')).toBeFalsy()) | ||
.then(() => { | ||
expect(document.querySelector('.ms-legend')).toBeTruthy(); | ||
const img = document.querySelector('.ms-legend img'); | ||
expect(img.getAttribute('src')).toBe('data:image/png;base64,imageData'); | ||
expect(img.getAttribute('width')).toBe('30'); | ||
expect(img.getAttribute('height')).toBe('20'); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
it('should show error message when the legend request fails', (done) => { | ||
mockAxios.onGet().reply(500); | ||
act(() => { | ||
ReactDOM.render(<ArcGISLegend node={{ url: '/rest/MapServer' }}/>, document.getElementById("container")); | ||
}); | ||
waitFor(() => expect(document.querySelector('.mapstore-small-size-loader')).toBeFalsy()) | ||
.then(() => { | ||
expect(document.querySelector('.ms-arcgis-legend').innerText).toBe('layerProperties.legenderror'); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.