annotation_tooltip.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import _ from 'lodash';
  2. import $ from 'jquery';
  3. import coreModule from 'app/core/core_module';
  4. import alertDef from '../alerting/state/alertDef';
  5. import { DashboardSrv } from '../dashboard/services/DashboardSrv';
  6. import { ContextSrv } from 'app/core/services/context_srv';
  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) {
  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) {
  49. header += `<div class="graph-annotation__user" bs-tooltip="'Created by ${event.login}'"><img src="${
  50. event.avatarUrl
  51. }" /></div>`;
  52. }
  53. header += `
  54. <span class="graph-annotation__title ${titleStateClass}">${sanitizeString(title)}</span>
  55. <span class="graph-annotation__time">${dashboard.formatDate(event.min)}</span>
  56. `;
  57. // Show edit icon only for users with at least Editor role
  58. if (event.id && dashboard.meta.canEdit) {
  59. header += `
  60. <span class="pointer graph-annotation__edit-icon" ng-click="onEdit()">
  61. <i class="fa fa-pencil-square"></i>
  62. </span>
  63. `;
  64. }
  65. header += `</div>`;
  66. tooltip += header;
  67. tooltip += '<div class="graph-annotation__body">';
  68. if (text) {
  69. tooltip += '<div>' + sanitizeString(text.replace(/\n/g, '<br>')) + '</div>';
  70. }
  71. const tags = event.tags;
  72. if (tags && tags.length) {
  73. scope.tags = tags;
  74. tooltip +=
  75. '<span class="label label-tag small" ng-repeat="tag in tags" tag-color-from-name="tag">{{tag}}</span><br/>';
  76. }
  77. tooltip += '</div>';
  78. tooltip += '</div>';
  79. const $tooltip = $(tooltip);
  80. $tooltip.appendTo(element);
  81. $compile(element.contents())(scope);
  82. },
  83. };
  84. }
  85. coreModule.directive('annotationTooltip', annotationTooltipDirective);