grafana/public/app/features/panel/panel_directive.ts

95 lines
2.6 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import angular from 'angular';
// @ts-ignore
2018-03-27 15:27:23 -05:00
import baron from 'baron';
import { PanelEvents } from '@grafana/data';
import { PanelModel } from '../dashboard/state';
import { PanelCtrl } from './panel_ctrl';
2018-08-26 13:19:23 -05:00
const module = angular.module('grafana.directives');
2018-08-26 13:19:23 -05:00
const panelTemplate = `
<ng-transclude class="panel-height-helper"></ng-transclude>
2016-02-05 04:05:47 -06:00
`;
module.directive('grafanaPanel', ($rootScope, $document, $timeout) => {
return {
2017-12-20 05:33:33 -06:00
restrict: 'E',
2016-02-05 04:05:47 -06:00
template: panelTemplate,
transclude: true,
2017-12-20 05:33:33 -06:00
scope: { ctrl: '=' },
link: (scope: any, elem) => {
const ctrl: PanelCtrl = scope.ctrl;
const panel: PanelModel = scope.ctrl.panel;
2018-11-19 10:41:48 -06:00
let panelScrollbar: any;
2018-11-19 10:41:48 -06:00
2018-03-27 15:27:23 -05:00
function resizeScrollableContent() {
if (panelScrollbar) {
panelScrollbar.update();
}
}
ctrl.events.on(PanelEvents.componentDidMount, () => {
if ((ctrl as any).__proto__.constructor.scrollable) {
2018-03-27 15:27:23 -05:00
const scrollRootClass = 'baron baron__root baron__clipper panel-content--scrollable';
const scrollerClass = 'baron__scroller';
const scrollBarHTML = `
<div class="baron__track">
<div class="baron__bar"></div>
</div>
`;
const scrollRoot = elem;
const scroller = elem.find(':first').find(':first');
2018-03-27 15:27:23 -05:00
scrollRoot.addClass(scrollRootClass);
$(scrollBarHTML).appendTo(scrollRoot);
scroller.addClass(scrollerClass);
panelScrollbar = baron({
root: scrollRoot[0],
scroller: scroller[0],
bar: '.baron__bar',
barOnCls: '_scrollbar',
scrollingCls: '_scrolling',
});
2018-04-06 12:00:03 -05:00
panelScrollbar.scroll();
}
2017-11-22 06:32:54 -06:00
});
function onPanelSizeChanged() {
$timeout(() => {
2018-03-27 15:27:23 -05:00
resizeScrollableContent();
ctrl.render();
});
}
function onPanelModelRender(payload?: any) {
ctrl.height = scope.$parent.$parent.size.height;
ctrl.width = scope.$parent.$parent.size.width;
}
function onPanelModelRefresh() {
ctrl.height = scope.$parent.$parent.size.height;
ctrl.width = scope.$parent.$parent.size.width;
}
2018-11-19 10:41:48 -06:00
panel.events.on(PanelEvents.refresh, onPanelModelRefresh);
panel.events.on(PanelEvents.render, onPanelModelRender);
panel.events.on(PanelEvents.panelSizeChanged, onPanelSizeChanged);
scope.$on('$destroy', () => {
elem.off();
panel.events.emit(PanelEvents.panelTeardown);
panel.events.removeAllListeners();
if (panelScrollbar) {
2018-03-27 15:27:23 -05:00
panelScrollbar.dispose();
}
});
2017-12-20 05:33:33 -06:00
},
};
});