// 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!./Screenshot/nls/resources");
import Widget = require("esri/widgets/Widget");
import {
subclass,
property,
aliasOf
} from "esri/core/accessorSupport/decorators";
import MapView = require("esri/views/MapView");
import SceneView = require("esri/views/SceneView");
import watchUtils = require("esri/core/watchUtils");
import Handles = require("esri/core/Handles");
import { accessibleHandler, tsx, storeNode } from "esri/widgets/support/widget";
import ScreenshotViewModel = require("./Screenshot/ScreenshotViewModel");
import Legend = require("esri/widgets/Legend");
import FeatureWidget = require("esri/widgets/Feature");
const CSS = {
base: "esri-screenshot",
widget: "esri-widget esri-widget--panel",
screenshotBtn: "esri-screenshot__btn",
mainContainer: "esri-screenshot__main-container",
panelTitle: "esri-screenshot__panel-title",
panelSubTitle: "esri-screenshot__panel-subtitle",
screenshotOption: "esri-screenshot__screenshot-option",
buttonContainer: "esri-screenshot__screenshot-button-container",
hide: "esri-screenshot--hide",
screenshotCursor: "esri-screenshot__cursor",
maskDiv: "esri-screenshot__mask-div",
actionBtn: "esri-screenshot__action-btn",
screenshotImg: "esri-screenshot__js-screenshot-image",
screenshotDiv: "esri-screenshot__screenshot-div",
screenshotImgContainer: "esri-screenshot__screenshot-img-container",
downloadBtn: "esri-screenshot__download-btn",
backBtn: "esri-screenshot__back-btn",
showOverlay: "esri-screenshot--show-overlay",
hideOverlay: "esri-screenshot--hide-overlay",
pointerCursor: "esri-screenshot--pointer",
disabledCursor: "esri-screenshot--disabled",
featureWarning: "esri-screenshot__feature-warning",
featureWarningTextContainer:
"esri-screenshot__feature-warning-text-container",
warningSVG: "esri-screenshot__warning-svg",
selectFeatureText: "esri-screenshot__select-feature-text",
screenshotfieldSetCheckbox: "esri-screenshot__field-set-checkbox",
offScreenPopupContainer: "esri-screenshot__offscreen-pop-up-container",
offScreenLegendContainer: "esri-screenshot__offscreen-legend-container",
screenshotClose: "esri-screenshot__close-button",
closeButtonContainer: "esri-screenshot__close-button-container",
screenshotPreviewContainer: "esri-screenshot__img-preview-container",
selectLayout: "esri-screenshot__select-layout"
};
@subclass("Screenshot")
class Screenshot extends Widget {
constructor(value?: unknown) {
super(value);
}
private _maskNode: HTMLElement = null;
private _screenshotImgNode: HTMLImageElement = null;
private _activeScreenshotBtnNode: HTMLButtonElement = null;
private _selectFeatureAlertIsVisible: boolean = null;
private _offscreenPopupContainer: HTMLElement = null;
private _offscreenLegendContainer: HTMLElement = null;
private _handles: Handles = new Handles();
private _elementOptions: {
legend?: boolean;
popup?: boolean;
custom?: boolean;
} = {};
@aliasOf("viewModel.custom")
custom: { label: string; element: HTMLElement } = null;
@property()
disableCustom = false;
@aliasOf("viewModel.enableLegendOption")
enableLegendOption: boolean = null;
@aliasOf("viewModel.enablePopupOption")
enablePopupOption: boolean = null;
@aliasOf("viewModel.featureWidget")
@property({
readOnly: true
})
featureWidget: FeatureWidget = null;
@property()
iconClass = "esri-icon-media";
@aliasOf("viewModel.includeCustomInScreenshot")
@property()
includeCustomInScreenshot: boolean = null;
@aliasOf("viewModel.includeLegendInScreenshot")
@property()
includeLegendInScreenshot: boolean = null;
@property()
includeLayoutOption = false;
@aliasOf("viewModel.includePopupInScreenshot")
@property()
includePopupInScreenshot: boolean = null;
@property()
label = i18n.widgetLabel;
@aliasOf("viewModel.legendWidget")
@property({
readOnly: true
})
legendWidget: Legend = null;
@aliasOf("viewModel.screenshotModeIsActive")
@property()
screenshotModeIsActive: boolean = null;
@property()
theme = "light";
@aliasOf("viewModel.view")
@property()
view: MapView | SceneView = null;
@aliasOf("viewModel.outputLayout")
@property()
outputLayout: "vertical" | "horizontal" = null;
@aliasOf("viewModel.previewTitleInputNode")
@property()
previewTitleInputNode: HTMLInputElement = null;
@property()
previewContainer: HTMLDivElement = null;
@property()
viewModel: ScreenshotViewModel = new ScreenshotViewModel();
postInitialize() {
this.own([
this._togglePopupAlert(),
this._generateOffScreenPopup(),
this._watchSelectedFeature(),
watchUtils.when(this, "legendWidget", () => {
this.scheduleRender();
}),
watchUtils.when(
this,
["enableLegendOption", "enablePopupOption", "custom"],
() => {
if (this.enableLegendOption) {
this._elementOptions.legend = this.includeLegendInScreenshot;
}
if (this.enablePopupOption) {
this._elementOptions.popup = this.includePopupInScreenshot;
}
if (this.custom) {
this._elementOptions.custom = this.includeCustomInScreenshot;
}
}
),
watchUtils.whenOnce(this, "previewContainer", () => {
document.addEventListener("keydown", e => {
if (this.viewModel.previewIsVisible) {
const selectors =
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
const focusableElements =
this.previewContainer.querySelectorAll(selectors);
const firstFocusableElement =
focusableElements[0] as HTMLInputElement;
const lastFocusableElement = focusableElements[
focusableElements.length - 1
] as HTMLButtonElement;
this._handleTab(e, firstFocusableElement, lastFocusableElement);
}
});
})
]);
const offScreenPopupContainer = document.createElement("div");
const offScreenLegendContainer = document.createElement("div");
offScreenPopupContainer.classList.add(CSS.offScreenPopupContainer);
offScreenLegendContainer.classList.add(CSS.offScreenLegendContainer);
document.body.appendChild(offScreenPopupContainer);
document.body.appendChild(offScreenLegendContainer);
this._offscreenPopupContainer = offScreenPopupContainer;
this._offscreenLegendContainer = offScreenLegendContainer;
}
render(): any {
const { screenshotModeIsActive } = this.viewModel;
const screenshotPanel = this._renderScreenshotPanel();
const screenshotPreviewOverlay = this._renderScreenshotPreviewOverlay();
const maskNode = this._renderMaskNode(screenshotModeIsActive);
const optOutOfScreenshotButton = this._renderOptOutOfScreenshotButton();
if (this.legendWidget && !this.legendWidget.container) {
this.legendWidget.container = this._offscreenLegendContainer;
}
return (
{screenshotModeIsActive ? (
{optOutOfScreenshotButton}
) : (
screenshotPanel
)}
{screenshotPreviewOverlay}
{maskNode}
);
}
destroy() {
this._handles.removeAll();
this._handles.destroy();
this._handles = null;
this._maskNode = null;
this._screenshotImgNode = null;
}
@accessibleHandler()
activateScreenshot(): void {
if (this.viewModel.screenshotModeIsActive) {
return;
}
this.viewModel.screenshotModeIsActive = true;
this.view.container.classList.add(CSS.screenshotCursor);
this.viewModel.dragHandler = this.view.on("drag", (event: Event) => {
this.viewModel.setScreenshotArea(
event,
this._maskNode,
this._screenshotImgNode,
this.viewModel.dragHandler
);
});
this.scheduleRender();
}
private _downloadImage() {
this.viewModel.downloadImage();
}
private _renderScreenshotPanel(): any {
const { screenshotTitle, screenshotSubtitle } = i18n;
const fieldSet = this._renderFieldSet();
const setMapAreaButton = this._renderSetMapAreaButton();
const featureWarning = this._renderFeatureWarning();
const screenshotLayout = this.includeLayoutOption
? this._renderScreenshotLayout()
: null;
return (
{screenshotTitle}
{this.enableLegendOption || this.enablePopupOption || this.custom ? (
{screenshotSubtitle}
) : null}
{this.enableLegendOption || this.enablePopupOption || this.custom
? fieldSet
: null}
{this.enablePopupOption ? featureWarning : null}
{screenshotLayout}
{setMapAreaButton}
);
}
private _renderScreenshotLayout(): any {
const elementOptionKeys = Object.keys(this._elementOptions);
const allDisabled = elementOptionKeys.every(
key => !this._elementOptions[key]
);
return (
);
}
private _renderFeatureWarning(): any {
return (
{this._selectFeatureAlertIsVisible ? (
) : null}
);
}
private _renderFieldSet(): any {
const { legend, popup } = i18n;
return (
{this.enableLegendOption ? (
) : null}
{this.enablePopupOption ? (
) : null}
{this.custom ? (
) : null}
);
}
private _renderSetMapAreaButton(): any {
const { setScreenshotArea } = i18n;
return (
{setScreenshotArea}
);
}
private _renderScreenshotPreviewOverlay(): any {
const { previewIsVisible } = this.viewModel;
const overlayIsVisible = {
[CSS.showOverlay]: previewIsVisible,
[CSS.hideOverlay]: !previewIsVisible
};
const screenshotPreviewBtns = this._renderScreenshotPreviewBtns();
return (
);
}
private _renderScreenshotPreviewBtns(): any {
return (
);
}
private _addCloseEventListener(node: HTMLElement): void {
node.addEventListener("click", () => {
this._closePreview();
});
}
private _downloadEventListener(node: HTMLElement) {
node.addEventListener("click", () => {
this._downloadImage();
});
}
private _renderMaskNode(screenshotModeIsActive: boolean): any {
const maskDivIsHidden = {
[CSS.hide]: !screenshotModeIsActive
};
return (
);
}
private _renderOptOutOfScreenshotButton(): any {
return (
);
}
@accessibleHandler()
deactivateScreenshot(): void {
this.viewModel.screenshotModeIsActive = false;
this.view.container.classList.remove(CSS.screenshotCursor);
if (this.featureWidget && this.featureWidget.graphic) {
this.featureWidget.graphic = null;
}
if (this.viewModel.dragHandler) {
this.viewModel.dragHandler.remove();
this.viewModel.dragHandler = null;
}
window.setTimeout(() => {
this._activeScreenshotBtnNode.focus();
}, 10);
this.scheduleRender();
}
@accessibleHandler()
private _toggleLegend(event: Event): void {
const node = event.currentTarget as HTMLInputElement;
this.includeLegendInScreenshot = node.checked;
this._elementOptions.legend = node.checked;
this.scheduleRender();
}
@accessibleHandler()
private _togglePopup(event: Event): void {
const node = event.currentTarget as HTMLInputElement;
this.includePopupInScreenshot = node.checked;
this._elementOptions.popup = node.checked;
this.scheduleRender();
}
@accessibleHandler()
private _toggleCustom(event: Event): void {
const node = event.currentTarget as HTMLInputElement;
this.includeCustomInScreenshot = node.checked;
this._elementOptions.custom = node.checked;
this.scheduleRender();
}
private _closePreview(): void {
const { viewModel } = this;
viewModel.previewIsVisible = false;
viewModel.screenshotModeIsActive = false;
if (this?.view?.popup) {
this.view.popup.clear();
}
window.setTimeout(() => {
this._activeScreenshotBtnNode.focus();
}, 10);
this.scheduleRender();
}
private _generateOffScreenPopup(): __esri.WatchHandle {
return watchUtils.watch(this, "view.popup.visible", () => {
if (!this.view) {
return;
}
if (this?.view?.popup?.visible && this._offscreenPopupContainer) {
if (!this.featureWidget) {
this._set(
"featureWidget",
new FeatureWidget({
container: this._offscreenPopupContainer,
graphic: this.view.popup.selectedFeature,
map: this.view.map,
spatialReference: this.view.spatialReference
})
);
this._selectFeatureAlertIsVisible = false;
this.scheduleRender();
}
}
});
}
private _togglePopupAlert(): __esri.WatchHandle {
return watchUtils.init(this, "enablePopupOption", () => {
if (this.enablePopupOption) {
this.own([
watchUtils.watch(
this,
[
"includePopupInScreenshot",
"featureWidget",
"featureWidget.graphic"
],
() => {
this._triggerAlert();
}
),
watchUtils.init(this, "includePopupInScreenshot", () => {
this._triggerAlert();
})
]);
}
});
}
private _triggerAlert(): void {
if (
this.includePopupInScreenshot &&
(!this.featureWidget ||
(this.featureWidget && !this.featureWidget.graphic))
) {
this._selectFeatureAlertIsVisible = true;
} else {
this._selectFeatureAlertIsVisible = false;
}
this.scheduleRender();
}
private _watchSelectedFeature(): __esri.WatchHandle {
return watchUtils.watch(this, "view.popup.selectedFeature", () => {
if (
this.featureWidget &&
this.view &&
this.view?.popup &&
this.view?.popup?.selectedFeature
) {
while (
this._offscreenPopupContainer &&
this._offscreenPopupContainer.firstChild
) {
this._offscreenPopupContainer.removeChild(
this._offscreenPopupContainer.firstChild
);
}
this.featureWidget.graphic = null;
this._set("featureWidget", null);
}
if (this?.view?.popup) {
this._set(
"featureWidget",
new FeatureWidget({
container: this._offscreenPopupContainer,
graphic: this.view.popup.selectedFeature,
map: this.view.map,
spatialReference: this.view.spatialReference
})
);
}
this.scheduleRender();
});
}
private _updateLayoutOption(e: Event) {
const node = e.target as HTMLSelectElement;
this.outputLayout = node.value as "horizontal" | "vertical";
}
private _attachToBody(this: HTMLElement, node) {
document.body.appendChild(node);
}
private _handleTab(e, firstFocusableElement, lastFocusableElement) {
let isTabPressed = e.key === "Tab" || e.keyCode === 9;
if (!isTabPressed) {
return;
}
if (e.shiftKey) {
if (document.activeElement === firstFocusableElement) {
const interval = setInterval(() => {
lastFocusableElement.focus();
if (document.activeElement === lastFocusableElement) {
clearInterval(interval);
return;
}
}, 0);
e.preventDefault();
}
} else {
if (document.activeElement === lastFocusableElement) {
const interval = setInterval(() => {
firstFocusableElement.focus();
if (document.activeElement === firstFocusableElement) {
clearInterval(interval);
return;
}
}, 0);
e.preventDefault();
}
}
}
}
export = Screenshot;