| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- ///<reference path="../../../headers/common.d.ts" />
- import _ from 'lodash';
- import angular from 'angular';
- import moment from 'moment';
- import * as rangeUtil from 'app/core/utils/rangeutil';
- export class TimePickerCtrl {
- static tooltipFormat = 'MMM D, YYYY HH:mm:ss';
- static defaults = {
- time_options: ['5m', '15m', '1h', '6h', '12h', '24h', '2d', '7d', '30d'],
- refresh_intervals: ['5s', '10s', '30s', '1m', '5m', '15m', '30m', '1h', '2h', '1d'],
- };
- dashboard: any;
- panel: any;
- absolute: any;
- timeRaw: any;
- tooltip: string;
- rangeString: string;
- timeOptions: any;
- refresh: any;
- isOpen: boolean;
- isUtc: boolean;
- /** @ngInject */
- constructor(private $scope, private $rootScope, private timeSrv) {
- $scope.ctrl = this;
- $rootScope.onAppEvent('zoom-out', () => this.zoom(2), $scope);
- $rootScope.onAppEvent('refresh', () => this.init(), $scope);
- $rootScope.onAppEvent('dash-editor-hidden', () => this.isOpen = false, $scope);
- this.init();
- }
- init() {
- this.panel = this.dashboard.timepicker;
- _.defaults(this.panel, TimePickerCtrl.defaults);
- var time = angular.copy(this.timeSrv.timeRange());
- var timeRaw = angular.copy(this.timeSrv.timeRange(false));
- if (this.dashboard.timezone === 'browser') {
- time.from.local();
- time.to.local();
- if (moment.isMoment(timeRaw.from)) {
- timeRaw.from.local();
- }
- if (moment.isMoment(timeRaw.to)) {
- timeRaw.to.local();
- }
- } else {
- this.isUtc = true;
- }
- this.rangeString = rangeUtil.describeTimeRange(timeRaw);
- this.absolute = {fromJs: time.from.toDate(), toJs: time.to.toDate()};
- this.tooltip = this.dashboard.formatDate(time.from) + ' <br>to<br>';
- this.tooltip += this.dashboard.formatDate(time.to);
- // do not update time raw when dropdown is open
- // as auto refresh will reset the from/to input fields
- if (!this.isOpen) {
- this.timeRaw = timeRaw;
- }
- }
- zoom(factor) {
- var range = this.timeSrv.timeRange();
- var timespan = (range.to.valueOf() - range.from.valueOf());
- var center = range.to.valueOf() - timespan/2;
- var to = (center + (timespan*factor)/2);
- var from = (center - (timespan*factor)/2);
- if (to > Date.now() && range.to <= Date.now()) {
- var offset = to - Date.now();
- from = from - offset;
- to = Date.now();
- }
- this.timeSrv.setTime({from: moment.utc(from), to: moment.utc(to) });
- }
- openDropdown() {
- this.init();
- this.isOpen = true;
- this.timeOptions = rangeUtil.getRelativeTimesList(this.panel, this.rangeString);
- this.refresh = {
- value: this.dashboard.refresh,
- options: _.map(this.panel.refresh_intervals, (interval: any) => {
- return {text: interval, value: interval};
- })
- };
- this.refresh.options.unshift({text: 'off'});
- this.$rootScope.appEvent('show-dash-editor', {
- src: 'app/features/dashboard/timepicker/dropdown.html',
- scope: this.$scope,
- cssClass: 'gf-timepicker-dropdown',
- });
- }
- applyCustom() {
- if (this.refresh.value !== this.dashboard.refresh) {
- this.timeSrv.setAutoRefresh(this.refresh.value);
- }
- this.timeSrv.setTime(this.timeRaw, true);
- this.$rootScope.appEvent('hide-dash-editor');
- }
- absoluteFromChanged() {
- this.timeRaw.from = this.getAbsoluteMomentForTimezone(this.absolute.fromJs);
- }
- absoluteToChanged() {
- this.timeRaw.to = this.getAbsoluteMomentForTimezone(this.absolute.toJs);
- }
- getAbsoluteMomentForTimezone(jsDate) {
- return this.dashboard.timezone === 'browser' ? moment(jsDate) : moment(jsDate).utc();
- }
- setRelativeFilter(timespan) {
- this.panel.now = true;
- var range = {from: timespan.from, to: timespan.to};
- if (this.panel.nowDelay && range.to === 'now') {
- range.to = 'now-' + this.panel.nowDelay;
- }
- this.timeSrv.setTime(range);
- this.$rootScope.appEvent('hide-dash-editor');
- }
- }
- export function settingsDirective() {
- 'use strict';
- return {
- restrict: 'E',
- templateUrl: 'app/features/dashboard/timepicker/settings.html',
- controller: TimePickerCtrl,
- bindToController: true,
- controllerAs: 'ctrl',
- scope: {
- dashboard: "="
- }
- };
- }
- export function timePickerDirective() {
- 'use strict';
- return {
- restrict: 'E',
- templateUrl: 'app/features/dashboard/timepicker/timepicker.html',
- controller: TimePickerCtrl,
- bindToController: true,
- controllerAs: 'ctrl',
- scope: {
- dashboard: "="
- }
- };
- }
- angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
- angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);
- import {inputDateDirective} from './input_date';
- angular.module("grafana.directives").directive('inputDatetime', inputDateDirective);
|