module.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. define([
  2. 'angular',
  3. 'lodash',
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.directives');
  8. module.directive('dashLinksEditor', function() {
  9. return {
  10. scope: {
  11. dashboard: "="
  12. },
  13. restrict: 'E',
  14. controller: 'DashLinkEditorCtrl',
  15. templateUrl: 'app/features/dashlinks/editor.html',
  16. link: function() {
  17. }
  18. };
  19. });
  20. module.directive('dashLinksContainer', function() {
  21. return {
  22. scope: {
  23. links: "="
  24. },
  25. restrict: 'E',
  26. controller: 'DashLinksContainerCtrl',
  27. template: '<dash-link ng-repeat="link in generatedLinks" link="link"></dash-link>',
  28. link: function() { }
  29. };
  30. });
  31. module.directive('dashLink', function(templateSrv) {
  32. return {
  33. scope: {
  34. link: "="
  35. },
  36. restrict: 'E',
  37. controller: 'DashLinkCtrl',
  38. templateUrl: 'app/features/dashlinks/module.html',
  39. link: function(scope, elem) {
  40. var anchor = elem.find('a');
  41. var icon = elem.find('i');
  42. var span = elem.find('span');
  43. function update() {
  44. span.text(templateSrv.replace(scope.link.title));
  45. anchor.attr("href", templateSrv.replace(scope.link.url));
  46. }
  47. // tooltip
  48. elem.find('a').tooltip({ title: scope.link.tooltip, html: true, container: 'body' });
  49. icon.attr('class', scope.link.icon);
  50. update();
  51. scope.$on('refresh', update);
  52. }
  53. };
  54. });
  55. module.controller("DashLinksContainerCtrl", function($scope, $rootScope, $q, backendSrv) {
  56. function buildLinks(linkDef) {
  57. if (linkDef.type === 'dashboards') {
  58. return backendSrv.search({tag: linkDef.tag}).then(function(results) {
  59. return _.map(results.dashboards, function(dash) {
  60. return {
  61. title: dash.title,
  62. url: 'dashboard/db/'+ dash.slug,
  63. icon: 'fa fa-th-large'
  64. };
  65. });
  66. });
  67. }
  68. if (linkDef.type === 'link') {
  69. return $q.when([{ url: linkDef.url, title: linkDef.title, icon: 'fa fa-external-link', }]);
  70. }
  71. return $q.when([]);
  72. }
  73. function updateDashLinks() {
  74. var promises = _.map($scope.links, buildLinks);
  75. $q.all(promises).then(function(results) {
  76. $scope.generatedLinks = _.flatten(results);
  77. });
  78. }
  79. updateDashLinks();
  80. $rootScope.onAppEvent('dash-links-updated', updateDashLinks);
  81. });
  82. module.controller("DashLinkCtrl", function($scope) {
  83. if ($scope.link.type === 'dashboards') {
  84. $scope.searchHits = [];
  85. }
  86. });
  87. module.controller('DashLinkEditorCtrl', function($scope, backendSrv, $rootScope) {
  88. $scope.dashboard.links = $scope.dashboard.links || [];
  89. $scope.addLink = function() {
  90. $scope.dashboard.links.push({
  91. type: 'dashboard',
  92. name: 'Related dashboard'
  93. });
  94. };
  95. $scope.updated = function() {
  96. $rootScope.appEvent('dash-links-updated');
  97. };
  98. $scope.searchDashboards = function(queryStr, callback) {
  99. var query = {query: queryStr};
  100. backendSrv.search(query).then(function(result) {
  101. var dashboards = _.map(result.dashboards, function(dash) {
  102. return dash.title;
  103. });
  104. callback(dashboards);
  105. });
  106. };
  107. $scope.deleteLink = function(link) {
  108. $scope.dashboard.links = _.without($scope.dashboard.links, link);
  109. };
  110. });
  111. });