search.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. $timeout(function() {
  17. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  18. $scope.query.query = '';
  19. $scope.search();
  20. }, 100);
  21. };
  22. $scope.keyDown = function (evt) {
  23. if (evt.keyCode === 27) {
  24. $scope.dismiss();
  25. }
  26. if (evt.keyCode === 40) {
  27. $scope.moveSelection(1);
  28. }
  29. if (evt.keyCode === 38) {
  30. $scope.moveSelection(-1);
  31. }
  32. if (evt.keyCode === 13) {
  33. if ($scope.query.tagcloud) {
  34. var tag = $scope.results.tags[$scope.selectedIndex];
  35. if (tag) {
  36. $scope.filterByTag(tag.term);
  37. }
  38. return;
  39. }
  40. var selectedDash = $scope.results.dashboards[$scope.selectedIndex];
  41. if (selectedDash) {
  42. $location.search({});
  43. $location.path(selectedDash.url);
  44. }
  45. }
  46. };
  47. $scope.moveSelection = function(direction) {
  48. $scope.selectedIndex = Math.max(Math.min($scope.selectedIndex + direction, $scope.resultCount - 1), 0);
  49. };
  50. $scope.searchDashboards = function() {
  51. $scope.currentSearchId = $scope.currentSearchId + 1;
  52. var localSearchId = $scope.currentSearchId;
  53. return backendSrv.search($scope.query).then(function(results) {
  54. if (localSearchId < $scope.currentSearchId) { return; }
  55. $scope.resultCount = results.tagsOnly ? results.tags.length : results.dashboards.length;
  56. $scope.results.tags = results.tags;
  57. $scope.results.dashboards = _.map(results.dashboards, function(dash) {
  58. dash.url = 'dashboard/db/' + dash.slug;
  59. return dash;
  60. });
  61. if ($scope.queryHasNoFilters()) {
  62. $scope.results.dashboards.unshift({ title: 'Home', url: config.appSubUrl + '/', isHome: true });
  63. }
  64. });
  65. };
  66. $scope.queryHasNoFilters = function() {
  67. var query = $scope.query;
  68. return query.query === '' && query.starred === false && query.tag === '';
  69. };
  70. $scope.filterByTag = function(tag, evt) {
  71. $scope.query.tag = tag;
  72. $scope.query.tagcloud = false;
  73. $scope.search();
  74. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  75. if (evt) {
  76. evt.stopPropagation();
  77. evt.preventDefault();
  78. }
  79. };
  80. $scope.showTags = function() {
  81. $scope.query.tagcloud = !$scope.query.tagcloud;
  82. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  83. $scope.search();
  84. };
  85. $scope.showStarred = function() {
  86. $scope.query.starred = !$scope.query.starred;
  87. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  88. $scope.search();
  89. };
  90. $scope.search = function() {
  91. $scope.showImport = false;
  92. $scope.selectedIndex = 0;
  93. $scope.searchDashboards();
  94. };
  95. $scope.addMetricToCurrentDashboard = function (metricId) {
  96. $scope.dashboard.rows.push({
  97. title: '',
  98. height: '250px',
  99. editable: true,
  100. panels: [
  101. {
  102. type: 'graphite',
  103. title: 'test',
  104. span: 12,
  105. targets: [{ target: metricId }]
  106. }
  107. ]
  108. });
  109. };
  110. $scope.toggleImport = function () {
  111. $scope.showImport = !$scope.showImport;
  112. };
  113. $scope.newDashboard = function() {
  114. $location.url('dashboard/new');
  115. };
  116. });
  117. module.directive('xngFocus', function() {
  118. return function(scope, element, attrs) {
  119. element.click(function(e) {
  120. e.stopPropagation();
  121. });
  122. scope.$watch(attrs.xngFocus,function (newValue) {
  123. if (!newValue) {
  124. return;
  125. }
  126. setTimeout(function() {
  127. element.focus();
  128. var pos = element.val().length * 2;
  129. element[0].setSelectionRange(pos, pos);
  130. }, 200);
  131. },true);
  132. };
  133. });
  134. module.directive('tagColorFromName', function() {
  135. function djb2(str) {
  136. var hash = 5381;
  137. for (var i = 0; i < str.length; i++) {
  138. hash = ((hash << 5) + hash) + str.charCodeAt(i); /* hash * 33 + c */
  139. }
  140. return hash;
  141. }
  142. return {
  143. scope: { tag: "=" },
  144. link: function (scope, element) {
  145. var name = scope.tag;
  146. var hash = djb2(name.toLowerCase());
  147. var colors = [
  148. "#E24D42","#1F78C1","#BA43A9","#705DA0","#466803",
  149. "#508642","#447EBC","#C15C17","#890F02","#757575",
  150. "#0A437C","#6D1F62","#584477","#629E51","#2F4F4F",
  151. "#BF1B00","#806EB7","#8a2eb8", "#699e00","#000000",
  152. "#3F6833","#2F575E","#99440A","#E0752D","#0E4AB4",
  153. "#58140C","#052B51","#511749","#3F2B5B",
  154. ];
  155. var color = colors[Math.abs(hash % colors.length)];
  156. element.css("background-color", color);
  157. }
  158. };
  159. });
  160. });