search.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config',
  5. 'jquery'
  6. ],
  7. function (angular, _, config, $) {
  8. 'use strict';
  9. var module = angular.module('grafana.controllers');
  10. module.controller('SearchCtrl', function($scope, $rootScope, $element, $location, datasourceSrv, $timeout) {
  11. $scope.init = function() {
  12. $scope.giveSearchFocus = 0;
  13. $scope.selectedIndex = -1;
  14. $scope.results = {dashboards: [], tags: [], metrics: []};
  15. $scope.query = { query: 'title:' };
  16. $scope.db = datasourceSrv.getGrafanaDB();
  17. $timeout(function() {
  18. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  19. $scope.query.query = 'title:';
  20. $scope.search();
  21. }, 100);
  22. };
  23. $scope.keyDown = function (evt) {
  24. if (evt.keyCode === 27) {
  25. $element.find('.dropdown-toggle').dropdown('toggle');
  26. }
  27. if (evt.keyCode === 40) {
  28. $scope.selectedIndex++;
  29. }
  30. if (evt.keyCode === 38) {
  31. $scope.selectedIndex--;
  32. }
  33. if (evt.keyCode === 13) {
  34. if ($scope.tagsOnly) {
  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.id);
  45. setTimeout(function() {
  46. $('body').click(); // hack to force dropdown to close;
  47. });
  48. }
  49. }
  50. };
  51. $scope.goToDashboard = function(id) {
  52. $location.path("/dashboard/db/" + id);
  53. };
  54. $scope.shareDashboard = function(title, id, $event) {
  55. $event.stopPropagation();
  56. var baseUrl = window.location.href.replace(window.location.hash,'');
  57. $scope.share = {
  58. title: title,
  59. url: baseUrl + '#dashboard/db/' + encodeURIComponent(id)
  60. };
  61. };
  62. $scope.searchDashboards = function(queryString) {
  63. return $scope.db.searchDashboards(queryString)
  64. .then(function(results) {
  65. $scope.tagsOnly = results.tagsOnly;
  66. $scope.results.dashboards = results.dashboards;
  67. $scope.results.tags = results.tags;
  68. });
  69. };
  70. $scope.filterByTag = function(tag, evt) {
  71. $scope.query.query = "tags:" + tag + " AND title:";
  72. $scope.search();
  73. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  74. if (evt) {
  75. evt.stopPropagation();
  76. evt.preventDefault();
  77. }
  78. };
  79. $scope.showTags = function(evt) {
  80. evt.stopPropagation();
  81. $scope.tagsOnly = !$scope.tagsOnly;
  82. $scope.query.query = $scope.tagsOnly ? "tags!:" : "";
  83. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  84. $scope.selectedIndex = -1;
  85. $scope.search();
  86. };
  87. $scope.search = function() {
  88. $scope.showImport = false;
  89. $scope.selectedIndex = 0;
  90. $scope.searchDashboards($scope.query.query);
  91. };
  92. $scope.addMetricToCurrentDashboard = function (metricId) {
  93. $scope.dashboard.rows.push({
  94. title: '',
  95. height: '250px',
  96. editable: true,
  97. panels: [
  98. {
  99. type: 'graphite',
  100. title: 'test',
  101. span: 12,
  102. targets: [{ target: metricId }]
  103. }
  104. ]
  105. });
  106. };
  107. $scope.toggleImport = function ($event) {
  108. $event.stopPropagation();
  109. $scope.showImport = !$scope.showImport;
  110. };
  111. $scope.newDashboard = function() {
  112. $location.url('/dashboard/file/empty.json');
  113. };
  114. });
  115. module.directive('xngFocus', function() {
  116. return function(scope, element, attrs) {
  117. $(element).click(function(e) {
  118. e.stopPropagation();
  119. });
  120. scope.$watch(attrs.xngFocus,function (newValue) {
  121. setTimeout(function() {
  122. newValue && element.focus();
  123. }, 200);
  124. },true);
  125. };
  126. });
  127. module.directive('tagColorFromName', function() {
  128. function djb2(str) {
  129. var hash = 5381;
  130. for (var i = 0; i < str.length; i++) {
  131. hash = ((hash << 5) + hash) + str.charCodeAt(i); /* hash * 33 + c */
  132. }
  133. return hash;
  134. }
  135. return function (scope, element) {
  136. var name = _.isString(scope.tag) ? scope.tag : scope.tag.term;
  137. var hash = djb2(name.toLowerCase());
  138. var colors = [
  139. "#E24D42","#1F78C1","#BA43A9","#705DA0","#466803",
  140. "#508642","#447EBC","#C15C17","#890F02","#757575",
  141. "#0A437C","#6D1F62","#584477","#629E51","#2F4F4F",
  142. "#BF1B00","#806EB7","#8a2eb8", "#699e00","#000000",
  143. "#3F6833","#2F575E","#99440A","#E0752D","#0E4AB4",
  144. "#58140C","#052B51","#511749","#3F2B5B",
  145. ];
  146. var color = colors[Math.abs(hash % colors.length)];
  147. element.css("background-color", color);
  148. };
  149. });
  150. });