/** * Toolbar module for wikiEditor */ ( function () { $.wikiEditor.modules.toolbar = { /** * API accessible functions */ api: { addToToolbar: function ( context, data ) { var section, type, i, group, $group, $section, $sections, $tabs, tool, $pages, $index, page, $table, $characters, actions, $divSections, $visibleSection, smooth = true; for ( type in data ) { switch ( type ) { case 'sections': $sections = context.modules.toolbar.$toolbar.find( 'div.sections' ); $tabs = context.modules.toolbar.$toolbar.find( 'div.tabs' ); for ( section in data[ type ] ) { if ( section === 'main' ) { // Section context.modules.toolbar.$toolbar.prepend( $.wikiEditor.modules.toolbar.fn.buildSection( context, section, data[ type ][ section ] ) ); continue; } // Section $sections.append( $.wikiEditor.modules.toolbar.fn.buildSection( context, section, data[ type ][ section ] ) ); // Tab $tabs.append( $.wikiEditor.modules.toolbar.fn.buildTab( context, section, data[ type ][ section ] ) ); } break; case 'groups': if ( !( 'section' in data ) ) { continue; } $section = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section' ); for ( group in data[ type ] ) { // Group $section.append( $.wikiEditor.modules.toolbar.fn.buildGroup( context, group, data[ type ][ group ] ) ); } smooth = false; break; case 'tools': if ( !( 'section' in data && 'group' in data ) ) { continue; } $group = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section ' + 'div[rel="' + data.group + '"].group' ); for ( tool in data[ type ] ) { // Tool $group.append( $.wikiEditor.modules.toolbar.fn.buildTool( context, tool, data[ type ][ tool ] ) ); } if ( $group.children().length ) { $group.removeClass( 'empty' ); } smooth = false; break; case 'pages': if ( !( 'section' in data ) ) { continue; } $pages = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section .pages' ); $index = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section .index' ); for ( page in data[ type ] ) { // Page $pages.append( $.wikiEditor.modules.toolbar.fn.buildPage( context, page, data[ type ][ page ] ) ); // Index $index.append( $.wikiEditor.modules.toolbar.fn.buildBookmark( context, page, data[ type ][ page ] ) ); } $.wikiEditor.modules.toolbar.fn.updateBookletSelection( context, data.section, $pages, $index ); smooth = false; break; case 'rows': if ( !( 'section' in data && 'page' in data ) ) { continue; } $table = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section ' + 'div[rel="' + data.page + '"].page table' ); for ( i = 0; i < data.rows.length; i++ ) { // Row $table.append( $.wikiEditor.modules.toolbar.fn.buildRow( context, data.rows[ i ] ) ); } smooth = false; break; case 'characters': if ( !( 'section' in data && 'page' in data ) ) { continue; } $characters = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section ' + 'div[rel="' + data.page + '"].page div' ); actions = $characters.data( 'actions' ); for ( i = 0; i < data.characters.length; i++ ) { // Character $characters.append( $( $.wikiEditor.modules.toolbar.fn.buildCharacter( data.characters[ i ], actions ) ) .mousedown( function ( e ) { // No dragging! e.preventDefault(); return false; } ) .click( function ( e ) { $.wikiEditor.modules.toolbar.fn.doAction( $( this ).parent().data( 'context' ), $( this ).parent().data( 'actions' )[ $( this ).attr( 'rel' ) ] ); e.preventDefault(); return false; } ) ); } smooth = false; break; default: break; } } // Fix div.section size after adding things; if smooth is true uses a smooth // animation, otherwise just change height (breaking any ongoing animation) $divSections = context.modules.toolbar.$toolbar.find( 'div.sections' ); $visibleSection = $divSections.find( '.section-visible' ); if ( $visibleSection.length ) { if ( smooth ) { $divSections.animate( { height: $visibleSection.outerHeight() }, 'fast' ); } else { $divSections.height( $visibleSection.outerHeight() ); } } }, removeFromToolbar: function ( context, data ) { var index, $group, tab, target, group; if ( typeof data.section === 'string' ) { // Section tab = 'div.tabs span[rel="' + data.section + '"].tab'; target = 'div[rel="' + data.section + '"].section'; 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 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(); $.wikiEditor.modules.toolbar.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 ) { $group = context.modules.toolbar.$toolbar.find( group ); if ( $group.children().length === 0 ) { $group.addClass( 'empty' ); } } } } }, /** * Event handlers */ evt: { /** * @param {Object} context */ resize: function ( context ) { context.$ui.find( '.sections' ).height( context.$ui.find( '.sections .section-visible' ).outerHeight() ); } }, /** * 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 = $( '