@ckeditor/ckeditor5-ui 40.0.0 → 40.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/LICENSE.md +3 -3
  2. package/lang/translations/pt-br.po +1 -1
  3. package/lang/translations/ug.po +26 -26
  4. package/package.json +3 -3
  5. package/src/arialiveannouncer.d.ts +94 -0
  6. package/src/arialiveannouncer.js +113 -0
  7. package/src/augmentation.d.ts +86 -86
  8. package/src/augmentation.js +5 -5
  9. package/src/autocomplete/autocompleteview.d.ts +81 -81
  10. package/src/autocomplete/autocompleteview.js +153 -146
  11. package/src/bindings/addkeyboardhandlingforgrid.d.ts +27 -27
  12. package/src/bindings/addkeyboardhandlingforgrid.js +107 -107
  13. package/src/bindings/clickoutsidehandler.d.ts +28 -28
  14. package/src/bindings/clickoutsidehandler.js +36 -36
  15. package/src/bindings/csstransitiondisablermixin.d.ts +40 -40
  16. package/src/bindings/csstransitiondisablermixin.js +55 -55
  17. package/src/bindings/injectcsstransitiondisabler.d.ts +59 -59
  18. package/src/bindings/injectcsstransitiondisabler.js +71 -71
  19. package/src/bindings/preventdefault.d.ts +33 -33
  20. package/src/bindings/preventdefault.js +34 -34
  21. package/src/bindings/submithandler.d.ts +57 -57
  22. package/src/bindings/submithandler.js +47 -47
  23. package/src/button/button.d.ts +172 -178
  24. package/src/button/button.js +5 -5
  25. package/src/button/buttonlabel.d.ts +34 -34
  26. package/src/button/buttonlabel.js +5 -5
  27. package/src/button/buttonlabelview.d.ts +31 -31
  28. package/src/button/buttonlabelview.js +42 -42
  29. package/src/button/buttonview.d.ts +181 -185
  30. package/src/button/buttonview.js +217 -219
  31. package/src/button/switchbuttonview.d.ts +45 -45
  32. package/src/button/switchbuttonview.js +75 -75
  33. package/src/colorgrid/colorgridview.d.ts +132 -132
  34. package/src/colorgrid/colorgridview.js +124 -124
  35. package/src/colorgrid/colortileview.d.ts +28 -28
  36. package/src/colorgrid/colortileview.js +40 -40
  37. package/src/colorgrid/utils.d.ts +47 -47
  38. package/src/colorgrid/utils.js +84 -84
  39. package/src/colorpicker/colorpickerview.d.ts +137 -137
  40. package/src/colorpicker/colorpickerview.js +270 -270
  41. package/src/colorpicker/utils.d.ts +43 -43
  42. package/src/colorpicker/utils.js +99 -99
  43. package/src/colorselector/colorgridsfragmentview.d.ts +194 -194
  44. package/src/colorselector/colorgridsfragmentview.js +289 -289
  45. package/src/colorselector/colorpickerfragmentview.d.ts +128 -128
  46. package/src/colorselector/colorpickerfragmentview.js +205 -205
  47. package/src/colorselector/colorselectorview.d.ts +242 -242
  48. package/src/colorselector/colorselectorview.js +256 -256
  49. package/src/colorselector/documentcolorcollection.d.ts +70 -70
  50. package/src/colorselector/documentcolorcollection.js +42 -42
  51. package/src/componentfactory.d.ts +81 -81
  52. package/src/componentfactory.js +104 -104
  53. package/src/dropdown/button/dropdownbutton.d.ts +25 -25
  54. package/src/dropdown/button/dropdownbutton.js +5 -5
  55. package/src/dropdown/button/dropdownbuttonview.d.ts +48 -48
  56. package/src/dropdown/button/dropdownbuttonview.js +66 -66
  57. package/src/dropdown/button/splitbuttonview.d.ts +161 -161
  58. package/src/dropdown/button/splitbuttonview.js +152 -152
  59. package/src/dropdown/dropdownpanelfocusable.d.ts +21 -21
  60. package/src/dropdown/dropdownpanelfocusable.js +5 -5
  61. package/src/dropdown/dropdownpanelview.d.ts +62 -62
  62. package/src/dropdown/dropdownpanelview.js +97 -97
  63. package/src/dropdown/dropdownview.d.ts +315 -315
  64. package/src/dropdown/dropdownview.js +379 -379
  65. package/src/dropdown/utils.d.ts +235 -235
  66. package/src/dropdown/utils.js +463 -458
  67. package/src/editableui/editableuiview.d.ts +72 -72
  68. package/src/editableui/editableuiview.js +112 -112
  69. package/src/editableui/inline/inlineeditableuiview.d.ts +40 -40
  70. package/src/editableui/inline/inlineeditableuiview.js +48 -48
  71. package/src/editorui/bodycollection.d.ts +55 -55
  72. package/src/editorui/bodycollection.js +84 -84
  73. package/src/editorui/boxed/boxededitoruiview.d.ts +40 -40
  74. package/src/editorui/boxed/boxededitoruiview.js +81 -81
  75. package/src/editorui/editorui.d.ts +288 -282
  76. package/src/editorui/editorui.js +412 -410
  77. package/src/editorui/editoruiview.d.ts +39 -39
  78. package/src/editorui/editoruiview.js +38 -38
  79. package/src/editorui/poweredby.d.ts +71 -71
  80. package/src/editorui/poweredby.js +276 -276
  81. package/src/focuscycler.d.ts +226 -226
  82. package/src/focuscycler.js +245 -245
  83. package/src/formheader/formheaderview.d.ts +59 -59
  84. package/src/formheader/formheaderview.js +69 -69
  85. package/src/highlightedtext/highlightedtextview.d.ts +38 -38
  86. package/src/highlightedtext/highlightedtextview.js +102 -102
  87. package/src/icon/iconview.d.ts +85 -85
  88. package/src/icon/iconview.js +114 -114
  89. package/src/iframe/iframeview.d.ts +50 -50
  90. package/src/iframe/iframeview.js +63 -63
  91. package/src/index.d.ts +73 -73
  92. package/src/index.js +70 -70
  93. package/src/input/inputbase.d.ts +107 -107
  94. package/src/input/inputbase.js +110 -110
  95. package/src/input/inputview.d.ts +36 -36
  96. package/src/input/inputview.js +24 -24
  97. package/src/inputnumber/inputnumberview.d.ts +49 -49
  98. package/src/inputnumber/inputnumberview.js +40 -40
  99. package/src/inputtext/inputtextview.d.ts +18 -18
  100. package/src/inputtext/inputtextview.js +27 -27
  101. package/src/label/labelview.d.ts +36 -36
  102. package/src/label/labelview.js +41 -41
  103. package/src/labeledfield/labeledfieldview.d.ts +187 -187
  104. package/src/labeledfield/labeledfieldview.js +157 -157
  105. package/src/labeledfield/utils.d.ts +123 -123
  106. package/src/labeledfield/utils.js +176 -176
  107. package/src/labeledinput/labeledinputview.d.ts +125 -125
  108. package/src/labeledinput/labeledinputview.js +125 -125
  109. package/src/list/listitemgroupview.d.ts +59 -51
  110. package/src/list/listitemgroupview.js +63 -75
  111. package/src/list/listitemview.d.ts +36 -36
  112. package/src/list/listitemview.js +42 -42
  113. package/src/list/listseparatorview.d.ts +18 -18
  114. package/src/list/listseparatorview.js +28 -28
  115. package/src/list/listview.d.ts +122 -122
  116. package/src/list/listview.js +187 -187
  117. package/src/model.d.ts +22 -22
  118. package/src/model.js +31 -31
  119. package/src/notification/notification.d.ts +211 -211
  120. package/src/notification/notification.js +187 -187
  121. package/src/panel/balloon/balloonpanelview.d.ts +685 -685
  122. package/src/panel/balloon/balloonpanelview.js +1010 -1010
  123. package/src/panel/balloon/contextualballoon.d.ts +299 -299
  124. package/src/panel/balloon/contextualballoon.js +572 -572
  125. package/src/panel/sticky/stickypanelview.d.ts +156 -156
  126. package/src/panel/sticky/stickypanelview.js +234 -234
  127. package/src/search/filteredview.d.ts +31 -31
  128. package/src/search/filteredview.js +5 -5
  129. package/src/search/searchinfoview.d.ts +45 -45
  130. package/src/search/searchinfoview.js +59 -59
  131. package/src/search/searchresultsview.d.ts +54 -54
  132. package/src/search/searchresultsview.js +65 -65
  133. package/src/search/text/searchtextqueryview.d.ts +76 -76
  134. package/src/search/text/searchtextqueryview.js +75 -75
  135. package/src/search/text/searchtextview.d.ts +219 -219
  136. package/src/search/text/searchtextview.js +201 -201
  137. package/src/spinner/spinnerview.d.ts +25 -25
  138. package/src/spinner/spinnerview.js +38 -38
  139. package/src/template.d.ts +942 -942
  140. package/src/template.js +1294 -1294
  141. package/src/textarea/textareaview.d.ts +88 -88
  142. package/src/textarea/textareaview.js +142 -140
  143. package/src/toolbar/balloon/balloontoolbar.d.ts +122 -122
  144. package/src/toolbar/balloon/balloontoolbar.js +300 -300
  145. package/src/toolbar/block/blockbuttonview.d.ts +35 -35
  146. package/src/toolbar/block/blockbuttonview.js +41 -41
  147. package/src/toolbar/block/blocktoolbar.d.ts +161 -161
  148. package/src/toolbar/block/blocktoolbar.js +395 -395
  149. package/src/toolbar/normalizetoolbarconfig.d.ts +40 -40
  150. package/src/toolbar/normalizetoolbarconfig.js +52 -51
  151. package/src/toolbar/toolbarlinebreakview.d.ts +18 -18
  152. package/src/toolbar/toolbarlinebreakview.js +28 -28
  153. package/src/toolbar/toolbarseparatorview.d.ts +18 -18
  154. package/src/toolbar/toolbarseparatorview.js +28 -28
  155. package/src/toolbar/toolbarview.d.ts +266 -266
  156. package/src/toolbar/toolbarview.js +719 -719
  157. package/src/tooltipmanager.d.ts +180 -180
  158. package/src/tooltipmanager.js +353 -353
  159. package/src/view.d.ts +422 -422
  160. package/src/view.js +396 -396
  161. package/src/viewcollection.d.ts +139 -139
  162. package/src/viewcollection.js +206 -206
  163. package/theme/components/arialiveannouncer/arialiveannouncer.css +10 -0
  164. package/theme/components/button/button.css +9 -1
  165. package/theme/components/formheader/formheader.css +0 -4
