info_popover.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import _ from 'lodash';
  2. import coreModule from 'app/core/core_module';
  3. // @ts-ignore
  4. import Drop from 'tether-drop';
  5. export function infoPopover() {
  6. return {
  7. restrict: 'E',
  8. template: '<i class="fa fa-info-circle"></i>',
  9. transclude: true,
  10. link: (scope: any, elem: any, attrs: any, ctrl: any, transclude: any) => {
  11. const offset = attrs.offset || '0 -10px';
  12. const position = attrs.position || 'right middle';
  13. let classes = 'drop-help drop-hide-out-of-bounds';
  14. const openOn = 'hover';
  15. elem.addClass('gf-form-help-icon');
  16. if (attrs.wide) {
  17. classes += ' drop-wide';
  18. }
  19. if (attrs.mode) {
  20. elem.addClass('gf-form-help-icon--' + attrs.mode);
  21. }
  22. transclude((clone: any, newScope: any) => {
  23. const content = document.createElement('div');
  24. content.className = 'markdown-html';
  25. _.each(clone, node => {
  26. content.appendChild(node);
  27. });
  28. const dropOptions = {
  29. target: elem[0],
  30. content: content,
  31. position: position,
  32. classes: classes,
  33. openOn: openOn,
  34. hoverOpenDelay: 400,
  35. tetherOptions: {
  36. offset: offset,
  37. constraints: [
  38. {
  39. to: 'window',
  40. attachment: 'together',
  41. pin: true,
  42. },
  43. ],
  44. },
  45. };
  46. // Create drop in next digest after directive content is rendered.
  47. scope.$applyAsync(() => {
  48. const drop = new Drop(dropOptions);
  49. const unbind = scope.$on('$destroy', () => {
  50. drop.destroy();
  51. unbind();
  52. });
  53. });
  54. });
  55. },
  56. };
  57. }
  58. coreModule.directive('infoPopover', infoPopover);