/*! grafana - v4.5.1 - 2017-09-19
 * Copyright (c) 2017 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["moment","lodash","app/core/core_module","app/core/utils/kbn","app/core/utils/datemath"],function(a,b){"use strict";var c,d,e,f,g,h;b&&b.id;return{setters:[function(a){c=a},function(a){d=a},function(a){e=a},function(a){f=a},function(a){g=a}],execute:function(){h=function(){function a(a,b,c,d,e){var f=this;this.$rootScope=a,this.$timeout=b,this.$location=c,this.timer=d,this.contextSrv=e,this.time={from:"6h",to:"now"},a.$on("zoom-out",this.zoomOut.bind(this)),a.$on("$routeUpdate",this.routeUpdated.bind(this)),document.addEventListener("visibilitychange",function(){f.autoRefreshBlocked&&"visible"===document.visibilityState&&(f.autoRefreshBlocked=!1,f.refreshDashboard())})}return a.$inject=["$rootScope","$timeout","$location","timer","contextSrv"],a.prototype.init=function(a){this.timer.cancelAll(),this.dashboard=a,this.time=a.time,this.refresh=a.refresh,this.initTimeFromUrl(),this.parseTime(),this.timeAtLoad=d.default.cloneDeep(this.time),this.refresh&&this.setAutoRefresh(this.refresh)},a.prototype.parseTime=function(){d.default.isString(this.time.from)&&this.time.from.indexOf("Z")>=0&&(this.time.from=c.default(this.time.from).utc()),d.default.isString(this.time.to)&&this.time.to.indexOf("Z")>=0&&(this.time.to=c.default(this.time.to).utc())},a.prototype.parseUrlParam=function(a){if(a.indexOf("now")!==-1)return a;if(8===a.length)return c.default.utc(a,"YYYYMMDD");if(15===a.length)return c.default.utc(a,"YYYYMMDDTHHmmss");if(!isNaN(a)){var b=parseInt(a);return c.default.utc(b)}return null},a.prototype.initTimeFromUrl=function(){var a=this.$location.search();a.from&&(this.time.from=this.parseUrlParam(a.from)||this.time.from),a.to&&(this.time.to=this.parseUrlParam(a.to)||this.time.to),a.refresh&&(this.refresh=a.refresh||this.refresh)},a.prototype.routeUpdated=function(){var a=this.$location.search(),b=this.timeRangeForUrl();a.from&&a.to?a.from===b.from&&a.to===b.to||(this.initTimeFromUrl(),this.setTime(this.time,!0)):this.timeHasChangedSinceLoad()&&this.setTime(this.timeAtLoad,!0)},a.prototype.timeHasChangedSinceLoad=function(){return this.timeAtLoad.from!==this.time.from||this.timeAtLoad.to!==this.time.to},a.prototype.setAutoRefresh=function(a){var b=this;if(this.dashboard.refresh=a,this.cancelNextRefresh(),a){var c=f.default.interval_to_ms(a);this.refreshTimer=this.timer.register(this.$timeout(function(){b.startNextRefreshTimer(c),b.refreshDashboard()},c))}var d=this.$location.search();a?(d.refresh=a,this.$location.search(d)):d.refresh&&(delete d.refresh,this.$location.search(d))},a.prototype.refreshDashboard=function(){this.$rootScope.$broadcast("refresh")},a.prototype.startNextRefreshTimer=function(a){var b=this;this.cancelNextRefresh(),this.refreshTimer=this.timer.register(this.$timeout(function(){b.startNextRefreshTimer(a),b.contextSrv.isGrafanaVisible()?b.refreshDashboard():b.autoRefreshBlocked=!0},a))},a.prototype.cancelNextRefresh=function(){this.timer.cancel(this.refreshTimer)},a.prototype.setTime=function(a,b){if(d.default.extend(this.time,a),c.default.isMoment(a.to)?(this.oldRefresh=this.dashboard.refresh||this.oldRefresh,this.setAutoRefresh(!1)):this.oldRefresh&&this.oldRefresh!==this.dashboard.refresh&&(this.setAutoRefresh(this.oldRefresh),this.oldRefresh=null),b!==!0){var e=this.timeRangeForUrl(),f=this.$location.search();f.from=e.from,f.to=e.to,this.$location.search(f)}this.$rootScope.appEvent("time-range-changed",this.time),this.$timeout(this.refreshDashboard.bind(this),0)},a.prototype.timeRangeForUrl=function(){var a=this.timeRange().raw;return c.default.isMoment(a.from)&&(a.from=a.from.valueOf().toString()),c.default.isMoment(a.to)&&(a.to=a.to.valueOf().toString()),a},a.prototype.timeRange=function(){var a={from:c.default.isMoment(this.time.from)?c.default(this.time.from):this.time.from,to:c.default.isMoment(this.time.to)?c.default(this.time.to):this.time.to};return{from:g.parse(a.from,!1),to:g.parse(a.to,!0),raw:a}},a.prototype.zoomOut=function(a,b){var d=this.timeRange(),e=d.to.valueOf()-d.from.valueOf(),f=d.to.valueOf()-e/2,g=f+e*b/2,h=f-e*b/2;if(g>Date.now()&&d.to<=Date.now()){var i=g-Date.now();h-=i,g=Date.now()}this.setTime({from:c.default.utc(h),to:c.default.utc(g)})},a}(),e.default.service("timeSrv",h)}}});