@@ -1,458 +1,463 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module ui/dropdown/utils
7
- */
8
- import DropdownPanelView from './dropdownpanelview';
9
- import DropdownView from './dropdownview';
10
- import DropdownButtonView from './button/dropdownbuttonview';
11
- import ToolbarView from '../toolbar/toolbarview';
12
- import ListView from '../list/listview';
13
- import ListItemView from '../list/listitemview';
14
- import ListSeparatorView from '../list/listseparatorview';
15
- import ButtonView from '../button/buttonview';
16
- import SplitButtonView from './button/splitbuttonview';
17
- import SwitchButtonView from '../button/switchbuttonview';
18
- import ViewCollection from '../viewcollection';
19
- import clickOutsideHandler from '../bindings/clickoutsidehandler';
20
- import { global, priorities, logWarning } from '@ckeditor/ckeditor5-utils';
21
- import '../../theme/components/dropdown/toolbardropdown.css';
22
- import '../../theme/components/dropdown/listdropdown.css';
23
- import ListItemGroupView from '../list/listitemgroupview';
24
- /**
25
- * A helper for creating dropdowns. It creates an instance of a {@link module:ui/dropdown/dropdownview~DropdownView dropdown},
26
- * with a {@link module:ui/dropdown/button/dropdownbutton~DropdownButton button},
27
- * {@link module:ui/dropdown/dropdownpanelview~DropdownPanelView panel} and all standard dropdown's behaviors.
28
- *
29
- * # Creating dropdowns
30
- *
31
- * By default, the default {@link module:ui/dropdown/button/dropdownbuttonview~DropdownButtonView} class is used as
32
- * definition of the button:
33
- *
34
- * ```ts
35
- * const dropdown = createDropdown( model );
36
- *
37
- * // Configure dropdown's button properties:
38
- * dropdown.buttonView.set( {
39
- * label: 'A dropdown',
40
- * withText: true
41
- * } );
42
- *
43
- * dropdown.render();
44
- *
45
- * // Will render a dropdown labeled "A dropdown" with an empty panel.
46
- * document.body.appendChild( dropdown.element );
47
- * ```
48
- *
49
- * You can also provide other button views (they need to implement the
50
- * {@link module:ui/dropdown/button/dropdownbutton~DropdownButton} interface). For instance, you can use
51
- * {@link module:ui/dropdown/button/splitbuttonview~SplitButtonView} to create a dropdown with a split button.
52
- *
53
- * ```ts
54
- * const dropdown = createDropdown( locale, SplitButtonView );
55
- *
56
- * // Configure dropdown's button properties:
57
- * dropdown.buttonView.set( {
58
- * label: 'A dropdown',
59
- * withText: true
60
- * } );
61
- *
62
- * dropdown.buttonView.on( 'execute', () => {
63
- * // Add the behavior of the "action part" of the split button.
64
- * // Split button consists of the "action part" and "arrow part".
65
- * // The arrow opens the dropdown while the action part can have some other behavior.
66
- * } );
67
- *
68
- * dropdown.render();
69
- *
70
- * // Will render a dropdown labeled "A dropdown" with an empty panel.
71
- * document.body.appendChild( dropdown.element );
72
- * ```
73
- *
74
- * # Adding content to the dropdown's panel
75
- *
76
- * The content of the panel can be inserted directly into the `dropdown.panelView.element`:
77
- *
78
- * ```ts
79
- * dropdown.panelView.element.textContent = 'Content of the panel';
80
- * ```
81
- *
82
- * However, most of the time you will want to add there either a {@link module:ui/list/listview~ListView list of options}
83
- * or a list of buttons (i.e. a {@link module:ui/toolbar/toolbarview~ToolbarView toolbar}).
84
- * To simplify the task, you can use, respectively, {@link module:ui/dropdown/utils~addListToDropdown} or
85
- * {@link module:ui/dropdown/utils~addToolbarToDropdown} utils.
86
- *
87
- * @param locale The locale instance.
88
- * @param ButtonClass The dropdown button view class. Needs to implement the
89
- * {@link module:ui/dropdown/button/dropdownbutton~DropdownButton} interface.
90
- * @returns The dropdown view instance.
91
- */
92
- export function createDropdown(locale, ButtonClass = DropdownButtonView) {
93
- const buttonView = new ButtonClass(locale);
94
- const panelView = new DropdownPanelView(locale);
95
- const dropdownView = new DropdownView(locale, buttonView, panelView);
96
- buttonView.bind('isEnabled').to(dropdownView);
97
- if (buttonView instanceof SplitButtonView) {
98
- buttonView.arrowView.bind('isOn').to(dropdownView, 'isOpen');
99
- }
100
- else {
101
- buttonView.bind('isOn').to(dropdownView, 'isOpen');
102
- }
103
- addDefaultBehavior(dropdownView);
104
- return dropdownView;
105
- }
106
- /**
107
- * Adds an instance of {@link module:ui/toolbar/toolbarview~ToolbarView} to a dropdown.
108
- *
109
- * ```ts
110
- * const buttonsCreator = () => {
111
- * const buttons = [];
112
- *
113
- * // Either create a new ButtonView instance or create existing.
114
- * buttons.push( new ButtonView() );
115
- * buttons.push( editor.ui.componentFactory.create( 'someButton' ) );
116
- * };
117
- *
118
- * const dropdown = createDropdown( locale );
119
- *
120
- * addToolbarToDropdown( dropdown, buttonsCreator, { isVertical: true } );
121
- *
122
- * // Will render a vertical button dropdown labeled "A button dropdown"
123
- * // with a button group in the panel containing two buttons.
124
- * // Buttons inside the dropdown will be created on first dropdown panel open.
125
- * dropdown.render()
126
- * document.body.appendChild( dropdown.element );
127
- * ```
128
- *
129
- * **Note:** To improve the accessibility, you can tell the dropdown to focus the first active button of the toolbar when the dropdown
130
- * {@link module:ui/dropdown/dropdownview~DropdownView#isOpen gets open}. See the documentation of `options` to learn more.
131
- *
132
- * **Note:** Toolbar view will be created on first open of the dropdown.
133
- *
134
- * See {@link module:ui/dropdown/utils~createDropdown} and {@link module:ui/toolbar/toolbarview~ToolbarView}.
135
- *
136
- * @param dropdownView A dropdown instance to which `ToolbarView` will be added.
137
- * @param options.enableActiveItemFocusOnDropdownOpen When set `true`, the focus will automatically move to the first
138
- * active {@link module:ui/toolbar/toolbarview~ToolbarView#items item} of the toolbar upon
139
- * {@link module:ui/dropdown/dropdownview~DropdownView#isOpen opening} the dropdown. Active items are those with the `isOn` property set
140
- * `true` (for instance {@link module:ui/button/buttonview~ButtonView buttons}). If no active items is found, the toolbar will be focused
141
- * as a whole resulting in the focus moving to its first focusable item (default behavior of
142
- * {@link module:ui/dropdown/dropdownview~DropdownView}).
143
- * @param options.ariaLabel Label used by assistive technologies to describe toolbar element.
144
- * @param options.maxWidth The maximum width of the toolbar element.
145
- * Details: {@link module:ui/toolbar/toolbarview~ToolbarView#maxWidth}.
146
- * @param options.class An additional CSS class added to the toolbar element.
147
- * @param options.isCompact When set true, makes the toolbar look compact with toolbar element.
148
- * @param options.isVertical Controls the orientation of toolbar items.
149
- */
150
- export function addToolbarToDropdown(dropdownView, buttonsOrCallback, options = {}) {
151
- dropdownView.extendTemplate({
152
- attributes: {
153
- class: ['ck-toolbar-dropdown']
154
- }
155
- });
156
- if (dropdownView.isOpen) {
157
- addToolbarToOpenDropdown(dropdownView, buttonsOrCallback, options);
158
- }
159
- else {
160
- dropdownView.once('change:isOpen', () => addToolbarToOpenDropdown(dropdownView, buttonsOrCallback, options), { priority: 'highest' });
161
- }
162
- if (options.enableActiveItemFocusOnDropdownOpen) {
163
- // Accessibility: Focus the first active button in the toolbar when the dropdown gets open.
164
- focusChildOnDropdownOpen(dropdownView, () => dropdownView.toolbarView.items.find((item) => item.isOn));
165
- }
166
- }
167
- /**
168
- * Adds an instance of {@link module:ui/toolbar/toolbarview~ToolbarView} to a dropdown.
169
- */
170
- function addToolbarToOpenDropdown(dropdownView, buttonsOrCallback, options) {
171
- const locale = dropdownView.locale;
172
- const t = locale.t;
173
- const toolbarView = dropdownView.toolbarView = new ToolbarView(locale);
174
- const buttons = typeof buttonsOrCallback == 'function' ? buttonsOrCallback() : buttonsOrCallback;
175
- toolbarView.ariaLabel = options.ariaLabel || t('Dropdown toolbar');
176
- if (options.maxWidth) {
177
- toolbarView.maxWidth = options.maxWidth;
178
- }
179
- if (options.class) {
180
- toolbarView.class = options.class;
181
- }
182
- if (options.isCompact) {
183
- toolbarView.isCompact = options.isCompact;
184
- }
185
- if (options.isVertical) {
186
- toolbarView.isVertical = true;
187
- }
188
- if (buttons instanceof ViewCollection) {
189
- toolbarView.items.bindTo(buttons).using(item => item);
190
- }
191
- else {
192
- toolbarView.items.addMany(buttons);
193
- }
194
- dropdownView.panelView.children.add(toolbarView);
195
- toolbarView.items.delegate('execute').to(dropdownView);
196
- }
197
- /**
198
- * Adds an instance of {@link module:ui/list/listview~ListView} to a dropdown.
199
- *
200
- * ```ts
201
- * const items = new Collection();
202
- *
203
- * items.add( {
204
- * type: 'button',
205
- * model: new Model( {
206
- * withText: true,
207
- * label: 'First item',
208
- * labelStyle: 'color: red'
209
- * } )
210
- * } );
211
- *
212
- * items.add( {
213
- * type: 'button',
214
- * model: new Model( {
215
- * withText: true,
216
- * label: 'Second item',
217
- * labelStyle: 'color: green',
218
- * class: 'foo'
219
- * } )
220
- * } );
221
- *
222
- * const dropdown = createDropdown( locale );
223
- *
224
- * addListToDropdown( dropdown, items );
225
- *
226
- * // Will render a dropdown with a list in the panel containing two items.
227
- * dropdown.render()
228
- * document.body.appendChild( dropdown.element );
229
- * ```
230
- *
231
- * The `items` collection passed to this methods controls the presence and attributes of respective
232
- * {@link module:ui/list/listitemview~ListItemView list items}.
233
- *
234
- * **Note:** To improve the accessibility, when a list is added to the dropdown using this helper the dropdown will automatically attempt
235
- * to focus the first active item (a host to a {@link module:ui/button/buttonview~ButtonView} with
236
- * {@link module:ui/button/buttonview~ButtonView#isOn} set `true`) or the very first item when none are active.
237
- *
238
- * **Note:** List view will be created on first open of the dropdown.
239
- *
240
- * See {@link module:ui/dropdown/utils~createDropdown} and {@link module:list/list~List}.
241
- *
242
- * @param dropdownView A dropdown instance to which `ListVIew` will be added.
243
- * @param itemsOrCallback A collection of the list item definitions or a callback returning a list item definitions to populate the list.
244
- * @param options.ariaLabel Label used by assistive technologies to describe list element.
245
- * @param options.role Will be reflected by the `role` DOM attribute in `ListVIew` and used by assistive technologies.
246
- */
247
- export function addListToDropdown(dropdownView, itemsOrCallback, options = {}) {
248
- if (dropdownView.isOpen) {
249
- addListToOpenDropdown(dropdownView, itemsOrCallback, options);
250
- }
251
- else {
252
- dropdownView.once('change:isOpen', () => addListToOpenDropdown(dropdownView, itemsOrCallback, options), { priority: 'highest' });
253
- }
254
- // Accessibility: Focus the first active button in the list when the dropdown gets open.
255
- focusChildOnDropdownOpen(dropdownView, () => dropdownView.listView.items.find(item => {
256
- if (item instanceof ListItemView) {
257
- return item.children.first.isOn;
258
- }
259
- return false;
260
- }));
261
- }
262
- /**
263
- * Adds an instance of {@link module:ui/list/listview~ListView} to a dropdown.
264
- */
265
- function addListToOpenDropdown(dropdownView, itemsOrCallback, options) {
266
- const locale = dropdownView.locale;
267
- const listView = dropdownView.listView = new ListView(locale);
268
- const items = typeof itemsOrCallback == 'function' ? itemsOrCallback() : itemsOrCallback;
269
- listView.ariaLabel = options.ariaLabel;
270
- listView.role = options.role;
271
- bindViewCollectionItemsToDefinitions(dropdownView, listView.items, items, locale);
272
- dropdownView.panelView.children.add(listView);
273
- listView.items.delegate('execute').to(dropdownView);
274
- }
275
- /**
276
- * A helper to be used on an existing {@link module:ui/dropdown/dropdownview~DropdownView} that focuses
277
- * a specific child in DOM when the dropdown {@link module:ui/dropdown/dropdownview~DropdownView#isOpen gets open}.
278
- *
279
- * @param dropdownView A dropdown instance to which the focus behavior will be added.
280
- * @param childSelectorCallback A callback executed when the dropdown gets open. It should return a {@link module:ui/view~View}
281
- * instance (child of {@link module:ui/dropdown/dropdownview~DropdownView#panelView}) that will get focused or a falsy value.
282
- * If falsy value is returned, a default behavior of the dropdown will engage focusing the first focusable child in
283
- * the {@link module:ui/dropdown/dropdownview~DropdownView#panelView}.
284
- */
285
- export function focusChildOnDropdownOpen(dropdownView, childSelectorCallback) {
286
- dropdownView.on('change:isOpen', () => {
287
- if (!dropdownView.isOpen) {
288
- return;
289
- }
290
- const childToFocus = childSelectorCallback();
291
- if (!childToFocus) {
292
- return;
293
- }
294
- if (typeof childToFocus.focus === 'function') {
295
- childToFocus.focus();
296
- }
297
- else {
298
- /**
299
- * The child view of a {@link module:ui/dropdown/dropdownview~DropdownView dropdown} is missing the `focus()` method
300
- * and could not be focused when the dropdown got {@link module:ui/dropdown/dropdownview~DropdownView#isOpen open}.
301
- *
302
- * Making the content of a dropdown focusable in this case greatly improves the accessibility. Please make the view instance
303
- * implements the {@link module:ui/dropdown/dropdownpanelfocusable~DropdownPanelFocusable focusable interface} for the best user
304
- * experience.
305
- *
306
- * @error ui-dropdown-focus-child-on-open-child-missing-focus
307
- * @param {module:ui/view~View} view
308
- */
309
- logWarning('ui-dropdown-focus-child-on-open-child-missing-focus', { view: childToFocus });
310
- }
311
- // * Let the panel show up first (do not focus an invisible element).
312
- // * Execute after focusDropdownPanelOnOpen(). See focusDropdownPanelOnOpen() to learn more.
313
- }, { priority: priorities.low - 10 });
314
- }
315
- /**
316
- * Add a set of default behaviors to dropdown view.
317
- */
318
- function addDefaultBehavior(dropdownView) {
319
- closeDropdownOnClickOutside(dropdownView);
320
- closeDropdownOnExecute(dropdownView);
321
- closeDropdownOnBlur(dropdownView);
322
- focusDropdownContentsOnArrows(dropdownView);
323
- focusDropdownButtonOnClose(dropdownView);
324
- focusDropdownPanelOnOpen(dropdownView);
325
- }
326
- /**
327
- * Adds a behavior to a dropdownView that closes opened dropdown when user clicks outside the dropdown.
328
- */
329
- function closeDropdownOnClickOutside(dropdownView) {
330
- dropdownView.on('render', () => {
331
- clickOutsideHandler({
332
- emitter: dropdownView,
333
- activator: () => dropdownView.isOpen,
334
- callback: () => {
335
- dropdownView.isOpen = false;
336
- },
337
- contextElements: () => [
338
- dropdownView.element,
339
- ...dropdownView.focusTracker._elements
340
- ]
341
- });
342
- });
343
- }
344
- /**
345
- * Adds a behavior to a dropdownView that closes the dropdown view on "execute" event.
346
- */
347
- function closeDropdownOnExecute(dropdownView) {
348
- // Close the dropdown when one of the list items has been executed.
349
- dropdownView.on('execute', evt => {
350
- // Toggling a switch button view should not close the dropdown.
351
- if (evt.source instanceof SwitchButtonView) {
352
- return;
353
- }
354
- dropdownView.isOpen = false;
355
- });
356
- }
357
- /**
358
- * Adds a behavior to a dropdown view that closes opened dropdown when it loses focus.
359
- */
360
- function closeDropdownOnBlur(dropdownView) {
361
- dropdownView.focusTracker.on('change:isFocused', (evt, name, isFocused) => {
362
- if (dropdownView.isOpen && !isFocused) {
363
- dropdownView.isOpen = false;
364
- }
365
- });
366
- }
367
- /**
368
- * Adds a behavior to a dropdownView that focuses the dropdown's panel view contents on keystrokes.
369
- */
370
- function focusDropdownContentsOnArrows(dropdownView) {
371
- // If the dropdown panel is already open, the arrow down key should focus the first child of the #panelView.
372
- dropdownView.keystrokes.set('arrowdown', (data, cancel) => {
373
- if (dropdownView.isOpen) {
374
- dropdownView.panelView.focus();
375
- cancel();
376
- }
377
- });
378
- // If the dropdown panel is already open, the arrow up key should focus the last child of the #panelView.
379
- dropdownView.keystrokes.set('arrowup', (data, cancel) => {
380
- if (dropdownView.isOpen) {
381
- dropdownView.panelView.focusLast();
382
- cancel();
383
- }
384
- });
385
- }
386
- /**
387
- * Adds a behavior that focuses the #buttonView when the dropdown was closed but focus was within the #panelView element.
388
- * This makes sure the focus is never lost.
389
- */
390
- function focusDropdownButtonOnClose(dropdownView) {
391
- dropdownView.on('change:isOpen', (evt, name, isOpen) => {
392
- if (isOpen) {
393
- return;
394
- }
395
- const element = dropdownView.panelView.element;
396
- // If the dropdown was closed, move the focus back to the button (#12125).
397
- // Don't touch the focus, if it moved somewhere else (e.g. moved to the editing root on #execute) (#12178).
398
- // Note: Don't use the state of the DropdownView#focusTracker here. It fires #blur with the timeout.
399
- if (element && element.contains(global.document.activeElement)) {
400
- dropdownView.buttonView.focus();
401
- }
402
- });
403
- }
404
- /**
405
- * Adds a behavior that focuses the #panelView when dropdown gets open (accessibility).
406
- */
407
- function focusDropdownPanelOnOpen(dropdownView) {
408
- dropdownView.on('change:isOpen', (evt, name, isOpen) => {
409
- if (!isOpen) {
410
- return;
411
- }
412
- // Focus the first item in the dropdown when the dropdown opened.
413
- dropdownView.panelView.focus();
414
- // * Let the panel show up first (do not focus an invisible element).
415
- // * Also, execute before focusChildOnDropdownOpen() to make sure this helper does not break the
416
- // focus of a specific child by kicking in too late and resetting the focus in the panel.
417
- }, { priority: 'low' });
418
- }
419
- /**
420
- * This helper populates a dropdown list with items and groups according to the
421
- * collection of item definitions. A permanent binding is created in this process allowing
422
- * dynamic management of the dropdown list content.
423
- *
424
- * @param dropdownView
425
- * @param listItems
426
- * @param definitions
427
- * @param locale
428
- */
429
- function bindViewCollectionItemsToDefinitions(dropdownView, listItems, definitions, locale) {
430
- listItems.bindTo(definitions).using(def => {
431
- if (def.type === 'separator') {
432
- return new ListSeparatorView(locale);
433
- }
434
- else if (def.type === 'group') {
435
- const groupView = new ListItemGroupView(locale);
436
- groupView.set({ label: def.label });
437
- bindViewCollectionItemsToDefinitions(dropdownView, groupView.items, def.items, locale);
438
- groupView.items.delegate('execute').to(dropdownView);
439
- return groupView;
440
- }
441
- else if (def.type === 'button' || def.type === 'switchbutton') {
442
- const listItemView = new ListItemView(locale);
443
- let buttonView;
444
- if (def.type === 'button') {
445
- buttonView = new ButtonView(locale);
446
- }
447
- else {
448
- buttonView = new SwitchButtonView(locale);
449
- }
450
- // Bind all model properties to the button view.
451
- buttonView.bind(...Object.keys(def.model)).to(def.model);
452
- buttonView.delegate('execute').to(listItemView);
453
- listItemView.children.add(buttonView);
454
- return listItemView;
455
- }
456
- return null;
457
- });
458
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module ui/dropdown/utils
7
+ */
8
+ import DropdownPanelView from './dropdownpanelview';
9
+ import DropdownView from './dropdownview';
10
+ import DropdownButtonView from './button/dropdownbuttonview';
11
+ import ToolbarView from '../toolbar/toolbarview';
12
+ import ListView from '../list/listview';
13
+ import ListItemView from '../list/listitemview';
14
+ import ListSeparatorView from '../list/listseparatorview';
15
+ import ButtonView from '../button/buttonview';
16
+ import SplitButtonView from './button/splitbuttonview';
17
+ import SwitchButtonView from '../button/switchbuttonview';
18
+ import ViewCollection from '../viewcollection';
19
+ import clickOutsideHandler from '../bindings/clickoutsidehandler';
20
+ import { global, priorities, logWarning } from '@ckeditor/ckeditor5-utils';
21
+ import '../../theme/components/dropdown/toolbardropdown.css';
22
+ import '../../theme/components/dropdown/listdropdown.css';
23
+ import ListItemGroupView from '../list/listitemgroupview';
24
+ /**
25
+ * A helper for creating dropdowns. It creates an instance of a {@link module:ui/dropdown/dropdownview~DropdownView dropdown},
26
+ * with a {@link module:ui/dropdown/button/dropdownbutton~DropdownButton button},
27
+ * {@link module:ui/dropdown/dropdownpanelview~DropdownPanelView panel} and all standard dropdown's behaviors.
28
+ *
29
+ * # Creating dropdowns
30
+ *
31
+ * By default, the default {@link module:ui/dropdown/button/dropdownbuttonview~DropdownButtonView} class is used as
32
+ * definition of the button:
33
+ *
34
+ * ```ts
35
+ * const dropdown = createDropdown( model );
36
+ *
37
+ * // Configure dropdown's button properties:
38
+ * dropdown.buttonView.set( {
39
+ * label: 'A dropdown',
40
+ * withText: true
41
+ * } );
42
+ *
43
+ * dropdown.render();
44
+ *
45
+ * // Will render a dropdown labeled "A dropdown" with an empty panel.
46
+ * document.body.appendChild( dropdown.element );
47
+ * ```
48
+ *
49
+ * You can also provide other button views (they need to implement the
50
+ * {@link module:ui/dropdown/button/dropdownbutton~DropdownButton} interface). For instance, you can use
51
+ * {@link module:ui/dropdown/button/splitbuttonview~SplitButtonView} to create a dropdown with a split button.
52
+ *
53
+ * ```ts
54
+ * const dropdown = createDropdown( locale, SplitButtonView );
55
+ *
56
+ * // Configure dropdown's button properties:
57
+ * dropdown.buttonView.set( {
58
+ * label: 'A dropdown',
59
+ * withText: true
60
+ * } );
61
+ *
62
+ * dropdown.buttonView.on( 'execute', () => {
63
+ * // Add the behavior of the "action part" of the split button.
64
+ * // Split button consists of the "action part" and "arrow part".
65
+ * // The arrow opens the dropdown while the action part can have some other behavior.
66
+ * } );
67
+ *
68
+ * dropdown.render();
69
+ *
70
+ * // Will render a dropdown labeled "A dropdown" with an empty panel.
71
+ * document.body.appendChild( dropdown.element );
72
+ * ```
73
+ *
74
+ * # Adding content to the dropdown's panel
75
+ *
76
+ * The content of the panel can be inserted directly into the `dropdown.panelView.element`:
77
+ *
78
+ * ```ts
79
+ * dropdown.panelView.element.textContent = 'Content of the panel';
80
+ * ```
81
+ *
82
+ * However, most of the time you will want to add there either a {@link module:ui/list/listview~ListView list of options}
83
+ * or a list of buttons (i.e. a {@link module:ui/toolbar/toolbarview~ToolbarView toolbar}).
84
+ * To simplify the task, you can use, respectively, {@link module:ui/dropdown/utils~addListToDropdown} or
85
+ * {@link module:ui/dropdown/utils~addToolbarToDropdown} utils.
86
+ *
87
+ * @param locale The locale instance.
88
+ * @param ButtonClass The dropdown button view class. Needs to implement the
89
+ * {@link module:ui/dropdown/button/dropdownbutton~DropdownButton} interface.
90
+ * @returns The dropdown view instance.
91
+ */
92
+ export function createDropdown(locale, ButtonClass = DropdownButtonView) {
93
+ const buttonView = new ButtonClass(locale);
94
+ const panelView = new DropdownPanelView(locale);
95
+ const dropdownView = new DropdownView(locale, buttonView, panelView);
96
+ buttonView.bind('isEnabled').to(dropdownView);
97
+ if (buttonView instanceof SplitButtonView) {
98
+ buttonView.arrowView.bind('isOn').to(dropdownView, 'isOpen');
99
+ }
100
+ else {
101
+ buttonView.bind('isOn').to(dropdownView, 'isOpen');
102
+ }
103
+ addDefaultBehavior(dropdownView);
104
+ return dropdownView;
105
+ }
106
+ /**
107
+ * Adds an instance of {@link module:ui/toolbar/toolbarview~ToolbarView} to a dropdown.
108
+ *
109
+ * ```ts
110
+ * const buttonsCreator = () => {
111
+ * const buttons = [];
112
+ *
113
+ * // Either create a new ButtonView instance or create existing.
114
+ * buttons.push( new ButtonView() );
115
+ * buttons.push( editor.ui.componentFactory.create( 'someButton' ) );
116
+ * };
117
+ *
118
+ * const dropdown = createDropdown( locale );
119
+ *
120
+ * addToolbarToDropdown( dropdown, buttonsCreator, { isVertical: true } );
121
+ *
122
+ * // Will render a vertical button dropdown labeled "A button dropdown"
123
+ * // with a button group in the panel containing two buttons.
124
+ * // Buttons inside the dropdown will be created on first dropdown panel open.
125
+ * dropdown.render()
126
+ * document.body.appendChild( dropdown.element );
127
+ * ```
128
+ *
129
+ * **Note:** To improve the accessibility, you can tell the dropdown to focus the first active button of the toolbar when the dropdown
130
+ * {@link module:ui/dropdown/dropdownview~DropdownView#isOpen gets open}. See the documentation of `options` to learn more.
131
+ *
132
+ * **Note:** Toolbar view will be created on first open of the dropdown.
133
+ *
134
+ * See {@link module:ui/dropdown/utils~createDropdown} and {@link module:ui/toolbar/toolbarview~ToolbarView}.
135
+ *
136
+ * @param dropdownView A dropdown instance to which `ToolbarView` will be added.
137
+ * @param options.enableActiveItemFocusOnDropdownOpen When set `true`, the focus will automatically move to the first
138
+ * active {@link module:ui/toolbar/toolbarview~ToolbarView#items item} of the toolbar upon
139
+ * {@link module:ui/dropdown/dropdownview~DropdownView#isOpen opening} the dropdown. Active items are those with the `isOn` property set
140
+ * `true` (for instance {@link module:ui/button/buttonview~ButtonView buttons}). If no active items is found, the toolbar will be focused
141
+ * as a whole resulting in the focus moving to its first focusable item (default behavior of
142
+ * {@link module:ui/dropdown/dropdownview~DropdownView}).
143
+ * @param options.ariaLabel Label used by assistive technologies to describe toolbar element.
144
+ * @param options.maxWidth The maximum width of the toolbar element.
145
+ * Details: {@link module:ui/toolbar/toolbarview~ToolbarView#maxWidth}.
146
+ * @param options.class An additional CSS class added to the toolbar element.
147
+ * @param options.isCompact When set true, makes the toolbar look compact with toolbar element.
148
+ * @param options.isVertical Controls the orientation of toolbar items.
149
+ */
150
+ export function addToolbarToDropdown(dropdownView, buttonsOrCallback, options = {}) {
151
+ dropdownView.extendTemplate({
152
+ attributes: {
153
+ class: ['ck-toolbar-dropdown']
154
+ }
155
+ });
156
+ if (dropdownView.isOpen) {
157
+ addToolbarToOpenDropdown(dropdownView, buttonsOrCallback, options);
158
+ }
159
+ else {
160
+ dropdownView.once('change:isOpen', () => addToolbarToOpenDropdown(dropdownView, buttonsOrCallback, options), { priority: 'highest' });
161
+ }
162
+ if (options.enableActiveItemFocusOnDropdownOpen) {
163
+ // Accessibility: Focus the first active button in the toolbar when the dropdown gets open.
164
+ focusChildOnDropdownOpen(dropdownView, () => dropdownView.toolbarView.items.find((item) => item.isOn));
165
+ }
166
+ }
167
+ /**
168
+ * Adds an instance of {@link module:ui/toolbar/toolbarview~ToolbarView} to a dropdown.
169
+ */
170
+ function addToolbarToOpenDropdown(dropdownView, buttonsOrCallback, options) {
171
+ const locale = dropdownView.locale;
172
+ const t = locale.t;
173
+ const toolbarView = dropdownView.toolbarView = new ToolbarView(locale);
174
+ const buttons = typeof buttonsOrCallback == 'function' ? buttonsOrCallback() : buttonsOrCallback;
175
+ toolbarView.ariaLabel = options.ariaLabel || t('Dropdown toolbar');
176
+ if (options.maxWidth) {
177
+ toolbarView.maxWidth = options.maxWidth;
178
+ }
179
+ if (options.class) {
180
+ toolbarView.class = options.class;
181
+ }
182
+ if (options.isCompact) {
183
+ toolbarView.isCompact = options.isCompact;
184
+ }
185
+ if (options.isVertical) {
186
+ toolbarView.isVertical = true;
187
+ }
188
+ if (buttons instanceof ViewCollection) {
189
+ toolbarView.items.bindTo(buttons).using(item => item);
190
+ }
191
+ else {
192
+ toolbarView.items.addMany(buttons);
193
+ }
194
+ dropdownView.panelView.children.add(toolbarView);
195
+ toolbarView.items.delegate('execute').to(dropdownView);
196
+ }
197
+ /**
198
+ * Adds an instance of {@link module:ui/list/listview~ListView} to a dropdown.
199
+ *
200
+ * ```ts
201
+ * const items = new Collection();
202
+ *
203
+ * items.add( {
204
+ * type: 'button',
205
+ * model: new Model( {
206
+ * withText: true,
207
+ * label: 'First item',
208
+ * labelStyle: 'color: red'
209
+ * } )
210
+ * } );
211
+ *
212
+ * items.add( {
213
+ * type: 'button',
214
+ * model: new Model( {
215
+ * withText: true,
216
+ * label: 'Second item',
217
+ * labelStyle: 'color: green',
218
+ * class: 'foo'
219
+ * } )
220
+ * } );
221
+ *
222
+ * const dropdown = createDropdown( locale );
223
+ *
224
+ * addListToDropdown( dropdown, items );
225
+ *
226
+ * // Will render a dropdown with a list in the panel containing two items.
227
+ * dropdown.render()
228
+ * document.body.appendChild( dropdown.element );
229
+ * ```
230
+ *
231
+ * The `items` collection passed to this methods controls the presence and attributes of respective
232
+ * {@link module:ui/list/listitemview~ListItemView list items}.
233
+ *
234
+ * **Note:** To improve the accessibility, when a list is added to the dropdown using this helper the dropdown will automatically attempt
235
+ * to focus the first active item (a host to a {@link module:ui/button/buttonview~ButtonView} with
236
+ * {@link module:ui/button/buttonview~ButtonView#isOn} set `true`) or the very first item when none are active.
237
+ *
238
+ * **Note:** List view will be created on first open of the dropdown.
239
+ *
240
+ * See {@link module:ui/dropdown/utils~createDropdown} and {@link module:list/list~List}.
241
+ *
242
+ * @param dropdownView A dropdown instance to which `ListVIew` will be added.
243
+ * @param itemsOrCallback A collection of the list item definitions or a callback returning a list item definitions to populate the list.
244
+ * @param options.ariaLabel Label used by assistive technologies to describe list element.
245
+ * @param options.role Will be reflected by the `role` DOM attribute in `ListVIew` and used by assistive technologies.
246
+ */
247
+ export function addListToDropdown(dropdownView, itemsOrCallback, options = {}) {
248
+ if (dropdownView.isOpen) {
249
+ addListToOpenDropdown(dropdownView, itemsOrCallback, options);
250
+ }
251
+ else {
252
+ dropdownView.once('change:isOpen', () => addListToOpenDropdown(dropdownView, itemsOrCallback, options), { priority: 'highest' });
253
+ }
254
+ // Accessibility: Focus the first active button in the list when the dropdown gets open.
255
+ focusChildOnDropdownOpen(dropdownView, () => dropdownView.listView.items.find(item => {
256
+ if (item instanceof ListItemView) {
257
+ return item.children.first.isOn;
258
+ }
259
+ return false;
260
+ }));
261
+ }
262
+ /**
263
+ * Adds an instance of {@link module:ui/list/listview~ListView} to a dropdown.
264
+ */
265
+ function addListToOpenDropdown(dropdownView, itemsOrCallback, options) {
266
+ const locale = dropdownView.locale;
267
+ const listView = dropdownView.listView = new ListView(locale);
268
+ const items = typeof itemsOrCallback == 'function' ? itemsOrCallback() : itemsOrCallback;
269
+ listView.ariaLabel = options.ariaLabel;
270
+ listView.role = options.role;
271
+ bindViewCollectionItemsToDefinitions(dropdownView, listView.items, items, locale);
272
+ dropdownView.panelView.children.add(listView);
273
+ listView.items.delegate('execute').to(dropdownView);
274
+ }
275
+ /**
276
+ * A helper to be used on an existing {@link module:ui/dropdown/dropdownview~DropdownView} that focuses
277
+ * a specific child in DOM when the dropdown {@link module:ui/dropdown/dropdownview~DropdownView#isOpen gets open}.
278
+ *
279
+ * @param dropdownView A dropdown instance to which the focus behavior will be added.
280
+ * @param childSelectorCallback A callback executed when the dropdown gets open. It should return a {@link module:ui/view~View}
281
+ * instance (child of {@link module:ui/dropdown/dropdownview~DropdownView#panelView}) that will get focused or a falsy value.
282
+ * If falsy value is returned, a default behavior of the dropdown will engage focusing the first focusable child in
283
+ * the {@link module:ui/dropdown/dropdownview~DropdownView#panelView}.
284
+ */
285
+ export function focusChildOnDropdownOpen(dropdownView, childSelectorCallback) {
286
+ dropdownView.on('change:isOpen', () => {
287
+ if (!dropdownView.isOpen) {
288
+ return;
289
+ }
290
+ const childToFocus = childSelectorCallback();
291
+ if (!childToFocus) {
292
+ return;
293
+ }
294
+ if (typeof childToFocus.focus === 'function') {
295
+ childToFocus.focus();
296
+ }
297
+ else {
298
+ /**
299
+ * The child view of a {@link module:ui/dropdown/dropdownview~DropdownView dropdown} is missing the `focus()` method
300
+ * and could not be focused when the dropdown got {@link module:ui/dropdown/dropdownview~DropdownView#isOpen open}.
301
+ *
302
+ * Making the content of a dropdown focusable in this case greatly improves the accessibility. Please make the view instance
303
+ * implements the {@link module:ui/dropdown/dropdownpanelfocusable~DropdownPanelFocusable focusable interface} for the best user
304
+ * experience.
305
+ *
306
+ * @error ui-dropdown-focus-child-on-open-child-missing-focus
307
+ * @param {module:ui/view~View} view
308
+ */
309
+ logWarning('ui-dropdown-focus-child-on-open-child-missing-focus', { view: childToFocus });
310
+ }
311
+ // * Let the panel show up first (do not focus an invisible element).
312
+ // * Execute after focusDropdownPanelOnOpen(). See focusDropdownPanelOnOpen() to learn more.
313
+ }, { priority: priorities.low - 10 });
314
+ }
315
+ /**
316
+ * Add a set of default behaviors to dropdown view.
317
+ */
318
+ function addDefaultBehavior(dropdownView) {
319
+ closeDropdownOnClickOutside(dropdownView);
320
+ closeDropdownOnExecute(dropdownView);
321
+ closeDropdownOnBlur(dropdownView);
322
+ focusDropdownContentsOnArrows(dropdownView);
323
+ focusDropdownButtonOnClose(dropdownView);
324
+ focusDropdownPanelOnOpen(dropdownView);
325
+ }
326
+ /**
327
+ * Adds a behavior to a dropdownView that closes opened dropdown when user clicks outside the dropdown.
328
+ */
329
+ function closeDropdownOnClickOutside(dropdownView) {
330
+ dropdownView.on('render', () => {
331
+ clickOutsideHandler({
332
+ emitter: dropdownView,
333
+ activator: () => dropdownView.isOpen,
334
+ callback: () => {
335
+ dropdownView.isOpen = false;
336
+ },
337
+ contextElements: () => [
338
+ dropdownView.element,
339
+ ...dropdownView.focusTracker._elements
340
+ ]
341
+ });
342
+ });
343
+ }
344
+ /**
345
+ * Adds a behavior to a dropdownView that closes the dropdown view on "execute" event.
346
+ */
347
+ function closeDropdownOnExecute(dropdownView) {
348
+ // Close the dropdown when one of the list items has been executed.
349
+ dropdownView.on('execute', evt => {
350
+ // Toggling a switch button view should not close the dropdown.
351
+ if (evt.source instanceof SwitchButtonView) {
352
+ return;
353
+ }
354
+ dropdownView.isOpen = false;
355
+ });
356
+ }
357
+ /**
358
+ * Adds a behavior to a dropdown view that closes opened dropdown when it loses focus.
359
+ */
360
+ function closeDropdownOnBlur(dropdownView) {
361
+ dropdownView.focusTracker.on('change:isFocused', (evt, name, isFocused) => {
362
+ if (dropdownView.isOpen && !isFocused) {
363
+ dropdownView.isOpen = false;
364
+ }
365
+ });
366
+ }
367
+ /**
368
+ * Adds a behavior to a dropdownView that focuses the dropdown's panel view contents on keystrokes.
369
+ */
370
+ function focusDropdownContentsOnArrows(dropdownView) {
371
+ // If the dropdown panel is already open, the arrow down key should focus the first child of the #panelView.
372
+ dropdownView.keystrokes.set('arrowdown', (data, cancel) => {
373
+ if (dropdownView.isOpen) {
374
+ dropdownView.panelView.focus();
375
+ cancel();
376
+ }
377
+ });
378
+ // If the dropdown panel is already open, the arrow up key should focus the last child of the #panelView.
379
+ dropdownView.keystrokes.set('arrowup', (data, cancel) => {
380
+ if (dropdownView.isOpen) {
381
+ dropdownView.panelView.focusLast();
382
+ cancel();
383
+ }
384
+ });
385
+ }
386
+ /**
387
+ * Adds a behavior that focuses the #buttonView when the dropdown was closed but focus was within the #panelView element.
388
+ * This makes sure the focus is never lost.
389
+ */
390
+ function focusDropdownButtonOnClose(dropdownView) {
391
+ dropdownView.on('change:isOpen', (evt, name, isOpen) => {
392
+ if (isOpen) {
393
+ return;
394
+ }
395
+ const element = dropdownView.panelView.element;
396
+ // If the dropdown was closed, move the focus back to the button (#12125).
397
+ // Don't touch the focus, if it moved somewhere else (e.g. moved to the editing root on #execute) (#12178).
398
+ // Note: Don't use the state of the DropdownView#focusTracker here. It fires #blur with the timeout.
399
+ if (element && element.contains(global.document.activeElement)) {
400
+ dropdownView.buttonView.focus();
401
+ }
402
+ });
403
+ }
404
+ /**
405
+ * Adds a behavior that focuses the #panelView when dropdown gets open (accessibility).
406
+ */
407
+ function focusDropdownPanelOnOpen(dropdownView) {
408
+ dropdownView.on('change:isOpen', (evt, name, isOpen) => {
409
+ if (!isOpen) {
410
+ return;
411
+ }
412
+ // Focus the first item in the dropdown when the dropdown opened.
413
+ dropdownView.panelView.focus();
414
+ // * Let the panel show up first (do not focus an invisible element).
415
+ // * Also, execute before focusChildOnDropdownOpen() to make sure this helper does not break the
416
+ // focus of a specific child by kicking in too late and resetting the focus in the panel.
417
+ }, { priority: 'low' });
418
+ }
419
+ /**
420
+ * This helper populates a dropdown list with items and groups according to the
421
+ * collection of item definitions. A permanent binding is created in this process allowing
422
+ * dynamic management of the dropdown list content.
423
+ *
424
+ * @param dropdownView
425
+ * @param listItems
426
+ * @param definitions
427
+ * @param locale
428
+ */
429
+ function bindViewCollectionItemsToDefinitions(dropdownView, listItems, definitions, locale) {
430
+ listItems.bindTo(definitions).using(def => {
431
+ if (def.type === 'separator') {
432
+ return new ListSeparatorView(locale);
433
+ }
434
+ else if (def.type === 'group') {
435
+ const groupView = new ListItemGroupView(locale);
436
+ groupView.set({ label: def.label });
437
+ bindViewCollectionItemsToDefinitions(dropdownView, groupView.items, def.items, locale);
438
+ groupView.items.delegate('execute').to(dropdownView);
439
+ return groupView;
440
+ }
441
+ else if (def.type === 'button' || def.type === 'switchbutton') {
442
+ const listItemView = new ListItemView(locale);
443
+ let buttonView;
444
+ if (def.type === 'button') {
445
+ buttonView = new ButtonView(locale);
446
+ buttonView.extendTemplate({
447
+ attributes: {
448
+ 'aria-checked': buttonView.bindTemplate.to('isOn')
449
+ }
450
+ });
451
+ }
452
+ else {
453
+ buttonView = new SwitchButtonView(locale);
454
+ }
455
+ // Bind all model properties to the button view.
456
+ buttonView.bind(...Object.keys(def.model)).to(def.model);
457
+ buttonView.delegate('execute').to(listItemView);
458
+ listItemView.children.add(buttonView);
459
+ return listItemView;
460
+ }
461
+ return null;
462
+ });
463
+ }