/
menubarmenuview.ts
257 lines (220 loc) · 6.94 KB
/
menubarmenuview.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
/**
* @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/menubar/menubarmenuview
*/
import {
FocusTracker,
KeystrokeHandler,
getOptimalPosition,
type Locale,
type PositioningFunction,
type ObservableChangeEvent
} from '@ckeditor/ckeditor5-utils';
import MenuBarMenuButtonView from './menubarmenubuttonview.js';
import { MenuBarMenuBehaviors, MenuBarMenuViewPanelPositioningFunctions } from './utils.js';
import type { FocusableView } from '../focuscycler.js';
import View from '../view.js';
import {
default as MenuBarMenuPanelView,
type MenuBarMenuPanelPosition
} from './menubarmenupanelview.js';
import '../../theme/components/menubar/menubarmenu.css';
/**
* A menu view for the {@link module:ui/menubar/menubarview~MenuBarView}. Menus are building blocks of the menu bar,
* they host other sub-menus and menu items (buttons) that users can interact with.
*/
export default class MenuBarMenuView extends View implements FocusableView {
/**
* Button of the menu view.
*/
public readonly buttonView: MenuBarMenuButtonView;
/**
* Panel of the menu. It hosts children of the menu.
*/
public readonly panelView: MenuBarMenuPanelView;
/**
* Tracks information about the DOM focus in the menu.
*/
public readonly focusTracker: FocusTracker;
/**
* Instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}. It manages
* keystrokes of the menu.
*/
public readonly keystrokes: KeystrokeHandler;
/**
* Controls whether the menu is open, i.e. shows or hides the {@link #panelView panel}.
*
* @observable
*/
declare public isOpen: boolean;
/**
* Controls whether the menu is enabled, i.e. its {@link #buttonView} can be clicked.
*
* @observable
*/
declare public isEnabled: boolean;
/**
* (Optional) The additional CSS class set on the menu {@link #element}.
*
* @observable
*/
declare public class: string | undefined;
/**
* The name of the position of the {@link #panelView}, relative to the menu.
*
* **Note**: The value is updated each time the panel gets {@link #isOpen open}.
*
* @observable
* @default 'w'
*/
declare public panelPosition: MenuBarMenuPanelPosition;
/**
* The parent menu view of the menu. It is `null` for top-level menus.
*
* See {@link module:ui/menubar/menubarview~MenuBarView#registerMenu}.
*/
declare public parentMenuView: MenuBarMenuView | null;
/**
* Creates an instance of the menu view.
*
* @param locale The localization services instance.
*/
constructor( locale: Locale ) {
super( locale );
const bind = this.bindTemplate;
this.buttonView = new MenuBarMenuButtonView( locale );
this.buttonView.delegate( 'mouseenter' ).to( this );
this.buttonView.bind( 'isOn', 'isEnabled' ).to( this, 'isOpen', 'isEnabled' );
this.panelView = new MenuBarMenuPanelView( locale );
this.panelView.bind( 'isVisible' ).to( this, 'isOpen' );
this.keystrokes = new KeystrokeHandler();
this.focusTracker = new FocusTracker();
this.set( 'isOpen', false );
this.set( 'isEnabled', true );
this.set( 'panelPosition', 'w' );
this.set( 'class', undefined );
this.set( 'parentMenuView', null );
this.setTemplate( {
tag: 'div',
attributes: {
class: [
'ck',
'ck-menu-bar__menu',
bind.to( 'class' ),
bind.if( 'isEnabled', 'ck-disabled', value => !value ),
bind.if( 'parentMenuView', 'ck-menu-bar__menu_top-level', value => !value )
]
},
children: [
this.buttonView,
this.panelView
]
} );
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
this.focusTracker.add( this.buttonView.element! );
this.focusTracker.add( this.panelView.element! );
// Listen for keystrokes coming from within #element.
this.keystrokes.listenTo( this.element! );
MenuBarMenuBehaviors.closeOnEscKey( this );
this._repositionPanelOnOpen();
}
// For now, this method cannot be called in the render process because the `parentMenuView` may be assigned
// after the rendering process.
//
// TODO: We should reconsider the way we handle this logic.
/**
* Attach all keyboard behaviors for the menu bar view.
*
* @internal
*/
public _attachBehaviors(): void {
// Top-level menus.
if ( !this.parentMenuView ) {
this._propagateArrowKeystrokeEvents();
MenuBarMenuBehaviors.openAndFocusPanelOnArrowDownKey( this );
MenuBarMenuBehaviors.toggleOnButtonClick( this );
} else {
MenuBarMenuBehaviors.openOnButtonClick( this );
MenuBarMenuBehaviors.openOnArrowRightKey( this );
MenuBarMenuBehaviors.closeOnArrowLeftKey( this );
MenuBarMenuBehaviors.closeOnParentClose( this );
}
}
/**
* Fires `arrowright` and `arrowleft` events when the user pressed corresponding arrow keys.
*/
private _propagateArrowKeystrokeEvents(): void {
this.keystrokes.set( 'arrowright', ( data, cancel ) => {
this.fire( 'arrowright' );
cancel();
} );
this.keystrokes.set( 'arrowleft', ( data, cancel ) => {
this.fire( 'arrowleft' );
cancel();
} );
}
/**
* Sets the position of the panel when the menu opens. The panel is positioned
* so that it optimally uses the available space in the viewport.
*/
private _repositionPanelOnOpen(): void {
// Let the menu control the position of the panel. The position must be updated every time the menu is open.
this.on<ObservableChangeEvent<boolean>>( 'change:isOpen', ( evt, name, isOpen ) => {
if ( !isOpen ) {
return;
}
const optimalPanelPosition = MenuBarMenuView._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 MenuBarMenuPanelPosition;
} );
}
/**
* @inheritDoc
*/
public focus(): void {
this.buttonView.focus();
}
/**
* Positioning functions for the {@link #panelView} . They change depending on the role of the menu (top-level vs sub-menu) in
* the {@link module:ui/menubar/menubarview~MenuBarView menu bar} and the UI language direction.
*/
public get _panelPositions(): Array<PositioningFunction> {
const {
southEast, southWest, northEast, northWest,
westSouth, eastSouth, westNorth, eastNorth
} = MenuBarMenuViewPanelPositioningFunctions;
if ( this.locale!.uiLanguageDirection === 'ltr' ) {
if ( this.parentMenuView ) {
return [ eastSouth, eastNorth, westSouth, westNorth ];
} else {
return [ southEast, southWest, northEast, northWest ];
}
} else {
if ( this.parentMenuView ) {
return [ westSouth, westNorth, eastSouth, eastNorth ];
} else {
return [ southWest, southEast, northWest, northEast ];
}
}
}
/**
* A function used to calculate the optimal position for the dropdown panel.
*
* Referenced for unit testing purposes.
*/
private static _getOptimalPosition = getOptimalPosition;
}