-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
collapsibleview.ts
157 lines (131 loc) · 3.73 KB
/
collapsibleview.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
/**
* @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/collapsible/collapsibleview
*/
import type { Locale } from '@ckeditor/ckeditor5-utils';
import View from '../view.js';
import ButtonView from '../button/buttonview.js';
import type ViewCollection from '../viewcollection.js';
import type { FocusableView } from '../focuscycler.js';
import dropdownArrowIcon from '../../theme/icons/dropdown-arrow.svg';
import '../../theme/components/collapsible/collapsible.css';
/**
* A collapsible UI component. Consists of a labeled button and a container which can be collapsed
* by clicking the button. The collapsible container can be a host to other UI views.
*
* @internal
*/
export default class CollapsibleView extends View {
/**
* `true` when the container with {@link #children} is collapsed. `false` otherwise.
*
* @observable
*/
declare public isCollapsed: boolean;
/**
* The text label of the {@link #buttonView}.
*
* @observable
* @default 'Show more'
*/
declare public label: string;
/**
* The ID of the label inside the {@link #buttonView} that describes the collapsible
* container for assistive technologies. Set after the button was {@link #render rendered}.
*
* @internal
* @readonly
* @observable
*/
declare public _collapsibleAriaLabelUid: string | undefined;
/**
* The main button that, when clicked, collapses or expands the container with {@link #children}.
*/
public readonly buttonView: ButtonView;
/**
* A collection of the child views that can be collapsed by clicking the {@link #buttonView}.
*/
public readonly children: ViewCollection<FocusableView>;
/**
* Creates an instance of the collapsible view.
*
* @param locale The {@link module:core/editor/editor~Editor#locale} instance.
* @param childViews An optional array of initial child views to be inserted into the collapsible.
*/
constructor( locale: Locale, childViews?: Array<FocusableView> ) {
super( locale );
const bind = this.bindTemplate;
this.set( 'isCollapsed', false );
this.set( 'label', '' );
this.buttonView = this._createButtonView();
this.children = this.createCollection();
this.set( '_collapsibleAriaLabelUid', undefined );
if ( childViews ) {
this.children.addMany( childViews );
}
this.setTemplate( {
tag: 'div',
attributes: {
class: [
'ck',
'ck-collapsible',
bind.if( 'isCollapsed', 'ck-collapsible_collapsed' )
]
},
children: [
this.buttonView,
{
tag: 'div',
attributes: {
class: [
'ck',
'ck-collapsible__children'
],
role: 'region',
hidden: bind.if( 'isCollapsed', 'hidden' ),
'aria-labelledby': bind.to( '_collapsibleAriaLabelUid' )
},
children: this.children
}
]
} );
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
this._collapsibleAriaLabelUid = this.buttonView.labelView.element!.id;
}
/**
* Focuses the first focusable.
*/
public focus(): void {
this.buttonView.focus();
}
/**
* Creates the main {@link #buttonView} of the collapsible.
*/
private _createButtonView() {
const buttonView = new ButtonView( this.locale );
const bind = buttonView.bindTemplate;
buttonView.set( {
withText: true,
icon: dropdownArrowIcon
} );
buttonView.extendTemplate( {
attributes: {
'aria-expanded': bind.to( 'isOn', value => String( value ) )
}
} );
buttonView.bind( 'label' ).to( this );
buttonView.bind( 'isOn' ).to( this, 'isCollapsed', isCollapsed => !isCollapsed );
buttonView.on( 'execute', () => {
this.isCollapsed = !this.isCollapsed;
} );
return buttonView;
}
}