module.ts 4.5 KB

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