module.ts 4.6 KB

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