// 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.​ // esri.core.accessorSupport import { aliasOf, property, subclass, } from "esri/core/accessorSupport/decorators"; import { watch } from "esri/core/reactiveUtils"; // esri.widgets.support.widget import { tsx } from "esri/widgets/support/widget"; // esri.widgets import Widget = require("esri/widgets/Widget"); import { getLocale } from "esri/intl"; // dojo import i18n = require("dojo/i18n!./FilterList/nls/resources"); import { Expression, ExtentSelector, FilterLayers, FilterOutput, LayerExpression, ResetFilter, } from "./FilterList/interfaces/interfaces"; import FilterListViewModel = require("./FilterList/FilterListViewModel"); const CSS = { baseDark: "esri-filter-list esri-filter-list--dark", baseLight: "esri-filter-list esri-filter-list--light", filterContainer: "esri-filter-list__filter-container", headerContainer: "esri-filter-list__header-container", resetContainer: "esri-filter-list__reset-container", resetBtn: "esri-filter-list__reset-btn", optionalBtn: "esri-filter-list__optional-btn", filterItem: { single: "esri-filter-list__filter-item-container esri-filter-list__filter-item-container--single", accordion: "esri-filter-list__filter-item-container esri-filter-list__filter-item-container--accordion", userInput: "esri-filter-list__filter-item-container esri-filter-list__filter-item-container--user-input", }, filterItemTitle: "esri-filter-list__filter-title", checkboxContainer: "esri-filter-list__checkbox-container", numberInputContainer: "esri-filter-list__number-input-container", dateInputContainer: "esri-filter-list__date-picker-input-container", operatorDesc: "esri-filter-list__operator-description", }; @subclass("FilterList") class FilterList extends Widget { // ---------------------------------- // // Public Variables // // ---------------------------------- @aliasOf("viewModel.map") map: __esri.WebMap; @aliasOf("viewModel.layerExpressions") layerExpressions: LayerExpression[]; @property() viewModel: FilterListViewModel = new FilterListViewModel(); @aliasOf("viewModel.theme") theme: "dark" | "light"; @aliasOf("viewModel.extentSelector") extentSelector: boolean; @aliasOf("viewModel.extentSelectorConfig") extentSelectorConfig: ExtentSelector; @property() headerTag: string = "h3"; @property() optionalBtnText: string = "Close Filter"; @aliasOf("viewModel.expandFilters") expandFilters: boolean; @property() optionalBtnOnClick: Function; @aliasOf("viewModel.layers") layers: FilterLayers; @aliasOf("viewModel.output") output: FilterOutput; // ---------------------------------- // // Private Variables // // ---------------------------------- private _reset: ResetFilter; private _isSingleFilterConfig: boolean; private _headerTitle: HTMLElement; private _locale: string; // ---------------------------------- // // Lifecycle methods // // ---------------------------------- constructor(properties?: any) { super(properties); } postInitialize() { this._locale = getLocale(); this.own([ watch( () => this.layerExpressions, () => { this._initExpressions(); this._reset = { disabled: this.layerExpressions && this.layerExpressions.length ? false : true, color: this.layerExpressions && this.layerExpressions.length ? "brand" : "inverse", }; }, { initial: true } ), ]); } render() { const filterConfig = this._initFilterConfig(); const header = this._renderFilterHeader(); const reset = this.optionalBtnOnClick ? this._renderOptionalButton() : this._renderReset(); return (
{header} {filterConfig} {reset}
); } // ---------------------------------- // // Private methods // // ---------------------------------- private _renderFilterHeader(): any { return (
); } private _renderLayerAccordion(): any { return ( {this.layerExpressions.map((layerExpression) => { return this._renderFilterAccordionItem(layerExpression); })} ); } private _renderFilterAccordionItem(layerExpression: LayerExpression): any { const filter = this._renderFilter(layerExpression); const { operator } = layerExpression; const operatorTranslation = operator?.trim() === "OR" ? "orOperator" : "andOperator"; return ( {filter} ); } private _renderFilter(layerExpression: LayerExpression): any { const { id } = layerExpression; return layerExpression.expressions.map((expression, index) => { return expression.definitionExpression ? (

{expression.name}

) : ( this._initInput(id, expression) ); }); } private _renderReset(): any { return (
{i18n.resetFilter}
); } private _renderOptionalButton(): any { return (
{i18n.resetFilter} {this.optionalBtnText}
); } private _renderDatePicker(layerId: string, expression: Expression) { return ( ); } private _renderNumberSlider(layerId: string, expression: Expression) { const check = expression?.min != null && expression?.max != null; return check ? ( ) : null; } private _renderCombobox(layerId: string, expression: Expression) { const comboItems = expression?.selectFields?.map((field, index) => { const name = expression.type === "coded-value" ? expression.codedValues[field] : field; const selectedFields = expression?.selectedFields as string[]; const selected = selectedFields?.includes(field) ?? false; return ( ); }); return ( ); } private _initFilterConfig(): any { if (this.layerExpressions && this.layerExpressions.length) { if (this.layerExpressions.length === 1) { const { operator } = this.layerExpressions[0]; const operatorTranslation = operator?.trim() === "OR" ? "orOperator" : "andOperator"; this._isSingleFilterConfig = true; return (

{i18n?.[operatorTranslation]}

{this._renderFilter(this.layerExpressions[0])}
); } else if (this.layerExpressions.length > 1) { this._isSingleFilterConfig = false; return this._renderLayerAccordion(); } } return; } private _initInput(layerId: string, expression: Expression) { const { type } = expression; if (type === "string" || type == "coded-value") { return this._renderCombobox(layerId, expression); } else if (type === "number" || type == "range") { return this._renderNumberSlider(layerId, expression); } else if (type === "date") { return this._renderDatePicker(layerId, expression); } } private _handleResetFilter(): void { const resetLayers: FilterOutput[] = []; this.layerExpressions.map((layerExpression) => { resetLayers.push({ id: layerExpression.id, definitionExpression: "", }); }); this.viewModel.handleResetFilter(); this.scheduleRender(); this.emit("filterListReset", resetLayers); } private _createHeaderTitle(header: HTMLDivElement): void { this._headerTitle = document.createElement(this.headerTag); this._headerTitle.innerHTML = i18n.selectFilter; header.prepend(this._headerTitle); } private _initExpressions(): void { this.layerExpressions?.forEach((layerExpression) => { const { id } = layerExpression; this.layers[id] = { expressions: {}, operator: layerExpression?.operator ?? " AND ", }; layerExpression.expressions?.forEach((expression, index) => { expression.definitionExpressionId = `${id}-${index}`; if (!expression.checked) { expression.checked = false; } this.viewModel.setInitExpression(id, expression, () => this.scheduleRender() ); }); }); } } export = FilterList;