-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShare.jsx
130 lines (126 loc) · 5.29 KB
/
Share.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Copyright 2016-2021 Sourcepole AG
* 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 {connect} from 'react-redux';
import PropTypes from 'prop-types';
import url from 'url';
import {addMarker, removeMarker} from '../actions/layers';
import ShareSocials from '../components/share/ShareSocials';
import ShareQRCode from '../components/share/ShareQRCode';
import ShareLink from '../components/share/ShareLink';
import SideBar from '../components/SideBar';
import ToggleSwitch from '../components/widgets/ToggleSwitch';
import LocaleUtils from '../utils/LocaleUtils';
import CoordinatesUtils from '../utils/CoordinatesUtils';
import {generatePermaLink} from '../utils/PermaLinkUtils';
import './style/Share.css';
/**
* Share the current map as a URL/permalink.
*
* Compact permalinks will be generated if `permalinkServiceUrl` in `config.json` points to a `qwc-permalink-service`.
*/
class Share extends React.Component {
static propTypes = {
addMarker: PropTypes.func,
removeMarker: PropTypes.func,
/** Show the map URL. */
showLink: PropTypes.bool,
/** Show the QR code of the map URL. */
showQRCode: PropTypes.bool,
/** Show the social buttons. Either `true` or `false`to enable/disable all, or an array of specific buttons to display (possible choices: `email`, `facebook`, `twitter`, `linkedin`, `whatsapp`). */
showSocials: PropTypes.oneOfType([PropTypes.bool, PropTypes.arrayOf(PropTypes.string)]),
/** The side of the application on which to display the sidebar. */
side: PropTypes.string,
state: PropTypes.object
};
static defaultProps = {
showSocials: true,
showLink: true,
showQRCode: true,
side: 'right'
};
state = {
location: "",
expires: null,
pin: false
};
componentDidUpdate(prevProps, prevState) {
const isVisible = this.props.state.task.id === "Share";
const wasVisible = prevProps.state.task.id === "Share";
if (isVisible !== wasVisible || this.state.pin !== prevState.pin || this.props.state.map.center !== prevProps.state.map.center) {
if (isVisible && this.state.pin) {
this.props.addMarker('sharecenter', this.props.state.map.center, '', this.props.state.map.projection);
} else if (wasVisible) {
this.props.removeMarker('sharecenter');
}
}
if (isVisible !== wasVisible || this.props.state.map.center !== prevProps.state.map.center) {
this.setState({location: "", expires: null});
}
}
renderBody = () => {
let shareUrl = this.state.location || 'about:blank';
if (this.state.pin && this.state.location) {
const urlParts = url.parse(shareUrl, true);
urlParts.query.hc = 1;
if (!urlParts.query.c) {
const posCrs = urlParts.query.crs || this.props.state.map.projection;
const prec = CoordinatesUtils.getUnits(posCrs) === 'degrees' ? 4 : 0;
urlParts.query.c = this.props.state.map.center.map(x => x.toFixed(prec)).join(",");
}
delete urlParts.search;
shareUrl = url.format(urlParts);
}
const shareSocials = this.props.showSocials !== false ? <ShareSocials shareTitle={LocaleUtils.tr("share.shareTitle")} shareUrl={shareUrl} showSocials={this.props.showSocials}/> : null;
const shareLink = this.props.showLink ? <ShareLink shareUrl={shareUrl}/> : null;
const shareQRCode = this.props.showQRCode ? <ShareQRCode shareUrl={shareUrl}/> : null;
return (
<div>
<div className="share-option-pin">
<span>{LocaleUtils.tr("share.showpin")}</span>
<ToggleSwitch active={this.state.pin} onChange={active => this.setState({pin: active})} />
</div>
{!this.state.location ? (
<div className="share-reload-overlay">
<button className="button" onClick={this.refreshPermalink}>{LocaleUtils.tr("share.refresh")}</button>
</div>
) : (
<div className="share-body">
{shareSocials}
{shareLink}
{shareQRCode}
{this.state.expires ? (
<div className="share-validity">{LocaleUtils.tr("share.expires", this.state.expires)}</div>
) : null}
</div>
)}
</div>
);
};
render() {
return (
<SideBar icon="share" id="Share" onShow={this.onShow} side={this.props.side}
title="appmenu.items.Share" width="20em">
{() => ({
body: this.renderBody()
})}
</SideBar>
);
}
refreshPermalink = () => {
generatePermaLink(this.props.state, ((permalink, expires) => {
this.setState({location: permalink, expires: expires});
}));
};
}
export default connect(state => ({
state
}), {
addMarker: addMarker,
removeMarker: removeMarker
})(Share);