module.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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: '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="submenu-item dropdown">' +
  43. '<a class="pointer dash-nav-link" 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}}"><i class="fa fa-th-large"></i> {{dash.title}}</a></li>' +
  49. '</ul';
  50. }
  51. elem.html(template);
  52. $compile(elem.contents())(scope);
  53. var anchor = elem.find('a');
  54. var icon = elem.find('i');
  55. var span = elem.find('span');
  56. function update() {
  57. var linkInfo = linkSrv.getAnchorInfo(link);
  58. span.text(linkInfo.title);
  59. anchor.attr("href", linkInfo.href);
  60. }
  61. // tooltip
  62. elem.find('a').tooltip({ title: scope.link.tooltip, html: true, container: 'body' });
  63. icon.attr('class', 'fa fa-fw ' + scope.link.icon);
  64. anchor.attr('target', scope.link.target);
  65. // fix for menus on the far right
  66. if (link.asDropdown && scope.$last) {
  67. elem.find('.dropdown-menu').addClass('pull-right');
  68. }
  69. update();
  70. scope.$on('refresh', update);
  71. }
  72. };
  73. });
  74. module.controller("DashLinksContainerCtrl", function($scope, $rootScope, $q, backendSrv, dashboardSrv, linkSrv) {
  75. var currentDashId = dashboardSrv.getCurrent().id;
  76. function buildLinks(linkDef) {
  77. if (linkDef.type === 'dashboards') {
  78. if (!linkDef.tags) {
  79. console.log('Dashboard link missing tag');
  80. return $q.when([]);
  81. }
  82. if (linkDef.asDropdown) {
  83. return $q.when([{
  84. title: linkDef.title,
  85. tags: linkDef.tags,
  86. keepTime: linkDef.keepTime,
  87. includeVars: linkDef.includeVars,
  88. icon: "fa fa-bars",
  89. asDropdown: true
  90. }]);
  91. }
  92. return $scope.searchDashboards(linkDef, 7);
  93. }
  94. if (linkDef.type === 'link') {
  95. return $q.when([{
  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. return $q.when([]);
  106. }
  107. function updateDashLinks() {
  108. var promises = _.map($scope.links, buildLinks);
  109. $q.all(promises).then(function(results) {
  110. $scope.generatedLinks = _.flatten(results);
  111. });
  112. }
  113. $scope.searchDashboards = function(link, limit) {
  114. return backendSrv.search({tag: link.tags, limit: limit}).then(function(results) {
  115. return _.reduce(results, function(memo, dash) {
  116. // do not add current dashboard
  117. if (dash.id !== currentDashId) {
  118. memo.push({
  119. title: dash.title,
  120. url: 'dashboard/' + dash.uri,
  121. icon: 'fa fa-th-large',
  122. keepTime: link.keepTime,
  123. includeVars: link.includeVars
  124. });
  125. }
  126. return memo;
  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, $rootScope);
  140. });
  141. module.controller('DashLinkEditorCtrl', function($scope, $rootScope) {
  142. $scope.iconMap = iconMap;
  143. $scope.dashboard.links = $scope.dashboard.links || [];
  144. $scope.addLink = function() {
  145. $scope.dashboard.links.push({ type: 'dashboards', icon: 'external link' });
  146. $scope.updateSubmenuVisibility();
  147. $scope.updated();
  148. };
  149. $scope.moveLink = function(index, dir) {
  150. _.move($scope.dashboard.links, index, index+dir);
  151. $scope.updated();
  152. };
  153. $scope.updated = function() {
  154. $rootScope.appEvent('dash-links-updated');
  155. };
  156. $scope.deleteLink = function(index) {
  157. $scope.dashboard.links.splice(index, 1);
  158. $scope.updateSubmenuVisibility();
  159. $scope.updated();
  160. };
  161. });
  162. });