Debian Codegraph @

extensions/WikiEditor/modules/jquery.wikiEditor.toolbar.js

raw symbols SCIP
1 /**
2 * Toolbar module for wikiEditor
3 *
4 * @memberof module:ext.wikiEditor
5 */
6 var toolbarModule = {
7
8 /**
9 * API accessible functions
10 */
11 api: {
12 addToToolbar: function ( context, data ) {
13
14 for ( var type in data ) {
15 var i;
16 switch ( type ) {
17 case 'sections':
18 var $sections = context.modules.toolbar.$toolbar.find( 'div.sections' );
19 var $tabs = context.modules.toolbar.$toolbar.find( 'div.tabs' );
20 for ( var section in data[ type ] ) {
21 if ( section === 'main' || section === 'secondary' ) {
22 // Section
23 context.modules.toolbar.$toolbar.prepend(
24 toolbarModule.fn.buildSection(
25 context, section, data[ type ][ section ]
26 )
27 );
28 continue;
29 }
30 // Section
31 $sections.append(
32 toolbarModule.fn.buildSection( context, section, data[ type ][ section ] )
33 );
34 // Tab
35 $tabs.append(
36 toolbarModule.fn.buildTab( context, section, data[ type ][ section ] )
37 );
38 }
39 break;
40 case 'groups':
41 if ( !( 'section' in data ) ) {
42 continue;
43 }
44 var $section = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section' );
45 for ( var group in data[ type ] ) {
46 // Group
47 $section.append(
48 toolbarModule.fn.buildGroup( context, group, data[ type ][ group ] )
49 );
50 }
51 break;
52 case 'tools':
53 if ( !( 'section' in data && 'group' in data ) ) {
54 continue;
55 }
56 var $group = context.modules.toolbar.$toolbar.find(
57 'div[rel="' + data.section + '"].section ' +
58 'div[rel="' + data.group + '"].group'
59 );
60 for ( var tool in data[ type ] ) {
61 // Tool
62 $group.append( toolbarModule.fn.buildTool( context, tool, data[ type ][ tool ] ) );
63 }
64 if ( $group.children().length ) {
65 $group.removeClass( 'empty' );
66 }
67 break;
68 case 'pages':
69 if ( !( 'section' in data ) ) {
70 continue;
71 }
72 var $pages = context.modules.toolbar.$toolbar.find(
73 'div[rel="' + data.section + '"].section .pages'
74 );
75 var $index = context.modules.toolbar.$toolbar.find(
76 'div[rel="' + data.section + '"].section .index'
77 );
78 for ( var page in data[ type ] ) {
79 // Page
80 $pages.append( toolbarModule.fn.buildPage( context, page, data[ type ][ page ] ) );
81 // Index
82 $index.append(
83 toolbarModule.fn.buildBookmark( context, page, data[ type ][ page ] )
84 );
85 }
86 toolbarModule.fn.updateBookletSelection( context, data.section, $pages, $index );
87 break;
88 case 'rows':
89 if ( !( 'section' in data && 'page' in data ) ) {
90 continue;
91 }
92 var $table = context.modules.toolbar.$toolbar.find(
93 'div[rel="' + data.section + '"].section ' +
94 'div[rel="' + data.page + '"].page table'
95 );
96 for ( i = 0; i < data.rows.length; i++ ) {
97 // Row
98 $table.append( toolbarModule.fn.buildRow( context, data.rows[ i ] ) );
99 }
100 break;
101 case 'characters':
102 if ( !( 'section' in data && 'page' in data ) ) {
103 continue;
104 }
105 var $characters = context.modules.toolbar.$toolbar.find(
106 'div[rel="' + data.section + '"].section ' +
107 'div[rel="' + data.page + '"].page div'
108 );
109 var actions = $characters.data( 'actions' );
110 for ( i = 0; i < data.characters.length; i++ ) {
111 // Character
112 $characters.append(
113 $( toolbarModule.fn.buildCharacter( data.characters[ i ], actions ) )
114 .on( 'mousedown', function ( e ) {
115 // No dragging!
116 e.preventDefault();
117 return false;
118 } )
119 .on( 'click', function ( e ) {
120 toolbarModule.fn.doAction( $( this ).parent().data( 'context' ),
121 $( this ).parent().data( 'actions' )[ $( this ).attr( 'rel' ) ] );
122 e.preventDefault();
123 return false;
124 } )
125 );
126 }
127 break;
128 default: break;
129 }
130 }
131 },
132 removeFromToolbar: function ( context, data ) {
133 if ( typeof data.section === 'string' ) {
134 // Section
135 var tab = 'div.tabs span[rel="' + data.section + '"].tab';
136 var target = 'div[rel="' + data.section + '"].section';
137 var group = null;
138 if ( typeof data.group === 'string' ) {
139 // Toolbar group
140 target += ' div[rel="' + data.group + '"].group';
141 if ( typeof data.tool === 'string' ) {
142 // Save for later checking if empty
143 group = target;
144 // Tool
145 target = target + ' [rel="' + data.tool + '"].tool';
146 }
147 } else if ( typeof data.page === 'string' ) {
148 // Booklet page
149 var index = target + ' div.index div[rel="' + data.page + '"]';
150 target += ' div.pages div[rel="' + data.page + '"].page';
151 if ( typeof data.character === 'string' ) {
152 // Character
153 target += ' span[rel="' + data.character + '"]';
154 } else if ( typeof data.row === 'number' ) {
155 // Table row
156 target += ' table tr:not(:has(th)):eq(' + data.row + ')';
157 } else {
158 // Just a page, remove the index too!
159 context.modules.toolbar.$toolbar.find( index ).remove();
160 toolbarModule.fn.updateBookletSelection(
161 context,
162 data.section,
163 context.modules.toolbar.$toolbar.find( target ),
164 context.modules.toolbar.$toolbar.find( index )
165 );
166 }
167 } else {
168 // Just a section, remove the tab too!
169 context.modules.toolbar.$toolbar.find( tab ).remove();
170 }
171 context.modules.toolbar.$toolbar.find( target ).remove();
172 // Hide empty groups
173 if ( group ) {
174 var $group = context.modules.toolbar.$toolbar.find( group );
175 if ( $group.children().length === 0 ) {
176 $group.addClass( 'empty' );
177 }
178 }
179 }
180 }
181 },
182
183 /**
184 * Internally used functions
185 */
186 fn: {
187 /**
188 * Creates a toolbar module within a wikiEditor
189 *
190 * @param {Object} context Context object of editor to create module in
191 * @param {Object} config Configuration object to create module from
192 */
193 create: function ( context, config ) {
194 if ( '$toolbar' in context.modules.toolbar ) {
195 return;
196 }
197 context.modules.toolbar.$toolbar = $( '<div>' )
198 .addClass( 'wikiEditor-ui-toolbar' )
199 .attr( 'id', 'wikiEditor-ui-toolbar' );
200 toolbarModule.fn.build( context, config );
201 context.$ui.find( '.wikiEditor-ui-top' ).append( context.modules.toolbar.$toolbar );
202 },
203 /**
204 * Performs an operation based on parameters
205 *
206 * @param {Object} context
207 * @param {Object} action
208 */
209 doAction: function ( context, action ) {
210 switch ( action.type ) {
211 case 'replace':
212 case 'encapsulate':
213 if ( context.$textarea.prop( 'readonly' ) ) {
214 break;
215 }
216 var parts = {
217 pre: $.wikiEditor.autoMsg( action.options, 'pre' ),
218 peri: $.wikiEditor.autoMsg( action.options, 'peri' ),
219 post: $.wikiEditor.autoMsg( action.options, 'post' )
220 };
221 var replace = action.type === 'replace';
222 if ( 'regex' in action.options && 'regexReplace' in action.options ) {
223 var selection = context.$textarea.textSelection( 'getSelection' );
224 if ( selection !== '' && action.options.regex.test( selection ) ) {
225 parts.peri = selection.replace( action.options.regex,
226 action.options.regexReplace );
227 parts.pre = parts.post = '';
228 replace = true;
229 }
230 }
231 context.$textarea.textSelection(
232 'encapsulateSelection',
233 Object.assign( {}, action.options, parts, { replace: replace } )
234 );
235 break;
236 case 'callback':
237 if ( typeof action.execute === 'function' ) {
238 action.execute( context );
239 }
240 break;
241 case 'dialog':
242 context.fn.saveSelection();
243 context.$textarea.wikiEditor( 'openDialog', action.module );
244 break;
245 default: break;
246 }
247 },
248 buildGroup: function ( context, id, group ) {
249 var $group = $( '<div>' ).attr( { class: 'group group-' + id, rel: id } ),
250 label = $.wikiEditor.autoMsg( group, 'label' );
251 if ( label ) {
252 $( '<span>' ).addClass( 'label' ).text( label ).appendTo( $group );
253 }
254 var empty = true;
255 if ( 'tools' in group ) {
256 for ( var tool in group.tools ) {
257 var $tool = toolbarModule.fn.buildTool( context, tool, group.tools[ tool ] );
258 if ( $tool ) {
259 // Consider a group with only hidden tools empty as well
260 // .is( ':visible' ) always returns false because tool is not attached to the DOM yet
261 empty = empty && $tool.css( 'display' ) === 'none';
262 $group.append( $tool );
263 }
264 }
265 }
266 if ( empty ) {
267 $group.addClass( 'empty' );
268 }
269 return $group;
270 },
271 buildTool: function ( context, id, tool ) {
272 if ( 'filters' in tool ) {
273 for ( var i = 0; i < tool.filters.length; i++ ) {
274 if ( $( tool.filters[ i ] ).length === 0 ) {
275 return null;
276 }
277 }
278 }
279 var label = $.wikiEditor.autoMsg( tool, 'label' );
280 switch ( tool.type ) {
281 case 'button':
282 case 'toggle':
283 var $button;
284 if ( tool.oouiIcon ) {
285 var config = {
286 framed: false,
287 classes: [ 'tool' ],
288 icon: tool.oouiIcon,
289 title: label
290 };
291 var oouiButton;
292 if ( tool.type === 'button' ) {
293 oouiButton = new OO.ui.ButtonWidget( config );
294 } else if ( tool.type === 'toggle' ) {
295 oouiButton = new OO.ui.ToggleButtonWidget( config );
296 }
297 $button = oouiButton.$element
298 .attr( 'rel', id )
299 .data( 'ooui', oouiButton );
300 } else {
301 $button = $( '<a>' )
302 .attr( {
303 tabindex: 0,
304 title: label,
305 rel: id,
306 role: 'button',
307 class: 'tool tool-button'
308 } )
309 .text( label );
310 if ( tool.icon ) {
311 var icon = $.wikiEditor.autoIcon(
312 tool.icon,
313 $.wikiEditor.imgPath + 'toolbar/'
314 );
315 $button.css( 'background-image', 'url(' + icon + ')' );
316 }
317 }
318 $button.data( 'setActive', function ( active ) {
319 $button.toggleClass( 'tool-active', active );
320
321 // OOUI button
322 if ( $button.data( 'ooui' ) && tool.type === 'toggle' ) {
323 $button.data( 'ooui' ).setValue( active );
324 // Use progressive icon in WMUI theme
325 if ( OO.ui.WikimediaUITheme && OO.ui.theme instanceof OO.ui.WikimediaUITheme ) {
326 // Wait for updateElementClasses to run
327 setTimeout( function () {
328 $button.data( 'ooui' ).$icon.toggleClass( 'oo-ui-image-progressive', active );
329 } );
330 }
331 }
332 } );
333 if ( 'action' in tool ) {
334 $button
335 .data( 'action', tool.action )
336 .data( 'context', context )
337 .on( 'mousedown', function ( e ) {
338 // No dragging!
339 e.preventDefault();
340 return false;
341 } );
342 if ( $button.data( 'ooui' ) ) {
343 $button.data( 'ooui' ).on( 'click', function () {
344 toolbarModule.fn.doAction(
345 context, tool.action
346 );
347 } );
348 } else {
349 $button.on( 'click keydown', function ( e ) {
350 if (
351 e.type === 'click' ||
352 e.type === 'keydown' && e.key === 'Enter'
353 ) {
354 toolbarModule.fn.doAction(
355 context, tool.action
356 );
357 e.preventDefault();
358 return false;
359 }
360 } );
361 }
362 }
363 return $button;
364 case 'select':
365 var menuId = 'menu-' + Date.now();
366 var $select = $( '<div>' )
367 .attr( { rel: id, class: 'tool tool-select' } );
368 var $options = $( '<div>' ).addClass( 'options' );
369 if ( 'list' in tool ) {
370 for ( var option in tool.list ) {
371 var optionLabel = $.wikiEditor.autoMsg( tool.list[ option ], 'label' );
372 $options.append(
373 $( '<a>' )
374 .data( 'action', tool.list[ option ].action )
375 .data( 'context', context )
376 .on( 'mousedown', function ( e ) {
377 // No dragging!
378 e.preventDefault();
379 return false;
380 } )
381 .on( 'click keydown', function ( e ) {
382 if (
383 e.type === 'click' ||
384 e.type === 'keydown' && e.key === 'Enter'
385 ) {
386 toolbarModule.fn.doAction(
387 $( this ).data( 'context' ), $( this ).data( 'action' ), $( this )
388 );
389 // Hide the dropdown
390 $( this ).closest( '.tool-select' ).removeClass( 'options-shown' );
391 e.preventDefault();
392 return false;
393 }
394 } )
395 .text( optionLabel )
396 .addClass( 'option' )
397 .attr( { rel: option, tabindex: 0, role: 'menuitem' } )
398 );
399 }
400 }
401 $select.append( $( '<a>' )
402 .addClass( 'label skin-invert' )
403 .text( label )
404 .data( 'options', $options )
405 .attr( { role: 'button', tabindex: 0, 'aria-expanded': false, 'aria-controls': menuId, 'aria-haspopup': 'menu' } )
406 .on( 'mousedown', function ( e ) {
407 // No dragging!
408 e.preventDefault();
409 return false;
410 } )
411 .on( 'click keydown', function ( e ) {
412 if (
413 e.type === 'click' ||
414 e.type === 'keydown' && e.key === 'Enter'
415 ) {
416 var $opts = $( this ).data( 'options' );
417 // eslint-disable-next-line no-jquery/no-class-state
418 var canShowOptions = !$opts.closest( '.tool-select' ).hasClass( 'options-shown' );
419 $opts.closest( '.tool-select' ).toggleClass( 'options-shown', canShowOptions );
420 $( this ).attr( 'aria-expanded', canShowOptions.toString() );
421 e.preventDefault();
422 return false;
423 }
424 } )
425 );
426 $select.append( $( '<div>' ).addClass( 'menu' ).append( $options ) );
427 return $select;
428 case 'element':
429 // A raw 'element' type can be {htmlString|Element|Text|Array|jQuery|OO.ui.HTMLSnippet|function}.
430 var $element;
431 if ( tool.element instanceof OO.ui.HtmlSnippet ) {
432 $element = tool.element.toString();
433 } else if ( typeof tool.element === 'function' ) {
434 $element = tool.element( context );
435 } else {
436 $element = tool.element;
437 }
438 return $( '<div>' )
439 .attr( { rel: id, class: 'tool tool-element' } )
440 .append( $element );
441 default:
442 return null;
443 }
444 },
445 buildBookmark: function ( context, id, page ) {
446 var label = $.wikiEditor.autoMsg( page, 'label' );
447 return $( '<div>' )
448 .text( label )
449 .attr( {
450 rel: id,
451 role: 'option'
452 } )
453 .data( 'context', context )
454 .on( 'mousedown', function ( e ) {
455 // No dragging!
456 e.preventDefault();
457 return false;
458 } )
459 .on( 'click', function ( event ) {
460 $( this ).parent().parent().find( '.page' ).hide();
461 $( this ).parent().parent().find( '.page-' + $( this ).attr( 'rel' ) ).show().trigger( 'loadPage' );
462 $( this )
463 .addClass( 'current' )
464 .siblings().removeClass( 'current' );
465 var section = $( this ).parent().parent().attr( 'rel' );
466 $.cookie(
467 'wikiEditor-' + $( this ).data( 'context' ).instance + '-booklet-' + section + '-page',
468 $( this ).attr( 'rel' ),
469 { expires: 30, path: '/' }
470 );
471 // No dragging!
472 event.preventDefault();
473 return false;
474 } );
475 },
476 buildPage: function ( context, id, page, deferLoad ) {
477 var $page = $( '<div>' ).attr( {
478 class: 'page page-' + id,
479 rel: id
480 } );
481 if ( deferLoad ) {
482 $page.one( 'loadPage', function () {
483 toolbarModule.fn.reallyBuildPage( context, id, page, $page );
484 } );
485 } else {
486 toolbarModule.fn.reallyBuildPage( context, id, page, $page );
487 }
488 return $page;
489 },
490 reallyBuildPage: function ( context, id, page, $page ) {
491 var i;
492 switch ( page.layout ) {
493 case 'table':
494 $page.addClass( 'page-table' );
495 var html = '';
496 if ( 'headings' in page ) {
497 html += toolbarModule.fn.buildHeading( context, page.headings );
498 }
499 if ( 'rows' in page ) {
500 for ( i = 0; i < page.rows.length; i++ ) {
501 html += toolbarModule.fn.buildRow( context, page.rows[ i ] );
502 }
503 }
504 $page.html( '<table class="table-' + id + '">' + html + '</table>' );
505 break;
506 case 'characters':
507 $page.addClass( 'page-characters' );
508 var $characters = $( '<div>' ).data( 'context', context ).data( 'actions', {} );
509 var actions = $characters.data( 'actions' );
510 if ( 'language' in page ) {
511 $characters.attr( 'lang', page.language );
512 }
513 if ( 'direction' in page ) {
514 $characters.attr( 'dir', page.direction );
515 } else {
516 // By default it should be explicit ltr for all scripts.
517 // Without this some conjoined ltr characters look
518 // weird in rtl wikis.
519 $characters.attr( 'dir', 'ltr' );
520 }
521 if ( 'characters' in page ) {
522 html = '';
523 for ( i = 0; i < page.characters.length; i++ ) {
524 html += toolbarModule.fn.buildCharacter( page.characters[ i ], actions );
525 }
526 $characters
527 .html( html )
528 .children()
529 .attr( 'role', 'option' )
530 .on( 'mousedown', function ( e ) {
531 // No dragging!
532 e.preventDefault();
533 return false;
534 } )
535 .on( 'click', function ( e ) {
536 toolbarModule.fn.doAction(
537 $( this ).parent().data( 'context' ),
538 $( this ).parent().data( 'actions' )[ $( this ).attr( 'rel' ) ],
539 $( this )
540 );
541 e.preventDefault();
542 return false;
543 } );
544 }
545 $page.append( $characters );
546 break;
547 }
548 },
549 buildHeading: function ( context, headings ) {
550 var html = '';
551 for ( var i = 0; i < headings.length; i++ ) {
552 html += '<th>' + $.wikiEditor.autoSafeMsg( headings[ i ], [ 'html', 'text' ] ) + '</th>';
553 }
554 return '<tr>' + html + '</tr>';
555 },
556 buildRow: function ( context, row ) {
557 var html = '';
558 for ( var cell in row ) {
559 // FIXME: This currently needs to use the "unsafe" .text() message because it embeds raw HTML
560 // in the messages (as used exclusively by the 'help' toolbar panel).
561 html += '<td class="cell cell-' + cell + '"><span>' +
562 $.wikiEditor.autoMsg( row[ cell ], [ 'html', 'text' ] ) + '</span></td>';
563 }
564 return '<tr>' + html + '</tr>';
565 },
566 buildCharacter: function ( character, actions ) {
567 if ( typeof character === 'string' ) {
568 character = {
569 label: character,
570 action: {
571 type: 'replace',
572 options: {
573 peri: character,
574 selectPeri: false
575 }
576 }
577 };
578 // In some cases the label for the character isn't the same as the
579 // character that gets inserted (e.g. Hebrew vowels)
580 } else if ( character && 0 in character && 1 in character ) {
581 character = {
582 label: character[ 0 ],
583 action: {
584 type: 'replace',
585 options: {
586 peri: character[ 1 ],
587 selectPeri: false
588 }
589 }
590 };
591 }
592 if ( character && 'action' in character && 'label' in character ) {
593 actions[ character.label ] = character.action;
594 // eslint-disable-next-line mediawiki/msg-doc
595 var title = character.titleMsg ? mw.msg( character.titleMsg ) : character.title;
596 return mw.html.element(
597 'span',
598 { rel: character.label, title: title || false },
599 character.label
600 );
601 }
602 mw.log( 'A character for the toolbar was undefined. This is not supposed to happen. Double check the config.' );
603 // bug 31673; also an additional fix for bug 24208...
604 return '';
605 },
606 buildTab: function ( context, id, section ) {
607 var selected = $.cookie( 'wikiEditor-' + context.instance + '-toolbar-section' );
608 // Re-save cookie
609 if ( selected !== null ) {
610 $.cookie( 'wikiEditor-' + context.instance + '-toolbar-section', selected, { expires: 30, path: '/' } );
611 }
612 var $link =
613 $( '<a>' )
614 .addClass( selected === id ? 'current' : null )
615 .addClass( 'skin-invert' )
616 .attr( {
617 tabindex: 0,
618 role: 'button',
619 'aria-expanded': ( selected === id ).toString(),
620 'aria-controls': 'wikiEditor-section-' + id
621 } )
622 .text( $.wikiEditor.autoMsg( section, 'label' ) )
623 .data( 'context', context )
624 .on( 'mouseup', function () {
625 $( this ).trigger( 'blur' );
626 } )
627 .on( 'mousedown', function ( e ) {
628 // No dragging!
629 e.preventDefault();
630 return false;
631 } )
632 .on( 'click keydown', function ( e ) {
633 if (
634 e.type !== 'click' &&
635 ( e.type !== 'keydown' || e.key !== 'Enter' )
636 ) {
637 return;
638 }
639 // We have to set aria-pressed over here, as NVDA wont recognize it
640 // if we do it in the below .each as it seems
641 $( this ).attr( 'aria-pressed', 'true' );
642 $( '.tab > a' ).each( function ( i, elem ) {
643 if ( elem !== e.target ) {
644 $( elem ).attr( 'aria-pressed', 'false' );
645 }
646 } );
647 var $sections = $( this ).data( 'context' ).$ui.find( '.sections' );
648 var $section = $sections.find( '.section-' + $( this ).parent().attr( 'rel' ) );
649 // eslint-disable-next-line no-jquery/no-class-state
650 var show = !$section.hasClass( 'section-visible' );
651 $sections.find( '.section-visible' )
652 .removeClass( 'section-visible' )
653 .addClass( 'section-hidden' );
654
655 $( this )
656 .attr( 'aria-expanded', 'false' )
657 .parent().parent().find( 'a' ).removeClass( 'current' );
658 if ( show ) {
659 $section
660 .removeClass( 'section-hidden' )
661 .attr( 'aria-expanded', 'true' )
662 .addClass( 'section-visible' );
663
664 $( this ).attr( 'aria-expanded', 'true' )
665 .addClass( 'current' );
666 }
667
668 // Save the currently visible section
669 $.cookie(
670 'wikiEditor-' + $( this ).data( 'context' ).instance + '-toolbar-section',
671 show ? $section.attr( 'rel' ) : null,
672 { expires: 30, path: '/' }
673 );
674
675 e.preventDefault();
676 return false;
677 } );
678 return $( '<span>' )
679 .attr( {
680 class: 'tab tab-' + id,
681 rel: id
682 } )
683 .append( $link );
684 },
685 buildSection: function ( context, id, section ) {
686 var $section = $( '<div>' ).attr( {
687 class: section.type + ' section section-' + id,
688 rel: id,
689 id: 'wikiEditor-section-' + id
690 } );
691 var selected = $.cookie( 'wikiEditor-' + context.instance + '-toolbar-section' );
692 var show = selected === id;
693
694 toolbarModule.fn.reallyBuildSection( context, id, section, $section, section.deferLoad );
695
696 // Show or hide section
697 if ( id !== 'main' && id !== 'secondary' ) {
698 $section
699 .attr( 'aria-expanded', show.toString() )
700 .addClass( show ? 'section-visible' : 'section-hidden' );
701 }
702 return $section;
703 },
704 reallyBuildSection: function ( context, id, section, $section, deferLoad ) {
705 context.$textarea.trigger( 'wikiEditor-toolbar-buildSection-' + $section.attr( 'rel' ), [ section ] );
706 switch ( section.type ) {
707 case 'toolbar':
708 if ( 'groups' in section ) {
709 for ( var group in section.groups ) {
710 $section.append(
711 toolbarModule.fn.buildGroup( context, group, section.groups[ group ] )
712 );
713 }
714 }
715 break;
716 case 'booklet':
717 var $pages = $( '<div>' )
718 .addClass( 'pages' )
719 .attr( {
720 tabindex: '0',
721 role: 'listbox'
722 } )
723 .on( 'keydown', function ( event ) {
724 var $selected = $pages.children().filter( function () {
725 return $( this ).css( 'display' ) !== 'none';
726 } );
727 $.wikiEditor.modules.toolbar.fn.handleKeyDown( $selected.children().first(), event, $pages );
728 } );
729 var $index = $( '<div>' )
730 .addClass( 'index' )
731 .attr( {
732 tabindex: '0',
733 role: 'listbox'
734 } )
735 .on( 'keydown', function ( event ) {
736 $.wikiEditor.modules.toolbar.fn.handleKeyDown( $index, event, $index );
737 } );
738 if ( 'pages' in section ) {
739 for ( var page in section.pages ) {
740 $pages.append(
741 toolbarModule.fn.buildPage( context, page, section.pages[ page ], deferLoad )
742 );
743 $index.append(
744 toolbarModule.fn.buildBookmark( context, page, section.pages[ page ] )
745 );
746 }
747 }
748 $section.append( $index, $pages );
749 toolbarModule.fn.updateBookletSelection( context, id, $pages, $index );
750 break;
751 }
752 },
753 updateBookletSelection: function ( context, id, $pages, $index ) {
754 var cookie = 'wikiEditor-' + context.instance + '-booklet-' + id + '-page',
755 selected = $.cookie( cookie );
756 // Re-save cookie
757 if ( selected !== null ) {
758 $.cookie( cookie, selected, { expires: 30, path: '/' } );
759 }
760 var $selectedIndex = $index.find( '*[rel="' + selected + '"]' );
761 if ( $selectedIndex.length === 0 ) {
762 $selectedIndex = $index.children().eq( 0 );
763 selected = $selectedIndex.attr( 'rel' );
764 }
765 $pages.children().hide();
766 $pages.find( '*[rel="' + selected + '"]' ).show().trigger( 'loadPage' );
767 $index.children().removeClass( 'current' );
768 $selectedIndex.addClass( 'current' );
769 },
770 build: function ( context, config ) {
771 var $tabs = $( '<div>' ).addClass( 'tabs' ).appendTo( context.modules.toolbar.$toolbar ),
772 $sections = $( '<div>' ).addClass( 'sections' ).appendTo( context.modules.toolbar.$toolbar );
773 context.modules.toolbar.$toolbar.append( $( '<div>' ).css( 'clear', 'both' ) );
774 for ( var section in config ) {
775 if ( section === 'main' || section === 'secondary' ) {
776 context.modules.toolbar.$toolbar.prepend(
777 toolbarModule.fn.buildSection( context, section, config[ section ] )
778 );
779 } else {
780 $sections.append( toolbarModule.fn.buildSection( context, section, config[ section ] ) );
781 $tabs.append( toolbarModule.fn.buildTab( context, section, config[ section ] ) );
782 }
783 }
784 setTimeout( function () {
785 context.$textarea.trigger( 'wikiEditor-toolbar-doneInitialSections' );
786 // Use hook for attaching new toolbar tools to avoid race conditions
787 mw.hook( 'wikiEditor.toolbarReady' ).fire( context.$textarea );
788 } );
789 },
790 handleKeyDown: function ( $element, event, $parent ) {
791 var $currentItem = $element.find( '.wikiEditor-character-highlighted' ),
792 optionOffset = $parent.find( '.wikiEditor-character-highlighted' ).offset(),
793 optionTop = optionOffset ? optionOffset.top : 0,
794 selectTop = $parent.offset().top;
795
796 var $nextItem;
797 switch ( event.keyCode ) {
798 // Up arrow
799 case 38:
800 if ( $currentItem.length ) {
801 $currentItem.removeClass( 'wikiEditor-character-highlighted' );
802 $nextItem = $currentItem.prev();
803 $nextItem = $nextItem.length ? $nextItem : $currentItem;
804 $nextItem.addClass( 'wikiEditor-character-highlighted' );
805 } else {
806 $element.children().first().addClass( 'wikiEditor-character-highlighted' );
807 }
808 event.preventDefault();
809 event.stopPropagation();
810 break;
811 // Down arrow
812 case 40:
813 if ( $currentItem.length ) {
814 $currentItem.removeClass( 'wikiEditor-character-highlighted' );
815 $nextItem = $currentItem.next();
816 $nextItem = $nextItem.length ? $nextItem : $currentItem;
817 $nextItem.addClass( 'wikiEditor-character-highlighted' );
818 } else {
819 $element.children().first().addClass( 'wikiEditor-character-highlighted' );
820 }
821 event.preventDefault();
822 event.stopPropagation();
823 break;
824 // Enter
825 case 13:
826 $currentItem.trigger( 'click' );
827 break;
828 }
829 $parent.scrollTop( $parent.scrollTop() + ( optionTop - selectTop ) );
830 }
831 }
832 };
833
834 module.exports = toolbarModule;