UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 4.39 kB
import{_ as t}from"../../chunks/tslib.es6.js";import"../../geometry.js";import{result as e}from"../../core/asyncUtils.js";import i from"../../core/Collection.js";import{HandleOwner as r}from"../../core/HandleOwner.js";import{watch as o,on as s,when as n}from"../../core/reactiveUtils.js";import{property as a}from"../../core/accessorSupport/decorators/property.js";import"../../core/accessorSupport/ensureType.js";import"../../core/arrayUtils.js";import{subclass as c}from"../../core/accessorSupport/decorators/subclass.js";import{extentContainsPoint as u}from"../../geometry/support/contains.js";import{geographicToWebMercator as l,project as h}from"../../geometry/support/webMercatorUtils.js";import p from"../../geometry/SpatialReference.js";function d(t,e){return t&&"copyright"in t&&(!e||"function"==typeof t.originOf&&"user"===t.originOf("copyright"))}function m(t,e){return t.length!==e.length||t.some(((t,i)=>t.text!==e[i].text))}function f(t,e,i){if(!i||!e)return;t.find((t=>t.layerView===e&&t.text===i))||t.push({text:i,layerView:e})}function y(t){return"bing-maps"===t.type}const b=[];let g=class extends r{constructor(t){super(t),this._clear=()=>{this._fetchedAttributionData.clear(),this._pendingAttributions.clear(),this.handles.remove("suspension"),this.notifyChange("state")},this._pendingAttributions=new Set,this._fetchedAttributionData=new Map,this.items=new i,this.view=null,this._allLayerViewsChange=t=>{this.handles.remove("suspension");const e=this.get("view.allLayerViews");e&&this.handles.add(e.map((t=>o((()=>[t.suspended,t.layer?.attributionVisible]),(()=>this._updateAttributionItems())))),"suspension"),t&&t.removed&&t.removed.forEach((t=>{this._pendingAttributions.delete(t),this._fetchedAttributionData.delete(t)})),this._updateAttributionItems()},this.handles.add([s((()=>this.view?.allLayerViews),"change",(t=>this._allLayerViewsChange(t)),{onListenerAdd:()=>this._allLayerViewsChange(),onListenerRemove:this._clear}),n((()=>!0===this.view?.stationary),(()=>this._updateAttributionItems()))])}destroy(){this.view=null,this._fetchedAttributionData.clear(),this._pendingAttributions.clear(),this.items.removeAll()}get state(){return this.get("view.ready")?this._pendingAttributions.size>0?"loading":"ready":"disabled"}_updateAttributionItems(){const t=this.view,e=t?.allLayerViews;b.length=0,t&&e?(e.forEach((e=>{if(e.suspended||!e.layer?.attributionVisible)return;const i=e.layer;if(d(i,"user"))return void f(b,e,i.copyright);if(i.hasAttributionData){if(this._fetchedAttributionData.has(e)){const r=this._fetchedAttributionData.get(e);return void(r?f(b,e,this._getDynamicAttribution(r,t,i)):d(i)&&f(b,e,i.copyright))}return void this._fetchAttributionData(e)}const r=i.get("portalItem.accessInformation");f(b,e,r||i.copyright)})),m(this.items,b)&&(this.items.removeAll(),this.items.addMany(b)),b.length=0,this.notifyChange("state")):this._clear()}async _fetchAttributionData(t){if(this._pendingAttributions.has(t))return;this._pendingAttributions.add(t);const i=await e(t.layer.fetchAttributionData());if(this._pendingAttributions.has(t)){const e=i.ok?this._createContributionIndex(i.value,y(t.layer)):null;this._pendingAttributions.delete(t),this._fetchedAttributionData.set(t,e)}this._updateAttributionItems()}_createContributionIndex(t,e){const i=t.contributors,r={};if(!i)return r;for(let o=0;o<i.length;o++){const t=i[o],s=t.coverageAreas;if(!s)return;for(const i of s){const s=i.bbox,n=i.zoomMin-(e&&i.zoomMin?1:0),a=i.zoomMax-(e&&i.zoomMax?1:0),c={xmin:s[1],ymin:s[0],xmax:s[3],ymax:s[2],spatialReference:p.WGS84},u={extent:l(c),attribution:t.attribution||"",score:null!=i.score?i.score:100,id:o};for(let t=n;t<=a;t++)r[t]=r[t]||[],r[t].push(u)}}return r.maxKey=Math.max.apply(null,Object.keys(r)),r}_getDynamicAttribution(t,e,i){const{extent:r,scale:o}=e;let s=i.tileInfo?.scaleToZoom(o)??0;if(s=Math.min(t.maxKey??0,Math.round(s)),!r||null==s||s<=-1)return"";const n=t[s],a=h(r.center.clone().normalize(),e.spatialReference),c={};return n?n.filter((t=>{const e=t.id,i=!c[e]&&a&&t.extent&&u(t.extent,a);return i&&(c[e]=!0),i})).sort(((t,e)=>e.score-t.score||t.objectId-e.objectId)).map((t=>t.attribution)).join(", "):""}};t([a({readOnly:!0,type:i})],g.prototype,"items",void 0),t([a({readOnly:!0})],g.prototype,"state",null),t([a()],g.prototype,"view",void 0),g=t([c("esri.widgets.Attribution.AttributionViewModel")],g);const _=g;export{_ as default};