UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) 5.55 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.19/LICENSE.txt */ import{__decorate as t}from"tslib";import{createTask as e}from"../../core/asyncUtils.js";import{EventedAccessor as s}from"../../core/Evented.js";import l from"../../core/Logger.js";import{abortMaybe as i}from"../../core/maybe.js";import{isAbortError as r,throwIfAborted as o}from"../../core/promiseUtils.js";import{watch as a,syncAndInitial as n,sync as p,when as u}from"../../core/reactiveUtils.js";import{property as m,subclass as d}from"../../core/accessorSupport/decorators.js";import{UpdatingHandles as c}from"../../core/support/UpdatingHandles.js";import{getTemplatesForLayers as h,isSharedTemplateOrMetadata as y}from"../../editing/templateUtils.js";import{getEffectiveLayerCapabilities as f,isSubtypeGroupLayer as g}from"../../layers/support/layerUtils.js";import{isTemplateItemGroup as _,mapItemsByTemplate as I,isTemplateGroupInfo as F,loadLayerWithTemplates as b,makeTemplateItemInfos as v,makeGroupItemInfos as T,nullGroupBy as j}from"./featureTemplatesUtils.js";import w from"./TemplateItem.js";import B from"./TemplateItemGroup.js";const k=({layer:t})=>({key:t,label:t.title??""}),A=({layer:t})=>({key:t.geometryType,label:t.geometryType??""});let U=class extends s{constructor(t){super(t),this._allItems=[],this._updatingHandles=new c,this._updateTask=null,this.disabled=!1,this.disabledItemFunction=null,this.filterFunction=null,this.selectedItem=null,this.templateInfos=null,this.view=null}initialize(){this._get("groupBy")||(this.groupBy="layer"),this.addHandles([a(()=>[this.templateInfos,this.layers],([t],e)=>{t&&t===e?.at(0)||(this._updateTask=i(this._updateTask),t?this._updateAllItems(t):this._getTemplatesAndUpdateAllItems().catch(t=>{r(t)||l.getLogger(this).error("feature-templates: failed updating templates",t)}))},n),a(()=>this._groupByFunction,()=>{null==this.templateInfos&&(this._updateTask=i(this._updateTask),this._getTemplatesAndUpdateAllItems().catch(t=>{r(t)||l.getLogger(this).error("feature-templates: failed updating templates",t)}))},p),a(()=>this.filterFunction,t=>{for(const e of this._allItems)_(e)&&(e.filterFunction=t)},p)])}destroy(){this._updatingHandles.destroy()}set groupBy(t){if(this._set("groupBy",t),"function"!=typeof t)switch(t){case"layer":this._groupByFunction=k;break;case"geometry":this._groupByFunction=A;break;default:this._groupByFunction=null}else this._groupByFunction=e=>this._ensureGroupByObject(t(e))}get layers(){return this._get("layers")}set layers(t){const e="layers";if(this.removeHandles(e),t){const s=()=>this.notifyChange("state");this.addHandles(t.map(t=>u(()=>t.loadStatus,s)),e)}this._set("layers",t)}get state(){return this.updating?"loading":0!==this.layers.length||this.templateInfos?.length?"ready":"disabled"}get numberOfFeatureTemplates(){return this._allItems.reduce((t,e)=>_(e)?t+e.allItems.length:t+1,0)}get items(){const{filterFunction:t}=this;return null==t?this._allItems:this._allItems.filter(e=>_(e)?e.items.length>0:t(e))}get updating(){return this._updatingHandles.updating}refresh(){this.notifyChange("filterFunction");for(const t of this._allItems)_(t)&&t.reapplyFilter()}select(t,{emit:e=!0}={}){const s=this.selectedItem,l=t?.clone()||null;this._set("selectedItem",l),e&&this.emit("select",{item:l,oldItem:s,template:l?.template??null})}_createItem(t,e){return new w({disabledFunction:this.disabledItemFunction,layer:e,template:t})}_ensureGroupByObject(t){return"string"==typeof t?{key:t,label:t}:t}_updateAllItems(t){const e=this._allItems;if(0===t.length)return H(e),void(this._allItems=[]);const[s,l]=I(e),i=t=>{const e=s.get(t.template);return e?(s.delete(t.template),e):this._createItem(t.template,t.layer)},{filterFunction:r}=this,o=t.filter(O).map(t=>F(t)?new B({filterFunction:r,label:t.label,allItems:t.templateInfos.filter(O).map(t=>i(t))}):i(t));for(const a of[...s.values(),...l])a.destroy();this._allItems=o}async _getTemplatesFromLayers(t){const e=[],s={signal:t};await Promise.all(this.layers.map(async l=>{await b(l,s),o(t);const i=f(l)?.operations;i?.supportsEditing&&i?.supportsAdd&&(g(l)?e.push(...l.sublayers):e.push(l))}));const l=await h({layers:e,view:this.view,signal:t,queryOptions:{returnVisibleTemplatesOnly:!0}});o(t);const i=v(l),r=this._groupByFunction;if(null==r)return i;const a=T(i,r);return 1===a.length&&a[0].label===j.label?a[0].templateInfos:a}async _getTemplatesAndUpdateAllItems(){if(0===this.layers.length)return;const t=e(async t=>{const e=await this._getTemplatesFromLayers(t);o(t),this._updateAllItems(e)});this._updateTask=t,await this._updatingHandles.addPromise(t.promise)}};function H(t){for(const e of t){if(_(e))for(const t of e.items)t.destroy();e.destroy()}}function O(t){return F(t)?t.templateInfos.some(O):!y(t.template)||!1!==t.template.visible}t([m()],U.prototype,"_allItems",void 0),t([m()],U.prototype,"_groupByFunction",void 0),t([m()],U.prototype,"_updatingHandles",void 0),t([m()],U.prototype,"disabled",void 0),t([m()],U.prototype,"disabledItemFunction",void 0),t([m({value:null})],U.prototype,"filterFunction",void 0),t([m()],U.prototype,"groupBy",null),t([m({value:[]})],U.prototype,"layers",null),t([m()],U.prototype,"state",null),t([m({readOnly:!0})],U.prototype,"numberOfFeatureTemplates",null),t([m({readOnly:!0})],U.prototype,"items",null),t([m({readOnly:!0})],U.prototype,"selectedItem",void 0),t([m()],U.prototype,"templateInfos",void 0),t([m()],U.prototype,"updating",null),t([m()],U.prototype,"view",void 0),U=t([d("esri.widgets.FeatureTemplates.FeatureTemplatesViewModel")],U);const L=U;export{L as default};