module.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. define([
  2. 'angular',
  3. 'lodash',
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. angular
  8. .module('grafana.directives')
  9. .directive('dashLinksEditor', function() {
  10. return {
  11. scope: {
  12. dashboard: "="
  13. },
  14. restrict: 'E',
  15. controller: 'DashLinkEditorCtrl',
  16. templateUrl: 'app/features/dashlinks/editor.html',
  17. link: function() {
  18. }
  19. };
  20. }).directive('dashLink', function(linkSrv, $rootScope) {
  21. return {
  22. scope: {
  23. link: "="
  24. },
  25. restrict: 'E',
  26. controller: 'DashLinkCtrl',
  27. templateUrl: 'app/features/dashlinks/module.html',
  28. link: function(scope, elem) {
  29. var linkInfo;
  30. var anchor = elem.find('a');
  31. var icon = elem.find('i');
  32. var span = elem.find('span');
  33. function update() {
  34. linkInfo = linkSrv.getPanelLinkAnchorInfo(scope.link);
  35. span.text(linkInfo.title);
  36. anchor.attr("href", linkInfo.href);
  37. if (scope.link.type === 'absolute') {
  38. icon.attr('class', 'fa fw fa-external-link');
  39. anchor.attr('target', '_blank');
  40. } else {
  41. icon.attr('class', 'fa fw fa-th-large');
  42. anchor.attr('target', '');
  43. }
  44. }
  45. // tooltip
  46. elem.find('a').tooltip({
  47. title: function () {
  48. if (scope.link.tooltip) {
  49. return scope.link.tooltip;
  50. }
  51. else if (scope.link.type === 'dashboard') {
  52. return 'Open dashboard';
  53. } else if (scope.link.type === 'absolute') {
  54. return 'Open external page';
  55. }
  56. },
  57. html: true,
  58. container: 'body', // Grafana change
  59. });
  60. update();
  61. scope.$on('refresh', update);
  62. $rootScope.onAppEvent('dash-links-updated', update);
  63. }
  64. };
  65. })
  66. .controller("DashLinkCtrl", function() {
  67. })
  68. .controller('DashLinkEditorCtrl', function($scope, backendSrv, $rootScope) {
  69. $scope.dashboard.links = $scope.dashboard.links || [];
  70. $scope.addLink = function() {
  71. $scope.dashboard.links.push({
  72. type: 'dashboard',
  73. name: 'Related dashboard'
  74. });
  75. };
  76. $scope.updated = function() {
  77. $rootScope.appEvent('dash-links-updated');
  78. };
  79. $scope.searchDashboards = function(queryStr, callback) {
  80. var query = {query: queryStr};
  81. backendSrv.search(query).then(function(result) {
  82. var dashboards = _.map(result.dashboards, function(dash) {
  83. return dash.title;
  84. });
  85. callback(dashboards);
  86. });
  87. };
  88. $scope.deleteLink = function(link) {
  89. $scope.dashboard.links = _.without($scope.dashboard.links, link);
  90. };
  91. });
  92. });