actionUtils.js 4.0 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 e from"../../core/Error.js";import t from"../../core/Logger.js";import{throwIfNotAbortError as r}from"../../core/promiseUtils.js";import{zoomToFeature as o,zoomToClusteredFeatures as i,browseClusteredFeatures as n,removeSelectedFeature as a}from"./actions.js";const s="esri.widgets.Popup.PopupViewModel",c=t.getLogger(s),u=t=>{const{event:s,view:c}=t,{action:u}=s,d=c&&c.popup;if(!u)return Promise.reject(new e("trigger-action:missing-arguments","Event has no action"));if(!d)return Promise.reject(new e("trigger-action:missing-arguments","view.popup is missing"));const{disabled:l,id:g}=u;if(!g)return Promise.reject(new e("trigger-action:invalid-action","action.id is missing"));if(l)return Promise.reject(new e("trigger-action:invalid-action","Action is disabled"));if(g===o.id)return w(d.viewModel).catch(r);if(g===i.id)return p(d.viewModel);if(g===n.id)return d.featureMenuOpen=!d.featureMenuOpen,d.viewModel.browseClusterEnabled=!d.viewModel.browseClusterEnabled,Promise.resolve();if(d.viewModel.browseClusterEnabled=!1,g===a.id){d.close();const{selectedFeature:t}=d;if(!t)return Promise.reject(new e(`trigger-action:${a.id}`,"selectedFeature is required",{selectedFeature:t}));const{sourceLayer:r}=t;return r?r.remove(t):c.graphics.remove(t),Promise.resolve()}return Promise.resolve()};function d(e){const{selectedFeature:t,location:r,view:o}=e;if(!o)return null;if("3d"===o.type)return t||r;return e.get("selectedFeature.geometry")||r}function l(e){return e?.isAggregate&&"cluster"===e?.sourceLayer?.featureReduction?.type}async function g(e,t){if("3d"!==t?.type||!e||"esri.Graphic"!==e.declaredClass)return!0;const r=t.getViewForGraphic(e);if(r&&"whenGraphicBounds"in r){let t;try{t=await r.whenGraphicBounds(e,{useViewElevation:!0})}catch(o){}return!t||!t.boundingBox||t.boundingBox[0]===t.boundingBox[3]&&t.boundingBox[1]===t.boundingBox[4]&&t.boundingBox[2]===t.boundingBox[5]}return!0}async function w(t){const{location:r,selectedFeature:i,view:n,zoomFactor:a}=t,s=d(t);if(!s){const t=new e("zoom-to:invalid-target-or-view","Cannot zoom to location without a target and view.",{target:s,view:n});throw c.error(t),t}const u=n.scale/a,l=t.get("selectedFeature.geometry")||r,w=l&&"point"===l.type&&await g(i,n);o.active=!0,o.disabled=!0;try{await t.zoomTo({target:{target:s,scale:w?u:void 0}})}catch(p){const t=new e("zoom-to:invalid-graphic","Could not zoom to the location of the graphic.",{graphic:i});c.error(t)}finally{o.active=!1,o.disabled=!1,t.zoomToLocation=null,w&&(t.location=l)}}async function p(t){const{selectedFeature:r,view:o}=t;if("2d"!==o?.type){const t=new e("zoomToCluster:invalid-view","View must be 2d MapView.",{view:o});throw c.error(t),t}if(!l(r)){const t=new e("zoomToCluster:invalid-selectedFeature","Selected feature must represent an aggregate/cluster graphic.",{selectedFeature:r});throw c.error(t),t}const n=r.sourceLayer,a=await o.whenLayerView(n),s=a.createQuery();s.aggregateIds=[r.getObjectId()],i.active=!0,i.disabled=!0;const{extent:u}=await a.queryExtent(s);await t.zoomTo({target:u}),i.active=!1,i.disabled=!1}async function f(e){const{selectedFeature:t,view:r}=e,o=t.sourceLayer,i=await r.whenLayerView(o),n=i.createQuery();n.aggregateIds=[t.getObjectId()];const{extent:a}=await i.queryExtent(n);e.selectedClusterBoundaryFeature.geometry=a,r.graphics.add(e.selectedClusterBoundaryFeature)}async function m(e){const{selectedFeature:t,view:r}=e,o=t.sourceLayer,i=await r.whenLayerView(o),a=i.createQuery();a.aggregateIds=[t.getObjectId()],n.active=!0,n.disabled=!0;const{features:s}=await i.queryFeatures(a);n.active=!1,n.disabled=!1,r.popup.open({features:[t].concat(s),featureMenuOpen:!0})}function v(e){const t=e.features.filter((e=>l(e)));t.length&&(e.features=t)}export{m as browseAggregateFeatures,f as displayClusterExtent,d as getSelectedTarget,l as isClusterFeature,v as removeClusteredFeaturesForBrowsing,u as triggerAction,p as zoomToClusterExtent,w as zoomToLocation};