grafana/public/app/features/dashboard/dashgrid/PanelHeader/PanelHeader.tsx

123 lines
3.5 KiB
TypeScript
Raw Normal View History

import React, { Component } from 'react';
import classNames from 'classnames';
import { isEqual } from 'lodash';
import { DataLink, ScopedVars } from '@grafana/data';
import { ClickOutsideWrapper } from '@grafana/ui';
import { e2e } from '@grafana/e2e';
import PanelHeaderCorner from './PanelHeaderCorner';
2018-11-07 06:55:02 -06:00
import { PanelHeaderMenu } from './PanelHeaderMenu';
2019-01-15 10:15:46 -06:00
import templateSrv from 'app/features/templating/template_srv';
2018-11-07 06:55:02 -06:00
import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
import { PanelModel } from 'app/features/dashboard/state/PanelModel';
import { getPanelLinksSupplier } from 'app/features/panel/panellinks/linkSuppliers';
2018-11-07 06:55:02 -06:00
export interface Props {
panel: PanelModel;
dashboard: DashboardModel;
timeInfo: string;
title?: string;
description?: string;
2019-03-04 03:42:59 -06:00
scopedVars?: ScopedVars;
Graph: Add data links feature (click on graph) (#17267) * WIP: initial panel links editor * WIP: Added dashboard migration to new panel drilldown link schema * Make link_srv interpolate new variables * Fix failing tests * Drilldown: Add context menu to graph viz (#17284) * Add simple context menu for adding graph annotations and showing drilldown links * Close graph context menu when user start scrolling * Move context menu component to grafana/ui * Make graph context menu appear on click, use cmd/ctrl click for quick annotations * Move graph context menu controller to separate file * Drilldown: datapoint variables interpolation (#17328) * Add simple context menu for adding graph annotations and showing drilldown links * Close graph context menu when user start scrolling * Move context menu component to grafana/ui * Make graph context menu appear on click, use cmd/ctrl click for quick annotations * Add util for absolute time range transformation * Add series name and datapoint timestamp interpolation * Rename drilldown link variables tot snake case, use const values instead of strings in tests * Bring LinkSrv.getPanelLinkAnchorInfo for compatibility reasons and add deprecation warning * Rename seriesLabel to seriesName * Drilldown: use separate editors for panel and series links (#17355) * Use correct target ini context menu links * Rename PanelLinksEditor to DrilldownLinksEditor and mote it to grafana/ui * Expose DrilldownLinksEditor as an angular directive * Enable visualization specifix drilldown links * Props interfaces rename * Drilldown: Add variables suggestion and syntax highlighting for drilldown link editor (#17391) * Add variables suggestion in drilldown link editor * Enable prism * Fix backspace not working * Move slate value helpers to grafana/ui * Add syntax higlighting for links input * Rename drilldown link components to data links * Add template variabe suggestions * Bugfix * Fix regexp not working in Firefox * Display correct links in panel header corner * bugfix * bugfix * Bugfix * Context menu UI tweaks * Use data link terminology instead of drilldown * DataLinks: changed autocomplete syntax * Use singular form for data link * Use the same syntax higlighting for built-in and template variables in data links editor * UI improvements to context menu * UI review tweaks * Tweak layout of data link editor * Fix vertical spacing * Remove data link header in context menu * Remove pointer cursor from series label in context menu * Fix variable selection on click * DataLinks: migrations for old links * Update docs about data links * Use value time instead of time range when interpolating datapoint timestamp * Remove not used util * Update docs * Moved icon a bit more down * Interpolate value ts only when using __value_time variable * Bring href property back to LinkModel * Add any type annotations * Fix TS error on slate's Value type * minor changes
2019-06-25 04:38:51 -05:00
links?: DataLink[];
2019-02-12 10:01:07 -06:00
error?: string;
isFullscreen: boolean;
}
interface ClickCoordinates {
x: number;
y: number;
}
2018-11-13 10:00:28 -06:00
interface State {
panelMenuOpen: boolean;
}
export class PanelHeader extends Component<Props, State> {
clickCoordinates: ClickCoordinates = { x: 0, y: 0 };
2018-11-13 10:00:28 -06:00
state = {
panelMenuOpen: false,
clickCoordinates: { x: 0, y: 0 },
};
eventToClickCoordinates = (event: React.MouseEvent<HTMLDivElement>) => {
return {
x: event.clientX,
y: event.clientY,
};
};
onMouseDown = (event: React.MouseEvent<HTMLDivElement>) => {
this.clickCoordinates = this.eventToClickCoordinates(event);
2018-11-13 10:00:28 -06:00
};
isClick = (clickCoordinates: ClickCoordinates) => {
return isEqual(clickCoordinates, this.clickCoordinates);
};
onMenuToggle = (event: React.MouseEvent<HTMLDivElement>) => {
if (this.isClick(this.eventToClickCoordinates(event))) {
event.stopPropagation();
2018-11-13 10:00:28 -06:00
this.setState(prevState => ({
panelMenuOpen: !prevState.panelMenuOpen,
}));
}
2018-11-13 10:00:28 -06:00
};
closeMenu = () => {
this.setState({
panelMenuOpen: false,
});
};
render() {
const { panel, dashboard, timeInfo, scopedVars, error, isFullscreen } = this.props;
2019-01-15 10:15:46 -06:00
const title = templateSrv.replaceWithText(panel.title, scopedVars);
const panelHeaderClass = classNames({
'panel-header': true,
'grid-drag-handle': !isFullscreen,
});
return (
<>
<div className={panelHeaderClass}>
<PanelHeaderCorner
panel={panel}
title={panel.title}
description={panel.description}
scopedVars={panel.scopedVars}
links={getPanelLinksSupplier(panel)}
error={error}
/>
<div
className="panel-title-container"
onClick={this.onMenuToggle}
onMouseDown={this.onMouseDown}
aria-label={e2e.pages.Dashboard.Panels.Panel.selectors.title(title)}
>
<div className="panel-title">
<span className="icon-gf panel-alert-icon" />
<span className="panel-title-text">
2019-01-15 10:15:46 -06:00
{title} <span className="fa fa-caret-down panel-menu-toggle" />
</span>
{this.state.panelMenuOpen && (
<ClickOutsideWrapper onClick={this.closeMenu}>
<PanelHeaderMenu panel={panel} dashboard={dashboard} />
</ClickOutsideWrapper>
)}
{timeInfo && (
<span className="panel-time-info">
<i className="fa fa-clock-o" /> {timeInfo}
</span>
)}
</div>
</div>
</div>
</>
);
}
}