@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 9.75 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{__decorate as e}from"tslib";import{isSome as t,difference as s}from"../core/arrayUtils.js";import i from"../core/Collection.js";import{referenceSetter as o}from"../core/collectionUtils.js";import{deprecatedProperty as n,deprecatedFunction as r}from"../core/deprecate.js";import{EventedAccessor as a}from"../core/Evented.js";import{makeHandle as l}from"../core/handleUtils.js";import c from"../core/Logger.js";import{getOrCreateMapValue as h}from"../core/MapUtils.js";import{destroyMaybe as d}from"../core/maybe.js";import p from"../core/ReactiveMap.js";import{watch as u,initial as g,on as f}from"../core/reactiveUtils.js";import{addMany as m,symmetricDifference as y}from"../core/SetUtils.js";import{property as _,subclass as w}from"../core/accessorSupport/decorators.js";import{UpdatingHandles as v}from"../core/support/UpdatingHandles.js";import{getGraphicLayer as S}from"../graphic/graphicOriginUtils.js";import{isSubtypeSublayer as M,isSubtypeGroupLayer as E,isKnowledgeGraphLayer as b,isMapImageLayer as j}from"../layers/support/layerUtils.js";import H from"../rest/support/Query.js";import{isGraphic as L}from"../support/graphicInstanceUtils.js";import{isISelectableLayerWithObjectIds as V,isISelectableLayerViewWithObjectIds as U,isISelectableLayer as C,isISelectableLayerView as I,isTableLayer as F,isISelectableLayerViewWithHighlights as z,isISelectableLayerWithGraphics as O,isISelectableLayerBaseWithEditing as T}from"./support/selectionUtils.js";let A=class extends a{constructor(e){super(e),this._layerEditHandles=new i,this._selectionMap=new p,this._sources=new i,this._trashCan=[],this._updatingHandles=new v,this._vizTaskId=0,this.highlightEnabled=!0,this.highlightName="default",this.layerViewMap=new WeakMap,this.view=null}initialize(){this.addHandles([u(()=>[this.view,this.highlightEnabled],()=>{this._refreshVisualization()}),u(()=>this.view,e=>{e?.when().then(()=>this.syncSources())},g),f(()=>this.sources,"change",e=>{const t=this._selectionMap,s=[];for(const i of e.removed){const e=t.get(i);e&&(t.delete(i),e.highlightHandle?.remove(),s.push({layer:i,selection:[],added:[],removed:[...e.selection]}))}this._refreshListeners(),s.length&&this._onSelectionChange(s)},{onListenerAdd:()=>this._refreshListeners()})])}destroy(){this.clear(),this._layerEditHandles.destroyAll(),this._updatingHandles.destroy(),d(this._layerEditHandles),d(this._sources)}get count(){let e=0;for(const t of this._selectionMap.values())e+=t.selection.length;return e}get hasSelection(){return this.count>0}get selections(){return Array.from(this._selectionMap.entries()).map(e=>{const[t,s]=e;return{layer:t,selection:[...s.selection]}})}get showHighlight(){return this.highlightEnabled}set showHighlight(e){n(c.getLogger(this),"showHighlight",{replacement:"highlightEnabled"}),this.highlightEnabled=e}get sources(){return this._sources}set sources(e){o(e,this._sources)}get updating(){return this._updatingHandles.updating}add(e,t){const s=new Set;m(s,this._selectionMap.get(e)?.selection);for(const i of t)s.add(N(e,i));this._setSelection(e,Array.from(s))}clear(){const e=this._selectionMap.values();this._trashCan.push(...e);const t=[];for(const[s,i]of this._selectionMap.entries())t.push({layer:s,added:[],removed:[...i.selection],selection:[]});this._selectionMap.clear(),this._onSelectionChange(t)}getSelection(e){return this._selectionMap.get(e)?.selection}syncSources(){const e=new Set,t=this.view?.map;if(!t)return;const s=t=>{b(t)?(t.layers?.forEach(s),t.tables?.forEach(s)):j(t)?(t.sublayers?.forEach(s),t.subtables?.forEach(s)):E(t)?t.sublayers?.forEach(s):C(t)&&e.add(t)};t.allLayers.forEach(s),t.allTables.forEach(s),this.sources=[...e]}async getSelectedFeatures(e,s={},i="layerView"){const{view:o,selections:n}=this;if(!o&&"layerView"===i)return c.getLogger(this).warn("Cannot query layer views without a view."),[];const r=n.map(async t=>{const{layer:n,selection:r}=t;if(r.length&&V(n)&&(!e||e.includes(n))){if(o&&"layerView"===i&&!n.isTable){const e=await this._findLayerView(n);if(U(e))return P(e,r,s)}return P(n,r,s)}}).filter(t);return(await Promise.all(r)).filter(t)}updateSelection(e){const t=new Map;for(const[s,n]of this._selectionMap)t.set(s,new Set(n.selection));let i=!1;const o=e.current.concat(e.added);for(const s of o){const e=S(s);if(C(e)&&this.sources.includes(e)){const o=h(t,e,()=>new Set),n=N(e,s);null!=n&&(o.add(n),i=!0)}}for(const s of e.removed){const e=S(s);if(C(e)&&this.sources.includes(e)){const o=t.get(e),n=N(e,s);n&&o&&(o.delete(n),i=!0)}}if(i){const{_selectionMap:e,_trashCan:i}=this,o=[];for(const[n,r]of t){const t=e.get(n),a=Array.from(r);void 0!==t&&i.push(t),e.set(n,{selection:a}),o.push({layer:n,selection:a,...s(void 0!==t?t.selection:[],a)})}this._onSelectionChange(o)}}setSelection(e,t){r(c.getLogger(this),"SelectionManager.setSelection is deprecated. Use SelectionManager.replace instead.",{replacement:"SelectionManager.replace",warnOnce:!0}),this.replace(e,t)}appendToSelection(e,t){r(c.getLogger(this),"SelectionManager.appendToSelection is deprecated. Use SelectionManager.add instead.",{replacement:"SelectionManager.add",warnOnce:!0}),this.add(e,t)}remove(e,t){const s=this._selectionMap.get(e);if(!s)return;const i=[],o=new Set(t.map(t=>N(e,t)));for(const n of s.selection)o.has(n)||i.push(n);this._setSelection(e,i)}replace(e,t){this._setSelection(e,t.map(t=>N(e,t)))}removeFromSelection(e,t){r(c.getLogger(this),"SelectionManager.removeFromSelection is deprecated. Use SelectionManager.remove instead.",{replacement:"SelectionManager.remove",warnOnce:!0}),this.remove(e,t)}toggle(e,t){const s=this._selectionMap.get(e),i=t.map(t=>N(e,t));if(!s||0===s.selection.length)return void this._setSelection(e,i);const o=new Set(s.selection),n=new Set(i),r=y(o,n);this._setSelection(e,Array.from(r))}toggleInSelection(e,t){r(c.getLogger(this),"SelectionManager.toggleInSelection is deprecated. Use SelectionManager.toggle instead.",{replacement:"SelectionManager.toggle",warnOnce:!0}),this.toggle(e,t)}async _findLayerView(e){const t=this.layerViewMap.get(e);if(t||!this.view)return t;const s=M(e)&&e.parent?e.parent:e,i=await this.view.whenLayerView(s).catch(()=>{});return I(i)?(this.layerViewMap.set(e,i),i.addHandles(l(()=>this.layerViewMap.delete(e))),i):void 0}_onSelectionChange(e){this._refreshVisualization(),this.emit("selection-change",{view:this.view,changes:e})}async _refreshVisualization(){await this._updatingHandles.addPromise(this._updateVisualization())}async _updateVisualization(){for(this._vizTaskId++;this._trashCan.length>0;){const e=this._trashCan.pop();e?.highlightHandle?.remove()}const{_selectionMap:e,highlightName:t,sources:s}=this,i=this.view&&this.highlightEnabled,o=this._vizTaskId;for(const n of s){const s=e.get(n);if(s?.highlightHandle?.remove(),i&&s?.selection.length&&!F(n)){const e=await this._findLayerView(n);if(z(e)&&o===this._vizTaskId){const i=s.selection;s.highlightHandle=e.highlight(i,{name:t})}}}}_refreshListeners(){this._layerEditHandles.destroyAll();const e=new Set(this.sources.map(e=>M(e)?e.parent:e));for(const t of e)C(t)&&(O(t)?this._layerEditHandles.push(t.graphics.on("change",e=>this._onGraphicsLayerEdit(t,e))):T(t)&&this._layerEditHandles.push(t.on("edits",e=>this._onLayerEdit(t,e))))}_onLayerEdit(e,t){if(E(e))this._onParentLayerEdit(e,t);else if(t.deletedFeatures.length&&this._selectionMap.has(e)){const s=[];t.deletedFeatures.forEach(({error:e,objectId:t})=>{null!=t&&null==e&&s.push(t)}),this.remove(e,s)}}_onGraphicsLayerEdit(e,t){const{removed:s}=t;s.length&&this._selectionMap.has(e)&&this.remove(e,s)}_onParentLayerEdit(e,t){const{deletedFeatures:s,edits:i,updatedFeatures:o}=t;if(i?.updateFeatures?.forEach(t=>{const s=t.getObjectId()??t.attributes[e.objectIdField];if(null==s)return;if(o.find(e=>e.objectId===s)?.error)return;const i=e.findSublayerForFeature(t);if(!C(i))return;const n=e.sublayers.find(e=>!(!C(e)||!this.getSelection(e)?.includes(s)));C(n)&&n!==i&&(this.remove(n,[s]),this.add(i,[s]))}),s.length){const t=[];s.forEach(({error:e,objectId:s})=>{null!=s&&null==e&&t.push(s)}),e.sublayers.forEach(e=>{C(e)&&this._selectionMap.has(e)&&this.remove(e,t)})}}_setSelection(e,t){if(!this.sources.includes(e))throw new Error(`Cannot set selection on layer ${e.title} because it is not in 'sources'`);const i=this._selectionMap.get(e);if(!i||!k(i,{selection:t})){i&&this._trashCan.push(i),this._selectionMap.set(e,{selection:[...t]});const o=i?s(i.selection,t):{};this._onSelectionChange([{layer:e,selection:[...t],...o}])}}};e([_()],A.prototype,"_layerEditHandles",void 0),e([_()],A.prototype,"_selectionMap",void 0),e([_()],A.prototype,"_sources",void 0),e([_()],A.prototype,"_updatingHandles",void 0),e([_({readOnly:!0,nonNullable:!0})],A.prototype,"count",null),e([_({readOnly:!0,nonNullable:!0})],A.prototype,"hasSelection",null),e([_()],A.prototype,"highlightEnabled",void 0),e([_()],A.prototype,"highlightName",void 0),e([_()],A.prototype,"layerViewMap",void 0),e([_({readOnly:!0,nonNullable:!0})],A.prototype,"selections",null),e([_()],A.prototype,"showHighlight",null),e([_()],A.prototype,"sources",null),e([_()],A.prototype,"updating",null),e([_()],A.prototype,"view",void 0),A=e([w("esri.views.SelectionManager")],A);const N=(e,t)=>{if(V(e)&&L(t)){const e=t.getObjectId();if(null!=e)return e}return t},k=(e,t)=>{if(null==e&&null==t)return!0;if(null!=e&&null==t||null==e&&null!=t)return!1;if(null!=e&&null!=t&&null!=e.selection&&null!=t.selection){const s=[...e.selection],i=[...t.selection];if(s.length!==i.length)return!1;s.sort(),i.sort();for(let e=0;e<s.length;e++)if(s[e]!==i[e])return!1}return!0},P=async(e,t,s={})=>e.queryFeatures(new H({...s,objectIds:t})).then(t=>({data:t,layer:I(e)?e.layer:e})),q=A;export{q as default};