popover_srv.ts 1.6 KB

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