annotation_tooltip.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import $ from 'jquery';
  2. import { isString, escape } from 'lodash';
  3. import coreModule from 'app/angular/core_module';
  4. import { ContextSrv } from 'app/core/services/context_srv';
  5. import alertDef from 'app/features/alerting/state/alertDef';
  6. import { DashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
  7. /** @ngInject */
  8. export function annotationTooltipDirective(
  9. $sanitize: any,
  10. dashboardSrv: DashboardSrv,
  11. contextSrv: ContextSrv,
  12. $compile: any
  13. ) {
  14. function sanitizeString(str: string) {
  15. try {
  16. return $sanitize(str);
  17. } catch (err) {
  18. console.log('Could not sanitize annotation string, html escaping instead');
  19. return escape(str);
  20. }
  21. }
  22. return {
  23. restrict: 'E',
  24. scope: {
  25. event: '=',
  26. onEdit: '&',
  27. },
  28. link: (scope: any, element: JQuery) => {
  29. const event = scope.event;
  30. let title = event.title;
  31. let text = event.text;
  32. const dashboard = dashboardSrv.getCurrent();
  33. let tooltip = '<div class="graph-annotation">';
  34. let titleStateClass = '';
  35. if (event.alertId !== undefined && event.newState) {
  36. const stateModel = alertDef.getStateDisplayModel(event.newState);
  37. titleStateClass = stateModel.stateClass;
  38. title = `<i class="${stateModel.iconClass}"></i> ${stateModel.text}`;
  39. text = alertDef.getAlertAnnotationInfo(event);
  40. if (event.text) {
  41. text = text + '<br />' + event.text;
  42. }
  43. } else if (title) {
  44. text = title + '<br />' + (isString(text) ? text : '');
  45. title = '';
  46. }
  47. let header = `<div class="graph-annotation__header">`;
  48. if (event.login && event.avatarUrl) {
  49. header += `<div class="graph-annotation__user" bs-tooltip="'Created by ${event.login}'"><img src="${event.avatarUrl}" /></div>`;
  50. }
  51. header += `
  52. <span class="graph-annotation__title ${titleStateClass}">${sanitizeString(title)}</span>
  53. <span class="graph-annotation__time">${dashboard?.formatDate(event.min)}</span>
  54. `;
  55. // Show edit icon only for users with at least Editor role
  56. if (event.id && dashboard?.canEditAnnotations(event.dashboardId)) {
  57. header += `
  58. <span class="pointer graph-annotation__edit-icon" ng-click="onEdit()">
  59. <i class="fa fa-pencil-square"></i>
  60. </span>
  61. `;
  62. }
  63. header += `</div>`;
  64. tooltip += header;
  65. tooltip += '<div class="graph-annotation__body">';
  66. if (text) {
  67. tooltip += '<div ng-non-bindable>' + sanitizeString(text.replace(/\n/g, '<br>')) + '</div>';
  68. }
  69. const tags = event.tags;
  70. if (tags && tags.length) {
  71. scope.tags = tags;
  72. tooltip +=
  73. '<span class="label label-tag small" ng-repeat="tag in tags" tag-color-from-name="tag">{{tag}}</span><br/>';
  74. }
  75. tooltip += '</div>';
  76. tooltip += '</div>';
  77. const $tooltip = $(tooltip);
  78. $tooltip.appendTo(element);
  79. $compile(element.contents())(scope);
  80. },
  81. };
  82. }
  83. coreModule.directive('annotationTooltip', annotationTooltipDirective);