annotation_tooltip.ts 2.2 KB

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