2017-12-20 05:33:33 -06:00
|
|
|
import { coreModule } from 'app/core/core';
|
2019-10-31 04:48:05 -05:00
|
|
|
import { AngularPanelMenuItem } from '@grafana/data';
|
2017-04-11 03:05:30 -05:00
|
|
|
|
2018-08-26 13:19:23 -05:00
|
|
|
const template = `
|
2017-08-09 08:33:19 -05:00
|
|
|
<span class="panel-title">
|
2017-04-11 03:05:30 -05:00
|
|
|
<span class="icon-gf panel-alert-icon"></span>
|
2017-08-09 08:33:19 -05:00
|
|
|
<span class="panel-title-text">{{ctrl.panel.title | interpolateTemplateVars:this}}</span>
|
2017-04-11 03:05:30 -05:00
|
|
|
<span class="panel-menu-container dropdown">
|
|
|
|
<span class="fa fa-caret-down panel-menu-toggle" data-toggle="dropdown"></span>
|
2017-08-02 02:56:08 -05:00
|
|
|
<ul class="dropdown-menu dropdown-menu--menu panel-menu" role="menu">
|
2017-04-11 03:05:30 -05:00
|
|
|
</ul>
|
|
|
|
</span>
|
2018-06-08 08:49:30 -05:00
|
|
|
<span class="panel-time-info" ng-if="ctrl.timeInfo"><i class="fa fa-clock-o"></i> {{ctrl.timeInfo}}</span>
|
2017-04-11 03:05:30 -05:00
|
|
|
</span>`;
|
|
|
|
|
2019-07-18 01:03:04 -05:00
|
|
|
function renderMenuItem(item: AngularPanelMenuItem, ctrl: any) {
|
2017-12-20 05:33:33 -06:00
|
|
|
let html = '';
|
|
|
|
let listItemClass = '';
|
2017-08-02 02:22:22 -05:00
|
|
|
|
2017-08-03 06:26:30 -05:00
|
|
|
if (item.divider) {
|
|
|
|
return '<li class="divider"></li>';
|
|
|
|
}
|
|
|
|
|
2017-08-02 02:22:22 -05:00
|
|
|
if (item.submenu) {
|
2017-12-20 05:33:33 -06:00
|
|
|
listItemClass = 'dropdown-submenu';
|
2017-08-02 02:22:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
html += `<li class="${listItemClass}"><a `;
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (item.click) {
|
|
|
|
html += ` ng-click="${item.click}"`;
|
|
|
|
}
|
|
|
|
if (item.href) {
|
|
|
|
html += ` href="${item.href}"`;
|
|
|
|
}
|
2017-08-02 02:22:22 -05:00
|
|
|
|
|
|
|
html += `><i class="${item.icon}"></i>`;
|
2019-05-08 09:50:21 -05:00
|
|
|
html += `<span class="dropdown-item-text" aria-label="${item.text} panel menu item">${item.text}</span>`;
|
2017-08-02 02:22:22 -05:00
|
|
|
|
|
|
|
if (item.shortcut) {
|
|
|
|
html += `<span class="dropdown-menu-item-shortcut">${item.shortcut}</span>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
html += `</a>`;
|
|
|
|
|
|
|
|
if (item.submenu) {
|
2017-08-02 02:56:08 -05:00
|
|
|
html += '<ul class="dropdown-menu dropdown-menu--menu panel-menu">';
|
2018-08-26 10:14:40 -05:00
|
|
|
for (const subitem of item.submenu) {
|
2017-08-02 02:22:22 -05:00
|
|
|
html += renderMenuItem(subitem, ctrl);
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
html += '</ul>';
|
2017-08-02 02:22:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
html += `</li>`;
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
2019-09-02 05:47:33 -05:00
|
|
|
async function createMenuTemplate(ctrl: any) {
|
2017-12-20 05:33:33 -06:00
|
|
|
let html = '';
|
2017-08-02 02:22:22 -05:00
|
|
|
|
2019-09-02 05:47:33 -05:00
|
|
|
for (const item of await ctrl.getMenu()) {
|
2017-08-02 02:22:22 -05:00
|
|
|
html += renderMenuItem(item, ctrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
2018-08-31 09:40:43 -05:00
|
|
|
/** @ngInject */
|
2019-07-18 01:03:04 -05:00
|
|
|
function panelHeader($compile: any) {
|
2017-04-11 03:05:30 -05:00
|
|
|
return {
|
2017-12-20 05:33:33 -06:00
|
|
|
restrict: 'E',
|
2017-04-11 03:05:30 -05:00
|
|
|
template: template,
|
2019-07-18 01:03:04 -05:00
|
|
|
link: (scope: any, elem: any, attrs: any) => {
|
2018-08-26 10:14:40 -05:00
|
|
|
const menuElem = elem.find('.panel-menu');
|
2019-07-18 01:03:04 -05:00
|
|
|
let menuScope: any;
|
|
|
|
let isDragged: boolean;
|
2017-08-02 02:22:22 -05:00
|
|
|
|
2019-09-02 05:47:33 -05:00
|
|
|
elem.click(async (evt: any) => {
|
2017-11-24 12:44:47 -06:00
|
|
|
const targetClass = evt.target.className;
|
2017-06-08 09:22:51 -05:00
|
|
|
|
2017-08-02 02:22:22 -05:00
|
|
|
// remove existing scope
|
|
|
|
if (menuScope) {
|
|
|
|
menuScope.$destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
menuScope = scope.$new();
|
2019-09-02 05:47:33 -05:00
|
|
|
const menuHtml = await createMenuTemplate(scope.ctrl);
|
2017-08-02 02:22:22 -05:00
|
|
|
menuElem.html(menuHtml);
|
|
|
|
$compile(menuElem)(menuScope);
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
if (targetClass.indexOf('panel-title-text') >= 0 || targetClass.indexOf('panel-title') >= 0) {
|
2017-11-24 12:44:47 -06:00
|
|
|
togglePanelMenu(evt);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-07-18 01:03:04 -05:00
|
|
|
function togglePanelMenu(e: any) {
|
2017-11-24 12:44:47 -06:00
|
|
|
if (!isDragged) {
|
|
|
|
e.stopPropagation();
|
2017-12-20 05:33:33 -06:00
|
|
|
elem.find('[data-toggle=dropdown]').dropdown('toggle');
|
2017-11-24 12:44:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 01:03:04 -05:00
|
|
|
let mouseX: number, mouseY: number;
|
|
|
|
elem.mousedown((e: any) => {
|
2017-11-24 12:44:47 -06:00
|
|
|
mouseX = e.pageX;
|
|
|
|
mouseY = e.pageY;
|
|
|
|
});
|
|
|
|
|
2019-07-18 01:03:04 -05:00
|
|
|
elem.mouseup((e: any) => {
|
2017-11-24 12:44:47 -06:00
|
|
|
if (mouseX === e.pageX && mouseY === e.pageY) {
|
|
|
|
isDragged = false;
|
|
|
|
} else {
|
|
|
|
isDragged = true;
|
|
|
|
}
|
2017-06-08 09:22:51 -05:00
|
|
|
});
|
2017-12-20 05:33:33 -06:00
|
|
|
},
|
2017-04-11 03:05:30 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.directive('panelHeader', panelHeader);
|