popover_srv.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. '../core_module',
  6. ],
  7. function (angular, _, $, coreModule) {
  8. 'use strict';
  9. coreModule.service('popoverSrv', function($templateCache, $timeout, $q, $http, $compile) {
  10. this.getTemplate = function(url) {
  11. return $q.when($templateCache.get(url) || $http.get(url, {cache: true}));
  12. };
  13. this.show = function(options) {
  14. var popover;
  15. // hide other popovers
  16. $('.popover').each(function() {
  17. popover = $(this).prev().data('popover');
  18. if (popover) {
  19. popover.scope.$destroy();
  20. popover.destroy();
  21. }
  22. });
  23. options.scope.dismiss = function() {
  24. popover = options.element.data('popover');
  25. if (popover) {
  26. popover.destroy();
  27. }
  28. options.scope.$destroy();
  29. };
  30. this.getTemplate(options.templateUrl).then(function(result) {
  31. $timeout(function() {
  32. var template = _.isString(result) ? result : result.data;
  33. options.element.popover({
  34. content: template,
  35. placement: options.placement || 'bottom',
  36. html: true
  37. });
  38. popover = options.element.data('popover');
  39. popover.hasContent = function () {
  40. return template;
  41. };
  42. popover.toggle();
  43. popover.scope = options.scope;
  44. $compile(popover.$tip)(popover.scope);
  45. }, 1);
  46. });
  47. };
  48. });
  49. });