Stairs/web/static/assets/dist/stairs.js
2017-05-06 15:27:20 +02:00

1 line
5.2 KiB
JavaScript

define(["require","exports","knockout","log"],function(e,t,n,i){"use strict";t.__esModule=!0;var r=null,o=function(){function e(){this.Brightness=n.observable(0),this.Ease=n.observable(!1)}return e.prototype.read=function(e){this.Brightness(e.brightness)},e}();t.StairsStaticMode=o;var a=function(){function e(){this.Brightness=n.observableArray([])}return e.prototype.init=function(e){for(var t=[],i=0;i<e;i++)t.push({value:n.observable(0)});this.Brightness(t)},e.prototype.read=function(e){var t=e.brightness.map(function(e){return{value:n.observable(e)}});t.reverse(),this.Brightness(t)},e}();t.StairsCustomMode=a;var s=function(){function e(){this.Interval=n.observable(500),this.Brightness=n.observable(0)}return e.prototype.read=function(e){this.Interval(e.interval),this.Brightness(e.brightness)},e}();t.StairsAlternateMode=s;var u;!function(e){e[e.Unknown=0]="Unknown",e[e.Static=1]="Static",e[e.Custom=2]="Custom",e[e.Alternate=3]="Alternate"}(u=t.StairsMode||(t.StairsMode={}));var g=function(){function e(){this.Static=new o,this.Custom=new a,this.Alternate=new s,this.Current=n.observable(u.Unknown)}return e.prototype.read=function(e){switch(this.Current()){case u.Static:this.Static.read(e);break;case u.Custom:this.Custom.read(e);break;case u.Alternate:this.Alternate.read(e)}},e}();t.StairsModeParameters=g;var d=function(){function e(){this.Start=n.observable(0),this.End=n.observable(4095)}return e}();t.StairsRangeValue=d;var p=function(){function e(){this.UseScaling=n.observable(!1),this.Values=n.observableArray([])}return e.prototype.read=function(e){this.UseScaling(e.useScaling);var t=e.values.map(function(e){var t=new d;return t.Start(e.start),t.End(e.end),t});t.reverse(),this.Values(t)},e}();t.StairsRange=p;var l=function(){function e(){var e=this;this.Loading=n.observable(!1),this.SavingMode=n.observable(!1),this.SavingSettings=n.observable(!1),this.Saving=n.pureComputed(function(){return e.SavingMode()||e.SavingSettings()}),this.Mode=new g,this.Range=new p,this.pingTimer=null,this.pingRequest=null,this.updatingFromServer=!0,this.updateModeTimeout=null,this.updateRangeTimeout=null,this.updateMode=n.computed(function(){if(!e.Loading()&&!e.SavingMode.peek()){var t="/setMode/";switch(e.Mode.Current()){case u.Static:t+="Static?brightness="+encodeURIComponent(e.Mode.Static.Brightness().toString());break;case u.Custom:var n=e.Mode.Custom.Brightness().map(function(e){return e.value()});n.reverse(),t+="Custom?brightness="+encodeURIComponent(n.join());break;case u.Alternate:t+="Alternate?interval="+encodeURIComponent(e.Mode.Alternate.Interval().toString())+"&brightness="+encodeURIComponent(e.Mode.Alternate.Brightness().toString())}if(!e.updatingFromServer)return null!==e.updateModeTimeout&&(clearTimeout(e.updateModeTimeout),e.updateModeTimeout=null),e.updateModeTimeout=setTimeout(function(){i.Log.info("Stairs.updateMode",t),e.SavingMode(!0),$.ajax({url:t,dataType:"json",cache:!1}).always(function(){e.SavingMode(!1)}),clearTimeout(e.updateModeTimeout),e.updateModeTimeout=null},200),!0}}),this.updateSettings=n.computed(function(){if(!e.Loading()&&!e.SavingSettings.peek()){var t="/setRange?useScaling="+e.Range.UseScaling().toString(),n=e.Range.Values().map(function(e){return e.Start()}),r=e.Range.Values().map(function(e){return e.End()});if(n.reverse(),r.reverse(),t+="&start="+encodeURIComponent(n.join()),t+="&end="+encodeURIComponent(r.join()),!e.updatingFromServer)return null!==e.updateRangeTimeout&&(clearTimeout(e.updateRangeTimeout),e.updateRangeTimeout=null),e.updateRangeTimeout=setTimeout(function(){i.Log.info("Stairs.updateSettings",t),e.SavingSettings(!0),$.ajax({url:t,dataType:"json",cache:!1}).always(function(){e.SavingSettings(!1)}),clearTimeout(e.updateRangeTimeout),e.updateRangeTimeout=null},200),!0}}),this.ping()}return e.instance=function(){return null==r&&(r=new e),r},e.prototype.ping=function(){var e=this;return null!==this.pingRequest?void i.Log.verbose("Stairs.ping","Ping request already running, skipping"):(null!==this.pingTimer&&(clearTimeout(this.pingTimer),this.pingTimer=null),this.Loading()||this.Saving()?void(this.pingTimer=setTimeout(function(){return e.ping()},5e3)):(i.Log.verbose("Stairs.ping","Starting Ping request"),this.updatingFromServer=!0,this.Loading(!0),this.pingRequest=$.ajax({url:"/ping",dataType:"json",cache:!1}),this.pingRequest.done(function(t){i.Log.verbose("Stairs.ping",t),e.Mode.Custom.init(t.stepCount),$.when(e.getMode(),e.getRange()).done(function(){return e.pingComplete(!0)}).fail(function(){return e.pingComplete(!1)})}),void this.pingRequest.fail(function(){i.Log.warning("Stairs.ping","Ping failed"),e.pingComplete(!0)})))},e.prototype.pingComplete=function(e){var t=this;this.pingRequest=null,e||(this.pingTimer=setTimeout(function(){return t.ping()},5e3)),e&&(this.Loading(!1),this.updatingFromServer=!1)},e.prototype.getMode=function(){var e=this;i.Log.verbose("Stairs.getMode","Requesting Mode");var t=$.ajax({url:"/getMode",dataType:"json",cache:!1});return t.done(function(t){i.Log.verbose("Stairs.getMode",t),e.Mode.Current(t.mode),e.Mode.read(t.data)}),t},e.prototype.getRange=function(){var e=this;i.Log.verbose("Stairs.getRange","Requesting Range configuration");var t=$.ajax({url:"/getRange",dataType:"json",cache:!1});return t.done(function(t){i.Log.verbose("Stairs.getRange",t),e.Range.read(t)}),t},e}();t.Stairs=l});