module.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import {iconMap} from './editor';
  4. function dashLinksContainer() {
  5. return {
  6. scope: {
  7. links: "="
  8. },
  9. restrict: 'E',
  10. controller: 'DashLinksContainerCtrl',
  11. template: '<dash-link ng-repeat="link in generatedLinks" link="link"></dash-link>',
  12. link: function() { }
  13. };
  14. }
  15. /** @ngInject */
  16. function dashLink($compile, linkSrv) {
  17. return {
  18. restrict: 'E',
  19. link: function(scope, elem) {
  20. var link = scope.link;
  21. var template = '<div class="gf-form">' +
  22. '<a class="pointer gf-form-label" data-placement="bottom"' +
  23. (link.asDropdown ? ' ng-click="fillDropdown(link)" data-toggle="dropdown"' : "") + '>' +
  24. '<i></i> <span></span></a>';
  25. if (link.asDropdown) {
  26. template += '<ul class="dropdown-menu" role="menu">' +
  27. '<li ng-repeat="dash in link.searchHits"><a href="{{dash.url}}">{{dash.title}}</a></li>' +
  28. '</ul>';
  29. }
  30. template += '</div>';
  31. elem.html(template);
  32. $compile(elem.contents())(scope);
  33. var anchor = elem.find('a');
  34. var icon = elem.find('i');
  35. var span = elem.find('span');
  36. function update() {
  37. var linkInfo = linkSrv.getAnchorInfo(link);
  38. span.text(linkInfo.title);
  39. anchor.attr("href", linkInfo.href);
  40. }
  41. // tooltip
  42. elem.find('a').tooltip({ title: scope.link.tooltip, html: true, container: 'body' });
  43. icon.attr('class', 'fa fa-fw ' + scope.link.icon);
  44. anchor.attr('target', scope.link.target);
  45. // fix for menus on the far right
  46. if (link.asDropdown && scope.$last) {
  47. elem.find('.dropdown-menu').addClass('pull-right');
  48. }
  49. update();
  50. scope.$on('refresh', update);
  51. }
  52. };
  53. }
  54. export class DashLinksContainerCtrl {
  55. /** @ngInject */
  56. constructor($scope, $rootScope, $q, backendSrv, dashboardSrv, linkSrv) {
  57. var currentDashId = dashboardSrv.getCurrent().id;
  58. function buildLinks(linkDef) {
  59. if (linkDef.type === 'dashboards') {
  60. if (!linkDef.tags) {
  61. console.log('Dashboard link missing tag');
  62. return $q.when([]);
  63. }
  64. if (linkDef.asDropdown) {
  65. return $q.when([{
  66. title: linkDef.title,
  67. tags: linkDef.tags,
  68. keepTime: linkDef.keepTime,
  69. includeVars: linkDef.includeVars,
  70. icon: "fa fa-bars",
  71. asDropdown: true
  72. }]);
  73. }
  74. return $scope.searchDashboards(linkDef, 7);
  75. }
  76. if (linkDef.type === 'link') {
  77. return $q.when([{
  78. url: linkDef.url,
  79. title: linkDef.title,
  80. icon: iconMap[linkDef.icon],
  81. tooltip: linkDef.tooltip,
  82. target: linkDef.targetBlank ? "_blank" : "_self",
  83. keepTime: linkDef.keepTime,
  84. includeVars: linkDef.includeVars,
  85. }]);
  86. }
  87. return $q.when([]);
  88. }
  89. function updateDashLinks() {
  90. var promises = _.map($scope.links, buildLinks);
  91. $q.all(promises).then(function(results) {
  92. $scope.generatedLinks = _.flatten(results);
  93. });
  94. }
  95. $scope.searchDashboards = function(link, limit) {
  96. return backendSrv.search({tag: link.tags, limit: limit}).then(function(results) {
  97. return _.reduce(results, function(memo, dash) {
  98. // do not add current dashboard
  99. if (dash.id !== currentDashId) {
  100. memo.push({
  101. title: dash.title,
  102. url: 'dashboard/' + dash.uri,
  103. icon: 'fa fa-th-large',
  104. keepTime: link.keepTime,
  105. includeVars: link.includeVars
  106. });
  107. }
  108. return memo;
  109. }, []);
  110. });
  111. };
  112. $scope.fillDropdown = function(link) {
  113. $scope.searchDashboards(link, 100).then(function(results) {
  114. _.each(results, function(hit) {
  115. hit.url = linkSrv.getLinkUrl(hit);
  116. });
  117. link.searchHits = results;
  118. });
  119. };
  120. updateDashLinks();
  121. $rootScope.onAppEvent('dash-links-updated', updateDashLinks, $scope);
  122. }
  123. }
  124. angular.module('grafana.directives').directive('dashLinksContainer', dashLinksContainer);
  125. angular.module('grafana.directives').directive('dashLink', dashLink);
  126. angular.module('grafana.directives').controller("DashLinksContainerCtrl", DashLinksContainerCtrl);