search.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config',
  5. ],
  6. function (angular, _, config) {
  7. 'use strict';
  8. var module = angular.module('grafana.controllers');
  9. module.controller('SearchCtrl', function($scope, $location, $timeout, backendSrv) {
  10. $scope.init = function() {
  11. $scope.giveSearchFocus = 0;
  12. $scope.selectedIndex = -1;
  13. $scope.results = {dashboards: [], tags: [], metrics: []};
  14. $scope.query = { query: '', tag: '', starred: false };
  15. $scope.currentSearchId = 0;
  16. if ($scope.dashboardViewState.fullscreen) {
  17. $scope.exitFullscreen();
  18. }
  19. $timeout(function() {
  20. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  21. $scope.query.query = '';
  22. $scope.search();
  23. }, 100);
  24. };
  25. $scope.keyDown = function (evt) {
  26. if (evt.keyCode === 27) {
  27. $scope.dismiss();
  28. }
  29. if (evt.keyCode === 40) {
  30. $scope.moveSelection(1);
  31. }
  32. if (evt.keyCode === 38) {
  33. $scope.moveSelection(-1);
  34. }
  35. if (evt.keyCode === 13) {
  36. if ($scope.tagMode) {
  37. var tag = $scope.results[$scope.selectedIndex];
  38. if (tag) {
  39. $scope.filterByTag(tag.term);
  40. }
  41. return;
  42. }
  43. var selectedDash = $scope.results[$scope.selectedIndex];
  44. if (selectedDash) {
  45. $location.search({});
  46. $location.path(selectedDash.url);
  47. }
  48. }
  49. };
  50. $scope.moveSelection = function(direction) {
  51. var max = ($scope.results || []).length;
  52. var newIndex = $scope.selectedIndex + direction;
  53. $scope.selectedIndex = ((newIndex %= max) < 0) ? newIndex + max : newIndex;
  54. };
  55. $scope.searchDashboards = function() {
  56. $scope.tagsMode = false;
  57. $scope.currentSearchId = $scope.currentSearchId + 1;
  58. var localSearchId = $scope.currentSearchId;
  59. return backendSrv.search($scope.query).then(function(results) {
  60. if (localSearchId < $scope.currentSearchId) { return; }
  61. $scope.results = _.map(results, function(dash) {
  62. dash.url = 'dashboard/' + dash.uri;
  63. return dash;
  64. });
  65. if ($scope.queryHasNoFilters()) {
  66. $scope.results.unshift({ title: 'Home', url: config.appSubUrl + '/', type: 'dash-home' });
  67. }
  68. });
  69. };
  70. $scope.queryHasNoFilters = function() {
  71. var query = $scope.query;
  72. return query.query === '' && query.starred === false && query.tag === '';
  73. };
  74. $scope.filterByTag = function(tag, evt) {
  75. $scope.query.tag = tag;
  76. $scope.query.tagcloud = false;
  77. $scope.search();
  78. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  79. if (evt) {
  80. evt.stopPropagation();
  81. evt.preventDefault();
  82. }
  83. };
  84. $scope.getTags = function() {
  85. return backendSrv.get('/api/dashboards/tags').then(function(results) {
  86. $scope.tagsMode = true;
  87. $scope.results = results;
  88. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  89. });
  90. };
  91. $scope.showStarred = function() {
  92. $scope.query.starred = !$scope.query.starred;
  93. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  94. $scope.search();
  95. };
  96. $scope.search = function() {
  97. $scope.showImport = false;
  98. $scope.selectedIndex = 0;
  99. $scope.searchDashboards();
  100. };
  101. $scope.newDashboard = function() {
  102. $location.url('dashboard/new');
  103. };
  104. });
  105. module.directive('tagColorFromName', function() {
  106. function djb2(str) {
  107. var hash = 5381;
  108. for (var i = 0; i < str.length; i++) {
  109. hash = ((hash << 5) + hash) + str.charCodeAt(i); /* hash * 33 + c */
  110. }
  111. return hash;
  112. }
  113. return {
  114. scope: { tag: "=" },
  115. link: function (scope, element) {
  116. var name = scope.tag;
  117. var hash = djb2(name.toLowerCase());
  118. var colors = [
  119. "#E24D42","#1F78C1","#BA43A9","#705DA0","#466803",
  120. "#508642","#447EBC","#C15C17","#890F02","#757575",
  121. "#0A437C","#6D1F62","#584477","#629E51","#2F4F4F",
  122. "#BF1B00","#806EB7","#8a2eb8", "#699e00","#000000",
  123. "#3F6833","#2F575E","#99440A","#E0752D","#0E4AB4",
  124. "#58140C","#052B51","#511749","#3F2B5B",
  125. ];
  126. var color = colors[Math.abs(hash % colors.length)];
  127. element.css("background-color", color);
  128. }
  129. };
  130. });
  131. });