search.js 5.1 KB

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