grafana/public/app/plugins/panel/graph/GraphContextMenuCtrl.ts
Josh Hunt 3c6e0e8ef8
Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

68 lines
1.7 KiB
TypeScript

import { FlotDataPoint } from '@grafana/data';
import { MenuItemProps } from '@grafana/ui';
export class GraphContextMenuCtrl {
private source?: FlotDataPoint | null;
private scope?: any;
menuItemsSupplier?: () => MenuItemProps[];
scrollContextElement: HTMLElement | null = null;
position: {
x: number;
y: number;
} = { x: 0, y: 0 };
isVisible: boolean;
constructor($scope: any) {
this.isVisible = false;
this.scope = $scope;
}
onClose = () => {
if (this.scrollContextElement) {
this.scrollContextElement.removeEventListener('scroll', this.onClose);
}
this.scope.$apply(() => {
this.isVisible = false;
});
};
toggleMenu = (event?: { pageX: number; pageY: number }) => {
this.isVisible = !this.isVisible;
if (this.isVisible && this.scrollContextElement) {
this.scrollContextElement.addEventListener('scroll', this.onClose);
}
if (this.source) {
this.position = {
x: this.source.pageX,
y: this.source.pageY,
};
} else {
this.position = {
x: event ? event.pageX : 0,
y: event ? event.pageY : 0,
};
}
};
// Sets element which is considered as a scroll context of given context menu.
// Having access to this element allows scroll event attachement for menu to be closed when user scrolls
setScrollContextElement = (el: HTMLElement | null) => {
this.scrollContextElement = el;
};
setSource = (source: FlotDataPoint | null) => {
this.source = source;
};
getSource = () => {
return this.source;
};
setMenuItemsSupplier = (menuItemsSupplier: () => MenuItemProps[]) => {
this.menuItemsSupplier = menuItemsSupplier;
};
}