UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) 7.57 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{_ as t}from"../../chunks/tslib.es6.js";import e from"../../core/Collection.js";import i from"../../core/Evented.js";import n from"../../core/Logger.js";import{after as s,isAbortError as r}from"../../core/promiseUtils.js";import{watch as o,initial as l,syncAndInitial as a,whenOnce as m}from"../../core/reactiveUtils.js";import{offsetDate as u}from"../../core/timeUtils.js";import{property as p}from"../../core/accessorSupport/decorators/property.js";import{ensureType as h}from"../../core/accessorSupport/ensureType.js";import"../../core/has.js";import"../../core/RandomLCG.js";import{subclass as d}from"../../core/accessorSupport/decorators/subclass.js";import{T as c}from"../../chunks/TimeExtent.js";import{T as f}from"../../chunks/TimeInterval.js";import w from"../../webdoc/Widgets.js";import g from"../../webdoc/widgets/TimeSlider.js";const v=1e4;function T(t){return null!=t&&void 0!==t.count}function x(t){return null!=t&&void 0!==t.interval}function E(t){return null!=t&&void 0!==t.dates}let _=class extends i.EventedAccessor{constructor(t){super(t),this._animationController=null,this._isViewTimeExtentInitialized=!1,this._timerId=null,this.actions=new e,this.fullTimeExtent=null,this.loop=!1,this.mode="time-window",this.stops={count:10},this.timeExtent=null,this.view=null}initialize(){this.addHandles([o((()=>this.effectiveStops),(()=>{this.timeExtent??=this._getDefaultTimeExtent()}),l),o((()=>this.view),((t,e)=>{this._unregisterWidget(e),this._registerWidget(t)}),a),o((()=>this.timeExtent),(t=>{if(!this.view)return;const e=this.view.timeExtent;(t&&!t.isAllTime||e&&!e.isAllTime)&&(t&&e&&t.equals(e)||(this.view.timeExtent=t))}),l),o((()=>this.view?.timeExtent),(t=>{this._isViewTimeExtentInitialized?(t&&!t.isAllTime||this.timeExtent&&!this.timeExtent.isAllTime)&&(t&&this.timeExtent&&t.equals(this.timeExtent)||(this.timeExtent=t)):this._isViewTimeExtentInitialized=!0}))])}destroy(){null!=this._timerId&&(clearInterval(this._timerId),this._timerId=null),this._unregisterWidget(this.view),this.view=null,this._animationController&&(this._animationController.abort(),this._animationController=null)}get effectiveStops(){const{fullTimeExtent:t,stops:e}=this;if(E(e)){const{dates:i}=e;if(!i?.length)return null;const n=i.sort(((t,e)=>t.getTime()-e.getTime()));if(!t)return n;const{start:s,end:r}=t;if(!s||!r)return n;return n.filter((t=>!(t.getTime()<s.getTime()||t.getTime()>r.getTime())))}if(T(e)){const i=e.timeExtent??t;return this._divideTimeExtentByCount(i,e.count)}if(x(e)){const i=e.timeExtent??t;return this._divideTimeExtentByInterval(i,e.interval)}return[]}set playRate(t){t<=0||t>36e5||("playing"===this.state&&this._startAnimation(),this._set("playRate",t))}get state(){return this.fullTimeExtent?this._animationController?"playing":"ready":"disabled"}get timeExtentValues(){const{mode:t,timeExtent:e}=this;if(!e||e.isAllTime||e.isEmpty)return null;const{start:i,end:n}=e;switch(t){case"cumulative-from-end":case"instant":return i?[i.getTime()]:null;case"cumulative-from-start":return n?[n.getTime()]:null;case"time-window":return i&&n?[i.getTime(),n.getTime()]:null}}next(){this._step({forward:!0,allowRestart:!1})}play(){this._startAnimation()}previous(){this._step({forward:!1,allowRestart:!1})}stop(){this._stopAnimation()}triggerAction(t){this.emit("trigger-action",{action:t})}updateWebDocument(t){const e=new g({currentTimeExtent:this.timeExtent,fullTimeExtent:this.fullTimeExtent,loop:this.loop,numStops:T(this.stops)?this.stops.count:null,numThumbs:"time-window"===this.mode?2:1,stopDelay:this.playRate,stopInterval:x(this.stops)?this.stops.interval:null,stops:E(this.stops)?this.stops.dates:null});t.widgets?t.widgets.timeSlider=e:t.widgets=new w({timeSlider:e})}valuesToTimeExtent(t){if(!t)return null;const e=t.sort(((t,e)=>t-e)).map((t=>new Date(t))),i=e.length>0?e[0]:null,n=e.length>1?e[1]:null;switch(this.mode){case"time-window":return new c({start:i,end:n});case"instant":return new c({start:i,end:i});case"cumulative-from-start":return new c({start:null,end:i});case"cumulative-from-end":return new c({start:i,end:null});default:return c.allTime}}async _animate(){try{const t=this.view,e=this._animationController?.signal;await Promise.all([s(this.playRate,null,e),t&&m((()=>!t.updating),e)])}catch(t){return r(t)||n.getLogger(this).error(t),void(this._animationController=null)}this._step({forward:!0,allowRestart:!1}),this._animationController&&this._animate()}_divideTimeExtentByCount(t,e=10){if(!t||!e)return[];const{start:i,end:n}=t;if(!i||!n)return[];if(e>v)return this._divideTimeExtentByCount(t);const s=[],r=i.getTime(),o=n.getTime()-r;for(let l=0;l<=e;l++)s.push(new Date(r+l/e*o));return s}_divideTimeExtentByInterval(t,e,i=v){if(!t||!e)return[];const{start:n,end:s}=t;if(!n||!s)return[];const r=s.getTime()-n.getTime(),o=e.toMilliseconds();if(o<=0||r/o>i)return this._divideTimeExtentByCount(t);const l=[],{value:a,unit:m}=e;let p=n;for(;p.getTime()<=s.getTime();)l.push(new Date(p.getTime())),p=u(p,a,m);return l}_getDefaultTimeExtent(){const{effectiveStops:t,mode:e}=this;if(!t||!e)return null;switch(e){case"time-window":return t.length>1?new c({start:t[0],end:t[1]}):null;case"cumulative-from-start":return t.length>0?new c({start:null,end:t[0]}):null;case"cumulative-from-end":return t.length>0?new c({start:t[0],end:null}):null;case"instant":return t.length>0?new c({start:t[0],end:t[0]}):null;default:return null}}_registerWidget(t){if(t){t.persistableViewModels.includes(this)||t.persistableViewModels.add(this)}}_startAnimation(){this._stopAnimation(),this._animationController=new AbortController,this._step({forward:!0,allowRestart:!0}),this._animate()}_step(t){const{forward:e,allowRestart:i}=t,{effectiveStops:n}=this;if(!this.timeExtentValues||!n)return;const s=n.map((t=>t.getTime())),r=this.timeExtentValues.map((t=>{const e=s.indexOf(t);if(-1!==e)return e;const i=s.reduce(((e,i)=>Math.abs(i-t)<Math.abs(e-t)?i:e));return s.indexOf(i)})),o=r.map((t=>t+(e?1:-1))),l=o.some((t=>t<0||t>s.length-1)),{loop:a,state:m}=this;if(l)if(a||i){const t=Math.min(...r),i=Math.max(...r),n=(e?r.map((e=>e-t)):r.map((t=>t+(s.length-1-i)))).map((t=>s[t]));this.timeExtent=this.valuesToTimeExtent(n)}else"playing"===m&&this.stop();else{const t=o.map((t=>s[t]));this.timeExtent=this.valuesToTimeExtent(t)}}_stopAnimation(){null!=this._animationController&&(this._animationController.abort(),this._animationController=null)}_unregisterWidget(t){null!=t&&t.persistableViewModels.remove(this)}};t([p()],_.prototype,"_animationController",void 0),t([p({type:e})],_.prototype,"actions",void 0),t([p({readOnly:!0})],_.prototype,"effectiveStops",null),t([p({type:c})],_.prototype,"fullTimeExtent",void 0),t([p({nonNullable:!0})],_.prototype,"loop",void 0),t([p({nonNullable:!0})],_.prototype,"mode",void 0),t([p({nonNullable:!0,value:1e3})],_.prototype,"playRate",null),t([p({readOnly:!0})],_.prototype,"state",null),t([p({cast:t=>t?(x(t)&&(t.interval=h(f,t.interval)),(x(t)||T(t))&&(t.timeExtent=h(c,t.timeExtent)),t):null})],_.prototype,"stops",void 0),t([p({type:c})],_.prototype,"timeExtent",void 0),t([p({readOnly:!0})],_.prototype,"timeExtentValues",null),t([p()],_.prototype,"view",void 0),t([p()],_.prototype,"next",null),t([p()],_.prototype,"play",null),t([p()],_.prototype,"previous",null),t([p()],_.prototype,"stop",null),t([p()],_.prototype,"updateWebDocument",null),_=t([d("esri.widgets.TimeSlider.TimeSliderViewModel")],_);const y=_;export{y as default};