MapPage.tsx 8.95 KB
Newer Older
1
import * as React from 'react';
2
import { connect } from 'react-redux';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
3
import { Link } from 'react-router-dom';
4
5
6
import { translate } from 'react-i18next';
import { withStyles } from '@material-ui/core/styles';
import { bindActionCreators } from 'redux';
7
import { loadAccessionsMapInfo } from 'accessions/actions/public';
8
import AccessionFilter from 'model/accession/AccessionFilter';
9
import Loading from 'ui/common/Loading';
10
import AccessionMapInfo from 'model/accession/AccessionMapInfo';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
11
import MapLayer from 'model/genesys/MapTileLayer';
12
import Button from '@material-ui/core/Button';
13
import Tabs, { Tab } from 'ui/common/Tabs';
14
import PrettyFilters from 'ui/common/filter/PrettyFilters';
Oleksii Savran's avatar
Oleksii Savran committed
15
import ButtonBar from 'ui/common/buttons/ButtonBar';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
16
17
import ContentLayout from 'ui/layout/ContentLayout';
import MapConfigSection from './c/MapConfigSection';
18

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
19
import AccessionService from 'service/genesys/AccessionService';
20
21
22

let Map;
let TileLayer;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
23
let Popup;
Oleksii Savran's avatar
Oleksii Savran committed
24
let Rectangle;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
25
26

const popupContentLimit = 11;
27

28
29
30
31
interface IMapPageProps {
    classes?: any;
    t?: any;

32
    apiUrl: string;
33
    mapInfo: AccessionMapInfo;
34
35
    currentTab: string;
    filterCode: string;
36
    loadAccessionsMapInfo: any;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
37
    mapLayers: MapLayer[];
38
39
40
41
42
43
44
45
46
47
48
}

const styles = (theme) => ({
    leafletContainer: {
        width: '100%',
        minHeight: '500px',
        height: 'calc(100vh - 186px)',
    },
});

class BrowsePage extends React.Component<IMapPageProps, any> {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
49
50
51

    private clickTimeout;

52
    protected static needs = [
53
54
      ({ params: { filterCode } }) => {
        return loadAccessionsMapInfo(filterCode || '');
55
56
57
      },
    ];

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
58
    public state = {
Oleksii Savran's avatar
Oleksii Savran committed
59
60
      clickLocation: [],
      searchBox: null,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
61
62
63
64
      geoData: [],
      otherCount: 0,
    };

65
66
67
68
69
    constructor(props, context) {
        super(props, context);
        if (typeof window !== 'undefined') {
            Map = require('react-leaflet').Map;
            TileLayer = require('react-leaflet').TileLayer;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
70
            Popup = require('react-leaflet').Popup;
Oleksii Savran's avatar
Oleksii Savran committed
71
            Rectangle = require('react-leaflet').Rectangle;
72
73
74
75
76
        }
    }


    public componentWillMount() {
77
78
79
80
81
82
83
      const { mapInfo, filterCode, loadAccessionsMapInfo } = this.props;
      // console.log(`Filter code for map ${filterCode} ?== ${mapInfo && mapInfo.filterCode}`, filterCode, mapInfo ? mapInfo.filterCode : 'No mapInfo');
      if (mapInfo && mapInfo.filterCode !== filterCode) {
        // console.log(`mapInfo.filterCode !== filterCode. updatingRoute`);
        loadAccessionsMapInfo(filterCode || '');
      } else if (!mapInfo) {
        loadAccessionsMapInfo(filterCode || '');
84
85
86
      }
    }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

    private onMapClick = (e) => {
        if (this.clickTimeout) {
            console.log('prevented');
            clearTimeout(this.clickTimeout);
            this.clickTimeout = null;
            return;
        }
        this.clickTimeout = setTimeout(() => {
            console.log('started');
            this.clickTimeout = null;
            const currentZoom = e.target._zoom;
            if (currentZoom < 4) {
                return;
            }

            const {mapInfo: {filter}} = this.props;

            const correctLng = Math.abs(e.latlng.lng) > 180 ? (360 + e.latlng.lng) % 180 : e.latlng.lng;

            console.log(e);

Oleksii Savran's avatar
Oleksii Savran committed
109
110
111
112
113
114
            const width = Math.pow(2, currentZoom - 2);
            console.log(`zoom=${currentZoom} width=${width} diff=${ 3 / width}`);
            const searchBounds: number[][] = [
                [ e.latlng.lat - (3 / width), correctLng - (3 / width) ],
                [ e.latlng.lat + (3 / width), correctLng + (3 / width) ],
            ];
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
115
116
117
118
            const filterWithGeo = {
                ...filter,
                geo: {
                    longitude: {
Oleksii Savran's avatar
Oleksii Savran committed
119
120
                        ge: searchBounds[0][1],
                        le: searchBounds[1][1],
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
121
122
                    },
                    latitude: {
Oleksii Savran's avatar
Oleksii Savran committed
123
124
                        ge: searchBounds[0][0],
                        le: searchBounds[1][0],
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
125
126
127
128
                    },
                },
            };
            AccessionService.geoJson(filterWithGeo, popupContentLimit)
Oleksii Savran's avatar
Oleksii Savran committed
129
                .then((res) => this.setState({clickLocation: [ e.latlng.lat,  e.latlng.lng], searchBox: searchBounds, geoData: res.geoJson, otherCount: res.otherCount}));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
130
131
132
        }, 200);
    }

133
134
135
136
137
    /// Wrap loadAccessionsMapInfo dispatch and fills the current sort selection
    protected myApplyFilters = (filters: AccessionFilter) => {
      const { loadAccessionsMapInfo } = this.props;

      loadAccessionsMapInfo(filters);
138
139
140
    }

