3 * Backbone View providing the aural view of CKEditor keyboard UX configuration.
6 (function ($, Drupal, Backbone, _) {
7 Drupal.ckeditor.KeyboardView = Backbone.View.extend(/** @lends Drupal.ckeditor.KeyboardView# */{
10 * Backbone View for CKEditor toolbar configuration; keyboard UX.
14 * @augments Backbone.View
17 // Add keyboard arrow support.
18 this.$el.on('keydown.ckeditor', '.ckeditor-buttons a, .ckeditor-multiple-buttons a', this.onPressButton.bind(this));
19 this.$el.on('keydown.ckeditor', '[data-drupal-ckeditor-type="group"]', this.onPressGroup.bind(this));
29 * Handles keypresses on a CKEditor configuration button.
31 * @param {jQuery.Event} event
32 * The keypress event triggered.
34 onPressButton(event) {
37 63232, // Safari up arrow.
39 63233, // Safari down arrow.
41 const leftRightKeys = [
43 63234, // Safari left arrow.
45 63235, // Safari right arrow.
48 // Respond to an enter key press. Prevent the bubbling of the enter key
49 // press to the button group parent element.
50 if (event.keyCode === 13) {
51 event.stopPropagation();
54 // Only take action when a direction key is pressed.
55 if (_.indexOf(_.union(upDownKeys, leftRightKeys), event.keyCode) > -1) {
57 let $target = $(event.currentTarget);
58 let $button = $target.parent();
59 const $container = $button.parent();
60 let $group = $button.closest('.ckeditor-toolbar-group');
62 const containerType = $container.data('drupal-ckeditor-button-sorting');
63 const $availableButtons = this.$el.find('[data-drupal-ckeditor-button-sorting="source"]');
64 const $activeButtons = this.$el.find('.ckeditor-toolbar-active');
65 // The current location of the button, just in case it needs to be put
67 const $originalGroup = $group;
70 // Move available buttons between their container and the active
72 if (containerType === 'source') {
73 // Move the button to the active toolbar configuration when the down
74 // or up keys are pressed.
75 if (_.indexOf([40, 63233], event.keyCode) > -1) {
76 // Move the button to the first row, first button group index
78 $activeButtons.find('.ckeditor-toolbar-group-buttons').eq(0).prepend($button);
81 else if (containerType === 'target') {
82 // Move buttons between sibling buttons in a group and between groups.
83 if (_.indexOf(leftRightKeys, event.keyCode) > -1) {
85 const $siblings = $container.children();
86 const index = $siblings.index($button);
87 if (_.indexOf([37, 63234], event.keyCode) > -1) {
88 // Move between sibling buttons.
90 $button.insertBefore($container.children().eq(index - 1));
92 // Move between button groups and rows.
94 // Move between button groups.
95 $group = $container.parent().prev();
96 if ($group.length > 0) {
97 $group.find('.ckeditor-toolbar-group-buttons').append($button);
102 .closest('.ckeditor-row')
104 .find('.ckeditor-toolbar-group')
106 .find('.ckeditor-toolbar-group-buttons')
113 else if (_.indexOf([39, 63235], event.keyCode) > -1) {
114 // Move between sibling buttons.
115 if (index < ($siblings.length - 1)) {
116 $button.insertAfter($container.children().eq(index + 1));
118 // Move between button groups. Moving right at the end of a row
119 // will create a new group.
121 $container.parent().next().find('.ckeditor-toolbar-group-buttons').prepend($button);
125 // Move buttons between rows and the available button set.
126 else if (_.indexOf(upDownKeys, event.keyCode) > -1) {
127 dir = (_.indexOf([38, 63232], event.keyCode) > -1) ? 'prev' : 'next';
128 $row = $container.closest('.ckeditor-row')[dir]();
129 // Move the button back into the available button set.
130 if (dir === 'prev' && $row.length === 0) {
131 // If this is a divider, just destroy it.
132 if ($button.data('drupal-ckeditor-type') === 'separator') {
136 // Focus on the first button in the active toolbar.
138 .find('.ckeditor-toolbar-group-buttons')
145 // Otherwise, move it.
147 $availableButtons.prepend($button);
151 $row.find('.ckeditor-toolbar-group-buttons').eq(0).prepend($button);
155 // Move dividers between their container and the active toolbar.
156 else if (containerType === 'dividers') {
157 // Move the button to the active toolbar configuration when the down
158 // or up keys are pressed.
159 if (_.indexOf([40, 63233], event.keyCode) > -1) {
160 // Move the button to the first row, first button group index
162 $button = $button.clone(true);
163 $activeButtons.find('.ckeditor-toolbar-group-buttons').eq(0).prepend($button);
164 $target = $button.children();
169 // Attempt to move the button to the new toolbar position.
170 Drupal.ckeditor.registerButtonMove(this, $button, (result) => {
171 // Put the button back if the registration failed.
172 // If the button was in a row, then it was in the active toolbar
173 // configuration. The button was probably placed in a new group, but
174 // that action was canceled.
175 if (!result && $originalGroup) {
176 $originalGroup.find('.ckeditor-buttons').append($button);
178 // Otherwise refresh the sortables to acknowledge the new button
181 view.$el.find('.ui-sortable').sortable('refresh');
183 // Refocus the target button so that the user can continue from a
185 $target.trigger('focus');
188 event.preventDefault();
189 event.stopPropagation();
194 * Handles keypresses on a CKEditor configuration group.
196 * @param {jQuery.Event} event
197 * The keypress event triggered.
199 onPressGroup(event) {
202 63232, // Safari up arrow.
204 63233, // Safari down arrow.
206 const leftRightKeys = [
208 63234, // Safari left arrow.
210 63235, // Safari right arrow.
213 // Respond to an enter key press.
214 if (event.keyCode === 13) {
216 // Open the group renaming dialog in the next evaluation cycle so that
217 // this event can be cancelled and the bubbling wiped out. Otherwise,
218 // Firefox has issues because the page focus is shifted to the dialog
219 // along with the keydown event.
220 window.setTimeout(() => {
221 Drupal.ckeditor.openGroupNameDialog(view, $(event.currentTarget));
223 event.preventDefault();
224 event.stopPropagation();
227 // Respond to direction key presses.
228 if (_.indexOf(_.union(upDownKeys, leftRightKeys), event.keyCode) > -1) {
229 const $group = $(event.currentTarget);
230 const $container = $group.parent();
231 const $siblings = $container.children();
234 // Move groups between sibling groups.
235 if (_.indexOf(leftRightKeys, event.keyCode) > -1) {
236 index = $siblings.index($group);
237 // Move left between sibling groups.
238 if ((_.indexOf([37, 63234], event.keyCode) > -1)) {
240 $group.insertBefore($siblings.eq(index - 1));
242 // Wrap between rows. Insert the group before the placeholder group
243 // at the end of the previous row.
245 const $rowChildElement = $container
246 .closest('.ckeditor-row')
248 .find('.ckeditor-toolbar-groups')
251 $group.insertBefore($rowChildElement);
254 // Move right between sibling groups.
255 else if (_.indexOf([39, 63235], event.keyCode) > -1) {
256 // Move to the right if the next group is not a placeholder.
257 if (!$siblings.eq(index + 1).hasClass('placeholder')) {
258 $group.insertAfter($container.children().eq(index + 1));
260 // Wrap group between rows.
262 $container.closest('.ckeditor-row').next().find('.ckeditor-toolbar-groups').prepend($group);
266 // Move groups between rows.
267 else if (_.indexOf(upDownKeys, event.keyCode) > -1) {
268 dir = (_.indexOf([38, 63232], event.keyCode) > -1) ? 'prev' : 'next';
270 .closest('.ckeditor-row')[dir]()
271 .find('.ckeditor-toolbar-groups')
276 Drupal.ckeditor.registerGroupMove(this, $group);
277 $group.trigger('focus');
278 event.preventDefault();
279 event.stopPropagation();
283 }(jQuery, Drupal, Backbone, _));