|
20 | 20 | import { once } from 'lodash'; |
21 | 21 |
|
22 | 22 | import { wrapInI18nContext } from 'ui/i18n'; |
23 | | -import { Filter } from '@kbn/es-query'; |
24 | 23 |
|
25 | 24 | // @ts-ignore |
26 | 25 | import { uiModules } from 'ui/modules'; |
27 | 26 | import { npStart } from 'ui/new_platform'; |
28 | 27 | import { FilterBar, ApplyFiltersPopover } from '../filter'; |
29 | | -import template from './apply_filter_directive.html'; |
30 | 28 |
|
31 | 29 | // @ts-ignore |
32 | 30 | import { mapAndFlattenFilters } from '../filter/filter_manager/lib/map_and_flatten_filters'; |
@@ -76,35 +74,53 @@ export const initLegacyModule = once((): void => { |
76 | 74 | ['pluginDataStart', { watchDepth: 'reference' }], |
77 | 75 | ]); |
78 | 76 | }) |
79 | | - .directive('applyFiltersPopoverComponent', (reactDirective: any) => |
80 | | - reactDirective(wrapInI18nContext(ApplyFiltersPopover)) |
81 | | - ) |
82 | 77 | .directive('applyFiltersPopover', () => { |
83 | 78 | return { |
84 | | - template, |
85 | 79 | restrict: 'E', |
86 | | - scope: { |
87 | | - filters: '=', |
88 | | - onCancel: '=', |
89 | | - onSubmit: '=', |
90 | | - indexPatterns: '=', |
91 | | - }, |
92 | | - link($scope: any) { |
93 | | - $scope.state = {}; |
94 | | - |
95 | | - // Each time the new filters change we want to rebuild (not just re-render) the "apply filters" |
96 | | - // popover, because it has to reset its state whenever the new filters change. Setting a `key` |
97 | | - // property on the component accomplishes this due to how React handles the `key` property. |
98 | | - $scope.$watch('filters', (filters: any) => { |
99 | | - const mappedFilters: Filter[] = mapAndFlattenFilters(filters); |
100 | | - $scope.state = { |
101 | | - filters: mappedFilters, |
102 | | - key: Date.now(), |
103 | | - }; |
104 | | - }); |
| 80 | + template: '', |
| 81 | + compile: (elem: any) => { |
| 82 | + const child = document.createElement('apply-filters-popover-helper'); |
| 83 | + |
| 84 | + // Copy attributes to the child directive |
| 85 | + for (const attr of elem[0].attributes) { |
| 86 | + child.setAttribute(attr.name, attr.value); |
| 87 | + } |
| 88 | + |
| 89 | + // Add a key attribute that will force a full rerender every time that |
| 90 | + // a filter changes. |
| 91 | + child.setAttribute('key', 'key'); |
| 92 | + |
| 93 | + // Append helper directive |
| 94 | + elem.append(child); |
| 95 | + |
| 96 | + const linkFn = ($scope: any, _: any, $attr: any) => { |
| 97 | + // Watch only for filter changes to update key. |
| 98 | + $scope.$watch( |
| 99 | + () => { |
| 100 | + return $scope.$eval($attr.filters) || []; |
| 101 | + }, |
| 102 | + (newVal: any) => { |
| 103 | + $scope.key = Date.now(); |
| 104 | + }, |
| 105 | + true |
| 106 | + ); |
| 107 | + }; |
| 108 | + |
| 109 | + return linkFn; |
105 | 110 | }, |
106 | 111 | }; |
107 | | - }); |
| 112 | + }) |
| 113 | + .directive('applyFiltersPopoverHelper', (reactDirective: any) => |
| 114 | + reactDirective(wrapInI18nContext(ApplyFiltersPopover), [ |
| 115 | + ['filters', { watchDepth: 'collection' }], |
| 116 | + ['onCancel', { watchDepth: 'reference' }], |
| 117 | + ['onSubmit', { watchDepth: 'reference' }], |
| 118 | + ['indexPatterns', { watchDepth: 'collection' }], |
| 119 | + |
| 120 | + // Key is needed to trigger a full rerender of the component |
| 121 | + 'key', |
| 122 | + ]) |
| 123 | + ); |
108 | 124 |
|
109 | 125 | const module = uiModules.get('kibana/index_patterns'); |
110 | 126 | let _service: any; |
|
0 commit comments