From d42fef6752837c1b84f8eca9f4a0e9de807ae7e0 Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Tue, 10 Jul 2018 10:42:54 -0700 Subject: [PATCH] fix: Use config pinnedByDefault for filters created with filter editor (#20359) --- src/ui/public/filter_editor/filter_editor.js | 10 ++++---- .../lib/__tests__/filter_editor_utils.js | 23 ++++++++++++++++++- .../filter_editor/lib/filter_editor_utils.js | 5 ++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/ui/public/filter_editor/filter_editor.js b/src/ui/public/filter_editor/filter_editor.js index 4a46bcb54467a..880fbb5907a7f 100644 --- a/src/ui/public/filter_editor/filter_editor.js +++ b/src/ui/public/filter_editor/filter_editor.js @@ -35,7 +35,8 @@ import { getParamsFromFilter, isFilterValid, buildFilter, - areIndexPatternsProvided + areIndexPatternsProvided, + isFilterPinned } from './lib/filter_editor_utils'; import * as filterBuilder from '../filter_manager/lib'; import { keyMap } from '../utils/key_map'; @@ -54,7 +55,9 @@ module.directive('filterEditor', function ($timeout, indexPatterns) { }, controllerAs: 'filterEditor', bindToController: true, - controller: callAfterBindingsWorkaround(function ($scope, $element) { + controller: callAfterBindingsWorkaround(function ($scope, $element, config) { + const pinnedByDefault = config.get('filters:pinnedByDefault'); + this.init = async () => { if (!areIndexPatternsProvided(this.indexPatterns)) { const defaultIndexPattern = await indexPatterns.getDefault(); @@ -144,8 +147,7 @@ module.directive('filterEditor', function ($timeout, indexPatterns) { } newFilter.meta.disabled = filter.meta.disabled; newFilter.meta.alias = alias; - - const isPinned = _.get(filter, ['$state', 'store']) === 'globalState'; + const isPinned = isFilterPinned(filter, pinnedByDefault); return this.onSave({ filter, newFilter, isPinned }); }; diff --git a/src/ui/public/filter_editor/lib/__tests__/filter_editor_utils.js b/src/ui/public/filter_editor/lib/__tests__/filter_editor_utils.js index 82e8231ff9922..ef7eb15fd2536 100644 --- a/src/ui/public/filter_editor/lib/__tests__/filter_editor_utils.js +++ b/src/ui/public/filter_editor/lib/__tests__/filter_editor_utils.js @@ -40,7 +40,8 @@ import { getOperatorOptions, isFilterValid, buildFilter, - areIndexPatternsProvided + areIndexPatternsProvided, + isFilterPinned } from '../filter_editor_utils'; describe('FilterEditorUtils', function () { @@ -372,4 +373,24 @@ describe('FilterEditorUtils', function () { expect(areIndexPatternsProvided([indexPatternMock])).to.be(true); }); }); + + describe('isFilterPinned', function () { + it('should return false when the store is appState', function () { + const filter = { $state: { store: 'appState' } }; + expect(isFilterPinned(filter, false)).to.be(false); + expect(isFilterPinned(filter, true)).to.be(false); + }); + + it('should return true when the store is globalState', function () { + const filter = { $state: { store: 'globalState' } }; + expect(isFilterPinned(filter, false)).to.be(true); + expect(isFilterPinned(filter, true)).to.be(true); + }); + + it('should return the default when the store does not exist', function () { + const filter = {}; + expect(isFilterPinned(filter, false)).to.be(false); + expect(isFilterPinned(filter, true)).to.be(true); + }); + }); }); diff --git a/src/ui/public/filter_editor/lib/filter_editor_utils.js b/src/ui/public/filter_editor/lib/filter_editor_utils.js index 10138b5ad6c83..20f1160f1f7fd 100644 --- a/src/ui/public/filter_editor/lib/filter_editor_utils.js +++ b/src/ui/public/filter_editor/lib/filter_editor_utils.js @@ -104,3 +104,8 @@ export function buildFilter({ indexPattern, field, operator, params, filterBuild export function areIndexPatternsProvided(indexPatterns) { return _.compact(indexPatterns).length !== 0; } + +export function isFilterPinned(filter, pinnedByDefault) { + if (!filter.hasOwnProperty('$state')) return pinnedByDefault; + return filter.$state.store === 'globalState'; +}