Skip to content

Commit

Permalink
Hook up search bar to actual filters
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasolson committed Dec 19, 2018
1 parent 4b6a5e9 commit 888ffb2
Show file tree
Hide file tree
Showing 27 changed files with 720 additions and 262 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ import { tabifyAggResponse } from 'ui/agg_response/tabify';
import { showSaveModal } from 'ui/saved_objects/show_saved_object_save_modal';
import { SavedObjectSaveModal } from 'ui/saved_objects/components/saved_object_save_modal';
import { getRootBreadcrumbs, getSavedSearchBreadcrumbs } from '../breadcrumbs';
import { createPhraseFilter } from '../../../../../../ui/public/filter_bar/filters/phrase_filter';
import {
createMetaFilter, enable, disable, pin, unpin,
toggleDisabled,
toggleNegation, togglePinned,
} from '../../../../../../ui/public/filter_bar/filters/meta_filter';

const app = uiModules.get('apps/discover', [
'kibana/notify',
Expand Down Expand Up @@ -188,63 +182,6 @@ function discoverController(
requests: new RequestAdapter()
};


$scope.reactFilters = [
createMetaFilter(createPhraseFilter({ field: '@tags.keyword', value: 'security', index: 'foo' })),
createMetaFilter(createPhraseFilter({ field: '@tags.keyword', value: 'error', index: 'foo' })),
createMetaFilter(createPhraseFilter({ field: '@tags.keyword', value: 'info', index: 'foo' })),
createMetaFilter(createPhraseFilter({ field: '@tags.keyword', value: 'foo', index: 'foo' })),
];

$scope.onToggleNegate = (filter) => {
const index = $scope.reactFilters.indexOf(filter);
$scope.reactFilters[index] = toggleNegation(filter);
};

$scope.onTogglePin = (filter) => {
const index = $scope.reactFilters.indexOf(filter);
$scope.reactFilters[index] = togglePinned(filter);
};

$scope.onToggleDisabled = (filter) => {
const index = $scope.reactFilters.indexOf(filter);
$scope.reactFilters[index] = toggleDisabled(filter);
};

$scope.onDelete = (filterToDelete) => {
$scope.reactFilters = $scope.reactFilters.filter((filter) => filter !== filterToDelete);
};

$scope.onAllFiltersAction = (action) => {
if (action === 'delete') {
$scope.reactFilters = [];
}
else {
$scope.reactFilters.forEach((filter, index) => {
switch (action) {
case 'enable':
$scope.reactFilters[index] = enable(filter);
break;
case 'disable':
$scope.reactFilters[index] = disable(filter);
break;
case 'pin':
$scope.reactFilters[index] = pin(filter);
break;
case 'unpin':
$scope.reactFilters[index] = unpin(filter);
break;
case 'toggleNegate':
$scope.reactFilters[index] = toggleNegation(filter);
break;
case 'toggleDisabled':
$scope.reactFilters[index] = toggleDisabled(filter);
break;
}
});
}
};

$scope.getDocLink = getDocLink;
$scope.intervalOptions = intervalOptions;
$scope.showInterval = false;
Expand Down Expand Up @@ -413,6 +350,13 @@ function discoverController(

const $state = $scope.state = new AppState(getStateDefaults());

$scope.filters = queryFilter.getFilters();

$scope.onFiltersUpdated = filters => {
// The filters will automatically be set when the queryFilter emits an update event (see below)
queryFilter.setFilters(filters);
};

const getFieldCounts = async () => {
// the field counts aren't set until we have the data back,
// so we wait for the fetch to be done before proceeding
Expand Down Expand Up @@ -549,6 +493,7 @@ function discoverController(

// update data source when filters update
$scope.$listen(queryFilter, 'update', function () {
$scope.filters = queryFilter.getFilters();
return $scope.updateDataSource().then(function () {
$state.save();
});
Expand Down
8 changes: 2 additions & 6 deletions src/legacy/core_plugins/kibana/public/discover/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ <h1 tabindex="0" id="kui_local_breadcrumb" class="kuiLocalBreadcrumb">
on-query-submit="updateQueryAndFetch"
app-name="'discover'"
index-patterns="[indexPattern]"
filters="reactFilters"
on-toggle-filter-negate="onToggleNegate"
on-toggle-filter-pin="onTogglePin"
on-toggle-filter-disabled="onToggleDisabled"
on-filter-delete="onDelete"
on-all-filters-action="onAllFiltersAction"
filters="filters"
on-filters-updated="onFiltersUpdated"
></search-bar>
</div>
</div>
Expand Down
24 changes: 24 additions & 0 deletions src/ui/public/filter_bar/filters/custom_filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { MetaFilter } from './meta_filter';

export type CustomFilter = MetaFilter & {
query: any;
};
28 changes: 28 additions & 0 deletions src/ui/public/filter_bar/filters/exists_filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { FilterMeta, MetaFilter } from './meta_filter';

export type ExistsFilterMeta = FilterMeta & {
key: string; // The name of the field
};

export type ExistsFilter = MetaFilter & {
meta: ExistsFilterMeta;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { CustomFilter } from '../custom_filter';
import { FilterViews } from './index';

export function getCustomFilterViews(filter: CustomFilter): FilterViews {
return {
getDisplayText() {
return JSON.stringify(filter.query);
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { ExistsFilter } from '../exists_filter';
import { FilterViews } from './index';

export function getExistsFilterViews(filter: ExistsFilter): FilterViews {
return {
getDisplayText() {
return `${filter.meta.key} exists`;
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { GeoBoundingBoxFilter } from '../geo_bounding_box_filter';
import { FilterViews } from './index';

export function getGeoBoundingBoxFilterViews(filter: GeoBoundingBoxFilter): FilterViews {
return {
getDisplayText() {
const { meta } = filter;
const { key, params } = meta;
const { bottom_right: bottomRight, top_left: topLeft } = params;
return `${key}: ${JSON.stringify(topLeft)} to ${JSON.stringify(bottomRight)}`;
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { GeoPolygonFilter } from '../geo_polygon_filter';
import { FilterViews } from './index';

export function getGeoPolygonFilterViews(filter: GeoPolygonFilter): FilterViews {
return {
getDisplayText() {
const { meta } = filter;
const { key, params } = meta;
const { points } = params;
return `${key}: ${points.map(point => JSON.stringify(point)).join(', ')}`;
},
};
}
50 changes: 40 additions & 10 deletions src/ui/public/filter_bar/filters/filter_views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,56 @@
* under the License.
*/

import { MetaFilter } from 'src/ui/public/filter_bar/filters/meta_filter';
import { Filter } from 'ui/filter_bar/filters';
import { PhraseFilter } from 'ui/filter_bar/filters/phrase_filter';
import {
CustomFilter,
ExistsFilter,
GeoBoundingBoxFilter,
GeoPolygonFilter,
MetaFilter,
PhraseFilter,
PhrasesFilter,
QueryFilter,
RangeFilter,
} from '../index';
import { getCustomFilterViews } from './custom_filter_views';
import { getExistsFilterViews } from './exists_filter_views';
import { getGeoBoundingBoxFilterViews } from './geo_bounding_box_filter_views';
import { getGeoPolygonFilterViews } from './geo_polygon_filter_views';
import { getPhraseFilterViews } from './phrase_filter_views';
import { getPhrasesFilterViews } from './phrases_filter_views';
import { getQueryFilterViews } from './query_filter_views';
import { getRangeFilterViews } from './range_filter_views';

export interface FilterViews {
getDisplayText: () => string;
}

export function getFilterDisplayText(metaFilter: MetaFilter): string {
const prefix = metaFilter.negate ? 'NOT ' : '';
const filterText = getViewsForType(metaFilter.filter).getDisplayText();
export function getFilterDisplayText(filter: MetaFilter): string {
if (filter.meta.alias !== null) {
return filter.meta.alias;
}
const prefix = filter.meta.negate ? 'NOT ' : '';
const filterText = getViewsForType(filter).getDisplayText();
return `${prefix}${filterText}`;
}

function getViewsForType(filter: Filter) {
switch (filter.type) {
case 'PhraseFilter':
function getViewsForType(filter: MetaFilter) {
switch (filter.meta.type) {
case 'exists':
return getExistsFilterViews(filter as ExistsFilter);
case 'geo_bounding_box':
return getGeoBoundingBoxFilterViews(filter as GeoBoundingBoxFilter);
case 'geo_polygon':
return getGeoPolygonFilterViews(filter as GeoPolygonFilter);
case 'phrase':
return getPhraseFilterViews(filter as PhraseFilter);
case 'phrases':
return getPhrasesFilterViews(filter as PhrasesFilter);
case 'query_string':
return getQueryFilterViews(filter as QueryFilter);
case 'range':
return getRangeFilterViews(filter as RangeFilter);
default:
throw new Error(`Unknown type: ${filter.type}`);
return getCustomFilterViews(filter as CustomFilter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* under the License.
*/

import { FilterViews } from 'ui/filter_bar/filters/filter_views/index';
import { PhraseFilter } from 'ui/filter_bar/filters/phrase_filter';
import { PhraseFilter } from '../phrase_filter';
import { FilterViews } from './index';

export function getPhraseFilterViews(filter: PhraseFilter): FilterViews {
return {
getDisplayText() {
return `${filter.field} : ${filter.value}`;
return `${filter.meta.key} : ${filter.meta.value}`;
},
};
}
Loading

0 comments on commit 888ffb2

Please sign in to comment.