popover_srv.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import coreModule from 'app/core/core_module';
  4. import Drop from 'tether-drop';
  5. /** @ngInject **/
  6. function popoverSrv($compile, $rootScope, $timeout) {
  7. let openDrop = null;
  8. this.close = function() {
  9. if (openDrop) {
  10. openDrop.close();
  11. }
  12. };
  13. this.show = function(options) {
  14. if (openDrop) {
  15. openDrop.close();
  16. openDrop = null;
  17. }
  18. var scope = _.extend($rootScope.$new(true), options.model);
  19. var drop;
  20. var 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. var 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 function() {
  58. if (drop) {
  59. drop.close();
  60. }
  61. };
  62. };
  63. }
  64. coreModule.service('popoverSrv', popoverSrv);