@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
385 lines (378 loc) • 19.4 kB
JavaScript
import { html as uHtml } from 'uhtml';
import TwitterLogo from './images/twitter.svg';
import FacebookLogo from './images/facebook.svg';
import LinkedInLogo from './images/linkedin.svg';
import MailLogo from './images/mail.svg';
import ShareManager from '../../tools/share/sharemanager';
import LstuManager from './tools/lstumanager';
import GmfManager from './tools/gmfmanager';
import GirafeHTMLElement from '../../base/GirafeHTMLElement';
import SimpleMaskManager from '../../tools/layers/simplemaskmanager';
import MapManager from '../../tools/state/mapManager';
import { debounce } from '../../tools/utils/debounce';
class ShareComponent extends GirafeHTMLElement {
get iframeWidth() {
switch (this.iframeSize) {
case 'small':
return 400;
case 'medium':
return 600;
case 'large':
return 800;
default:
throw new Error('Unknown iframe size');
}
}
get iframeHeight() {
switch (this.iframeSize) {
case 'small':
return 300;
case 'medium':
return 450;
case 'large':
return 600;
default:
throw new Error('Unknown iframe size');
}
}
constructor() {
super('share');
Object.defineProperty(this, "template", {
enumerable: true,
configurable: true,
writable: true,
value: () => {
return uHtml `<style>
*{font-family:Arial,sans-serif}.hidden{display:none}.gg-rotate90{transform:rotate(90deg)}.gg-rotate180{transform:rotate(180deg)}.gg-rotate270{transform:rotate(270deg)}img{filter:var(--svg-filter)}img.legend-image{filter:var(--svg-map-filter);background:var(--svg-legend-bkg)}div{scrollbar-width:thin}a{color:var(--link-color)}a:visited{color:var(--link-color)}@keyframes spin{from{transform:rotate(0)}to{transform:rotate(360deg)}}@keyframes spin-wait{0%{transform:rotate(0)}7%{transform:rotate(360deg)}100%{transform:rotate(360deg)}}.gg-spin{animation-name:spin;animation-duration:2s;animation-iteration-count:infinite;animation-timing-function:linear}.gg-spin-wait{animation-name:spin-wait;animation-duration:10s;animation-iteration-count:infinite;animation-timing-function:linear}::-webkit-scrollbar{width:5px}::-webkit-scrollbar-thumb{background:#999}.gg-button,.gg-input,.gg-select,.gg-textarea{background-color:var(--bkg-color);color:var(--text-color);outline:0;border-radius:3px;border:var(--app-standard-border);display:inline-block;box-sizing:border-box;padding:0 0 0 .5rem;margin:0;cursor:pointer}.gg-label{background-color:var(--bkg-color);color:var(--text-color);border:none;display:inline-block;padding:0;margin:0}.gg-button,.gg-input,.gg-label,.gg-select{min-height:calc(var(--app-standard-height)/ 1.5);max-height:calc(var(--app-standard-height)/ 1.5);line-height:calc(var(--app-standard-height) / 1.5)}.gg-textarea{padding:.5rem;height:6rem}.gg-input{cursor:text}.gg-checkbox{accent-color:var(--text-color);width:1.2rem}.gg-range{accent-color:var(--text-color)}.gg-button{padding:0 .5rem}.gg-button.active{border:solid 1px var(--text-color-grad2);background-color:var(--text-color-grad2);color:var(--bkg-color)}.gg-button:disabled{background-color:#d3d3d3;color:grey;border:none;cursor:not-allowed}.gg-button>img{vertical-align:middle}.gg-icon-button{border:none;background-color:transparent;display:flex;flex-direction:column;color:var(--text-color);padding:0;align-items:center;justify-content:center;cursor:pointer}.gg-big,.gg-big-withtext{min-width:var(--app-standard-height);min-height:var(--app-standard-height);max-height:var(--app-standard-height)}.gg-big img,.gg-big-withtext img{margin:0;width:calc(var(--app-standard-height) - 1.5rem)}.gg-big-withtext span{font-size:.9rem;font-variant:small-caps;padding:0 1rem}.gg-medium,.gg-medium-withtext{flex-direction:row;min-width:calc(var(--app-standard-height)/ 1.2);min-height:calc(var(--app-standard-height)/ 1.2);max-height:calc(var(--app-standard-height)/ 1.2)}.gg-medium img{margin:0;width:calc(var(--app-standard-height)/ 2.4)}.gg-medium-withtext img{margin-left:.5rem;width:calc(var(--app-standard-height)/ 2.4)}.gg-medium-withtext span{font-size:.9rem;padding:0 1rem;padding-left:.5rem}.gg-small,.gg-small-withtext{flex-direction:row;min-width:calc(var(--app-standard-height)/ 2);min-height:calc(var(--app-standard-height)/ 2);max-height:calc(var(--app-standard-height)/ 2)}.gg-small img{margin:0;width:calc(var(--app-standard-height)/ 3)}.gg-small-withtext img{margin-left:.5rem;width:calc(var(--app-standard-height)/ 3)}.gg-small-withtext span{font-size:.9rem;padding:0 .5rem;padding-left:.3rem}.gg-button:hover,.gg-icon-button:hover,.gg-input:hover,.gg-select:hover,.gg-textarea:hover{background-color:var(--bkg-color-grad1)}.gg-opacity{opacity:.5}.gg-opacity:hover{background-color:transparent;opacity:1}.gg-tabs{display:flex;justify-content:left;margin:.5rem auto 0;border-bottom:1px solid grey;cursor:pointer;height:2rem;align-items:end;gap:1rem;margin-bottom:1rem}.gg-tab{border:none;background:0 0;cursor:pointer;padding:.5rem;color:var(--text-color)}.gg-tab.active{border-bottom:solid 2px var(--text-color-grad2);font-weight:600}.girafe-button-big,.girafe-button-large,.girafe-button-small,.girafe-button-tiny{border:none;background-color:transparent;display:flex;flex-direction:column;color:var(--text-color)}.girafe-button-big:hover,.girafe-button-large:hover,.girafe-button-small:hover,.girafe-button-tiny:hover{background-color:var(--bkg-color-grad1);cursor:pointer}.girafe-button-big.dark,.girafe-button-large.dark,.girafe-button-small.dark,.girafe-button-tiny.dark{background-color:var(--bkg-color);filter:invert(1)}.girafe-button-big{width:var(--app-standard-height);height:var(--app-standard-height);align-items:center;padding:1rem}.girafe-button-big img{overflow:hidden}.girafe-button-large{flex-direction:row}.girafe-button-large img{margin:.3rem;height:2rem}.girafe-button-large span{line-height:2rem;height:2rem;margin:.3rem}.girafe-button-small{min-width:calc(var(--app-standard-height)/ 2);height:calc(var(--app-standard-height)/ 2);align-items:center;padding:.5rem}.girafe-button-small img{overflow:hidden}.girafe-button-small span{width:100%;text-align:left;overflow:hidden;text-overflow:ellipsis}.girafe-button-tiny{width:1rem;height:1rem;align-items:center;padding:0}.girafe-button-tiny img{overflow:hidden}
</style><style>
#content{background:var(--bkg-color);color:var(--text-color);padding:1rem;margin:0}.group{padding-bottom:4em}.title{margin-top:0}.link{margin-top:.5rem;display:flex;flex-direction:column;gap:.2rem}.qrcode{margin:auto;margin-top:2rem;display:block;width:10rem;opacity:.7}.social{display:flex}.social button{margin:auto;margin-top:2rem;cursor:pointer;border:none;background-color:transparent}.social img{width:3rem;opacity:.7;filter:var(--svg-filter)}.social img:hover{opacity:1}input{font-size:.9rem;flex-grow:1}.error{order:3;color:var(--error-color);text-align:center;width:90%}.textarea-for-code{max-height:initial;line-height:1.1rem;font-size:.75rem;height:6.25rem;font-family:monospace,Arial,sans-serif}.copy-success,.copy-success:hover{background-color:var(--success-color)}
</style>
<div id="panel">
<div id="content">
<div class="${!this.urlShortener ? '' : 'hidden'}">
<div class="message" i18n="Error, the shortener service is not defined.">
Error, the shortener service is not defined.
</div>
</div>
<!-- share tab -->
<div id="share-map" class="${this.urlShortener ? 'group' : 'hidden'}">
<h3 class="title" i18n="Share this map">Share this map</h3>
<div>
<div class="link">
<input type="text" value="${this.shareLink}" class="gg-input" />
<button
id="btn-copy-short"
class="gg-button"
disabled="${this.loading || !this.shareLink}"
i18n="Copy link"
onclick="${() => this.copyToClipboard('short')}">
Copy link
</button>
</div>
</div>
<img alt="share-qrcode" class="${(this.qrCode ? 'qrcode' : 'hidden')}" src="${this.qrCode}" />
<div class="${(this.success ? 'hidden' : 'qrcode error')}">
<img alt="error-icon" src="icons/error.svg" />
<span i18n="Error, can not fetch the shortener service. Please try again later.">
Error, can not fetch the shortener service. Please try again later.</span
>
</div>
<div class="social">
<button onclick="${() => this.shareFacebook()}">
<img alt="share-facebook" src="${this.facebookLogo}" />
</button>
<button onclick="${() => this.shareTwitter()}">
<img alt="share-twitter" src="${this.twitterLogo}" />
</button>
<button onclick="${() => this.shareLinkedIn()}">
<img alt="share-linkedin" src="${this.linkedInLogo}" />
</button>
<button onclick="${() => this.shareMail()}">
<img alt="share-mail" src="${this.mailLogo}" />
</button>
</div>
</div>
<!-- embed tab -->
<div id="embed-map" class="${this.urlShortener ? 'group' : 'hidden'}">
<h3 class="title" i18n="Embed this map">Embed this map</h3>
<div class="link">
<select class="gg-select" id="logLevel" onchange="${(evt) => this.onSizeChanged(evt)}">
<option ?selected="${this.iframeSize === ''}" i18n="Select map size..." value="">Select map size...</option>
<option ?selected="${this.iframeSize === 'small'}" i18n="Small" value="small">Small</option>
<option ?selected="${this.iframeSize === 'medium'}" i18n="Medium" value="medium">Medium</option>
<option ?selected="${this.iframeSize === 'large'}" i18n="Large" value="large">Large</option>
</select>
<textarea value="${this.iframeCode}" class="gg-input gg-textarea textarea-for-code"></textarea>
<button
id="btn-copy-iframe"
class="gg-button"
disabled="${this.loading || !this.iframeSize || !this.iframeCode}"
i18n="Copy code"
onclick="${() => this.copyToClipboard('iframe')}">
Copy code
</button>
</div>
</div>
</div>
</div>
`;
}
});
Object.defineProperty(this, "visible", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
Object.defineProperty(this, "loading", {
enumerable: true,
configurable: true,
writable: true,
value: true
});
Object.defineProperty(this, "shareLink", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "qrCode", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "success", {
enumerable: true,
configurable: true,
writable: true,
value: true
});
Object.defineProperty(this, "twitterLogo", {
enumerable: true,
configurable: true,
writable: true,
value: TwitterLogo
});
Object.defineProperty(this, "facebookLogo", {
enumerable: true,
configurable: true,
writable: true,
value: FacebookLogo
});
Object.defineProperty(this, "linkedInLogo", {
enumerable: true,
configurable: true,
writable: true,
value: LinkedInLogo
});
Object.defineProperty(this, "mailLogo", {
enumerable: true,
configurable: true,
writable: true,
value: MailLogo
});
Object.defineProperty(this, "iframeUrl", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "iframeCode", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "shareManager", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "urlShortener", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "mapManager", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "simpleMaskManager", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "eventsCallbacks", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
Object.defineProperty(this, "iframeSize", {
enumerable: true,
configurable: true,
writable: true,
value: ''
});
this.shareManager = ShareManager.getInstance();
this.mapManager = MapManager.getInstance();
}
initializeShortenerService() {
switch (this.configManager.Config.share.service) {
case 'gmf':
this.urlShortener = new GmfManager(this.configManager.Config.share.createUrl);
break;
case 'lstu':
this.urlShortener = new LstuManager(this.configManager.Config.share.createUrl);
break;
}
}
render() {
this.visible ? this.renderComponent() : this.renderEmptyComponent();
super.girafeTranslate();
}
/**
* Renders the component by calling the necessary methods.
* @private
*/
renderComponent() {
super.render();
this.simpleMaskManager = new SimpleMaskManager(this.mapManager.getMap());
// While the component is visible, listen for changes in the state to update the shared link
this.registerEvents();
void this.generateShareLink();
}
/**
* Renders an empty component when it's not visible.
* @private
*/
renderEmptyComponent() {
this.simpleMaskManager?.setMaskVisibility(false);
this.iframeSize = '';
this.unregisterEvents();
this.renderEmpty();
}
registerEvents() {
// Use a debounced version of the share link generation with 500ms delay to reduce the number of times it's called
// from tree view changes (can go up to 100x times).
const debouncedShareLinkCallback = debounce(() => {
void this.generateShareLink();
}, 500);
this.eventsCallbacks.push(this.subscribe('position', () => debouncedShareLinkCallback()), this.subscribe('layers.layersList', () => debouncedShareLinkCallback()), this.subscribe(/layers\.layersList\..*\.activeState/, () => debouncedShareLinkCallback()), this.subscribe(/layers\.layersList\..*\.order/, () => debouncedShareLinkCallback()), this.subscribe('activeBasemap', () => debouncedShareLinkCallback()));
}
unregisterEvents() {
this.unsubscribe(this.eventsCallbacks);
this.eventsCallbacks.length = 0;
}
async generateShareLink() {
if (!this.urlShortener) {
return;
}
this.loading = true;
this.shareLink = '';
this.iframeUrl = '';
this.iframeCode = '';
this.refreshRender();
try {
const currentUrl = new URL(window.location.href);
const baseUrl = `${currentUrl.protocol}//${currentUrl.host}${currentUrl.pathname}`;
const hash = this.shareManager.getStateToShare();
// Get short URL
const longurl = `${baseUrl}#${hash}`;
let response = await this.urlShortener.shortenUrl(longurl);
this.shareLink = response.shorturl;
this.success = response.success;
this.qrCode = response.qrcode;
// Get short URL for iframe
const longIframeUrl = `${baseUrl}iframe.html#${hash}`;
response = await this.urlShortener.shortenUrl(longIframeUrl);
this.iframeUrl = response.shorturl;
this.setIframeCode();
}
finally {
this.loading = false;
this.refreshRender();
}
}
setIframeCode() {
if (this.iframeSize && this.iframeUrl) {
this.iframeCode = `<iframe title="iframe GeoGirafe" width="${this.iframeWidth}" height="${this.iframeHeight}" src="${this.iframeUrl}"></iframe>`;
}
else {
this.iframeCode = '';
}
}
closeWindow() {
this.state.interface.sharePanelVisible = false;
}
shareFacebook() {
window.open('https://www.facebook.com/sharer/sharer.php?u=' + this.shareLink, '_blank');
}
shareTwitter() {
window.open('https://twitter.com/intent/tweet?url=' + this.shareLink, '_blank');
}
shareLinkedIn() {
window.open('https://www.linkedin.com/shareArticle?url=' + this.shareLink, '_blank');
}
shareMail() {
const subject = encodeURIComponent('Check out this link');
const body = encodeURIComponent('I thought you might be interested in this link: ' + this.shareLink);
window.location.href = 'mailto:?subject=' + subject + '&body=' + body;
}
copyToClipboard(type) {
let textToCopy = '';
if (type === 'short') {
textToCopy = this.shareLink ?? '';
}
else if (type === 'iframe') {
textToCopy = this.iframeCode ?? '';
}
if (textToCopy) {
navigator.clipboard.writeText(textToCopy);
this.showCopySuccessEffect(`btn-copy-${type}`);
}
}
onSizeChanged(event) {
this.iframeSize = event.target?.value;
this.setIframeCode();
this.refreshRender();
if (this.iframeSize) {
this.showMapPreview();
}
else {
this.hideMapPreview();
}
}
/**
* Displays a greyed out area in the main map indicating the size of the embedded map.
*/
showMapPreview() {
this.simpleMaskManager?.setMaskSize([this.iframeWidth, this.iframeHeight]);
this.simpleMaskManager?.setMaskVisibility(true);
}
hideMapPreview() {
this.simpleMaskManager?.setMaskVisibility(false);
}
showCopySuccessEffect(btnId) {
const copyButton = this.shadow.getElementById(btnId);
copyButton.classList.add('copy-success');
setTimeout(() => copyButton.classList.remove('copy-success'), 1000);
}
connectedCallback() {
this.loadConfig().then(() => {
this.render();
this.initializeShortenerService();
this.subscribe('interface.sharePanelVisible', (_, newValue) => {
this.visible = newValue;
this.render();
});
});
}
}
export default ShareComponent;