search.js 5.0 KB

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