/*! grafana - v3.1.0 - 2016-07-18
 * Copyright (c) 2016 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["lodash","angular","moment","app/core/utils/rangeutil","./input_date"],function(a){function b(){return{restrict:"E",templateUrl:"public/app/features/dashboard/timepicker/settings.html",controller:i,bindToController:!0,controllerAs:"ctrl",scope:{dashboard:"="}}}function c(){return{restrict:"E",templateUrl:"public/app/features/dashboard/timepicker/timepicker.html",controller:i,bindToController:!0,controllerAs:"ctrl",scope:{dashboard:"="}}}var d,e,f,g,h,i;return a("settingsDirective",b),a("timePickerDirective",c),{setters:[function(a){d=a},function(a){e=a},function(a){f=a},function(a){g=a},function(a){h=a}],execute:function(){i=function(){function a(a,b,c){var d=this;this.$scope=a,this.$rootScope=b,this.timeSrv=c,a.ctrl=this,b.onAppEvent("zoom-out",function(){return d.zoom(2)},a),b.onAppEvent("shift-time-forward",function(){return d.move(1)},a),b.onAppEvent("shift-time-backward",function(){return d.move(-1)},a),b.onAppEvent("refresh",function(){return d.init()},a),b.onAppEvent("dash-editor-hidden",function(){return d.isOpen=!1},a),this.init()}return a.$inject=["$scope","$rootScope","timeSrv"],a.prototype.init=function(){this.panel=this.dashboard.timepicker,d["default"].defaults(this.panel,a.defaults);var b=e["default"].copy(this.timeSrv.timeRange()),c=e["default"].copy(this.timeSrv.timeRange(!1));this.dashboard.isTimezoneUtc()?this.isUtc=!0:(b.from.local(),b.to.local(),f["default"].isMoment(c.from)&&c.from.local(),f["default"].isMoment(c.to)&&c.to.local()),this.rangeString=g.describeTimeRange(c),this.absolute={fromJs:b.from.toDate(),toJs:b.to.toDate()},this.tooltip=this.dashboard.formatDate(b.from)+" <br>to<br>",this.tooltip+=this.dashboard.formatDate(b.to),this.isOpen||(this.timeRaw=c)},a.prototype.zoom=function(a){var b=this.timeSrv.timeRange(),c=b.to.valueOf()-b.from.valueOf(),d=b.to.valueOf()-c/2,e=d+c*a/2,g=d-c*a/2;if(e>Date.now()&&b.to<=Date.now()){var h=e-Date.now();g-=h,e=Date.now()}this.timeSrv.setTime({from:f["default"].utc(g),to:f["default"].utc(e)})},a.prototype.move=function(a){var b,c,d=this.timeSrv.timeRange(),e=(d.to.valueOf()-d.from.valueOf())/2;a===-1?(b=d.to.valueOf()-e,c=d.from.valueOf()-e):1===a?(b=d.to.valueOf()+e,c=d.from.valueOf()+e,b>Date.now()&&d.to<Date.now()&&(b=Date.now(),c=d.from.valueOf())):(b=d.to.valueOf(),c=d.from.valueOf()),this.timeSrv.setTime({from:f["default"].utc(c),to:f["default"].utc(b)})},a.prototype.openDropdown=function(){this.init(),this.isOpen=!0,this.timeOptions=g.getRelativeTimesList(this.panel,this.rangeString),this.refresh={value:this.dashboard.refresh,options:d["default"].map(this.panel.refresh_intervals,function(a){return{text:a,value:a}})},this.refresh.options.unshift({text:"off"}),this.$rootScope.appEvent("show-dash-editor",{src:"public/app/features/dashboard/timepicker/dropdown.html",scope:this.$scope,cssClass:"gf-timepicker-dropdown"})},a.prototype.applyCustom=function(){this.refresh.value!==this.dashboard.refresh&&this.timeSrv.setAutoRefresh(this.refresh.value),this.timeSrv.setTime(this.timeRaw,!0),this.$rootScope.appEvent("hide-dash-editor")},a.prototype.absoluteFromChanged=function(){this.timeRaw.from=this.getAbsoluteMomentForTimezone(this.absolute.fromJs)},a.prototype.absoluteToChanged=function(){this.timeRaw.to=this.getAbsoluteMomentForTimezone(this.absolute.toJs)},a.prototype.getAbsoluteMomentForTimezone=function(a){return this.dashboard.isTimezoneUtc()?f["default"](a).utc():f["default"](a)},a.prototype.setRelativeFilter=function(a){var b={from:a.from,to:a.to};this.panel.nowDelay&&"now"===b.to&&(b.to="now-"+this.panel.nowDelay),this.timeSrv.setTime(b),this.$rootScope.appEvent("hide-dash-editor")},a.tooltipFormat="MMM D, YYYY HH:mm:ss",a.defaults={time_options:["5m","15m","1h","6h","12h","24h","2d","7d","30d"],refresh_intervals:["5s","10s","30s","1m","5m","15m","30m","1h","2h","1d"]},a}(),a("TimePickerCtrl",i),e["default"].module("grafana.directives").directive("gfTimePickerSettings",b),e["default"].module("grafana.directives").directive("gfTimePicker",c),e["default"].module("grafana.directives").directive("inputDatetime",h.inputDateDirective)}}});