-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
searchtextqueryview.ts
151 lines (127 loc) · 3.88 KB
/
searchtextqueryview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module ui/search/text/searchtextqueryview
*/
import ButtonView from '../../button/buttonview.js';
import IconView from '../../icon/iconview.js';
import LabeledFieldView, { type LabeledFieldViewCreator } from '../../labeledfield/labeledfieldview.js';
import { createLabeledInputText } from '../../labeledfield/utils.js';
import type InputBase from '../../input/inputbase.js';
import type { Locale } from '@ckeditor/ckeditor5-utils';
import { icons } from '@ckeditor/ckeditor5-core';
/**
* A search input field for the {@link module:ui/search/text/searchtextview~SearchTextView} component.
*
* @internal
* @extends module:ui/labeledfield/labeledfieldview~LabeledFieldView
*/
export default class SearchTextQueryView<
TQueryFieldView extends InputBase<HTMLInputElement | HTMLTextAreaElement>
> extends LabeledFieldView<TQueryFieldView> {
/**
* The loupe icon displayed next to the {@link #fieldView}.
*/
public iconView?: IconView;
/**
* The button that clears and focuses the {@link #fieldView}.
*/
public resetButtonView?: ButtonView;
/**
* A reference to the view configuration.
*/
private readonly _viewConfig: SearchTextQueryViewConfig<TQueryFieldView>;
/**
* @inheritDoc
*/
constructor( locale: Locale, config: SearchTextQueryViewConfig<TQueryFieldView> ) {
const t = locale.t;
const viewConfig = Object.assign( {}, {
showResetButton: true,
showIcon: true,
creator: createLabeledInputText
}, config );
super( locale, viewConfig.creator as any );
this.label = config.label;
this._viewConfig = viewConfig;
if ( this._viewConfig.showIcon ) {
this.iconView = new IconView();
this.iconView.content = icons.loupe;
this.fieldWrapperChildren.add( this.iconView, 0 );
this.extendTemplate( {
attributes: {
class: 'ck-search__query_with-icon'
}
} );
}
if ( this._viewConfig.showResetButton ) {
this.resetButtonView = new ButtonView( locale );
this.resetButtonView.set( {
label: t( 'Clear' ),
icon: icons.cancel,
class: 'ck-search__reset',
isVisible: false,
tooltip: true
} );
this.resetButtonView.on( 'execute', () => {
this.reset();
this.focus();
this.fire<SearchTextQueryViewResetEvent>( 'reset' );
} );
this.resetButtonView.bind( 'isVisible' ).to( this.fieldView, 'isEmpty', isEmpty => !isEmpty );
this.fieldWrapperChildren.add( this.resetButtonView );
this.extendTemplate( {
attributes: {
class: 'ck-search__query_with-reset'
}
} );
}
}
/**
* Resets the search field to its default state.
*/
public reset(): void {
this.fieldView.reset();
if ( this._viewConfig.showResetButton ) {
this.resetButtonView!.isVisible = false;
}
}
}
/**
* An event fired when the field is reset using the
* {@link module:ui/search/text/searchtextqueryview~SearchTextQueryView#resetButtonView}.
*
* @eventName ~SearchTextQueryView#reset
*/
export type SearchTextQueryViewResetEvent = {
name: 'reset';
args: [];
};
/**
* The configuration of the {@link module:ui/search/text/searchtextqueryview~SearchTextQueryView} view.
*/
export interface SearchTextQueryViewConfig<TConfigSearchField extends InputBase<HTMLInputElement | HTMLTextAreaElement>> {
/**
* The human-readable label of the search field.
*/
label: string;
/**
* Determines whether the button that resets the search should be visible.
*
* @default true
*/
showResetButton?: boolean;
/**
* Determines whether the loupe icon should be visible.
*
* @default true
*/
showIcon?: boolean;
/**
* The function that creates the search field input view. By default, a plain
* {@link module:ui/inputtext/inputtextview~InputTextView} is used for this purpose.
*/
creator?: LabeledFieldViewCreator<TConfigSearchField>;
}