|
| 1 | +import type { SearchMode } from '@js/common'; |
1 | 2 | import messageLocalization from '@js/common/core/localization/message'; |
| 3 | +import type { dxElementWrapper } from '@js/core/renderer'; |
2 | 4 | import $ from '@js/core/renderer'; |
| 5 | +import type { DeferredObj } from '@js/core/utils/deferred'; |
3 | 6 | import { Deferred } from '@js/core/utils/deferred'; |
4 | | -import { extend } from '@js/core/utils/extend'; |
5 | | -import { stubComponent } from '@js/core/utils/stubs'; |
6 | | -import errors from '@js/ui/widget/ui.errors'; |
7 | | - |
8 | | -let EditorClass = stubComponent('TextBox'); |
9 | | - |
10 | | -export default { |
11 | | - _getDefaultOptions() { |
12 | | - return extend(this.callBase(), { |
13 | | - searchMode: '', |
14 | | - searchExpr: null, |
15 | | - searchValue: '', |
16 | | - searchEnabled: false, |
17 | | - searchEditorOptions: {}, |
18 | | - }); |
19 | | - }, |
20 | | - |
21 | | - _initMarkup(): void { |
22 | | - this._renderSearch(); |
23 | | - this.callBase(); |
24 | | - }, |
| 7 | +import type { ValueChangedEvent } from '@js/ui/text_box'; |
| 8 | +import type { SearchBoxMixinOptions } from '@js/ui/widget/ui.search_box_mixin'; |
| 9 | +import { stubComponent } from '@ts/core/utils/m_stubs'; |
| 10 | +import type TextBox from '@ts/ui/text_box/m_text_box'; |
| 11 | +import type { TextBoxProperties } from '@ts/ui/text_box/m_text_box'; |
25 | 12 |
|
26 | | - _renderSearch(): void { |
27 | | - const $element = this.$element(); |
28 | | - const searchEnabled = this.option('searchEnabled'); |
29 | | - const searchBoxClassName = this._addWidgetPrefix('search'); |
30 | | - const rootElementClassName = this._addWidgetPrefix('with-search'); |
| 13 | +export const getOperationBySearchMode = (searchMode?: SearchMode): string | undefined => (searchMode === 'equals' ? '=' : searchMode); |
31 | 14 |
|
32 | | - if (!searchEnabled) { |
33 | | - $element.removeClass(rootElementClassName); |
34 | | - this._removeSearchBox(); |
35 | | - return; |
36 | | - } |
| 15 | +export type SearchBoxControllerOptions = SearchBoxMixinOptions & { |
| 16 | + tabIndex?: number; |
| 17 | + onValueChanged?: (value: string) => void; |
| 18 | +}; |
37 | 19 |
|
38 | | - const editorOptions = this._getSearchEditorOptions(); |
| 20 | +class SearchBoxController { |
| 21 | + static EditorClass: (new (...args) => TextBox) = stubComponent('TextBox'); |
39 | 22 |
|
40 | | - if (this._searchEditor) { |
41 | | - this._searchEditor.option(editorOptions); |
42 | | - } else { |
43 | | - $element.addClass(rootElementClassName); |
44 | | - this._$searchEditorElement = $('<div>').addClass(searchBoxClassName).prependTo($element); |
45 | | - this._searchEditor = this._createComponent(this._$searchEditorElement, EditorClass, editorOptions); |
46 | | - } |
47 | | - }, |
| 23 | + _editor?: TextBox | null; |
48 | 24 |
|
49 | | - _removeSearchBox(): void { |
50 | | - this._$searchEditorElement && this._$searchEditorElement.remove(); |
51 | | - delete this._$searchEditorElement; |
52 | | - delete this._searchEditor; |
53 | | - }, |
| 25 | + _valueChangeDeferred!: DeferredObj<unknown>; |
54 | 26 |
|
55 | | - _getSearchEditorOptions() { |
56 | | - const that = this; |
57 | | - const userEditorOptions = that.option('searchEditorOptions'); |
58 | | - const searchText = messageLocalization.format('Search'); |
| 27 | + // eslint-disable-next-line no-restricted-globals |
| 28 | + _valueChangeTimeout!: ReturnType<typeof setTimeout>; |
59 | 29 |
|
60 | | - return extend({ |
61 | | - mode: 'search', |
62 | | - placeholder: searchText, |
63 | | - tabIndex: that.option('tabIndex'), |
64 | | - value: that.option('searchValue'), |
65 | | - valueChangeEvent: 'input', |
66 | | - inputAttr: { |
67 | | - 'aria-label': searchText, |
68 | | - }, |
69 | | - onValueChanged(e) { |
70 | | - const searchTimeout = that.option('searchTimeout'); |
71 | | - that._valueChangeDeferred = Deferred(); |
72 | | - clearTimeout(that._valueChangeTimeout); |
73 | | - |
74 | | - that._valueChangeDeferred.done(function () { |
75 | | - this.option('searchValue', e.value); |
76 | | - }.bind(that)); |
77 | | - |
78 | | - if (e.event && e.event.type === 'input' && searchTimeout) { |
79 | | - that._valueChangeTimeout = setTimeout(() => { |
80 | | - that._valueChangeDeferred.resolve(); |
81 | | - }, searchTimeout); |
82 | | - } else { |
83 | | - that._valueChangeDeferred.resolve(); |
84 | | - } |
85 | | - }, |
86 | | - }, userEditorOptions); |
87 | | - }, |
| 30 | + _onSearchBoxValueChanged?: (value: string) => void; |
88 | 31 |
|
89 | | - _getAriaTarget() { |
90 | | - if (this.option('searchEnabled')) { |
91 | | - return this._itemContainer(true); |
92 | | - } |
93 | | - return this.callBase(); |
94 | | - }, |
| 32 | + static setEditorClass(value: new (...args) => TextBox): void { |
| 33 | + SearchBoxController.EditorClass = value; |
| 34 | + } |
95 | 35 |
|
96 | | - _focusTarget() { |
97 | | - if (this.option('searchEnabled')) { |
98 | | - return this._itemContainer(true); |
99 | | - } |
| 36 | + render( |
| 37 | + widgetPrefix: string, |
| 38 | + $container: dxElementWrapper, |
| 39 | + options: SearchBoxControllerOptions, |
| 40 | + createEditorCallback: ( |
| 41 | + $element: dxElementWrapper, |
| 42 | + component: (new (...args) => TextBox), |
| 43 | + options: TextBoxProperties, |
| 44 | + ) => TextBox, |
| 45 | + ): void { |
| 46 | + const rootElementClassName = `${widgetPrefix}-with-search`; |
| 47 | + const searchBoxClassName = `${widgetPrefix}-search`; |
| 48 | + const { searchEnabled, onValueChanged } = options; |
100 | 49 |
|
101 | | - return this.callBase(); |
102 | | - }, |
| 50 | + this._onSearchBoxValueChanged = onValueChanged; |
103 | 51 |
|
104 | | - _updateFocusState(e, isFocused): void { |
105 | | - if (this.option('searchEnabled')) { |
106 | | - this._toggleFocusClass(isFocused, this.$element()); |
107 | | - } |
108 | | - this.callBase(e, isFocused); |
109 | | - }, |
110 | | - |
111 | | - getOperationBySearchMode(searchMode) { |
112 | | - return searchMode === 'equals' ? '=' : searchMode; |
113 | | - }, |
114 | | - |
115 | | - _optionChanged(args) { |
116 | | - switch (args.name) { |
117 | | - case 'searchEnabled': |
118 | | - case 'searchEditorOptions': |
119 | | - this._invalidate(); |
120 | | - break; |
121 | | - case 'searchExpr': |
122 | | - case 'searchMode': |
123 | | - case 'searchValue': |
124 | | - if (!this._dataSource) { |
125 | | - errors.log('W1009'); |
126 | | - return; |
127 | | - } |
128 | | - if (args.name === 'searchMode') { |
129 | | - this._dataSource.searchOperation(this.getOperationBySearchMode(args.value)); |
130 | | - } else { |
131 | | - this._dataSource[args.name](args.value); |
132 | | - } |
133 | | - this._dataSource.load(); |
134 | | - break; |
135 | | - case 'searchTimeout': |
136 | | - break; |
137 | | - default: |
138 | | - this.callBase(args); |
| 52 | + if (!searchEnabled) { |
| 53 | + $container.removeClass(rootElementClassName); |
| 54 | + this.remove(); |
| 55 | + return; |
139 | 56 | } |
140 | | - }, |
141 | 57 |
|
142 | | - focus() { |
143 | | - if (!this.option('focusedElement') && this.option('searchEnabled')) { |
144 | | - this._searchEditor && this._searchEditor.focus(); |
145 | | - return; |
| 58 | + if (this._editor) { |
| 59 | + this.updateEditorOptions(options); |
| 60 | + } else { |
| 61 | + const editorOptions = this._getEditorOptions(options); |
| 62 | + $container.addClass(rootElementClassName); |
| 63 | + const $editor = $('<div>') |
| 64 | + .addClass(searchBoxClassName) |
| 65 | + .prependTo($container); |
| 66 | + |
| 67 | + this._editor = createEditorCallback( |
| 68 | + $editor, |
| 69 | + SearchBoxController.EditorClass, |
| 70 | + editorOptions, |
| 71 | + ); |
146 | 72 | } |
| 73 | + } |
| 74 | + |
| 75 | + updateEditorOptions(options: SearchBoxControllerOptions): void { |
| 76 | + const editorOptions = this._getEditorOptions(options); |
| 77 | + this._editor?.option(editorOptions); |
| 78 | + } |
| 79 | + |
| 80 | + _getEditorOptions(options: SearchBoxControllerOptions): TextBoxProperties { |
| 81 | + const { |
| 82 | + tabIndex, |
| 83 | + searchValue, |
| 84 | + searchEditorOptions, |
| 85 | + searchTimeout, |
| 86 | + } = options; |
| 87 | + const placeholder = messageLocalization.format('Search'); |
| 88 | + |
| 89 | + return { |
| 90 | + mode: 'search', |
| 91 | + placeholder, |
| 92 | + tabIndex, |
| 93 | + value: searchValue, |
| 94 | + valueChangeEvent: 'input', |
| 95 | + inputAttr: { 'aria-label': placeholder }, |
| 96 | + // @ts-expect-error ts-error |
| 97 | + onValueChanged: (e: ValueChangedEvent): void => { |
| 98 | + this._onValueChanged(e, searchTimeout); |
| 99 | + }, |
| 100 | + ...searchEditorOptions, |
| 101 | + }; |
| 102 | + } |
147 | 103 |
|
148 | | - this.callBase(); |
149 | | - }, |
| 104 | + _onValueChanged(e: ValueChangedEvent, searchTimeout = 0): void { |
| 105 | + this._valueChangeDeferred = Deferred(); |
| 106 | + clearTimeout(this._valueChangeTimeout); |
150 | 107 |
|
151 | | - _cleanAria(): void { |
152 | | - const $element = this.$element(); |
| 108 | + this._valueChangeDeferred.done((): void => { |
| 109 | + this._onSearchBoxValueChanged?.(e.value); |
| 110 | + }); |
153 | 111 |
|
154 | | - this.setAria({ |
155 | | - role: null, |
156 | | - activedescendant: null, |
157 | | - }, $element); |
| 112 | + if (e.event?.type === 'input' && searchTimeout) { |
| 113 | + // eslint-disable-next-line no-restricted-globals |
| 114 | + this._valueChangeTimeout = setTimeout((): void => { |
| 115 | + this._valueChangeDeferred?.resolve(); |
| 116 | + }, searchTimeout); |
| 117 | + } else { |
| 118 | + this._valueChangeDeferred?.resolve(); |
| 119 | + } |
| 120 | + } |
158 | 121 |
|
159 | | - $element.attr('tabIndex', null); |
160 | | - }, |
| 122 | + resolveValueChange(): void { |
| 123 | + this._valueChangeDeferred?.resolve(); |
| 124 | + } |
161 | 125 |
|
162 | | - _clean(): void { |
163 | | - this.callBase(); |
164 | | - this._cleanAria(); |
165 | | - }, |
| 126 | + remove(): void { |
| 127 | + this._editor?.$element().remove(); |
| 128 | + this._editor = null; |
| 129 | + } |
166 | 130 |
|
167 | | - _refresh(): void { |
168 | | - if (this._valueChangeDeferred) { |
169 | | - this._valueChangeDeferred.resolve(); |
170 | | - } |
| 131 | + focus(): void { |
| 132 | + this._editor?.focus(); |
| 133 | + } |
171 | 134 |
|
172 | | - this.callBase(); |
173 | | - }, |
| 135 | + dispose(): void { |
| 136 | + this.remove(); |
| 137 | + } |
| 138 | +} |
174 | 139 |
|
175 | | - setEditorClass(value): void { |
176 | | - EditorClass = value; |
177 | | - }, |
178 | | -}; |
| 140 | +export default SearchBoxController; |
0 commit comments