popover_srv.ts 1.6 KB

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