annotationTooltip.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'lodash'
  5. ],
  6. function (angular, $, _) {
  7. 'use strict';
  8. angular
  9. .module('grafana.directives')
  10. .directive('annotationTooltip', function($sanitize, dashboardSrv, $compile) {
  11. function sanitizeString(str) {
  12. try {
  13. return $sanitize(str);
  14. }
  15. catch(err) {
  16. console.log('Could not sanitize annotation string, html escaping instead');
  17. return _.escape(str);
  18. }
  19. }
  20. return {
  21. link: function (scope, element) {
  22. var event = scope.event;
  23. var title = sanitizeString(event.title);
  24. var dashboard = dashboardSrv.getCurrent();
  25. var time = '<i>' + dashboard.formatDate(event.min) + '</i>';
  26. var tooltip = '<div class="graph-tooltip small"><div class="graph-tooltip-time">' + title + ' ' + time + '</div> ' ;
  27. if (event.text) {
  28. var text = sanitizeString(event.text);
  29. tooltip += text.replace(/\n/g, '<br>') + '<br>';
  30. }
  31. var tags = event.tags;
  32. if (_.isString(event.tags)) {
  33. tags = event.tags.split(',');
  34. if (tags.length === 1) {
  35. tags = event.tags.split(' ');
  36. }
  37. }
  38. if (tags && tags.length) {
  39. scope.tags = tags;
  40. tooltip += '<span class="label label-tag" ng-repeat="tag in tags" tag-color-from-name="tag">{{tag}}</span><br/>';
  41. }
  42. tooltip += "</div>";
  43. var $tooltip = $(tooltip);
  44. $tooltip.appendTo(element);
  45. $compile(element.contents())(scope);
  46. }
  47. };
  48. });
  49. });