@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 10.1 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{__decorate as t}from"tslib";import{deprecateUnnecessaryViewModel as e}from"../../core/deprecate.js";import{EventedAccessor as i}from"../../core/Evented.js";import{makeHandle as n}from"../../core/handleUtils.js";import s from"../../core/Logger.js";import{removeMaybe as a}from"../../core/maybe.js";import{when as l,initial as r,watch as h,on as o,syncAndInitial as g}from"../../core/reactiveUtils.js";import{schedule as d}from"../../core/scheduling.js";import{truncateLocalTime as p,isValidDate as u,resetUTCDate as y,offsetDate as c}from"../../core/timeUtils.js";import{property as m,subclass as _}from"../../core/accessorSupport/decorators.js";import f from"../../views/3d/environment/SunLighting.js";import D from"../../views/3d/environment/VirtualLighting.js";import{positionToTimezoneInfo as w}from"../../views/3d/support/earthUtils.js";import{dateTimeToSliderPos as U,sliderPosToDateTime as S,getSeasonFromDate as T,getNorthernHemisphereSeason as v,getSeasonDate as L,calculatePlaySpeed as P}from"./support/daylightUtils.js";import{SliderWithDropdownViewModel as b}from"./support/SliderWithDropdownViewModel.js";import{formatSliderLabel as C,getSunriseAndSunsetTimes as O}from"../support/timeWidgetUtils.js";let E=class extends i{constructor(t){super(t),this.view=null,this.timeSliderViewModel=new b({min:0,max:1439,values:[0],labelFormatFunction:C,inputFormatFunction:C}),this.lightingUpdateInterval=200,this._oldLighting=null,this.playSpeedMultiplier=1,this._lastTime=null,this._sunrise=null,this._sunset=null,this._cachedLightingDateUTC=new Date(0),this._cachedDisplayUTCOffset=0,this._lastLightingUpdate=0,this._lightingUpdateHandle=null,t?.suppressDeprecationWarning||e(s.getLogger(this),"Daylight","arcgis-daylight",{version:"4.34"})}normalizeCtorArgs(t={}){const{suppressDeprecationWarning:e,...i}=t;return i}initialize(){this.addHandles([l(()=>this.view,t=>t.when(()=>this._updateLighting()),r),h(()=>{const t=this._lighting;return"sun"===t?.type?t.date:null},t=>this._scheduleLightingUpdate(t)),o(()=>this._lighting,"timezone-will-change",t=>this._timezoneWillChange(t),{onListenerAdd:()=>this._timezoneWillChange(null)}),h(()=>!0===this.view?.stationary,()=>{(this.dayPlaying||this.yearPlaying)&&this._updateSunriseAndSunset()},r),h(()=>{const t=this.timeSliderViewModel;return{vm:t,state:t.state,sliderPosition:this.timeSliderPosition}},({vm:t,state:e,sliderPosition:i})=>{"ready"===e&&t.setValue(0,i)}),h(()=>this.timeSliderViewModel?.utcOffset,t=>{null!=t&&(this.utcOffset=t)}),h(()=>({utcOffset:this.utcOffset,sliderViewModel:this.timeSliderViewModel}),({utcOffset:t,sliderViewModel:e})=>{e&&(e.utcOffset=t)},g),h(()=>this.timeSliderViewModel.timezonePickerOpen,()=>this.stopPlaying()),h(()=>this.timeSliderViewModel.values,t=>this._setTimeSliderPosition(t?.[0]??0,{forceLightingUpdate:!1}))])}destroy(){this._cancelLightingUpdate(),this.view=null}get isSupported(){return null==this.view||"3d"===this.view.type}get utcOffset(){return this._cachedDisplayUTCOffset}set utcOffset(t){t!==this.utcOffset&&null!=this._lighting&&"virtual"!==this._lighting.type&&(this._lighting.displayUTCOffset=t,this._updateLighting())}get localDate(){return p(this._lightingDateDisplay)}set localDate(t){this.view?.ready?u(t)&&t.getTime()!==this.localDate.getTime()&&(this._lightingDateDisplay=y(this._lightingDateDisplay,t)):s.getLogger(this).error("The localDate property cannot be set before the view and the widget are ready.")}get timeSliderPosition(){return U(this._lightingDateDisplay)}set timeSliderPosition(t){this.view?.ready&&"ready"===this.timeSliderViewModel?.state?this._setTimeSliderPosition(t,{forceLightingUpdate:!0}):s.getLogger(this).error("The timeSliderPosition property cannot be set before the view and the widget are ready.")}_setTimeSliderPosition(t,e){Math.abs(t-this.timeSliderPosition)<=1/60||(this.stopPlaying(),this._onUserDateTimeChange(),e.forceLightingUpdate&&(this._cancelLightingUpdate(),this._updateLighting()),this._lightingDateDisplay=S(this._lightingDateDisplay,t))}_timezoneFromCamera(t,e){if(null==e||!t.cameraTrackingEnabled)return 0;const i=w([e.longitude,e.latitude]);return null==i?0:Math.round(i.hours+i.minutes/60+i.seconds/3600)}get directShadowsEnabled(){return this._lighting?.directShadowsEnabled??!1}set directShadowsEnabled(t){const e=this._lighting;e&&(e.directShadowsEnabled=t)}get sunLightingEnabled(){return"sun"===this._lightingType}set sunLightingEnabled(t){const e=this._environment;if(t===this._get("sunLightingEnabled")||null==e)return;const i=e.lighting,n=this._oldLighting;this._oldLighting=i;const s={directShadowsEnabled:i.directShadowsEnabled,cameraTrackingEnabled:i.cameraTrackingEnabled},a=t?"sun":"virtual";let l;l=null!=n&&n.type===a?n:t?new f:new D,l.set(s),e.lighting=l,t||(this.stopPlaying(),this.timeSliderViewModel.timezonePickerOpen=!1)}set playingState(t){this.playingState!==t&&(this._set("playingState",t),"none"!==t&&this.sunLightingEnabled&&(this._updateSunriseAndSunset(),this._lastTime=Date.now(),this._play(),this._onUserDateTimeChange()))}get dayPlaying(){return"day"===this.playingState}set dayPlaying(t){t?this.playingState="day":this.dayPlaying&&(this.playingState="none")}get yearPlaying(){return"year"===this.playingState}set yearPlaying(t){t?this.playingState="year":this.yearPlaying&&(this.playingState="none")}get currentSeason(){return T(this.localDate,this._currentHemisphere)}set currentSeason(t){this.stopPlaying(),this._onUserDateTimeChange();const e=v(t,this._currentHemisphere);this.localDate=L(this.localDate,e,0)}get _currentHemisphere(){const t=this.view?.camera?.position?.latitude;return null==t||t>=0?0:1}get _environment(){return this.view?.environment}get _lighting(){return this._environment?.lighting}get _lightingType(){return this._lighting?.type}get _lightingDateDisplay(){return c(this._cachedLightingDateUTC,this._cachedDisplayUTCOffset,"hours")}set _lightingDateDisplay(t){const e=this._lighting;if(null==e||!this.sunLightingEnabled||"virtual"===e.type||!u(t))return;const i=e.date,n=c(t,-this._cachedDisplayUTCOffset,"hours");n.getTime()!==i.getTime()&&(e.date=n,this._updateLighting())}stopPlaying(){this.playingState="none"}toggleDayPlaying(){this.dayPlaying=!this.dayPlaying}toggleYearPlaying(){this.yearPlaying=!this.yearPlaying}toggleSunLightingEnabled(){this.stopPlaying(),this.sunLightingEnabled=!this.sunLightingEnabled}toggleDirectShadowsEnabled(){this.stopPlaying(),this.directShadowsEnabled=!this.directShadowsEnabled}onDateChange(t){this.dayPlaying=!1,this.localDate=t,this._onUserDateTimeChange()}_updateLighting(t){const e=Date.now();this._lastLightingUpdate=e;const{view:i}=this,n=this._lighting;if(null==i||null==n||"virtual"===n.type)return;t??=n.date;const s=n.displayUTCOffset,a=null!=s?s:this._timezoneFromCamera(n,i.camera?.position);this._cachedLightingDateUTC.getTime()!==t.getTime()&&(this._cachedLightingDateUTC=new Date(t)),this._cachedDisplayUTCOffset!==a&&(this._cachedDisplayUTCOffset=a)}_timezoneWillChange(t){const e=this._lighting;if(null==e||"virtual"===e.type||!e.cameraTrackingEnabled)return;let i;if(t)i=t.timezoneOffset;else{if(null!=e.displayUTCOffset)return;i=f.calculateTimezoneOffset(e.positionTimezoneInfo)}e.displayUTCOffset=i,this._scheduleLightingUpdate()}_scheduleLightingUpdate(t){if(t&&(this._lightingUpdateHandle=a(this._lightingUpdateHandle),!u(t)))return;if(this._lightingUpdateHandle)return;const e=Date.now()-this._lastLightingUpdate,i=this.lightingUpdateInterval-e;let s=null;const l=()=>{this._updateLighting(t),this._lightingUpdateHandle===s&&(this._lightingUpdateHandle=null)};if(i<=0)this._lightingUpdateHandle=s=d(l);else{const t=setTimeout(l,i);this._lightingUpdateHandle=s=n(()=>clearTimeout(t))}}_cancelLightingUpdate(){this._lightingUpdateHandle=a(this._lightingUpdateHandle)}_play(){const t=this._lighting;if(null==t||!this.sunLightingEnabled||"virtual"===t.type)return;const e=t.date;if(this.dayPlaying||this.yearPlaying){const i=Date.now()-(this._lastTime??0);if(this.dayPlaying){this._lastTime=Date.now();const n=P(this._sunrise,this._sunset,e)*this.playSpeedMultiplier/100*i;if(n>0){let i=new Date(e.getTime()+n);const s=t.displayUTCOffset??0;if(((i.getUTCHours()+s)%24+24)%24<((e.getUTCHours()+s)%24+24)%24){const t=864e5;i=new Date(e.getTime()+n-t)}t.date=i}}else{if(i>1e3){this._lastTime=Date.now();const i=(e.getUTCMonth()+1)%12,n=new Date(e);n.setUTCMonth(i),t.date=n}}requestAnimationFrame(()=>this._play())}}_updateSunriseAndSunset(){const t=this._lighting;if(null==t||"virtual"===t.type||!this.sunLightingEnabled)return;const e=this.view?.camera?.position;if(null==e)return;const{latitude:i,longitude:n}=e,{date:s,displayUTCOffset:a}=t,l=O(s,i,n,a??0);l&&(this._sunrise=new Date(l.sunrise),this._sunset=new Date(l.sunset))}_onUserDateTimeChange(){this.emit("user-date-time-change",{})}};t([m()],E.prototype,"view",void 0),t([m({type:b,nonNullable:!0})],E.prototype,"timeSliderViewModel",void 0),t([m()],E.prototype,"isSupported",null),t([m()],E.prototype,"lightingUpdateInterval",void 0),t([m()],E.prototype,"utcOffset",null),t([m({type:Date})],E.prototype,"localDate",null),t([m()],E.prototype,"timeSliderPosition",null),t([m()],E.prototype,"directShadowsEnabled",null),t([m()],E.prototype,"sunLightingEnabled",null),t([m({type:["none","day","year"],value:"none"})],E.prototype,"playingState",null),t([m()],E.prototype,"dayPlaying",null),t([m()],E.prototype,"yearPlaying",null),t([m()],E.prototype,"playSpeedMultiplier",void 0),t([m()],E.prototype,"currentSeason",null),t([m()],E.prototype,"_lastTime",void 0),t([m()],E.prototype,"_sunrise",void 0),t([m()],E.prototype,"_sunset",void 0),t([m()],E.prototype,"_cachedLightingDateUTC",void 0),t([m()],E.prototype,"_cachedDisplayUTCOffset",void 0),t([m()],E.prototype,"_currentHemisphere",null),t([m()],E.prototype,"_environment",null),t([m()],E.prototype,"_lighting",null),t([m()],E.prototype,"_lightingType",null),t([m()],E.prototype,"_lightingDateDisplay",null),E=t([_("esri.widgets.Daylight.DaylightViewModel")],E);const M=E;export{M as default};