popover_srv.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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) {
  9. this.show = function(options) {
  10. var popoverScope = _.extend($rootScope.$new(true), options.model);
  11. var drop;
  12. function destroyDrop() {
  13. setTimeout(function() {
  14. if (drop.tether) {
  15. drop.destroy();
  16. }
  17. });
  18. }
  19. popoverScope.dismiss = function() {
  20. popoverScope.$destroy();
  21. destroyDrop();
  22. };
  23. var contentElement = document.createElement('div');
  24. contentElement.innerHTML = options.template;
  25. $compile(contentElement)(popoverScope);
  26. drop = new Drop({
  27. target: options.element,
  28. content: contentElement,
  29. position: options.position,
  30. classes: 'drop-popover',
  31. openOn: options.openOn || 'hover',
  32. hoverCloseDelay: 200,
  33. tetherOptions: {
  34. constraints: [{to: 'window', pin: true, attachment: "both"}]
  35. }
  36. });
  37. drop.on('close', () => {
  38. popoverScope.dismiss({fromDropClose: true});
  39. destroyDrop();
  40. if (options.onClose) {
  41. options.onClose();
  42. }
  43. });
  44. setTimeout(() => { drop.open(); }, 10);
  45. };
  46. }
  47. coreModule.service('popoverSrv', popoverSrv);