Skip to content

Commit

Permalink
fix: Use config pinnedByDefault for filters created with filter editor (
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasolson authored Jul 10, 2018
1 parent aaf1d7f commit d42fef6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/ui/public/filter_editor/filter_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand Down Expand Up @@ -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 });
};

Expand Down
23 changes: 22 additions & 1 deletion src/ui/public/filter_editor/lib/__tests__/filter_editor_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import {
getOperatorOptions,
isFilterValid,
buildFilter,
areIndexPatternsProvided
areIndexPatternsProvided,
isFilterPinned
} from '../filter_editor_utils';

describe('FilterEditorUtils', function () {
Expand Down Expand Up @@ -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);
});
});
});
5 changes: 5 additions & 0 deletions src/ui/public/filter_editor/lib/filter_editor_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

0 comments on commit d42fef6

Please sign in to comment.