popover_srv.ts 1.6 KB

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