2015-09-12 04:49:14 -05:00
|
|
|
///<reference path="../../../headers/common.d.ts" />
|
2015-09-17 15:44:59 -05:00
|
|
|
///<amd-dependency path="./input_date" name="inputDate" />
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2015-09-12 04:49:14 -05:00
|
|
|
import angular = require('angular');
|
|
|
|
import _ = require('lodash');
|
|
|
|
import moment = require('moment');
|
|
|
|
import kbn = require('kbn');
|
2015-09-16 12:49:05 -05:00
|
|
|
import dateMath = require('app/core/utils/datemath');
|
2015-09-17 02:44:51 -05:00
|
|
|
import rangeUtil = require('app/core/utils/rangeutil');
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2015-09-17 15:44:59 -05:00
|
|
|
declare var inputDate: any;
|
|
|
|
|
2015-09-12 04:49:14 -05:00
|
|
|
export class TimePickerCtrl {
|
2014-08-27 09:29:48 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
static defaults = {
|
|
|
|
status : "Stable",
|
2015-09-17 02:44:51 -05:00
|
|
|
time_options : ['5m','15m','1h','6h','12h','24h','2d','7d','30d'],
|
2015-09-12 05:52:50 -05:00
|
|
|
refresh_intervals : ['5s','10s','30s','1m','5m','15m','30m','1h','2h','1d'],
|
|
|
|
};
|
2014-01-06 12:00:08 -06:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
static patterns = {
|
2013-10-09 03:36:41 -05:00
|
|
|
date: /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/,
|
|
|
|
hour: /^([01]?[0-9]|2[0-3])$/,
|
|
|
|
minute: /^[0-5][0-9]$/,
|
|
|
|
second: /^[0-5][0-9]$/,
|
|
|
|
millisecond: /^[0-9]*$/
|
2015-09-12 05:52:50 -05:00
|
|
|
};
|
2013-10-09 03:36:41 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
constructor(private $scope : any, private $rootScope, private timeSrv) {
|
|
|
|
$scope.patterns = TimePickerCtrl.patterns;
|
2014-08-27 10:58:49 -05:00
|
|
|
$scope.timeSrv = timeSrv;
|
2015-09-12 05:52:50 -05:00
|
|
|
$scope.ctrl = this;
|
2014-08-27 10:58:49 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
$scope.$on('refresh', () => this.init());
|
2015-09-12 04:49:14 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
this.init();
|
|
|
|
}
|
2015-01-25 06:45:08 -06:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
init() {
|
|
|
|
this.$scope.panel = this.$scope.dashboard.timepicker;
|
2015-09-17 05:40:04 -05:00
|
|
|
this.$scope.panel.now = false;
|
2015-03-28 11:53:52 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
_.defaults(this.$scope.panel, TimePickerCtrl.defaults);
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2015-09-17 05:40:04 -05:00
|
|
|
var time = this.timeSrv.timeRange();
|
|
|
|
var timeRaw = this.timeSrv.timeRange(false);
|
2015-08-19 06:25:20 -05:00
|
|
|
|
2015-09-17 05:40:04 -05:00
|
|
|
if (_.isString(timeRaw.to) && timeRaw.to.indexOf('now') === 0) {
|
2015-09-12 05:52:50 -05:00
|
|
|
this.$scope.panel.now = true;
|
2015-09-17 15:44:59 -05:00
|
|
|
this.$scope.rangeString = rangeUtil.describeTimeRange(timeRaw);
|
|
|
|
} else {
|
|
|
|
let format = 'MMM D, YYYY HH:mm:ss';
|
|
|
|
this.$scope.rangeString = this.$scope.dashboard.formatDate(time.from, format) + ' to ' +
|
|
|
|
this.$scope.dashboard.formatDate(time.to, format);
|
2015-09-12 05:52:50 -05:00
|
|
|
}
|
2015-08-11 12:16:45 -05:00
|
|
|
|
2015-09-17 15:44:59 -05:00
|
|
|
this.$scope.timeRaw = timeRaw;
|
|
|
|
this.$scope.absolute = {form: time.from.toDate(), to: time.to.toDate()};
|
2015-09-12 05:52:50 -05:00
|
|
|
this.$scope.onAppEvent('zoom-out', function() {
|
|
|
|
this.$scope.zoom(2);
|
|
|
|
});
|
2013-10-09 03:36:41 -05:00
|
|
|
|
2015-09-17 15:44:59 -05:00
|
|
|
this.$scope.tooltip = this.$scope.dashboard.formatDate(time.from.date) + ' <br>to<br>';
|
|
|
|
this.$scope.tooltip += this.$scope.dashboard.formatDate(time.to.date);
|
2015-09-12 05:52:50 -05:00
|
|
|
}
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
loadTimeOptions() {
|
2015-09-17 15:44:59 -05:00
|
|
|
this.$scope.timeOptions = rangeUtil.getRelativeTimesList(this.$scope.panel, this.$scope.rangeString);
|
|
|
|
this.$scope.appEvent('show-dash-editor', {
|
|
|
|
src: 'app/features/dashboard/timepicker/dropdown.html',
|
|
|
|
scope: this.$scope,
|
|
|
|
cssClass: 'gf-timepicker-dropdown',
|
|
|
|
});
|
2015-09-12 05:52:50 -05:00
|
|
|
}
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
customTime() {
|
|
|
|
// Assume the form is valid since we're setting it to something valid
|
|
|
|
this.$scope.input.$setValidity("dummy", true);
|
2015-09-17 05:40:04 -05:00
|
|
|
this.$scope.temptime = angular.copy(this.$scope.time);
|
2015-09-12 05:52:50 -05:00
|
|
|
this.$scope.temptime.now = this.$scope.panel.now;
|
2013-12-30 17:27:17 -06:00
|
|
|
|
2015-09-17 05:40:04 -05:00
|
|
|
// this.$scope.temptime.from.date.setHours(0, 0, 0, 0);
|
|
|
|
// this.$scope.temptime.to.date.setHours(0, 0, 0, 0);
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
// Date picker needs the date to be at the start of the day
|
|
|
|
if (new Date().getTimezoneOffset() < 0) {
|
|
|
|
this.$scope.temptime.from.date = moment(this.$scope.temptime.from.date).add(1, 'days').toDate();
|
|
|
|
this.$scope.temptime.to.date = moment(this.$scope.temptime.to.date).add(1, 'days').toDate();
|
|
|
|
}
|
2015-01-25 06:45:08 -06:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
this.$scope.appEvent('show-dash-editor', {
|
|
|
|
src: 'app/features/dashboard/timepicker/custom.html',
|
|
|
|
scope: this.$scope
|
|
|
|
});
|
|
|
|
}
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
setAbsoluteTimeFilter(time) {
|
|
|
|
// Create filter object
|
|
|
|
var _filter = _.clone(time);
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
if (time.now) {
|
|
|
|
_filter.to = "now";
|
|
|
|
}
|
2013-10-09 03:36:41 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
this.timeSrv.setTime(_filter);
|
|
|
|
}
|
2014-08-14 15:30:19 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
setRelativeFilter(timespan) {
|
|
|
|
this.$scope.panel.now = true;
|
2014-08-14 15:30:19 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
var range = {from: timespan.from, to: timespan.to};
|
2014-08-15 01:02:16 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
if (this.$scope.panel.nowDelay) {
|
|
|
|
range.to = 'now-' + this.$scope.panel.nowDelay;
|
|
|
|
}
|
2013-10-09 03:36:41 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
this.timeSrv.setTime(range);
|
2015-09-17 15:44:59 -05:00
|
|
|
this.$scope.appEvent('hide-dash-editor');
|
2015-09-12 05:52:50 -05:00
|
|
|
}
|
2013-10-09 03:36:41 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
validate(time): any {
|
|
|
|
// Assume the form is valid. There is a hidden dummy input for invalidating it programatically.
|
|
|
|
this.$scope.input.$setValidity("dummy", true);
|
2015-03-28 11:53:52 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
var _from = this.datepickerToLocal(time.from.date);
|
|
|
|
var _to = this.datepickerToLocal(time.to.date);
|
|
|
|
var _t = time;
|
2015-03-28 11:53:52 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
if (this.$scope.input.$valid) {
|
|
|
|
_from.setHours(_t.from.hour, _t.from.minute, _t.from.second, _t.from.millisecond);
|
|
|
|
_to.setHours(_t.to.hour, _t.to.minute, _t.to.second, _t.to.millisecond);
|
2015-03-28 11:53:52 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
// Check that the objects are valid and to is after from
|
|
|
|
if (isNaN(_from.getTime()) || isNaN(_to.getTime()) || _from.getTime() >= _to.getTime()) {
|
|
|
|
this.$scope.input.$setValidity("dummy", false);
|
|
|
|
return false;
|
2015-03-28 11:53:52 -05:00
|
|
|
}
|
2015-09-12 05:52:50 -05:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-28 11:53:52 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
return { from: _from, to: _to, now: time.now };
|
|
|
|
}
|
2015-03-28 11:53:52 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
datepickerToLocal(date) {
|
|
|
|
date = moment(date).clone().toDate();
|
|
|
|
return moment(new Date(date.getTime() + date.getTimezoneOffset() * 60000)).toDate();
|
2015-09-12 04:49:14 -05:00
|
|
|
}
|
2015-09-12 05:52:50 -05:00
|
|
|
|
2015-09-12 04:49:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function settingsDirective() {
|
|
|
|
'use strict';
|
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
templateUrl: 'app/features/dashboard/timepicker/settings.html',
|
|
|
|
controller: TimePickerCtrl,
|
|
|
|
scope: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function timePickerDirective() {
|
|
|
|
'use strict';
|
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
templateUrl: 'app/features/dashboard/timepicker/timepicker.html',
|
|
|
|
controller: TimePickerCtrl,
|
|
|
|
scope: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
|
|
|
|
angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);
|