popover_srv.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { extend } from 'lodash';
  2. // @ts-ignore
  3. import Drop from 'tether-drop';
  4. import { GrafanaRootScope } from 'app/angular/GrafanaCtrl';
  5. import coreModule from 'app/angular/core_module';
  6. /** @ngInject */
  7. function popoverSrv(this: any, $compile: any, $rootScope: GrafanaRootScope, $timeout: any) {
  8. let openDrop: any = null;
  9. this.close = () => {
  10. if (openDrop) {
  11. openDrop.close();
  12. }
  13. };
  14. this.show = (options: any) => {
  15. if (openDrop) {
  16. openDrop.close();
  17. openDrop = null;
  18. }
  19. const scope = extend($rootScope.$new(true), options.model);
  20. let drop: any;
  21. const cleanUp = () => {
  22. setTimeout(() => {
  23. scope.$destroy();
  24. if (drop.tether) {
  25. drop.destroy();
  26. }
  27. if (options.onClose) {
  28. options.onClose();
  29. }
  30. });
  31. openDrop = null;
  32. };
  33. scope.dismiss = () => {
  34. drop.close();
  35. };
  36. const contentElement = document.createElement('div');
  37. contentElement.innerHTML = options.template;
  38. $compile(contentElement)(scope);
  39. $timeout(() => {
  40. drop = new Drop({
  41. target: options.element,
  42. content: contentElement,
  43. position: options.position,
  44. classes: options.classNames || 'drop-popover',
  45. openOn: options.openOn,
  46. hoverCloseDelay: 200,
  47. tetherOptions: {
  48. constraints: [{ to: 'scrollParent', attachment: 'together' }],
  49. },
  50. });
  51. drop.on('close', () => {
  52. cleanUp();
  53. });
  54. openDrop = drop;
  55. openDrop.open();
  56. }, 100);
  57. // return close function
  58. return () => {
  59. if (drop) {
  60. drop.close();
  61. }
  62. };
  63. };
  64. }
  65. coreModule.service('popoverSrv', popoverSrv);