    public render() {
Oleksii Savran's avatar
Oleksii Savran committed
141
        const { searchBox, geoData, otherCount } = this.state;
142
143
        const position = [30, 0];

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
144
        const { mapInfo, mapLayers, currentTab, classes, filterCode, t } = this.props;
145
146
147
148

        if (! mapInfo) {
          return <Loading />;
        }
149
150

        // const color = 'f00ba0';
151
        const layerUrl = `{s}/acn/tile/{z}/{x}/{y}?f=${filterCode ? filterCode : ''}`; // `&color=${color}`;
152
153

        return (
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
154
155
156
157
158
            <ContentLayout
              right={ <MapConfigSection/> }
              rightAlwaysCollapsible
              customHeaderHeight
            >
159
160
161
                <Tabs
                    tab={ currentTab }
                    actions={
Oleksii Savran's avatar
Oleksii Savran committed
162
                      <ButtonBar>
163
                        <span>
Matija Obreza's avatar
Matija Obreza committed
164
165
                          <form method="post" action="/proxy/api/v1/acn/downloadKml">
                            <input type="hidden" name="f" value={ filterCode } />
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
166
                            <Button type="submit">{ `${t('common:action.download')} ${t('accessions.public.p.map.kml')}` }</Button>
Matija Obreza's avatar
Matija Obreza committed
167
                          </form>
168
                        </span>
Oleksii Savran's avatar
Oleksii Savran committed
169
                      </ButtonBar>
170
171
                    }
                >
Viacheslav Pavlov's avatar
i18n    
Viacheslav Pavlov committed
172
173
174
                  <Tab name="data" to={ `/a/${filterCode || ''}` }>{ t('accessions.tab.data') }</Tab>
                  <Tab name="overview" to={ `/a/overview/${filterCode || ''}` }>{ t('accessions.tab.overview') }</Tab>
                  <Tab name="map" to={ `/a/map/${filterCode || '' }` }>{ t('accessions.tab.map') }</Tab>
175
176
177
                </Tabs>
                <PrettyFilters
                    prefix="accessions"
178
                    filterObj={ mapInfo && mapInfo.filter || {} }
179
180
181
                    onSubmit={ this.myApplyFilters }
                />
                <div className={ classes.leafletContainer }>
182
                { mapInfo && typeof window !== 'undefined' &&
183
                    <Map
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
184
                        onClick={ this.onMapClick }
185
                        center={ position }
186
187
                        zoom={ 3 } minZoom={ 2 } maxZoom={ 14 }
                        bounds={ mapInfo.bounds }>
188
                        <TileLayer
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
189
                            zIndex={ 0 }
190
191
192
193
                            opacity={ 0.50 }
                            attribution={ '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' }
                            url={ 'https://tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png' }
                        />
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
194
195
196

                        { mapLayers && mapLayers.filter((layer) => layer.enabled).map((layer, index) => <TileLayer zIndex={ index + 1 } key={ layer.name } { ...layer }/>) }

Matija Obreza's avatar
Matija Obreza committed
197
                        <TileLayer
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
198
                            zIndex={ mapLayers.length + 1 }
199
200
201
202
                            updateInterval={ 1000 }
                            updateWhenZooming={ false }
                            attribution="&amp;copy Accession localities from <a href=&quot;/&quot;>Genesys PGR</a>"
                            url={ layerUrl }
203
                            subdomains={ mapInfo.tileServers }
204
                        />
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
205
                        { geoData && geoData.length > 0 &&
Oleksii Savran's avatar
Oleksii Savran committed
206
207
208
209
                            <Rectangle
                              bounds={ searchBox }
                              ref={ (marker) => marker && marker.leafletElement.openPopup() }
                            >
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
210
211
                                <Popup open>
                                    <div>
212
                                        { geoData.map((feature, idx) => (<div key={ idx }><Link to={ `/a/${feature.properties.uuid}` }>{ `${feature.properties.accessionNumber} ${feature.properties.instCode}` }</Link></div>)) }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
213
214
215
                                        { otherCount > 0 && <div>{ t('accessions.public.p.map.andMore', {otherMore: otherCount}) }</div> }
                                    </div>
                                </Popup>
Oleksii Savran's avatar
Oleksii Savran committed
216
                            </Rectangle>
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
217
                        }
218
219
220
                    </Map>
                }
                </div>
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
221
            </ContentLayout>
222
223
224
225
226
        );
    }
}

const mapStateToProps = (state, ownProps) => ({
227
    mapInfo: state.accessions.public.mapInfo || undefined,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
228
    mapLayers: state.accessions.public.mapLayers,
229
230
231
232
233
    filterCode: ownProps.match.params.filterCode || '',
    currentTab: ownProps.match.params.tab || 'map', // current tab, or ownProps.location.pathname
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
234
    loadAccessionsMapInfo,
235
236
}, dispatch);

237
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(translate()(BrowsePage)));