module.ts 5.5 KB

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