| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- define([
- 'angular',
- 'underscore',
- 'config',
- 'jquery'
- ],
- function (angular, _, config, $) {
- 'use strict';
- var module = angular.module('kibana.controllers');
- module.controller('SearchCtrl', function($scope, $rootScope, $element, $location, elastic) {
- $scope.init = function() {
- $scope.giveSearchFocus = 0;
- $scope.selectedIndex = -1;
- $scope.results = {dashboards: [], tags: [], metrics: []};
- $scope.query = { query: 'title:' };
- $scope.onAppEvent('open-search', $scope.openSearch);
- };
- $scope.keyDown = function (evt) {
- if (evt.keyCode === 27) {
- $element.find('.dropdown-toggle').dropdown('toggle');
- }
- if (evt.keyCode === 40) {
- $scope.selectedIndex++;
- }
- if (evt.keyCode === 38) {
- $scope.selectedIndex--;
- }
- if (evt.keyCode === 13) {
- if ($scope.tagsOnly) {
- var tag = $scope.results.tags[$scope.selectedIndex];
- if (tag) {
- $scope.filterByTag(tag.term);
- }
- return;
- }
- var selectedDash = $scope.results.dashboards[$scope.selectedIndex];
- if (selectedDash) {
- $location.path("/dashboard/elasticsearch/" + encodeURIComponent(selectedDash._id));
- setTimeout(function() {
- $('body').click(); // hack to force dropdown to close;
- });
- }
- }
- };
- $scope.shareDashboard = function(title, id) {
- var baseUrl = window.location.href.replace(window.location.hash,'');
- $scope.share = {
- title: title,
- url: baseUrl + '#dashboard/elasticsearch/' + encodeURIComponent(id)
- };
- };
- $scope.searchDasboards = function(queryString) {
- var tagsOnly = queryString.indexOf('tags!:') === 0;
- if (tagsOnly) {
- var tagsQuery = queryString.substring(6, queryString.length);
- queryString = 'tags:' + tagsQuery + '*';
- }
- else {
- if (queryString.length === 0) {
- queryString = 'title:';
- }
- if (queryString[queryString.length - 1] !== '*') {
- queryString += '*';
- }
- }
- var query = {
- query: { query_string: { query: queryString } },
- facets: { tags: { terms: { field: "tags", order: "term", size: 50 } } },
- size: 20,
- sort: ["_uid"]
- };
- return elastic.post('/dashboard/_search', query)
- .then(function(results) {
- if(_.isUndefined(results.hits)) {
- $scope.results.dashboards = [];
- $scope.results.tags = [];
- return;
- }
- $scope.tagsOnly = tagsOnly;
- $scope.results.dashboards = results.hits.hits;
- $scope.results.tags = results.facets.tags.terms;
- });
- };
- $scope.filterByTag = function(tag, evt) {
- $scope.query.query = "tags:" + tag + " AND title:";
- $scope.search();
- $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
- if (evt) {
- evt.stopPropagation();
- evt.preventDefault();
- }
- };
- $scope.showTags = function(evt) {
- evt.stopPropagation();
- $scope.tagsOnly = !$scope.tagsOnly;
- $scope.query.query = $scope.tagsOnly ? "tags!:" : "";
- $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
- $scope.selectedIndex = -1;
- $scope.search();
- };
- $scope.search = function() {
- $scope.showImport = false;
- $scope.selectedIndex = -1;
- var queryStr = $scope.query.query.toLowerCase();
- if (queryStr.indexOf('m:') !== 0) {
- queryStr = queryStr.replace(' and ', ' AND ');
- $scope.searchDasboards(queryStr);
- return;
- }
- };
- $scope.openSearch = function (evt) {
- if (evt) {
- $element.find('.dropdown-toggle').dropdown('toggle');
- }
- $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
- $scope.query.query = 'title:';
- $scope.search();
- };
- $scope.addMetricToCurrentDashboard = function (metricId) {
- $scope.dashboard.rows.push({
- title: '',
- height: '250px',
- editable: true,
- panels: [
- {
- type: 'graphite',
- title: 'test',
- span: 12,
- targets: [{ target: metricId }]
- }
- ]
- });
- };
- $scope.toggleImport = function ($event) {
- $event.stopPropagation();
- $scope.showImport = !$scope.showImport;
- };
- $scope.newDashboard = function() {
- $location.url('/dashboard/file/empty.json');
- };
- });
- module.directive('xngFocus', function() {
- return function(scope, element, attrs) {
- $(element).click(function(e) {
- e.stopPropagation();
- });
- scope.$watch(attrs.xngFocus,function (newValue) {
- setTimeout(function() {
- newValue && element.focus();
- }, 200);
- },true);
- };
- });
- });
|