annotation_tooltip.ts 2.7 KB

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