FeatureTemplatesViewModel.js 4.8 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.25/esri/copyright.txt for details.
  4. */
  5. import{_ as e}from"../../chunks/tslib.es6.js";import"../../core/has.js";import t from"../../core/Evented.js";import{HandleOwnerMixin as r}from"../../core/HandleOwner.js";import{isSome as s,isNone as o}from"../../core/maybe.js";import l from"../../core/ObjectPool.js";import{when as a}from"../../core/reactiveUtils.js";import{property as i}from"../../core/accessorSupport/decorators/property.js";import"../../core/arrayUtils.js";import"../../core/accessorSupport/ensureType.js";import{subclass as n}from"../../core/accessorSupport/decorators/subclass.js";import p from"./TemplateItem.js";import u from"./TemplateItemGroup.js";var m;const y=({layer:e})=>({key:e,label:e.title}),c=({layer:e})=>({key:e.geometryType,label:e.geometryType});let h=m=class extends(r(t.EventedAccessor)){constructor(e){super(e),this._itemPool=new l(p),this._groupPool=new l(u),this.filterFunction=null,this.selectedItem=null}initialize(){this._get("groupBy")||(this.groupBy="layer")}destroy(){this._templateToLayer.clear()}get _templateToLayer(){const e=new Map;for(const[t,r]of this._featureTemplatesByLayer)for(const s of r)e.set(s,t);return e}set groupBy(e){if(this._set("groupBy",e),"function"!=typeof e)switch(e){case"none":this._groupByFunction=null;break;case"layer":this._groupByFunction=y;break;case"geometry":this._groupByFunction=c}else this._groupByFunction=t=>this._ensureGroupByObject(e(t))}set layers(e){const t="layers";if(this.handles.remove(t),e){const t=()=>this.notifyChange("state");this.handles.add(e.map((e=>a((()=>e.loadStatus),t))),"layers")}this._set("layers",e)}get layers(){return this._get("layers")}get state(){const{layers:e}=this;return e&&0!==e.length?e.some((e=>"loading"===e.loadStatus||"not-loaded"===e.loadStatus))?"loading":"ready":"disabled"}get _featureTemplatesByLayer(){if(!this.layers)return new Map;const e=[];for(const t of this.layers){const r="templates"in t&&t.templates?t.templates:[],s="types"in t&&t.types?f(t.types):[];e.push([t,[...r,...s]])}return new Map(e)}get numberOfFeatureTemplates(){return Array.from(this._featureTemplatesByLayer.values()).reduce(((e,t)=>e+t.length),0)}get items(){if(0===this.numberOfFeatureTemplates)return this._releasePreviousItems(),[];const e=this._featureTemplatesByLayer,t=[],r=s(this.filterFunction)?this.filterFunction:m._nullFilterFunction;for(const[s,o]of e)if(s.loaded){const{capabilities:{operations:{supportsEditing:e,supportsAdd:l}}}=s;if(e&&l)for(const a of o)t.push({layer:s,template:a,matchesFilter:r({label:a.name})})}if(o(this._groupByFunction)){const e=t.filter((({matchesFilter:e})=>e)).map((({template:e,layer:t})=>this._createItem(e,t)));return this._releasePreviousItems(),e}const l=new Map;for(const o of t){const{template:e,layer:t}=o,r=this._groupByFunction({template:e,layer:t}),{key:a,label:i}=s(r.key)?r:m.nullGroupBy;l.has(a)||l.set(a,{label:i,templateItemInfos:[]}),l.get(a).templateItemInfos.push(o)}const a=[];for(const s of l.values()){const{label:e,templateItemInfos:t}=s,o=t.filter((({matchesFilter:e})=>e)),l=r({label:e})?t:t.length>0?o:[];if(l.length>0){const t=l.map((({template:e,layer:t})=>this._createItem(e,t)));a.push(this._createGroup(e,t))}}return 1===a.length&&a[0].label===m.nullGroupBy.label?(this._releasePreviousItems(),a[0].items):(this._releasePreviousItems(),a)}refresh(){this.notifyChange("items")}select(e){const t=e?.clone()||null;this._set("selectedItem",t),this.emit("select",{item:t,template:t?.template||null})}_createItem(e,t){const r=this._itemPool.acquire();return r.set({template:e,layer:t}),r}_createGroup(e,t){const r=this._groupPool.acquire();return r.set("label",e),r.items=t,r}_releasePreviousItems(){this._destroyItems(this._get("items"))}_destroyItems(e){if(!e)return;e[0]instanceof p?e.forEach((e=>this._destroyItem(e))):e.forEach((e=>this._destroyGroup(e)))}_destroyGroup(e){e.items.forEach((e=>this._destroyItem(e))),e.items.length=0,this._groupPool.release(e)}_destroyItem(e){e.layer=null,e.template=null,this._itemPool.release(e)}_ensureGroupByObject(e){return"string"==typeof e?{key:e,label:e}:e}};function f(e){return e.reduce(((e,t)=>[...e,...t.templates]),[])}h.nullGroupBy={key:Symbol(),label:"Other​"},h._nullFilterFunction=e=>!0,e([i()],h.prototype,"_groupByFunction",void 0),e([i({readOnly:!0})],h.prototype,"_templateToLayer",null),e([i()],h.prototype,"filterFunction",void 0),e([i()],h.prototype,"groupBy",null),e([i()],h.prototype,"layers",null),e([i()],h.prototype,"state",null),e([i({readOnly:!0})],h.prototype,"_featureTemplatesByLayer",null),e([i({readOnly:!0})],h.prototype,"numberOfFeatureTemplates",null),e([i({readOnly:!0})],h.prototype,"items",null),e([i({readOnly:!0})],h.prototype,"selectedItem",void 0),e([i()],h.prototype,"select",null),h=m=e([n("esri.widgets.FeatureTemplates.FeatureTemplatesViewModel")],h);const d=h;export{d as default};