@@ -4,133 +4,111 @@ import { faCopyright, faInfo } from '@fortawesome/free-solid-svg-icons';
44import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
55import { WmsLayer } from '@terrestris/react-util/dist/Util/typeUtils' ;
66import { Checkbox , Tooltip } from 'antd' ;
7+ import { FrameState } from 'ol/Map' ;
78import { Attribution as OlAttribution } from 'ol/source/Source' ;
8- import * as React from 'react' ;
9+ import React , { useEffect , useState } from 'react' ;
910
10- interface AddWmsLayerEntryProps {
11+ export type AddWmsLayerEntryProps = {
12+ /**
13+ * Object containing layer information
14+ */
15+ wmsLayer : WmsLayer ;
1116 /**
1217 * Function returning a span with the textual representation of this layer
1318 * Default: Title of the layer and its abstract (if available)
1419 */
15- layerTextTemplateFn : ( layer : WmsLayer ) => React . ReactNode ;
20+ layerTextTemplateFn ? : ( layer : WmsLayer ) => JSX . Element ;
1621 /**
1722 * Optional text to be shown in Tooltip for a layer that can be queried
1823 */
19- layerQueryableText : string ;
24+ layerQueryableText ? : string ;
2025 /**
2126 * ARIA label for the icon which indicates that the layer has copyright / attribution
2227 * information. This label increases the accessibility of the generated HTML. If not
2328 * set explicitly, a generic English label will be added. Make sure to translate this
2429 * to the page language if needed.
2530 */
26- ariaLabelCopyright : string ;
31+ ariaLabelCopyright ? : string ;
2732 /**
2833 * ARIA label for the icon which indicates that the layer is queryable. This label
2934 * increases the accessibility of the generated HTML. If not set explicitly, a generic
3035 * English label will be added. Make sure to translate this to the page language if
3136 * needed.
3237 */
33- ariaLabelQueryable : string ;
34- /**
35- * Object containing layer information
36- */
37- wmsLayer : WmsLayer ;
38- }
39-
40- interface AddWmsLayerEntryState {
41- copyright : OlAttribution | null ;
42- queryable : boolean ;
43- }
38+ ariaLabelQueryable ?: string ;
39+ } ;
4440
4541/**
4642 * Class representing a layer parsed from capabilities document.
47- * This componment is used in AddWmsPanel
43+ * This component is used in AddWmsPanel
4844 *
49- * @class AddWmsLayerEntry
50- * @extends React.Component
5145 */
52- export class AddWmsLayerEntry extends React . Component < AddWmsLayerEntryProps , AddWmsLayerEntryState > {
53-
54- /**
55- * The defaultProps.
56- */
57- static defaultProps = {
58- layerQueryableText : 'Layer is queryable' ,
59- ariaLabelCopyright : 'Icon indicating that attribution information for layer is available' ,
60- ariaLabelQueryable : 'Icon indicating that the layer is queryable' ,
61- layerTextTemplateFn : ( wmsLayer : WmsLayer ) => {
62- const title = wmsLayer . get ( 'title' ) ;
63- const abstract = wmsLayer . get ( 'abstract' ) ;
64- return abstract ?
65- < span > { `${ title } - ${ abstract } :` } </ span > :
66- < span > { `${ title } ` } </ span > ;
67- }
68- } ;
69-
70- /**
71- * Create the AddWmsLayerEntry.
72- *
73- * @constructs AddWmsLayerEntry
74- */
75- constructor ( props : AddWmsLayerEntryProps ) {
76- super ( props ) ;
77- // TODO: getAttributions is not @api and returns a function in v6.5
78- this . state = {
79- copyright : props . wmsLayer . getSource ( ) ?. getAttributions ( ) || null ,
80- queryable : props . wmsLayer . get ( 'queryable' )
81- } ;
46+ export const AddWmsLayerEntry : React . FC < AddWmsLayerEntryProps > = ( {
47+ wmsLayer,
48+ layerQueryableText = 'Layer is queryable' ,
49+ ariaLabelCopyright = 'Icon indicating that attribution information for layer is available' ,
50+ ariaLabelQueryable = 'Icon indicating that the layer is queryable' ,
51+ layerTextTemplateFn = ( layer : WmsLayer ) => {
52+ const title = layer . get ( 'title' ) ;
53+ const abstract = layer . get ( 'abstract' ) ;
54+ return abstract ?
55+ < span > { `${ title } - ${ abstract } :` } </ span > :
56+ < span > { `${ title } ` } </ span > ;
8257 }
58+ } ) => {
8359
84- /**
85- * The render function
86- */
87- render ( ) {
88- const {
89- wmsLayer,
90- layerTextTemplateFn,
91- layerQueryableText,
92- ariaLabelCopyright,
93- ariaLabelQueryable
94- } = this . props ;
95-
96- const {
97- copyright,
98- queryable
99- } = this . state ;
60+ const [ copyright , setCopyright ] = useState < string | null > ( null ) ;
61+ const [ queryable , setQueryable ] = useState < boolean > ( ) ;
10062
101- const title = wmsLayer . get ( 'title' ) ;
102- const layerTextSpan = layerTextTemplateFn ( wmsLayer ) ;
63+ useEffect ( ( ) => {
64+ if ( wmsLayer ) {
65+ if ( wmsLayer . getSource ( ) ?. getAttributions ( ) ) {
66+ const attributionsFn = wmsLayer . getSource ( ) ?. getAttributions ( ) as OlAttribution ;
67+ const attributions = ( attributionsFn ( { } as FrameState ) ) ;
68+ if ( attributions ?. length > 0 ) {
69+ if ( Array . isArray ( attributions ) ) {
70+ setCopyright ( attributions . join ( ', ' ) ) ;
71+ } else {
72+ setCopyright ( attributions ) ;
73+ }
74+ }
75+ }
76+ setQueryable ( ! ! wmsLayer . get ( 'queryable' ) ) ;
77+ }
78+ } , [ wmsLayer ] ) ;
10379
104- return (
105- < Checkbox value = { title } className = "add-wms-layer-checkbox-line" >
106- < div className = "add-wms-layer-entry" >
107- { layerTextSpan }
108- {
109- copyright ? (
80+ return (
81+ < Checkbox
82+ className = "add-wms-layer-checkbox-line"
83+ value = { wmsLayer . get ( 'title' ) }
84+ >
85+ < div className = "add-wms-layer-entry" >
86+ { layerTextTemplateFn ( wmsLayer ) }
87+ {
88+ copyright && (
89+ < Tooltip title = { copyright } >
11090 < FontAwesomeIcon
11191 className = "add-wms-add-info-icon attribution-info"
11292 icon = { faCopyright }
11393 aria-label = { ariaLabelCopyright }
11494 />
115- )
116- : null
117- }
118- {
119- queryable ? (
120- < Tooltip title = { layerQueryableText } >
121- < FontAwesomeIcon
122- className = "add-wms-add-info-icon queryable-info"
123- icon = { faInfo }
124- aria-label = { ariaLabelQueryable }
125- />
126- </ Tooltip >
127- )
128- : null
129- }
130- </ div >
131- </ Checkbox >
132- ) ;
133- }
134- }
95+ </ Tooltip >
96+ )
97+ }
98+ {
99+ queryable && (
100+ < Tooltip title = { layerQueryableText } >
101+ < FontAwesomeIcon
102+ className = "add-wms-add-info-icon queryable-info"
103+ icon = { faInfo }
104+ aria-label = { ariaLabelQueryable }
105+ />
106+ </ Tooltip >
107+ )
108+ }
109+ </ div >
110+ </ Checkbox >
111+ ) ;
112+ } ;
135113
136114export default AddWmsLayerEntry ;
0 commit comments