module.ts 4.4 KB

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