UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) • 11.3 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import t from"../Basemap.js";import i from"../Viewpoint.js";import{result as n}from"../core/asyncUtils.js";import{ClonableMixin as r}from"../core/Clonable.js";import s from"../core/Collection.js";import{referenceSetter as o}from"../core/collectionUtils.js";import{EventedMixin as a}from"../core/Evented.js";import{JSONSupport as l}from"../core/JSONSupport.js";import p from"../core/Logger.js";import{moduloPositive as m}from"../core/mathUtils.js";import{destroyMaybe as h,removeMaybe as c}from"../core/maybe.js";import{onAbortOrThrow as d,createAbortError as u,isAbortError as y}from"../core/promiseUtils.js";import{whenOnce as g,when as b}from"../core/reactiveUtils.js";import{Milliseconds as v,Seconds as w}from"../core/time.js";import{property as f,cast as T,subclass as _}from"../core/accessorSupport/decorators.js";import{ensureType as j}from"../core/accessorSupport/ensureType.js";import{squaredDistance as L}from"../core/libs/gl-matrix-2/math/vec3.js";import{create as C}from"../core/libs/gl-matrix-2/factories/vec3f64.js";import E from"../layers/Layer.js";import{basemapEnsureType as S,clonePreservingTiledLayers as A}from"../support/basemapEnsureType.js";import{TaskPriority as V}from"../views/support/Scheduler.js";import k from"../webdoc/support/SlideThumbnail.js";import{timeExtent as U}from"../webdoc/support/timeProperties.js";import x from"./SunLighting.js";import F from"./VirtualLighting.js";import I from"./support/Description.js";import R from"./support/SlideElements.js";import D from"./support/SlideEnvironment.js";import O from"./support/SlideGround.js";import P from"./support/SlideVisibleLayer.js";import q from"./support/Title.js";let M=0;const N=s.ofType(P),B=["caption","cover","none"];let G=class extends(r(a(l))){constructor(e){super(e),this.id=Date.now().toString(16)+"-slide-"+M++,this.hidden=!1,this.viewpoint=null,this.ground=null,this.layout="caption",this.enabledFocusAreas=null,this.environment=new D,this.timeExtent=null,this.elements=new R,this.title=new q,this.description=new I,this.thumbnail=new k,this.visibleLayers=new N}destroy(){this.visibleLayers.removeAll(),this.enabledFocusAreas=null,this.basemap=null,this.thumbnail=h(this.thumbnail),this.description=null,this.title=null,this.thumbnail=null,this.elements.destroy()}get title(){return this._get("title")}set title(e){e="string"==typeof e?new q({text:e}):j(q,e),this._set("title",e)}get description(){return this._get("description")}set description(e){e="string"==typeof e?new I({text:e}):j(I,e),this._set("description",e)}get thumbnail(){return this._get("thumbnail")}set thumbnail(e){e="string"==typeof e?new k({url:e}):j(k,e),this._set("thumbnail",e)}get basemap(){return this._get("basemap")}set basemap(e){this._set("basemap",S(e))}set visibleLayers(e){this._set("visibleLayers",o(e,this._get("visibleLayers"),N))}castVisibleLayers(e){return e&&"function"==typeof e.map?e.map(e=>{if("string"==typeof e)return{id:e};if(e instanceof E){const t=J(e);return{id:e.id,sublayerIds:t}}return e.id?{id:e.id,sublayerIds:"sublayerIds"in e?e.sublayerIds:void 0}:(p.getLogger(this).warn('Invalid visible layer, expected { id }, Layer or "id"'),e)}):null}async _updateVisibleLayers(e){const t=[];await Promise.allSettled(this._getLayers(e.map).map(i=>e.whenLayerView(i).then(e=>{if(e.visible){const n=J(i);t.push(new P({id:e.layer.id,sublayerIds:n}))}})).toArray()),this.visibleLayers.removeAll(),this.visibleLayers.addMany(t)}async updateFrom(e,t){const i={format:"png",quality:80,width:120,height:75,disableDecorations:!0,...t?.screenshot};await e.when(),this.viewpoint=e.viewpoint.clone(),this.environment.lighting="virtual"===e.environment.lighting.type?F.prototype.clone.apply(e.environment.lighting):x.prototype.clone.apply(e.environment.lighting),this.environment.weather=e.environment.weather.clone(),this.enabledFocusAreas=new s(e.focusAreasView?.enabledAreas.map(({id:e})=>e)??[]),this.basemap=e.map.basemap?.clone()||null,this.ground=e.map.ground?O.fromGround(e.map.ground):null,this.timeExtent=e.timeExtent?.clone()??null,this.elements.analyses.updateFrom(e),await this._updateVisibleLayers(e);const n=await e.takeScreenshot(i);return this.thumbnail=new k({url:n.dataUrl}),this}async applyTo(e,t){this.emit("apply-slide-start"),null!=this._applyToController&&this._applyToController.abort();let i=new AbortController;this._applyToController=i;const r=d(t,()=>i?.abort()),s=()=>{this._applyToController===i&&(this._applyToController=null),i=null,r?.remove()};try{await g(()=>e.ready,i.signal)}catch(h){throw s(),h}if(i.signal.aborted)throw s(),this.emit("apply-slide-cancel"),u();const o=e.resourceController.scheduler.registerTask(V.SLIDE);let a=!1;const l={animate:!0,...t,signal:this._applyToController.signal},p=async()=>{await Promise.all([o.schedule(async()=>a=await this._setViewpointOfInterest(e,l)),o.schedule(()=>this._applyBasemap(e,l),l.signal),o.schedule(()=>this._loadLayersWithSublayerVisibility(e))]),await Promise.all([o.schedule(()=>this._applyLayerVisibility(e),l.signal),o.schedule(()=>this._applyFocusAreaEnabled(e.map?.focusAreas.areas)),o.schedule(()=>this._applyGround(e),l.signal),o.schedule(()=>this.elements.analyses.applyTo(e),l.signal),o.schedule(()=>this._applyViewpoint(e,l),l.signal),o.schedule(()=>e.timeExtent=this.timeExtent?.clone()??null,l.signal),o.schedule(()=>e.environment.weather=this.environment.weather.clone())])},m=await n(e.addUpdatingPromise(p()));if(a&&(e.contentCamera=null),o.remove(),s(),!1===m.ok)throw this.emit("apply-slide-cancel"),m.error;return this.emit("apply-slide-complete"),this}async _applyBasemap(e,t){if(this.basemap){try{await this.basemap.load(t)}catch(i){if(y(i))throw i}e.map.basemap=A(this.basemap,e.map.basemap)}}_applyGround(e){this.ground&&(e.map.ground=this.ground.cloneAndApplyTo(e.map.ground))}_applyFocusAreaEnabled(e){if(e&&this.enabledFocusAreas)for(const t of e)t.enabled=this.enabledFocusAreas.includes(t.id)}_getLayers(e){const t=new s;return this._collectLayers(e,t),this._collectLayers(e.ground,t),t}_collectLayers(e,t){e.layers.forEach(e=>{e.persistenceEnabled&&(t.add(e),"layers"in e&&this._collectLayers(e,t))})}async _loadLayersWithSublayerVisibility(e){this.visibleLayers&&await Promise.allSettled(this._getLayers(e.map).items.map(e=>{const t=this.visibleLayers.find(t=>t.id===e.id)?.sublayerIds;return t?e.load():null}))}_applyLayerVisibility(e){if(!this.visibleLayers)return;this._getLayers(e.map).forEach(e=>{const t=this.visibleLayers.find(t=>t.id===e.id);e.visible=null!=t;const i=t?.sublayerIds,n=H(e);i&&n&&n.forEach(e=>e.visible=i.includes(e.id))})}async _setViewpointOfInterest(e,t){if(e.state.fixedContentCamera||!this.viewpoint||t?.ignoreViewpoint||!t?.useDestinationCamera)return!1;const{toCameraAsync:i}=await import("../views/3d/support/viewpointUtils.js"),n=await i(e,this.viewpoint);return e.contentCamera=n,null!=n}async _applyViewpoint(e,t){if(this._applyCachedCameraTrackingEnabled(e),this.viewpoint&&!t.ignoreViewpoint){const i=this.environment.lighting;if(t.animate&&"sun"===i.type&&i.date)return this._animateToLighting(e,t);t.animate&&(e.environment.applyLighting(i.clone()),await e.goTo(this.viewpoint,t)),e.viewpoint=this.viewpoint}e.environment.applyLighting(this.environment.lighting.clone())}async _animateToLighting(e,t){let i=null;"virtual"!==e.environment.lighting.type&&"virtual"!==this.environment.lighting.type&&("global"===e.viewingMode&&(i=this._animateLightingWithCamera(e,e.environment.lighting,this.environment.lighting)),e.environment.cachedCameraTrackingEnabled=e.environment.lighting.cameraTrackingEnabled,e.environment.lighting.cameraTrackingEnabled=!1),e.environment.lighting.directShadowsEnabled=this.environment.lighting.directShadowsEnabled,"virtual"===this.environment.lighting.type||"virtual"===e.environment.lighting.type?(e.environment.applyLighting(this.environment.lighting.clone()),"virtual"!==e.environment.lighting.type&&(e.environment.cachedCameraTrackingEnabled=e.environment.lighting.cameraTrackingEnabled,e.environment.lighting.cameraTrackingEnabled=!1)):null!=this.environment.lighting.displayUTCOffset&&(e.environment.lighting.displayUTCOffset=this.environment.lighting.displayUTCOffset);return e.goTo(this.viewpoint,t).then(()=>{this._applyCachedCameraTrackingEnabled(e),e.environment.applyLighting(this.environment.lighting.clone())}).catch(t=>{throw null==e.animation&&this._applyCachedCameraTrackingEnabled(e),t}).finally(()=>{c(i)})}_applyCachedCameraTrackingEnabled(e){null!=e.environment.cachedCameraTrackingEnabled&&(e.environment.lighting.cameraTrackingEnabled=e.environment.cachedCameraTrackingEnabled,e.environment.cachedCameraTrackingEnabled=null)}_getTime(e){return[v(e.getTime()),w(3600*e.getUTCHours()+60*e.getUTCMinutes()+e.getUTCSeconds())]}_setTime(e,t,i){return e.setTime(t),e.setUTCHours(i/3600),e.setUTCMinutes(i%3600/60),e.setUTCSeconds(i%3600%60),e}_animateLightingWithCamera(e,t,i){const n=new Date(t.date.toString()),[r,s]=this._getTime(n),[o,a]=this._getTime(i.date),l=Q(s,a),p=e.renderCoordsHelper,m=C();p.toRenderCoords(e.camera.position,m);const h=C(),c=C();null!=this.viewpoint.camera&&p.toRenderCoords(this.viewpoint.camera.position,h);const d=new Date;return b(()=>e.camera,e=>{p.toRenderCoords(e.position,c);const i=L(m,c),n=L(h,c);let a=0;i+n!==0&&(a=i/(i+n));const u=r+(o-r)*a,y=X(s,w(l*a));t.date=this._setTime(d,u,y)})}write(e,t){const i=super.write(e,t);return i?.elements&&0===Object.keys(i.elements).length&&delete i.elements,i}static createFrom(e,t){return(new this).updateFrom(e,t)}};e([f({type:String,json:{write:{isRequired:!0}}})],G.prototype,"id",void 0),e([f({type:q,useTypeForAutocast:!1,json:{default:()=>new q({text:""}),write:{isRequired:!0}}})],G.prototype,"title",null),e([f({type:I,useTypeForAutocast:!1,json:{write:{overridePolicy:e=>({enabled:!(!e||!e.text)})}}})],G.prototype,"description",null),e([f({type:Boolean,nonNullable:!0,json:{write:!0,default:!1}})],G.prototype,"hidden",void 0),e([f({type:k,useTypeForAutocast:!1,json:{default:()=>new k({url:""}),write:{isRequired:!0}}})],G.prototype,"thumbnail",null),e([f({type:i,nonNullable:!0,json:{write:{isRequired:!0}}})],G.prototype,"viewpoint",void 0),e([f({type:t,useTypeForAutocast:!1,json:{read:{source:"baseMap"},write:{target:"baseMap"}}})],G.prototype,"basemap",null),e([f({type:O,json:{write:!0}})],G.prototype,"ground",void 0),e([f({type:B,json:{write:!0,default:"caption"},nonNullable:!0})],G.prototype,"layout",void 0),e([f({type:N,json:{write:{isRequired:!0}}})],G.prototype,"visibleLayers",null),e([T("visibleLayers")],G.prototype,"castVisibleLayers",null),e([f({type:s.ofType(String),json:{write:!0},clonable:e=>e?new s(e.items):null})],G.prototype,"enabledFocusAreas",void 0),e([f({type:D,json:{write:!0}})],G.prototype,"environment",void 0),e([f(U)],G.prototype,"timeExtent",void 0),e([f({type:R,nonNullable:!0,json:{write:!0}})],G.prototype,"elements",void 0),G=e([_("esri.webscene.Slide")],G);const W=G;function H(e){if("building-scene"===e.type||"map-image"===e.type)return e.allSublayers.toArray()}function J(e){const t=H(e);if(t)return t.filter(e=>e.visible).map(e=>e.id)}const z=w(86400),K=w(43200);function Q(e,t){let i=t-e;return i>K&&(i-=z),i<-K&&(i+=z),w(i)}function X(e,t){return w(m(e+t,z))}export{W as default};