/** * Toolbar module for wikiEditor * * @memberof module:ext.wikiEditor */ const toolbarModule = { /** * API accessible functions */ api: { addToToolbar: function ( context, data ) { for ( const type in data ) { switch ( type ) { case 'sections': { const $sections = context.modules.toolbar.$toolbar.find( 'div.sections' ); const $tabs = context.modules.toolbar.$toolbar.find( 'div.tabs' ); for ( const section in data[ type ] ) { if ( section === 'main' || section === 'secondary' ) { // Section context.modules.toolbar.$toolbar.prepend( toolbarModule.fn.buildSection( context, section, data[ type ][ section ] ) ); continue; } // Section $sections.append( toolbarModule.fn.buildSection( context, section, data[ type ][ section ] ) ); // Tab $tabs.append( toolbarModule.fn.buildTab( context, section, data[ type ][ section ] ) ); } break; } case 'groups': { if ( !( 'section' in data ) ) { continue; } const $section = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section' ); for ( const group in data[ type ] ) { // Group $section.append( toolbarModule.fn.buildGroup( context, group, data[ type ][ group ] ) ); } break; } case 'tools': { if ( !( 'section' in data && 'group' in data ) ) { continue; } const $group = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section ' + 'div[rel="' + data.group + '"].group' ); for ( const tool in data[ type ] ) { // Tool $group.append( toolbarModule.fn.buildTool( context, tool, data[ type ][ tool ] ) ); } if ( $group.children().length ) { $group.removeClass( 'empty' ); } break; } case 'pages': { if ( !( 'section' in data ) ) { continue; } const $pages = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section .pages' ); const $index = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section .index' ); for ( const page in data[ type ] ) { // Page $pages.append( toolbarModule.fn.buildPage( context, page, data[ type ][ page ] ) ); // Index $index.append( toolbarModule.fn.buildBookmark( context, page, data[ type ][ page ] ) ); } toolbarModule.fn.updateBookletSelection( context, data.section, $pages, $index ); break; } case 'rows': { if ( !( 'section' in data && 'page' in data ) ) { continue; } const $table = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section ' + 'div[rel="' + data.page + '"].page table' ); for ( let i = 0; i < data.rows.length; i++ ) { // Row $table.append( toolbarModule.fn.buildRow( context, data.rows[ i ] ) ); } break; } case 'characters': { if ( !( 'section' in data && 'page' in data ) ) { continue; } const $characters = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section ' + 'div[rel="' + data.page + '"].page div' ); const actions = $characters.data( 'actions' ); for ( let i = 0; i < data.characters.length; i++ ) { // Character $characters.append( $( toolbarModule.fn.buildCharacter( data.characters[ i ], actions ) ) .on( 'mousedown', ( e ) => { // No dragging! e.preventDefault(); return false; } ) .on( 'click', ( e ) => { const $character = $( e.target ); toolbarModule.fn.doAction( $character.parent().data( 'context' ), $character.parent().data( 'actions' )[ $character.attr( 'rel' ) ] ); e.preventDefault(); return false; } ) ); } break; } default: break; } } }, removeFromToolbar: function ( context, data ) { if ( typeof data.section === 'string' ) { // Section const tab = 'div.tabs span[rel="' + data.section + '"].tab'; let target = 'div[rel="' + data.section + '"].section'; let group = null; if ( typeof data.group === 'string' ) { // Toolbar group target += ' div[rel="' + data.group + '"].group'; if ( typeof data.tool === 'string' ) { // Save for later checking if empty group = target; // Tool target = target + ' [rel="' + data.tool + '"].tool'; } } else if ( typeof data.page === 'string' ) { // Booklet page const index = target + ' div.index div[rel="' + data.page + '"]'; target += ' div.pages div[rel="' + data.page + '"].page'; if ( typeof data.character === 'string' ) { // Character target += ' span[rel="' + data.character + '"]'; } else if ( typeof data.row === 'number' ) { // Table row target += ' table tr:not(:has(th)):eq(' + data.row + ')'; } else { // Just a page, remove the index too! context.modules.toolbar.$toolbar.find( index ).remove(); toolbarModule.fn.updateBookletSelection( context, data.section, context.modules.toolbar.$toolbar.find( target ), context.modules.toolbar.$toolbar.find( index ) ); } } else { // Just a section, remove the tab too! context.modules.toolbar.$toolbar.find( tab ).remove(); } context.modules.toolbar.$toolbar.find( target ).remove(); // Hide empty groups if ( group ) { const $group = context.modules.toolbar.$toolbar.find( group ); if ( $group.children().length === 0 ) { $group.addClass( 'empty' ); } } } } }, /** * Internally used functions */ fn: { /** * Creates a toolbar module within a wikiEditor * * @param {Object} context Context object of editor to create module in * @param {Object} config Configuration object to create module from */ create: function ( context, config ) { if ( '$toolbar' in context.modules.toolbar ) { return; } context.modules.toolbar.$toolbar = $( '