search.js 5.1 KB

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