AttributionViewModel.js 4.4 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 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 s,on as o,when as n}from"../../core/reactiveUtils.js";import{property as a}from"../../core/accessorSupport/decorators/property.js";import"../../core/arrayUtils.js";import"../../core/accessorSupport/ensureType.js";import{subclass as c}from"../../core/accessorSupport/decorators/subclass.js";import{extentContainsPoint as u}from"../../geometry/support/contains.js";import{geographicToWebMercator as h,project as l}from"../../geometry/support/webMercatorUtils.js";import d from"../../geometry/SpatialReference.js";function p(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=>s((()=>[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([o((()=>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.get("view.allLayerViews");b.length=0,t?(t.forEach((t=>{if(t.suspended||!t.layer?.attributionVisible)return;const e=t.layer;if(p(e,"user"))return void f(b,t,e.copyright);if(e.hasAttributionData){if(this._fetchedAttributionData.has(t)){const i=this._fetchedAttributionData.get(t);return void(i?f(b,t,this._getDynamicAttribution(i,this.view,e)):p(e)&&f(b,t,e.copyright))}return void this._fetchAttributionData(t)}const i=e.get("portalItem.accessInformation");f(b,t,i||e.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 s=0;s<i.length;s++){const t=i[s],o=t.coverageAreas;if(!o)return;for(const i of o){const o=i.bbox,n=i.zoomMin-(e&&i.zoomMin?1:0),a=i.zoomMax-(e&&i.zoomMax?1:0),c={xmin:o[1],ymin:o[0],xmax:o[3],ymax:o[2],spatialReference:d.WGS84},u={extent:h(c),attribution:t.attribution||"",score:null!=i.score?i.score:100,id:s};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:s}=e;let o=i.tileInfo.scaleToZoom(s);if(o=Math.min(t.maxKey,Math.round(o)),!r||null==o||o<=-1)return"";const n=t[o],a=l(r.center.clone().normalize(),e.spatialReference),c={};return n?n.filter((t=>{const e=!c[t.id]&&a&&u(t.extent,a);return e&&(c[t.id]=!0),e})).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};