-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
dropdownview.ts
556 lines (512 loc) · 15.1 KB
/
dropdownview.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/**
* @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/dropdown/dropdownview
*/
import View from '../view.js';
import type { default as DropdownButton, DropdownButtonOpenEvent } from './button/dropdownbutton.js';
import type { default as DropdownPanelView, PanelPosition } from './dropdownpanelview.js';
import type { FocusableView } from '../focuscycler.js';
import type ListView from '../list/listview.js';
import type ToolbarView from '../toolbar/toolbarview.js';
import {
KeystrokeHandler,
FocusTracker,
getOptimalPosition,
type Locale,
type ObservableChangeEvent,
type PositioningFunction
} from '@ckeditor/ckeditor5-utils';
import '../../theme/components/dropdown/dropdown.css';
/**
* The dropdown view class. It manages the dropdown button and dropdown panel.
*
* In most cases, the easiest way to create a dropdown is by using the {@link module:ui/dropdown/utils~createDropdown}
* util:
*
* ```ts
* const dropdown = createDropdown( locale );
*
* // Configure dropdown's button properties:
* dropdown.buttonView.set( {
* label: 'A dropdown',
* withText: true
* } );
*
* dropdown.render();
*
* dropdown.panelView.element.textContent = 'Content of the panel';
*
* // Will render a dropdown with a panel containing a "Content of the panel" text.
* document.body.appendChild( dropdown.element );
* ```
*
* If you want to add a richer content to the dropdown panel, you can use the {@link module:ui/dropdown/utils~addListToDropdown}
* and {@link module:ui/dropdown/utils~addToolbarToDropdown} helpers. See more examples in
* {@link module:ui/dropdown/utils~createDropdown} documentation.
*
* If you want to create a completely custom dropdown, then you can compose it manually:
*
* ```ts
* const button = new DropdownButtonView( locale );
* const panel = new DropdownPanelView( locale );
* const dropdown = new DropdownView( locale, button, panel );
*
* button.set( {
* label: 'A dropdown',
* withText: true
* } );
*
* dropdown.render();
*
* panel.element.textContent = 'Content of the panel';
*
* // Will render a dropdown with a panel containing a "Content of the panel" text.
* document.body.appendChild( dropdown.element );
* ```
*
* However, dropdown created this way will contain little behavior. You will need to implement handlers for actions
* such as {@link module:ui/bindings/clickoutsidehandler~clickOutsideHandler clicking outside an open dropdown}
* (which should close it) and support for arrow keys inside the panel. Therefore, unless you really know what
* you do and you really need to do it, it is recommended to use the {@link module:ui/dropdown/utils~createDropdown} helper.
*/
export default class DropdownView extends View<HTMLDivElement> {
/**
* Button of the dropdown view. Clicking the button opens the {@link #panelView}.
*/
public readonly buttonView: DropdownButton & FocusableView;
/**
* Panel of the dropdown. It opens when the {@link #buttonView} is
* {@link module:ui/button/button~Button#event:execute executed} (i.e. clicked).
*
* Child views can be added to the panel's `children` collection:
*
* ```ts
* dropdown.panelView.children.add( childView );
* ```
*
* See {@link module:ui/dropdown/dropdownpanelview~DropdownPanelView#children} and
* {@link module:ui/viewcollection~ViewCollection#add}.
*/
public readonly panelView: DropdownPanelView;
/**
* Tracks information about the DOM focus in the dropdown.
*/
public readonly focusTracker: FocusTracker;
/**
* Instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}. It manages
* keystrokes of the dropdown:
*
* * <kbd>▼</kbd> opens the dropdown,
* * <kbd>◀</kbd> and <kbd>Esc</kbd> closes the dropdown.
*/
public readonly keystrokes: KeystrokeHandler;
/**
* A child {@link module:ui/list/listview~ListView list view} of the dropdown located
* in its {@link module:ui/dropdown/dropdownview~DropdownView#panelView panel}.
*
* **Note**: Only supported when dropdown has list view added using {@link module:ui/dropdown/utils~addListToDropdown}.
*/
public listView?: ListView;
/**
* A child toolbar of the dropdown located in the
* {@link module:ui/dropdown/dropdownview~DropdownView#panelView panel}.
*
* **Note**: Only supported when dropdown has list view added using {@link module:ui/dropdown/utils~addToolbarToDropdown}.
*/
public toolbarView?: ToolbarView;
/**
* Controls whether the dropdown view is open, i.e. shows or hides the {@link #panelView panel}.
*
* **Note**: When the dropdown gets open, it will attempt to call `focus()` on the first child of its {@link #panelView}.
* See {@link module:ui/dropdown/utils~addToolbarToDropdown}, {@link module:ui/dropdown/utils~addListToDropdown}, and
* {@link module:ui/dropdown/utils~focusChildOnDropdownOpen} to learn more about focus management in dropdowns.
*
* @observable
*/
declare public isOpen: boolean;
/**
* Controls whether the dropdown is enabled, i.e. it can be clicked and execute an action.
*
* See {@link module:ui/button/buttonview~ButtonView#isEnabled}.
*
* @observable
*/
declare public isEnabled: boolean;
/**
* (Optional) The additional CSS class set on the dropdown {@link #element}.
*
* @observable
*/
declare public class: string | undefined;
/**
* (Optional) The `id` attribute of the dropdown (i.e. to pair with a `<label>` element).
*
* @observable
*/
declare public id: string | undefined;
/**
* The position of the panel, relative to the dropdown.
*
* **Note**: When `'auto'`, the panel will use one of the remaining positions to stay
* in the viewport, visible to the user. The positions correspond directly to
* {@link module:ui/dropdown/dropdownview~DropdownView.defaultPanelPositions default panel positions}.
*
* **Note**: This value has an impact on the
* {@link module:ui/dropdown/dropdownpanelview~DropdownPanelView#position} property
* each time the panel becomes {@link #isOpen open}.
*
* @observable
* @default 'auto'
*/
declare public panelPosition: PanelPosition | 'auto';
/**
* @observable
*/
declare public ariaDescribedById: string | undefined;
/**
* Creates an instance of the dropdown.
*
* Also see {@link #render}.
*
* @param locale The localization services instance.
*/
constructor(
locale: Locale | undefined,
buttonView: DropdownButton & FocusableView,
panelView: DropdownPanelView
) {
super( locale );
const bind = this.bindTemplate;
this.buttonView = buttonView;
this.panelView = panelView;
this.set( 'isOpen', false );
this.set( 'isEnabled', true );
this.set( 'class', undefined );
this.set( 'id', undefined );
this.set( 'panelPosition', 'auto' );
// Toggle the visibility of the panel when the dropdown becomes open.
this.panelView.bind( 'isVisible' ).to( this, 'isOpen' );
this.keystrokes = new KeystrokeHandler();
this.focusTracker = new FocusTracker();
this.setTemplate( {
tag: 'div',
attributes: {
class: [
'ck',
'ck-dropdown',
bind.to( 'class' ),
bind.if( 'isEnabled', 'ck-disabled', value => !value )
],
id: bind.to( 'id' ),
'aria-describedby': bind.to( 'ariaDescribedById' )
},
children: [
buttonView,
panelView
]
} );
buttonView.extendTemplate( {
attributes: {
class: [
'ck-dropdown__button'
],
'data-cke-tooltip-disabled': bind.to( 'isOpen' )
}
} );
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
this.focusTracker.add( this.buttonView.element! );
this.focusTracker.add( this.panelView.element! );
// Toggle the dropdown when its button has been clicked.
this.listenTo<DropdownButtonOpenEvent>( this.buttonView, 'open', () => {
this.isOpen = !this.isOpen;
} );
// Let the dropdown control the position of the panel. The position must
// be updated every time the dropdown is open.
this.on<ObservableChangeEvent<boolean>>( 'change:isOpen', ( evt, name, isOpen ) => {
if ( !isOpen ) {
return;
}
// If "auto", find the best position of the panel to fit into the viewport.
// Otherwise, simply assign the static position.
if ( this.panelPosition === 'auto' ) {
const optimalPanelPosition = DropdownView._getOptimalPosition( {
element: this.panelView.element!,
target: this.buttonView.element!,
fitInViewport: true,
positions: this._panelPositions
} );
this.panelView.position = (
optimalPanelPosition ? optimalPanelPosition.name : this._panelPositions[ 0 ].name
) as PanelPosition;
} else {
this.panelView.position = this.panelPosition;
}
} );
// Listen for keystrokes coming from within #element.
this.keystrokes.listenTo( this.element! );
const closeDropdown = ( data: unknown, cancel: () => void ) => {
if ( this.isOpen ) {
this.isOpen = false;
cancel();
}
};
// Open the dropdown panel using the arrow down key, just like with return or space.
this.keystrokes.set( 'arrowdown', ( data, cancel ) => {
// Don't open if the dropdown is disabled or already open.
if ( this.buttonView.isEnabled && !this.isOpen ) {
this.isOpen = true;
cancel();
}
} );
// Block the right arrow key (until nested dropdowns are implemented).
this.keystrokes.set( 'arrowright', ( data, cancel ) => {
if ( this.isOpen ) {
cancel();
}
} );
// Close the dropdown using the arrow left/escape key.
this.keystrokes.set( 'arrowleft', closeDropdown );
this.keystrokes.set( 'esc', closeDropdown );
}
/**
* Focuses the {@link #buttonView}.
*/
public focus(): void {
this.buttonView.focus();
}
/**
* Returns {@link #panelView panel} positions to be used by the
* {@link module:utils/dom/position~getOptimalPosition `getOptimalPosition()`}
* utility considering the direction of the language the UI of the editor is displayed in.
*/
private get _panelPositions(): Array<PositioningFunction> {
const {
south, north,
southEast, southWest,
northEast, northWest,
southMiddleEast, southMiddleWest,
northMiddleEast, northMiddleWest
} = DropdownView.defaultPanelPositions;
if ( this.locale!.uiLanguageDirection !== 'rtl' ) {
return [
southEast, southWest, southMiddleEast, southMiddleWest, south,
northEast, northWest, northMiddleEast, northMiddleWest, north
];
} else {
return [
southWest, southEast, southMiddleWest, southMiddleEast, south,
northWest, northEast, northMiddleWest, northMiddleEast, north
];
}
}
/**
* A set of positioning functions used by the dropdown view to determine
* the optimal position (i.e. fitting into the browser viewport) of its
* {@link module:ui/dropdown/dropdownview~DropdownView#panelView panel} when
* {@link module:ui/dropdown/dropdownview~DropdownView#panelPosition} is set to 'auto'`.
*
* The available positioning functions are as follow:
*
* **South**
*
* * `south`
*
* ```
* [ Button ]
* +-----------------+
* | Panel |
* +-----------------+
* ```
*
* * `southEast`
*
* ```
* [ Button ]
* +-----------------+
* | Panel |
* +-----------------+
* ```
*
* * `southWest`
*
* ```
* [ Button ]
* +-----------------+
* | Panel |
* +-----------------+
* ```
*
* * `southMiddleEast`
*
* ```
* [ Button ]
* +-----------------+
* | Panel |
* +-----------------+
* ```
*
* * `southMiddleWest`
*
* ```
* [ Button ]
* +-----------------+
* | Panel |
* +-----------------+
* ```
*
* **North**
*
* * `north`
*
* ```
* +-----------------+
* | Panel |
* +-----------------+
* [ Button ]
* ```
*
* * `northEast`
*
* ```
* +-----------------+
* | Panel |
* +-----------------+
* [ Button ]
* ```
*
* * `northWest`
*
* ```
* +-----------------+
* | Panel |
* +-----------------+
* [ Button ]
* ```
*
* * `northMiddleEast`
*
* ```
* +-----------------+
* | Panel |
* +-----------------+
* [ Button ]
* ```
*
* * `northMiddleWest`
*
* ```
* +-----------------+
* | Panel |
* +-----------------+
* [ Button ]
* ```
*
* Positioning functions are compatible with {@link module:utils/dom/position~DomPoint}.
*
* The name that position function returns will be reflected in dropdown panel's class that
* controls its placement. See {@link module:ui/dropdown/dropdownview~DropdownView#panelPosition}
* to learn more.
*/
public static defaultPanelPositions: Record<string, PositioningFunction> = {
south: ( buttonRect, panelRect ) => {
return {
top: buttonRect.bottom,
left: buttonRect.left - ( panelRect.width - buttonRect.width ) / 2,
name: 's'
};
},
southEast: buttonRect => {
return {
top: buttonRect.bottom,
left: buttonRect.left,
name: 'se'
};
},
southWest: ( buttonRect, panelRect ) => {
return {
top: buttonRect.bottom,
left: buttonRect.left - panelRect.width + buttonRect.width,
name: 'sw'
};
},
southMiddleEast: ( buttonRect, panelRect ) => {
return {
top: buttonRect.bottom,
left: buttonRect.left - ( panelRect.width - buttonRect.width ) / 4,
name: 'sme'
};
},
southMiddleWest: ( buttonRect, panelRect ) => {
return {
top: buttonRect.bottom,
left: buttonRect.left - ( panelRect.width - buttonRect.width ) * 3 / 4,
name: 'smw'
};
},
north: ( buttonRect, panelRect ) => {
return {
top: buttonRect.top - panelRect.height,
left: buttonRect.left - ( panelRect.width - buttonRect.width ) / 2,
name: 'n'
};
},
northEast: ( buttonRect, panelRect ) => {
return {
top: buttonRect.top - panelRect.height,
left: buttonRect.left,
name: 'ne'
};
},
northWest: ( buttonRect, panelRect ) => {
return {
top: buttonRect.top - panelRect.height,
left: buttonRect.left - panelRect.width + buttonRect.width,
name: 'nw'
};
},
northMiddleEast: ( buttonRect, panelRect ) => {
return {
top: buttonRect.top - panelRect.height,
left: buttonRect.left - ( panelRect.width - buttonRect.width ) / 4,
name: 'nme'
};
},
northMiddleWest: ( buttonRect, panelRect ) => {
return {
top: buttonRect.top - panelRect.height,
left: buttonRect.left - ( panelRect.width - buttonRect.width ) * 3 / 4,
name: 'nmw'
};
}
};
/**
* A function used to calculate the optimal position for the dropdown panel.
*/
private static _getOptimalPosition = getOptimalPosition;
}
/**
* Fired when the toolbar button or list item is executed.
*
* For {@link ~DropdownView#listView} It fires when a child of some {@link module:ui/list/listitemview~ListItemView}
* fired `execute`.
*
* For {@link ~DropdownView#toolbarView} It fires when one of the buttons has been
* {@link module:ui/button/button~Button#event:execute executed}.
*
* **Note**: Only supported when dropdown has list view added using {@link module:ui/dropdown/utils~addListToDropdown}
* or {@link module:ui/dropdown/utils~addToolbarToDropdown}.
*
* @eventName ~DropdownView#execute
*/
export type DropdownViewEvent = {
name: 'execute';
args: [];
};