module.js 5.3 KB

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