popoverSrv.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. ],
  6. function (angular, _, $) {
  7. 'use strict';
  8. var module = angular.module('grafana.services');
  9. module.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. });