@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 4.78 kB
JavaScript
import{_ as e}from"../../chunks/tslib.es6.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/accessorSupport/ensureType.js";import"../../core/arrayUtils.js";import{subclass as n}from"../../core/accessorSupport/decorators/subclass.js";import{getEffectiveLayerCapabilities as p}from"../../layers/support/layerUtils.js";import u from"./TemplateItem.js";import m from"./TemplateItemGroup.js";var y;const c=({layer:e})=>({key:e,label:e.title??""}),h=({layer:e})=>({key:e.geometryType,label:e.geometryType??""});let f=y=class extends(r(t.EventedAccessor)){constructor(e){super(e),this._itemPool=new l(u),this._groupPool=new l(m),this.filterFunction=null,this.selectedItem=null}initialize(){this._get("groupBy")||(this.groupBy="layer")}set groupBy(e){if(this._set("groupBy",e),"function"!=typeof e)switch(e){case"layer":this._groupByFunction=c;break;case"geometry":this._groupByFunction=h;break;default:this._groupByFunction=null}else this._groupByFunction=t=>this._ensureGroupByObject(e(t))}set layers(e){const t="layers";if(this.handles.remove(t),e){const r=()=>this.notifyChange("state");this.handles.add(e.map((e=>a((()=>e.loadStatus),r))),t)}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)if("subtype-group"===t.type)for(const r of t.sublayers){const t=d(r);e.push([r,t])}else e.push([t,d(t)]);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:y._nullFilterFunction;for(const[s,o]of e)if(s.loaded||"subtype-sublayer"===s.type&&s.parent?.loaded){const e=p(s)?.operations;if(e?.supportsEditing&&e?.supportsAdd)for(const l of o)t.push({layer:s,template:l,matchesFilter:r({label:l.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:y.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===y.nullGroupBy.label?(this._releasePreviousItems(),a[0].items):(this._releasePreviousItems(),a)}refresh(){this.notifyChange("items")}select(e,t=!0){const r=this.selectedItem,s=e?.clone()||null;this._set("selectedItem",s),t&&this.emit("select",{item:s,oldItem:r,template:s?.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 u?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}};f.nullGroupBy={key:Symbol(),label:"Other"},f._nullFilterFunction=e=>!0,e([i()],f.prototype,"_groupByFunction",void 0),e([i()],f.prototype,"filterFunction",void 0),e([i()],f.prototype,"groupBy",null),e([i()],f.prototype,"layers",null),e([i()],f.prototype,"state",null),e([i({readOnly:!0})],f.prototype,"_featureTemplatesByLayer",null),e([i({readOnly:!0})],f.prototype,"numberOfFeatureTemplates",null),e([i({readOnly:!0})],f.prototype,"items",null),e([i({readOnly:!0})],f.prototype,"selectedItem",void 0),e([i()],f.prototype,"select",null),f=y=e([n("esri.widgets.FeatureTemplates.FeatureTemplatesViewModel")],f);const d=e=>[..."templates"in e&&Array.isArray(e.templates)?e.templates:[],..."types"in e&&Array.isArray(e.types)?e.types.flatMap((e=>e.templates)):[]],_=f;export{_ as default};