-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
specialcharactersnavigationview.js
133 lines (110 loc) · 3.71 KB
/
specialcharactersnavigationview.js
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
/**
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module special-characters/ui/specialcharactersnavigationview
*/
import Collection from '@ckeditor/ckeditor5-utils/src/collection';
import Model from '@ckeditor/ckeditor5-ui/src/model';
import {
createDropdown,
addListToDropdown
} from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
import FormHeaderView from '@ckeditor/ckeditor5-ui/src/formheader/formheaderview';
/**
* A class representing the navigation part of the special characters UI. It is responsible
* for describing the feature and allowing the user to select a particular character group.
*
* @extends module:ui/formheader/formheaderview~FormHeaderView
*/
export default class SpecialCharactersNavigationView extends FormHeaderView {
/**
* Creates an instance of the {@link module:special-characters/ui/specialcharactersnavigationview~SpecialCharactersNavigationView}
* class.
*
* @param {module:utils/locale~Locale} locale The localization services instance.
* @param {Iterable.<String>} groupNames The names of the character groups.
*/
constructor( locale, groupNames ) {
super( locale );
const t = locale.t;
this.set( 'class', 'ck-special-characters-navigation' );
/**
* A dropdown that allows selecting a group of special characters to be displayed.
*
* @member {module:ui/dropdown/dropdownview~DropdownView}
*/
this.groupDropdownView = this._createGroupDropdown( groupNames );
this.groupDropdownView.panelPosition = locale.uiLanguageDirection === 'rtl' ? 'se' : 'sw';
/**
* @inheritDoc
*/
this.label = t( 'Special characters' );
/**
* @inheritDoc
*/
this.children.add( this.groupDropdownView );
}
/**
* Returns the name of the character group currently selected in the {@link #groupDropdownView}.
*
* @returns {String}
*/
get currentGroupName() {
return this.groupDropdownView.value;
}
/**
* Returns a dropdown that allows selecting character groups.
*
* @private
* @param {Iterable.<String>} groupNames The names of the character groups.
* @returns {module:ui/dropdown/dropdownview~DropdownView}
*/
_createGroupDropdown( groupNames ) {
const locale = this.locale;
const t = locale.t;
const dropdown = createDropdown( locale );
const groupDefinitions = this._getCharacterGroupListItemDefinitions( dropdown, groupNames );
dropdown.set( 'value', groupDefinitions.first.model.label );
dropdown.buttonView.bind( 'label' ).to( dropdown, 'value' );
dropdown.buttonView.set( {
isOn: false,
withText: true,
tooltip: t( 'Character categories' ),
class: [ 'ck-dropdown__button_label-width_auto' ]
} );
dropdown.on( 'execute', evt => {
dropdown.value = evt.source.label;
} );
dropdown.delegate( 'execute' ).to( this );
addListToDropdown( dropdown, groupDefinitions );
return dropdown;
}
/**
* Returns list item definitions to be used in the character group dropdown
* representing specific character groups.
*
* @private
* @param {module:ui/dropdown/dropdownview~DropdownView} dropdown
* @param {Iterable.<String>} groupNames The names of the character groups.
* @returns {Iterable.<module:ui/dropdown/utils~ListDropdownItemDefinition>}
*/
_getCharacterGroupListItemDefinitions( dropdown, groupNames ) {
const groupDefs = new Collection();
for ( const name of groupNames ) {
const definition = {
type: 'button',
model: new Model( {
label: name,
withText: true
} )
};
definition.model.bind( 'isOn' ).to( dropdown, 'value', value => {
return value === definition.model.label;
} );
groupDefs.add( definition );
}
return groupDefs;
}
}