import { isString, escape } from 'lodash'; import $ from 'jquery'; import coreModule from 'app/core/core_module'; import alertDef from '../alerting/state/alertDef'; import { DashboardSrv } from '../dashboard/services/DashboardSrv'; import { ContextSrv } from 'app/core/services/context_srv'; /** @ngInject */ export function annotationTooltipDirective( $sanitize: any, dashboardSrv: DashboardSrv, contextSrv: ContextSrv, $compile: any ) { function sanitizeString(str: string) { try { return $sanitize(str); } catch (err) { console.log('Could not sanitize annotation string, html escaping instead'); return escape(str); } } return { restrict: 'E', scope: { event: '=', onEdit: '&', }, link: (scope: any, element: JQuery) => { const event = scope.event; let title = event.title; let text = event.text; const dashboard = dashboardSrv.getCurrent(); let tooltip = '
'; let titleStateClass = ''; if (event.alertId) { const stateModel = alertDef.getStateDisplayModel(event.newState); titleStateClass = stateModel.stateClass; title = ` ${stateModel.text}`; text = alertDef.getAlertAnnotationInfo(event); if (event.text) { text = text + '
' + event.text; } } else if (title) { text = title + '
' + (isString(text) ? text : ''); title = ''; } let header = `
`; if (event.login) { header += `
`; } header += ` ${sanitizeString(title)} ${dashboard?.formatDate(event.min)} `; // Show edit icon only for users with at least Editor role if (event.id && dashboard?.canAddAnnotations()) { header += ` `; } header += `
`; tooltip += header; tooltip += '
'; if (text) { tooltip += '
' + sanitizeString(text.replace(/\n/g, '
')) + '
'; } const tags = event.tags; if (tags && tags.length) { scope.tags = tags; tooltip += '{{tag}}
'; } tooltip += '
'; tooltip += '
'; const $tooltip = $(tooltip); $tooltip.appendTo(element); $compile(element.contents())(scope); }, }; } coreModule.directive('annotationTooltip', annotationTooltipDirective);