module.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. scope: {
  20. dashboard: "="
  21. },
  22. restrict: 'E',
  23. controller: 'DashLinkEditorCtrl',
  24. templateUrl: 'app/features/dashlinks/editor.html',
  25. link: function() {
  26. }
  27. };
  28. });
  29. module.directive('dashLinksContainer', function() {
  30. return {
  31. scope: {
  32. links: "="
  33. },
  34. restrict: 'E',
  35. controller: 'DashLinksContainerCtrl',
  36. template: '<dash-link ng-repeat="link in generatedLinks" link="link"></dash-link>',
  37. link: function() { }
  38. };
  39. });
  40. module.directive('dashLink', function($compile, linkSrv) {
  41. return {
  42. restrict: 'E',
  43. link: function(scope, elem) {
  44. var link = scope.link;
  45. var template = '<div class="submenu-item dropdown">' +
  46. '<a class="pointer dash-nav-link" data-placement="bottom"' +
  47. (link.asDropdown ? ' ng-click="fillDropdown(link)" data-toggle="dropdown"' : "") + '>' +
  48. '<i></i> <span></span></a>';
  49. if (link.asDropdown) {
  50. template += '<ul class="dropdown-menu" role="menu">' +
  51. '<li ng-repeat="dash in link.searchHits"><a href="{{dash.url}}"><i class="fa fa-th-large"></i> {{dash.title}}</a></li>' +
  52. '</ul';
  53. }
  54. elem.html(template);
  55. $compile(elem.contents())(scope);
  56. var anchor = elem.find('a');
  57. var icon = elem.find('i');
  58. var span = elem.find('span');
  59. function update() {
  60. var linkInfo = linkSrv.getAnchorInfo(link);
  61. span.text(linkInfo.title);
  62. anchor.attr("href", linkInfo.href);
  63. }
  64. // tooltip
  65. elem.find('a').tooltip({ title: scope.link.tooltip, html: true, container: 'body' });
  66. icon.attr('class', 'fa fa-fw ' + scope.link.icon);
  67. anchor.attr('target', scope.link.target);
  68. update();
  69. scope.$on('refresh', update);
  70. }
  71. };
  72. });
  73. module.controller("DashLinksContainerCtrl", function($scope, $rootScope, $q, backendSrv, dashboardSrv, linkSrv) {
  74. var currentDashId = dashboardSrv.getCurrent().id;
  75. function buildLinks(linkDef) {
  76. if (linkDef.type === 'dashboards') {
  77. if (!linkDef.tag) {
  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. tag: linkDef.tag,
  85. icon: "fa fa-bars",
  86. asDropdown: true
  87. }]);
  88. }
  89. return $scope.searchDashboards(linkDef.tag);
  90. }
  91. if (linkDef.type === 'link') {
  92. return $q.when([{
  93. url: linkDef.url,
  94. title: linkDef.title,
  95. icon: iconMap[linkDef.icon],
  96. tooltip: linkDef.tooltip,
  97. target: linkDef.targetBlank ? "_blank" : "",
  98. }]);
  99. }
  100. return $q.when([]);
  101. }
  102. function updateDashLinks() {
  103. var promises = _.map($scope.links, buildLinks);
  104. $q.all(promises).then(function(results) {
  105. $scope.generatedLinks = _.flatten(results);
  106. });
  107. }
  108. $scope.searchDashboards = function(tag) {
  109. return backendSrv.search({tag: tag}).then(function(results) {
  110. return _.reduce(results.dashboards, function(memo, dash) {
  111. // do not add current dashboard
  112. if (dash.id !== currentDashId) {
  113. memo.push({ title: dash.title, url: 'dashboard/db/'+ dash.slug, icon: 'fa fa-th-large', addTime: true });
  114. }
  115. return memo;
  116. }, []);
  117. });
  118. };
  119. $scope.fillDropdown = function(link) {
  120. $scope.searchDashboards(link.tag).then(function(results) {
  121. _.each(results, function(hit) {
  122. hit.url = linkSrv.getLinkUrl(hit);
  123. });
  124. link.searchHits = results;
  125. });
  126. };
  127. updateDashLinks();
  128. $rootScope.onAppEvent('dash-links-updated', updateDashLinks);
  129. });
  130. module.controller('DashLinkEditorCtrl', function($scope, $rootScope) {
  131. $scope.iconMap = iconMap;
  132. $scope.dashboard.links = $scope.dashboard.links || [];
  133. $scope.addLink = function() {
  134. $scope.dashboard.links.push({ type: 'dashboards', icon: 'external link' });
  135. };
  136. $scope.moveLink = function(index, dir) {
  137. _.move($scope.dashboard.links, index, index+dir);
  138. $scope.updated();
  139. };
  140. $scope.updated = function() {
  141. $rootScope.appEvent('dash-links-updated');
  142. };
  143. $scope.deleteLink = function(link) {
  144. $scope.dashboard.links = _.without($scope.dashboard.links, link);
  145. };
  146. });
  147. });