// Copyright 2021 Esri // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.​ import i18n = require("dojo/i18n!./Share/nls/resources"); // esri.core import Collection = require("esri/core/Collection"); import watchUtils = require("esri/core/watchUtils"); import { substitute } from "esri/intl"; // esri.core.accessorSupport import { subclass, property, aliasOf } from "esri/core/accessorSupport/decorators"; // esri.views import MapView = require("esri/views/MapView"); import SceneView = require("esri/views/SceneView"); // esri.widgets import Widget = require("esri/widgets/Widget"); //esri.widgets.support import { accessibleHandler, tsx, storeNode } from "esri/widgets/support/widget"; import ShareViewModel = require("./Share/ShareViewModel"); import ShareItem = require("./Share/ShareItem"); import ShareFeatures = require("./Share/ShareFeatures"); import PortalItem = require("esri/portal/PortalItem"); const CSS = { base: "esri-share", shareModalStyles: "esri-share__share-modal", shareButton: "esri-share__share-button", shareModal: { shareIframe: { iframeContainer: "esri-share__iframe-container", iframeTabSectionContainer: "esri-share__iframe-tab-section-container", iframeInputContainer: "esri-share__iframe-input-container", iframePreview: "esri-share__iframe-preview", iframeInput: "esri-share__iframe-input" }, header: { heading: "esri-share__heading" }, main: { mainHeader: "esri-share__main-header", mainHR: "esri-share__hr", mainCopy: { copyContainer: "esri-share__copy-container", copyClipboardContainer: "esri-share__copy-clipboard-container" }, mainUrl: { inputGroup: "esri-share__copy-url-group", urlInput: "esri-share__url-input", linkGenerating: "esri-share--link-generating" }, mainShare: { shareContainer: "esri-share__share-container", shareItemContainer: "esri-share__share-item-container", shareIcons: { facebook: "icon-social-facebook", twitter: "icon-social-twitter", email: "icon-social-contact", linkedin: "icon-social-linkedin", pinterest: "icon-social-pinterest", rss: "icon-social-rss" } } } }, icons: { widgetIcon: "esri-icon-share", copy: "esri-share__copy-icon", svgIcon: "esri-share__svg-icon", facebook: "esri-share__facebook-icon", twitter: "esri-share__twitter-icon", linkedIn: "esri-share__linked-in-icon", mail: "esri-share__mail-icon" }, shareIcon: "esri-share__share-icon" }; @subclass("Share") class Share extends Widget { constructor(value?: unknown) { super(value); } private _shareModal: HTMLCalciteModalElement = null; private _beforeCloseIsSet = false; private _iframeInputNode: HTMLInputElement = null; private _urlInputNode: HTMLInputElement = null; @aliasOf("viewModel.view") @property() view: MapView | SceneView = null; @aliasOf("viewModel.shareModalOpened") shareModalOpened: boolean = null; @aliasOf("viewModel.shareItems") shareItems: Collection = null; @aliasOf("viewModel.shareFeatures") shareFeatures: ShareFeatures = null; @aliasOf("viewModel.shareUrl") shareUrl: string = null; @property() iconClass = CSS.icons.widgetIcon; @property() label = i18n.widgetLabel; @property() theme = "light"; @property({ type: ShareViewModel }) viewModel: ShareViewModel = new ShareViewModel(); postInitialize() { this.own([ watchUtils.whenTrue(this, "view.ready", () => { this.own([ watchUtils.init(this, "shareModalOpened", () => { this._detectWidgetToggle(); }) ]); }) ]); } destroy() { this._iframeInputNode = null; this._urlInputNode = null; } render() { const modal = this._renderShareModal(); return (
); } @accessibleHandler() private _toggleShareModal(): void { const { _shareModal } = this; if (_shareModal.hasAttribute("active")) { _shareModal.removeAttribute("active"); this.shareModalOpened = false; } else { _shareModal.setAttribute("active", "true"); this.shareModalOpened = true; } } private _detectWidgetToggle(): void { if (this.shareModalOpened) { this._generateUrl(); } this.scheduleRender(); } @accessibleHandler() private _copyUrlInput(): void { this._urlInputNode.focus(); this._urlInputNode.setSelectionRange(0, this._urlInputNode.value.length); document.execCommand("copy"); this.scheduleRender(); } @accessibleHandler() private _copyIframeInput(): void { this._iframeInputNode.focus(); this._iframeInputNode.setSelectionRange( 0, this._iframeInputNode.value.length ); document.execCommand("copy"); this.scheduleRender(); } @accessibleHandler() private _processShareItem(event: Event): void { const node = event.currentTarget as HTMLElement; const shareItem = node["data-share-item"] as ShareItem; const { urlTemplate } = shareItem; const portalItem = this.get("view.map.portalItem"); const title = portalItem ? substitute(i18n.urlTitle, { title: portalItem.title }) : null; const summary = portalItem ? substitute(i18n.urlSummary, { summary: portalItem.snippet }) : null; this._openUrl(this.shareUrl, title, summary, urlTemplate); } private _generateUrl(): void { this.viewModel.generateUrl(); } private _openUrl( url: string, title: string, summary: string, urlTemplate: string ): void { const urlToOpen = substitute(urlTemplate, { url, title, summary }); console.log(urlToOpen); window.open(urlToOpen); } private _renderShareModal(): any { const modalContentNode = this._renderModalContent(); return (

{i18n.heading}

{modalContentNode}
); } private _renderModalContent(): any { const sendALinkContentNode = this._renderSendALinkContent(); const embedMapContentNode = this._renderEmbedMapContent(); const { embedMap } = this.shareFeatures; return embedMap ? ( {i18n.sendLink} {i18n.embedMap} {sendALinkContentNode} {embedMapContentNode} ) : ( sendALinkContentNode ); } private _renderShareItem(shareItem: ShareItem): any { const { name, iconName } = shareItem; return ( ); } private _renderShareItems(): any[] { const shareServices = this.shareItems; const { shareIcons } = CSS.shareModal.main.mainShare; // Assign class names of icons to share item shareServices.forEach((shareItem: ShareItem) => { for (const icon in shareIcons) { if (icon === shareItem.id) { shareItem.iconName = shareIcons[shareItem.id]; } } }); return shareServices .toArray() .map(shareItems => this._renderShareItem(shareItems)); } private _renderShareItemContainer(): any { const { shareServices } = this.shareFeatures; const { state } = this.viewModel; const shareItemNodes = shareServices ? this._renderShareItems() : null; const shareItemNode = shareServices ? state === "ready" && shareItemNodes.length ? [shareItemNodes] : null : null; return (
{shareServices ? (
{i18n.subHeading}
{shareItemNode}
) : null}
); } private _renderCopyUrl(): any { const { copyToClipboard } = this.shareFeatures; return (
{copyToClipboard ? (
{i18n.clipboard}
) : null}
); } private _renderSendALinkContent(): any { const copyUrlNode = this._renderCopyUrl(); const shareServicesNode = this._renderShareItemContainer(); const { shareServices, copyToClipboard } = this.shareFeatures; const { state } = this.viewModel; return state === "ready" ? (
{shareServicesNode} {!copyToClipboard || !shareServices ? null : (
)} {copyUrlNode}
) : ( ); } private _renderCopyIframe(): any { const { embedCode, state } = this.viewModel; return (
{state === "ready" ? ( ) : (
{i18n.generateLink}
)}
); } private _renderEmbedMapContent(): any { const { embedMap } = this.shareFeatures; const { state } = this.viewModel; const copyIframeCodeNode = this._renderCopyIframe(); return embedMap ? (
{state === "ready" ? (

{i18n.clipboard}

{copyIframeCodeNode}
{embedMap ? ( state === "ready" ? ( this.shareModalOpened ? (