module.ts 4.8 KB

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