annotation_tooltip.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import $ from 'jquery';
  5. import coreModule from 'app/core/core_module';
  6. import alertDef from '../alerting/alert_def';
  7. /** @ngInject **/
  8. export function annotationTooltipDirective($sanitize, dashboardSrv, $compile) {
  9. function sanitizeString(str) {
  10. try {
  11. return $sanitize(str);
  12. } catch (err) {
  13. console.log('Could not sanitize annotation string, html escaping instead');
  14. return _.escape(str);
  15. }
  16. }
  17. return {
  18. restrict: 'E',
  19. scope: {
  20. "event": "=",
  21. },
  22. link: function(scope, element) {
  23. var event = scope.event;
  24. var title = event.title;
  25. var text = event.text;
  26. var dashboard = dashboardSrv.getCurrent();
  27. var tooltip = '<div class="graph-annotation">';
  28. var titleStateClass = '';
  29. if (event.source.name === 'panel-alert') {
  30. var stateModel = alertDef.getStateDisplayModel(event.newState);
  31. titleStateClass = stateModel.stateClass;
  32. title = `<i class="icon-gf ${stateModel.iconClass}"></i> ${stateModel.text}`;
  33. text = alertDef.getAlertAnnotationInfo(event);
  34. }
  35. tooltip += `
  36. <div class="graph-annotation-header">
  37. <span class="graph-annotation-title ${titleStateClass}">${sanitizeString(title)}</span>
  38. <span class="graph-annotation-time">${dashboard.formatDate(event.min)}</span>
  39. </div>
  40. `;
  41. tooltip += '<div class="graph-annotation-body">';
  42. if (text) {
  43. tooltip += sanitizeString(text).replace(/\n/g, '<br>') + '<br>';
  44. }
  45. var tags = event.tags;
  46. if (_.isString(event.tags)) {
  47. tags = event.tags.split(',');
  48. if (tags.length === 1) {
  49. tags = event.tags.split(' ');
  50. }
  51. }
  52. if (tags && tags.length) {
  53. scope.tags = tags;
  54. tooltip += '<span class="label label-tag small" ng-repeat="tag in tags" tag-color-from-name="tag">{{tag}}</span><br/>';
  55. }
  56. tooltip += "</div>";
  57. var $tooltip = $(tooltip);
  58. $tooltip.appendTo(element);
  59. $compile(element.contents())(scope);
  60. }
  61. };
  62. }
  63. coreModule.directive('annotationTooltip', annotationTooltipDirective);