2019-02-06 10:31:52 -06:00
|
|
|
// Libraries
|
2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
|
|
|
import $ from 'jquery';
|
2019-01-10 06:34:23 -06:00
|
|
|
import Drop from 'tether-drop';
|
2016-03-25 16:14:29 -05:00
|
|
|
|
2019-02-06 10:31:52 -06:00
|
|
|
// Utils and servies
|
|
|
|
import { colors } from '@grafana/ui';
|
2019-06-03 10:55:59 -05:00
|
|
|
import { setBackendSrv, BackendSrv, setDataSourceSrv } from '@grafana/runtime';
|
2019-02-06 10:31:52 -06:00
|
|
|
import config from 'app/core/config';
|
2017-12-20 05:33:33 -06:00
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
import { profiler } from 'app/core/profiler';
|
|
|
|
import appEvents from 'app/core/app_events';
|
2019-01-31 01:44:46 -06:00
|
|
|
import { TimeSrv, setTimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
2019-06-03 10:55:59 -05:00
|
|
|
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
|
2019-01-31 08:44:00 -06:00
|
|
|
import { KeybindingSrv, setKeybindingSrv } from 'app/core/services/keybindingSrv';
|
2019-06-03 10:55:59 -05:00
|
|
|
import { AngularLoader, setAngularLoader } from '@grafana/runtime';
|
2018-09-14 02:41:37 -05:00
|
|
|
import { configureStore } from 'app/store/configureStore';
|
2016-01-16 11:55:13 -06:00
|
|
|
|
2019-02-06 10:31:52 -06:00
|
|
|
// Types
|
|
|
|
import { KioskUrlValue } from 'app/types';
|
|
|
|
|
2016-01-16 11:55:13 -06:00
|
|
|
export class GrafanaCtrl {
|
|
|
|
/** @ngInject */
|
2018-04-26 04:58:42 -05:00
|
|
|
constructor(
|
|
|
|
$scope,
|
|
|
|
utilSrv,
|
|
|
|
$rootScope,
|
|
|
|
$controller,
|
|
|
|
contextSrv,
|
|
|
|
bridgeSrv,
|
|
|
|
backendSrv: BackendSrv,
|
2018-10-14 08:39:34 -05:00
|
|
|
timeSrv: TimeSrv,
|
2018-10-04 06:01:43 -05:00
|
|
|
datasourceSrv: DatasourceSrv,
|
2019-01-31 08:44:00 -06:00
|
|
|
keybindingSrv: KeybindingSrv,
|
2018-10-04 06:01:43 -05:00
|
|
|
angularLoader: AngularLoader
|
2018-04-26 04:58:42 -05:00
|
|
|
) {
|
2018-06-19 14:25:57 -05:00
|
|
|
// make angular loader service available to react components
|
|
|
|
setAngularLoader(angularLoader);
|
2018-07-11 13:23:07 -05:00
|
|
|
setBackendSrv(backendSrv);
|
2019-06-03 10:55:59 -05:00
|
|
|
setDataSourceSrv(datasourceSrv);
|
2018-10-14 08:39:34 -05:00
|
|
|
setTimeSrv(timeSrv);
|
2019-01-31 08:44:00 -06:00
|
|
|
setKeybindingSrv(keybindingSrv);
|
2018-09-18 08:19:43 -05:00
|
|
|
configureStore();
|
2017-12-21 04:56:45 -06:00
|
|
|
|
2018-09-04 07:27:03 -05:00
|
|
|
$scope.init = () => {
|
2016-01-16 11:55:13 -06:00
|
|
|
$scope.contextSrv = contextSrv;
|
2017-12-31 15:31:11 -06:00
|
|
|
$scope.appSubUrl = config.appSubUrl;
|
2016-06-07 23:51:01 -05:00
|
|
|
$scope._ = _;
|
2016-01-16 11:55:13 -06:00
|
|
|
|
2016-06-07 23:51:01 -05:00
|
|
|
profiler.init(config, $rootScope);
|
2016-01-16 11:55:13 -06:00
|
|
|
utilSrv.init();
|
2017-12-31 15:31:11 -06:00
|
|
|
bridgeSrv.init();
|
2016-01-16 11:55:13 -06:00
|
|
|
};
|
|
|
|
|
2018-01-09 04:13:37 -06:00
|
|
|
$rootScope.colors = colors;
|
2019-02-05 05:10:42 -06:00
|
|
|
|
2016-01-16 11:55:13 -06:00
|
|
|
$rootScope.onAppEvent = function(name, callback, localScope) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const unbind = $rootScope.$on(name, callback);
|
2018-08-30 01:58:43 -05:00
|
|
|
let callerScope = this;
|
2016-01-16 11:55:13 -06:00
|
|
|
if (callerScope.$id === 1 && !localScope) {
|
2017-12-20 05:33:33 -06:00
|
|
|
console.log('warning rootScope onAppEvent called without localscope');
|
2016-01-16 11:55:13 -06:00
|
|
|
}
|
|
|
|
if (localScope) {
|
|
|
|
callerScope = localScope;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
callerScope.$on('$destroy', unbind);
|
2016-01-16 11:55:13 -06:00
|
|
|
};
|
|
|
|
|
2018-09-04 07:27:03 -05:00
|
|
|
$rootScope.appEvent = (name, payload) => {
|
2016-01-16 11:55:13 -06:00
|
|
|
$rootScope.$emit(name, payload);
|
2016-03-25 16:14:29 -05:00
|
|
|
appEvents.emit(name, payload);
|
2016-01-16 11:55:13 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 07:18:22 -05:00
|
|
|
function setViewModeBodyClass(body: JQuery, mode: KioskUrlValue) {
|
2018-08-30 04:52:31 -05:00
|
|
|
body.removeClass('view-mode--tv');
|
|
|
|
body.removeClass('view-mode--kiosk');
|
|
|
|
body.removeClass('view-mode--inactive');
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case 'tv': {
|
|
|
|
body.addClass('view-mode--tv');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// 1 & true for legacy states
|
2018-10-23 08:37:11 -05:00
|
|
|
case '1':
|
2018-08-30 04:52:31 -05:00
|
|
|
case true: {
|
|
|
|
body.addClass('view-mode--kiosk');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 10:31:33 -06:00
|
|
|
/** @ngInject */
|
2017-12-31 15:31:11 -06:00
|
|
|
export function grafanaAppDirective(playlistSrv, contextSrv, $timeout, $rootScope, $location) {
|
2016-01-16 11:55:13 -06:00
|
|
|
return {
|
2017-12-20 05:33:33 -06:00
|
|
|
restrict: 'E',
|
2016-01-16 11:55:13 -06:00
|
|
|
controller: GrafanaCtrl,
|
|
|
|
link: (scope, elem) => {
|
2018-08-29 07:27:29 -05:00
|
|
|
const body = $('body');
|
2017-01-16 14:08:39 -06:00
|
|
|
|
|
|
|
// see https://github.com/zenorocha/clipboard.js/issues/155
|
2018-09-04 07:27:03 -05:00
|
|
|
$.fn.modal.Constructor.prototype.enforceFocus = () => {};
|
2016-02-12 08:00:47 -06:00
|
|
|
|
2018-08-31 02:57:27 -05:00
|
|
|
$('.preloader').remove();
|
2016-02-12 08:00:47 -06:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
appEvents.on('toggle-sidemenu-mobile', () => {
|
|
|
|
body.toggleClass('sidemenu-open--xs');
|
2017-12-07 09:25:21 -06:00
|
|
|
});
|
2017-12-04 05:37:00 -06:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
appEvents.on('toggle-sidemenu-hidden', () => {
|
|
|
|
body.toggleClass('sidemenu-hidden');
|
2017-12-04 05:37:00 -06:00
|
|
|
});
|
|
|
|
|
2019-02-04 14:08:30 -06:00
|
|
|
appEvents.on('playlist-started', () => {
|
|
|
|
elem.toggleClass('view-mode--playlist', true);
|
|
|
|
});
|
|
|
|
|
|
|
|
appEvents.on('playlist-stopped', () => {
|
|
|
|
elem.toggleClass('view-mode--playlist', false);
|
|
|
|
});
|
2018-02-04 10:32:27 -06:00
|
|
|
|
2018-02-15 05:42:17 -06:00
|
|
|
// check if we are in server side render
|
|
|
|
if (document.cookie.indexOf('renderKey') !== -1) {
|
|
|
|
body.addClass('body--phantomjs');
|
|
|
|
}
|
|
|
|
|
2016-01-17 10:24:10 -06:00
|
|
|
// tooltip removal fix
|
2016-02-12 11:55:45 -06:00
|
|
|
// manage page classes
|
2018-08-30 01:58:43 -05:00
|
|
|
let pageClass;
|
2018-09-04 07:27:03 -05:00
|
|
|
scope.$on('$routeChangeSuccess', (evt, data) => {
|
2016-02-12 11:55:45 -06:00
|
|
|
if (pageClass) {
|
|
|
|
body.removeClass(pageClass);
|
|
|
|
}
|
2017-06-02 07:00:42 -05:00
|
|
|
|
|
|
|
if (data.$$route) {
|
|
|
|
pageClass = data.$$route.pageClass;
|
|
|
|
if (pageClass) {
|
|
|
|
body.addClass(pageClass);
|
|
|
|
}
|
2016-02-12 11:55:45 -06:00
|
|
|
}
|
2017-06-02 07:00:42 -05:00
|
|
|
|
2017-12-07 09:25:21 -06:00
|
|
|
// clear body class sidemenu states
|
2017-12-20 05:33:33 -06:00
|
|
|
body.removeClass('sidemenu-open--xs');
|
2017-12-07 09:25:21 -06:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
$('#tooltip, .tooltip').remove();
|
2016-11-06 03:15:36 -06:00
|
|
|
|
|
|
|
// check for kiosk url param
|
2019-03-11 07:18:22 -05:00
|
|
|
setViewModeBodyClass(body, data.params.kiosk);
|
2018-04-12 14:14:58 -05:00
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
// close all drops
|
2018-08-26 10:14:40 -05:00
|
|
|
for (const drop of Drop.drops) {
|
2017-10-07 03:31:39 -05:00
|
|
|
drop.destroy();
|
|
|
|
}
|
2019-02-03 05:29:47 -06:00
|
|
|
|
|
|
|
appEvents.emit('hide-dash-search');
|
2016-01-17 10:24:10 -06:00
|
|
|
});
|
|
|
|
|
2016-11-05 08:23:37 -05:00
|
|
|
// handle kiosk mode
|
2019-03-04 09:33:00 -06:00
|
|
|
appEvents.on('toggle-kiosk-mode', (options: { exit?: boolean }) => {
|
|
|
|
const search: { kiosk?: KioskUrlValue } = $location.search();
|
2018-08-30 04:52:31 -05:00
|
|
|
|
|
|
|
if (options && options.exit) {
|
2018-10-23 09:22:00 -05:00
|
|
|
search.kiosk = '1';
|
2018-08-30 04:52:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (search.kiosk) {
|
|
|
|
case 'tv': {
|
2018-10-23 08:37:11 -05:00
|
|
|
search.kiosk = true;
|
2018-08-30 04:52:31 -05:00
|
|
|
appEvents.emit('alert-success', ['Press ESC to exit Kiosk mode']);
|
|
|
|
break;
|
|
|
|
}
|
2018-10-23 08:37:11 -05:00
|
|
|
case '1':
|
2018-08-30 04:52:31 -05:00
|
|
|
case true: {
|
|
|
|
delete search.kiosk;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
search.kiosk = 'tv';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 09:33:00 -06:00
|
|
|
$timeout(() => $location.search(search));
|
2019-03-11 07:18:22 -05:00
|
|
|
setViewModeBodyClass(body, search.kiosk);
|
2016-11-05 08:23:37 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// handle in active view state class
|
2018-08-30 01:58:43 -05:00
|
|
|
let lastActivity = new Date().getTime();
|
|
|
|
let activeUser = true;
|
2018-08-30 04:52:31 -05:00
|
|
|
const inActiveTimeLimit = 60 * 5000;
|
2016-11-05 08:23:37 -05:00
|
|
|
|
|
|
|
function checkForInActiveUser() {
|
|
|
|
if (!activeUser) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// only go to activity low mode on dashboard page
|
2017-12-20 05:33:33 -06:00
|
|
|
if (!body.hasClass('page-dashboard')) {
|
2016-11-05 08:23:37 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (new Date().getTime() - lastActivity > inActiveTimeLimit) {
|
2016-11-05 08:23:37 -05:00
|
|
|
activeUser = false;
|
2018-08-30 04:52:31 -05:00
|
|
|
body.addClass('view-mode--inactive');
|
2016-11-05 08:23:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function userActivityDetected() {
|
|
|
|
lastActivity = new Date().getTime();
|
|
|
|
if (!activeUser) {
|
|
|
|
activeUser = true;
|
2018-08-30 04:52:31 -05:00
|
|
|
body.removeClass('view-mode--inactive');
|
2016-11-05 08:23:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 07:44:55 -06:00
|
|
|
// mouse and keyboard is user activity
|
2016-11-05 08:23:37 -05:00
|
|
|
body.mousemove(userActivityDetected);
|
|
|
|
body.keydown(userActivityDetected);
|
2018-01-29 06:38:37 -06:00
|
|
|
// set useCapture = true to catch event here
|
2018-06-07 05:41:50 -05:00
|
|
|
document.addEventListener('wheel', userActivityDetected, { capture: true, passive: true });
|
2016-11-16 07:44:55 -06:00
|
|
|
// treat tab change as activity
|
2017-12-20 05:33:33 -06:00
|
|
|
document.addEventListener('visibilitychange', userActivityDetected);
|
2016-11-16 07:44:55 -06:00
|
|
|
|
|
|
|
// check every 2 seconds
|
|
|
|
setInterval(checkForInActiveUser, 2000);
|
2016-11-05 08:23:37 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
appEvents.on('toggle-view-mode', () => {
|
2016-11-05 08:23:37 -05:00
|
|
|
lastActivity = 0;
|
|
|
|
checkForInActiveUser();
|
|
|
|
});
|
|
|
|
|
2016-01-16 11:55:13 -06:00
|
|
|
// handle document clicks that should hide things
|
2018-09-04 07:27:03 -05:00
|
|
|
body.click(evt => {
|
2018-08-29 07:27:29 -05:00
|
|
|
const target = $(evt.target);
|
2016-01-18 05:12:05 -06:00
|
|
|
if (target.parents().length === 0) {
|
2016-01-16 11:55:13 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-25 10:12:11 -05:00
|
|
|
// ensure dropdown menu doesn't impact on z-index
|
|
|
|
body.find('.dropdown-menu-open').removeClass('dropdown-menu-open');
|
|
|
|
|
2016-11-03 16:14:29 -05:00
|
|
|
// for stuff that animates, slides out etc, clicking it needs to
|
|
|
|
// hide it right away
|
2018-08-29 07:27:29 -05:00
|
|
|
const clickAutoHide = target.closest('[data-click-hide]');
|
2016-11-03 16:14:29 -05:00
|
|
|
if (clickAutoHide.length) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const clickAutoHideParent = clickAutoHide.parent();
|
2016-11-03 16:29:53 -05:00
|
|
|
clickAutoHide.detach();
|
2018-09-04 07:27:03 -05:00
|
|
|
setTimeout(() => {
|
2016-11-03 16:29:53 -05:00
|
|
|
clickAutoHideParent.append(clickAutoHide);
|
|
|
|
}, 100);
|
2016-11-03 16:14:29 -05:00
|
|
|
}
|
|
|
|
|
2016-01-16 11:55:13 -06:00
|
|
|
// hide search
|
2017-12-20 05:33:33 -06:00
|
|
|
if (body.find('.search-container').length > 0) {
|
2017-12-21 01:39:31 -06:00
|
|
|
if (target.parents('.search-results-container, .search-field-wrapper').length === 0) {
|
2018-09-04 07:27:03 -05:00
|
|
|
scope.$apply(() => {
|
2017-12-20 05:33:33 -06:00
|
|
|
scope.appEvent('hide-dash-search');
|
2016-02-05 06:48:10 -06:00
|
|
|
});
|
2016-01-16 11:55:13 -06:00
|
|
|
}
|
|
|
|
}
|
2017-06-02 07:00:42 -05:00
|
|
|
|
2016-01-18 05:12:05 -06:00
|
|
|
// hide popovers
|
2018-08-29 07:27:29 -05:00
|
|
|
const popover = elem.find('.popover');
|
2017-12-21 01:39:31 -06:00
|
|
|
if (popover.length > 0 && target.parents('.graph-legend').length === 0) {
|
2016-01-18 05:12:05 -06:00
|
|
|
popover.hide();
|
|
|
|
}
|
2019-02-05 08:25:19 -06:00
|
|
|
|
|
|
|
// hide time picker
|
|
|
|
const timePickerDropDownIsOpen = elem.find('.gf-timepicker-dropdown').length > 0;
|
2019-02-05 08:29:19 -06:00
|
|
|
if (timePickerDropDownIsOpen) {
|
|
|
|
const targetIsInTimePickerDropDown = target.parents('.gf-timepicker-dropdown').length > 0;
|
|
|
|
const targetIsInTimePickerNav = target.parents('.gf-timepicker-nav').length > 0;
|
|
|
|
const targetIsDatePickerRowBtn = target.parents('td[id^="datepicker-"]').length > 0;
|
|
|
|
const targetIsDatePickerHeaderBtn = target.parents('button[id^="datepicker-"]').length > 0;
|
|
|
|
|
|
|
|
if (
|
|
|
|
targetIsInTimePickerNav ||
|
|
|
|
targetIsInTimePickerDropDown ||
|
|
|
|
targetIsDatePickerRowBtn ||
|
|
|
|
targetIsDatePickerHeaderBtn
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-05 08:25:19 -06:00
|
|
|
scope.$apply(() => {
|
|
|
|
scope.appEvent('closeTimepicker');
|
|
|
|
});
|
|
|
|
}
|
2016-01-16 11:55:13 -06:00
|
|
|
});
|
2017-12-20 05:33:33 -06:00
|
|
|
},
|
2016-01-16 11:55:13 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.directive('grafanaApp', grafanaAppDirective);
|