2010-09-15 03:08:35 +00:00
|
|
|
/**
|
|
|
|
* Toolbar module for wikiEditor
|
|
|
|
*/
|
2012-10-11 00:10:09 +00:00
|
|
|
( function ( mw, $ ) { $.wikiEditor.modules.toolbar = {
|
2010-09-15 03:08:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* API accessible functions
|
|
|
|
*/
|
|
|
|
api : {
|
|
|
|
addToToolbar : function( context, data ) {
|
2011-05-13 14:34:43 +00:00
|
|
|
|
2011-07-02 09:09:05 +00:00
|
|
|
var smooth = true, type, i;
|
2011-05-13 14:34:43 +00:00
|
|
|
|
2010-09-15 03:08:35 +00:00
|
|
|
for ( type in data ) {
|
|
|
|
switch ( type ) {
|
|
|
|
case 'sections':
|
2011-06-06 23:18:13 +00:00
|
|
|
var $sections = context.modules.toolbar.$toolbar.find( 'div.sections' ),
|
|
|
|
$tabs = context.modules.toolbar.$toolbar.find( 'div.tabs' );
|
|
|
|
for ( var section in data[type] ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-07-02 09:17:01 +00:00
|
|
|
var $section = context.modules.toolbar.$toolbar.find( 'div[rel="' + data.section + '"].section' );
|
2011-06-06 23:18:13 +00:00
|
|
|
for ( var group in data[type] ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
// Group
|
|
|
|
$section.append(
|
|
|
|
$.wikiEditor.modules.toolbar.fn.buildGroup( context, group, data[type][group] )
|
|
|
|
);
|
|
|
|
}
|
2011-05-13 14:34:43 +00:00
|
|
|
smooth = false;
|
2010-09-15 03:08:35 +00:00
|
|
|
break;
|
|
|
|
case 'tools':
|
|
|
|
if ( ! ( 'section' in data && 'group' in data ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var $group = context.modules.toolbar.$toolbar.find(
|
2011-07-03 22:42:28 +00:00
|
|
|
'div[rel="' + data.section + '"].section ' + 'div[rel="' + data.group + '"].group'
|
2010-09-15 03:08:35 +00:00
|
|
|
);
|
2011-06-06 23:18:13 +00:00
|
|
|
for ( var tool in data[type] ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
// Tool
|
2010-10-04 21:08:41 +00:00
|
|
|
$group.append( $.wikiEditor.modules.toolbar.fn.buildTool( context, tool, data[type][tool] ) );
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
|
|
|
if ( $group.children().length ) {
|
|
|
|
$group.show();
|
|
|
|
}
|
2011-05-13 14:34:43 +00:00
|
|
|
smooth = false;
|
2010-09-15 03:08:35 +00:00
|
|
|
break;
|
|
|
|
case 'pages':
|
|
|
|
if ( ! ( 'section' in data ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var $pages = context.modules.toolbar.$toolbar.find(
|
2011-07-02 09:17:01 +00:00
|
|
|
'div[rel="' + data.section + '"].section .pages'
|
2010-09-15 03:08:35 +00:00
|
|
|
);
|
|
|
|
var $index = context.modules.toolbar.$toolbar.find(
|
2011-07-02 09:17:01 +00:00
|
|
|
'div[rel="' + data.section + '"].section .index'
|
2010-09-15 03:08:35 +00:00
|
|
|
);
|
2011-06-06 23:18:13 +00:00
|
|
|
for ( var page in data[type] ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
// 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, page, $pages, $index );
|
2011-05-13 14:34:43 +00:00
|
|
|
smooth = false;
|
2010-09-15 03:08:35 +00:00
|
|
|
break;
|
|
|
|
case 'rows':
|
|
|
|
if ( ! ( 'section' in data && 'page' in data ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var $table = context.modules.toolbar.$toolbar.find(
|
2011-07-03 22:42:28 +00:00
|
|
|
'div[rel="' + data.section + '"].section ' + 'div[rel="' + data.page + '"].page table'
|
2010-09-15 03:08:35 +00:00
|
|
|
);
|
2011-07-02 09:09:05 +00:00
|
|
|
for ( i = 0; i < data.rows.length; i++ ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
// Row
|
2011-07-02 09:09:05 +00:00
|
|
|
$table.append( $.wikiEditor.modules.toolbar.fn.buildRow( context, data.rows[i] ) );
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
2011-05-13 14:34:43 +00:00
|
|
|
smooth = false;
|
2010-09-15 03:08:35 +00:00
|
|
|
break;
|
|
|
|
case 'characters':
|
|
|
|
if ( ! ( 'section' in data && 'page' in data ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-07-02 08:54:01 +00:00
|
|
|
var $characters = context.modules.toolbar.$toolbar.find(
|
2011-07-02 09:17:01 +00:00
|
|
|
'div[rel="' + data.section + '"].section ' + 'div[rel="' + data.page + '"].page div'
|
2010-09-15 03:08:35 +00:00
|
|
|
);
|
|
|
|
var actions = $characters.data( 'actions' );
|
2011-07-07 20:54:24 +00:00
|
|
|
for ( i = 0; i < data.characters.length; i++ ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
// Character
|
|
|
|
$characters
|
|
|
|
.append(
|
2011-07-02 09:09:05 +00:00
|
|
|
$( $.wikiEditor.modules.toolbar.fn.buildCharacter( data.characters[i], actions ) )
|
2010-09-15 03:08:35 +00:00
|
|
|
.mousedown( function( e ) {
|
2010-10-05 20:54:38 +00:00
|
|
|
context.fn.saveCursorAndScrollTop();
|
2010-09-15 03:08:35 +00:00
|
|
|
// 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;
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
}
|
2011-05-13 14:34:43 +00:00
|
|
|
smooth = false;
|
2010-09-15 03:08:35 +00:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2011-05-13 14:34:43 +00:00
|
|
|
|
|
|
|
// Fix div.section size after adding things; if smooth is true uses a smooth
|
|
|
|
// animation, otherwise just change height (breaking any ongoing animation)
|
2011-06-06 23:18:13 +00:00
|
|
|
var $divSections = context.modules.toolbar.$toolbar.find( 'div.sections' );
|
2011-07-02 08:54:01 +00:00
|
|
|
var $visibleSection = $divSections.find( '.section:visible' );
|
2011-06-06 23:18:13 +00:00
|
|
|
if ( $visibleSection.size() ) {
|
2011-05-13 14:34:43 +00:00
|
|
|
if ( smooth ) {
|
2011-06-06 23:18:13 +00:00
|
|
|
$divSections.animate( { 'height': $visibleSection.outerHeight() }, 'fast' );
|
2011-05-13 14:34:43 +00:00
|
|
|
} else {
|
2011-06-06 23:18:13 +00:00
|
|
|
$divSections.height( $visibleSection.outerHeight() );
|
2011-05-13 14:34:43 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-15 03:08:35 +00:00
|
|
|
},
|
|
|
|
removeFromToolbar : function( context, data ) {
|
|
|
|
if ( typeof data.section == 'string' ) {
|
|
|
|
// Section
|
2011-07-02 09:17:01 +00:00
|
|
|
var tab = 'div.tabs span[rel="' + data.section + '"].tab';
|
|
|
|
var target = 'div[rel="' + data.section + '"].section';
|
2010-09-15 03:08:35 +00:00
|
|
|
var group = null;
|
|
|
|
if ( typeof data.group == 'string' ) {
|
|
|
|
// Toolbar group
|
2011-07-02 09:17:01 +00:00
|
|
|
target += ' div[rel="' + data.group + '"].group';
|
2010-09-15 03:08:35 +00:00
|
|
|
if ( typeof data.tool == 'string' ) {
|
|
|
|
// Save for later checking if empty
|
|
|
|
group = target;
|
|
|
|
// Tool
|
2012-06-25 16:28:23 +00:00
|
|
|
target += ' a[rel="' + data.tool + '"].tool';
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
|
|
|
} else if ( typeof data.page == 'string' ) {
|
|
|
|
// Booklet page
|
2011-07-02 09:17:01 +00:00
|
|
|
var index = target + ' div.index div[rel="' + data.page + '"]';
|
|
|
|
target += ' div.pages div[rel="' + data.page + '"].page';
|
2010-09-15 03:08:35 +00:00
|
|
|
if ( typeof data.character == 'string' ) {
|
|
|
|
// Character
|
2011-07-02 09:17:01 +00:00
|
|
|
target += ' span[rel="' + data.character + '"]';
|
2010-09-15 03:08:35 +00:00
|
|
|
} 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,
|
|
|
|
null,
|
|
|
|
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 ) {
|
2011-06-06 23:18:13 +00:00
|
|
|
var $group = context.modules.toolbar.$toolbar.find( group );
|
|
|
|
if ( $group.children().length === 0 ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
$group.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Event handlers
|
|
|
|
*/
|
|
|
|
evt: {
|
|
|
|
resize: function( context, event ) {
|
|
|
|
context.$ui.find( '.sections' ).height( context.$ui.find( '.sections .section-visible' ).outerHeight() );
|
|
|
|
},
|
|
|
|
tocCollapse: function( context, event ) {
|
|
|
|
$.wikiEditor.modules.toolbar.evt.resize( context, event );
|
|
|
|
},
|
|
|
|
tocExpand: function( context, event ) {
|
|
|
|
$.wikiEditor.modules.toolbar.evt.resize( context, event );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2011-06-06 23:18:13 +00:00
|
|
|
context.modules.toolbar.$toolbar = $( '<div/>' )
|
2010-09-15 03:08:35 +00:00
|
|
|
.addClass( 'wikiEditor-ui-toolbar' )
|
|
|
|
.attr( 'id', 'wikiEditor-ui-toolbar' );
|
|
|
|
$.wikiEditor.modules.toolbar.fn.build( context, config );
|
|
|
|
context.$ui.find( '.wikiEditor-ui-top' ).append( context.modules.toolbar.$toolbar );
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Performs an operation based on parameters
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} action
|
|
|
|
* @param {Object} source
|
|
|
|
*/
|
|
|
|
doAction : function( context, action, source ) {
|
|
|
|
switch ( action.type ) {
|
|
|
|
case 'replace':
|
|
|
|
case 'encapsulate':
|
|
|
|
var parts = {
|
|
|
|
'pre' : $.wikiEditor.autoMsg( action.options, 'pre' ),
|
|
|
|
'peri' : $.wikiEditor.autoMsg( action.options, 'peri' ),
|
|
|
|
'post' : $.wikiEditor.autoMsg( action.options, 'post' )
|
|
|
|
};
|
|
|
|
var replace = action.type == 'replace';
|
|
|
|
if ( 'regex' in action.options && 'regexReplace' in action.options ) {
|
|
|
|
var selection = context.$textarea.textSelection( 'getSelection' );
|
2011-06-06 23:18:13 +00:00
|
|
|
if ( selection !== '' && selection.match( action.options.regex ) ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
parts.peri = selection.replace( action.options.regex,
|
|
|
|
action.options.regexReplace );
|
|
|
|
parts.pre = parts.post = '';
|
|
|
|
replace = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
context.$textarea.textSelection(
|
|
|
|
'encapsulateSelection',
|
|
|
|
$.extend( {}, action.options, parts, { 'replace': replace } )
|
|
|
|
);
|
2011-06-06 23:18:13 +00:00
|
|
|
if ( context.$iframe !== undefined ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
context.$iframe[0].contentWindow.focus();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'callback':
|
|
|
|
if ( typeof action.execute == 'function' ) {
|
|
|
|
action.execute( context );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'dialog':
|
|
|
|
context.fn.saveSelection();
|
|
|
|
context.$textarea.wikiEditor( 'openDialog', action.module );
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
buildGroup : function( context, id, group ) {
|
2011-06-06 23:18:13 +00:00
|
|
|
var $group = $( '<div/>' ).attr( { 'class' : 'group group-' + id, 'rel' : id } );
|
2010-09-15 03:08:35 +00:00
|
|
|
var label = $.wikiEditor.autoMsg( group, 'label' );
|
|
|
|
if ( label ) {
|
2013-04-08 17:13:51 +00:00
|
|
|
$group.append( '<span class="label">' + label + '</div>' );
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
|
|
|
var empty = true;
|
|
|
|
if ( 'tools' in group ) {
|
2011-06-06 23:18:13 +00:00
|
|
|
for ( var tool in group.tools ) {
|
|
|
|
tool = $.wikiEditor.modules.toolbar.fn.buildTool( context, tool, group.tools[tool] );
|
2010-09-15 03:08:35 +00:00
|
|
|
if ( tool ) {
|
|
|
|
// Consider a group with only hidden tools empty as well
|
|
|
|
// .is( ':visible' ) always returns false because tool is not attached to the DOM yet
|
|
|
|
empty = empty && tool.css( 'display' ) == 'none';
|
|
|
|
$group.append( tool );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( empty ) {
|
|
|
|
$group.hide();
|
|
|
|
}
|
|
|
|
return $group;
|
|
|
|
},
|
|
|
|
buildTool : function( context, id, tool ) {
|
|
|
|
if ( 'filters' in tool ) {
|
2011-07-02 09:09:05 +00:00
|
|
|
for ( var i = 0; i < tool.filters.length; i++ ) {
|
|
|
|
if ( $( tool.filters[i] ).size() === 0 ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var label = $.wikiEditor.autoMsg( tool, 'label' );
|
|
|
|
switch ( tool.type ) {
|
|
|
|
case 'button':
|
|
|
|
var src = $.wikiEditor.autoIcon( tool.icon, $.wikiEditor.imgPath + 'toolbar/' );
|
|
|
|
var $button = null;
|
|
|
|
if ( 'offset' in tool ) {
|
|
|
|
var offsetOrIcon = $.wikiEditor.autoIconOrOffset( tool.icon, tool.offset,
|
|
|
|
$.wikiEditor.imgPath + 'toolbar/'
|
|
|
|
);
|
|
|
|
if ( typeof offsetOrIcon == 'object' ) {
|
2012-06-02 11:47:01 +00:00
|
|
|
$button = $( '<a/>' )
|
2010-09-15 03:08:35 +00:00
|
|
|
.attr( {
|
2012-06-02 11:47:01 +00:00
|
|
|
'href' : '#',
|
2010-09-15 03:08:35 +00:00
|
|
|
'alt' : label,
|
|
|
|
'title' : label,
|
|
|
|
'rel' : id,
|
2010-10-01 21:58:04 +00:00
|
|
|
'class' : 'tool tool-button wikiEditor-toolbar-spritedButton'
|
2010-09-15 03:08:35 +00:00
|
|
|
} )
|
|
|
|
.text( label )
|
|
|
|
.css( 'backgroundPosition', offsetOrIcon[0] + 'px ' + offsetOrIcon[1] + 'px' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !$button ) {
|
2011-06-06 23:18:13 +00:00
|
|
|
$button = $( '<img/>' )
|
2010-09-15 03:08:35 +00:00
|
|
|
.attr( {
|
|
|
|
'src' : src,
|
|
|
|
'width' : 22,
|
|
|
|
'height' : 22,
|
|
|
|
'alt' : label,
|
|
|
|
'title' : label,
|
|
|
|
'rel' : id,
|
|
|
|
'class' : 'tool tool-button'
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
if ( 'action' in tool ) {
|
|
|
|
$button
|
|
|
|
.data( 'action', tool.action )
|
|
|
|
.data( 'context', context )
|
|
|
|
.mousedown( function( e ) {
|
2010-10-05 20:54:38 +00:00
|
|
|
context.fn.saveCursorAndScrollTop();
|
2010-09-15 03:08:35 +00:00
|
|
|
// No dragging!
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} )
|
|
|
|
.click( function( e ) {
|
|
|
|
$.wikiEditor.modules.toolbar.fn.doAction(
|
|
|
|
$(this).data( 'context' ), $(this).data( 'action' ), $(this)
|
|
|
|
);
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
return $button;
|
|
|
|
case 'select':
|
2011-06-06 23:18:13 +00:00
|
|
|
var $select = $( '<div/>' )
|
2010-09-15 03:08:35 +00:00
|
|
|
.attr( { 'rel' : id, 'class' : 'tool tool-select' } );
|
2011-06-06 23:18:13 +00:00
|
|
|
var $options = $( '<div/>' ).addClass( 'options' );
|
2010-09-15 03:08:35 +00:00
|
|
|
if ( 'list' in tool ) {
|
2011-06-06 23:18:13 +00:00
|
|
|
for ( var option in tool.list ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
var optionLabel = $.wikiEditor.autoMsg( tool.list[option], 'label' );
|
|
|
|
$options.append(
|
2011-06-06 23:18:13 +00:00
|
|
|
$( '<a/>' )
|
2010-09-15 03:08:35 +00:00
|
|
|
.data( 'action', tool.list[option].action )
|
|
|
|
.data( 'context', context )
|
|
|
|
.mousedown( function( e ) {
|
2010-10-05 20:54:38 +00:00
|
|
|
context.fn.saveCursorAndScrollTop();
|
2010-09-15 03:08:35 +00:00
|
|
|
// No dragging!
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} )
|
|
|
|
.click( function( e ) {
|
|
|
|
$.wikiEditor.modules.toolbar.fn.doAction(
|
|
|
|
$(this).data( 'context' ), $(this).data( 'action' ), $(this)
|
|
|
|
);
|
|
|
|
// Hide the dropdown
|
|
|
|
// Sanity check: if this somehow gets called while the dropdown
|
|
|
|
// is hidden, don't show it
|
|
|
|
if ( $(this).parent().is( ':visible' ) ) {
|
|
|
|
$(this).parent().animate( { 'opacity': 'toggle' }, 'fast' );
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} )
|
|
|
|
.text( optionLabel )
|
|
|
|
.addClass( 'option' )
|
|
|
|
.attr( { 'rel': option, 'href': '#' } )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2011-06-06 23:18:13 +00:00
|
|
|
$select.append( $( '<div/>' ).addClass( 'menu' ).append( $options ) );
|
|
|
|
$select.append( $( '<a/>' )
|
2010-09-15 03:08:35 +00:00
|
|
|
.addClass( 'label' )
|
|
|
|
.text( label )
|
|
|
|
.data( 'options', $options )
|
|
|
|
.attr( 'href', '#' )
|
|
|
|
.mousedown( function( e ) {
|
|
|
|
// No dragging!
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} )
|
|
|
|
.click( function( e ) {
|
|
|
|
$(this).data( 'options' ).animate( { 'opacity': 'toggle' }, 'fast' );
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
return $select;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
buildBookmark : function( context, id, page ) {
|
2011-11-20 15:02:20 +00:00
|
|
|
var label = $.wikiEditor.autoMsg( page, 'label' );
|
2011-06-06 23:18:13 +00:00
|
|
|
return $( '<div/>' )
|
2010-09-15 03:08:35 +00:00
|
|
|
.text( label )
|
|
|
|
.attr( 'rel', id )
|
|
|
|
.data( 'context', context )
|
|
|
|
.mousedown( function( e ) {
|
2011-11-20 15:02:20 +00:00
|
|
|
context.fn.saveCursorAndScrollTop();
|
2010-09-15 03:08:35 +00:00
|
|
|
// No dragging!
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} )
|
|
|
|
.click( function( event ) {
|
|
|
|
$(this).parent().parent().find( '.page' ).hide();
|
|
|
|
$(this).parent().parent().find( '.page-' + $(this).attr( 'rel' ) ).show();
|
|
|
|
$(this).siblings().removeClass( 'current' );
|
|
|
|
$(this).addClass( 'current' );
|
|
|
|
var section = $(this).parent().parent().attr( 'rel' );
|
|
|
|
$.cookie(
|
|
|
|
'wikiEditor-' + $(this).data( 'context' ).instance + '-booklet-' + section + '-page',
|
|
|
|
$(this).attr( 'rel' ),
|
|
|
|
{ expires: 30, path: '/' }
|
|
|
|
);
|
2011-11-20 15:02:20 +00:00
|
|
|
context.fn.restoreCursorAndScrollTop();
|
2010-09-15 03:08:35 +00:00
|
|
|
// No dragging!
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
2011-06-06 23:18:13 +00:00
|
|
|
} );
|
2010-09-15 03:08:35 +00:00
|
|
|
},
|
|
|
|
buildPage : function( context, id, page ) {
|
2011-07-02 09:09:05 +00:00
|
|
|
var html;
|
2011-06-06 23:18:13 +00:00
|
|
|
var $page = $( '<div/>' ).attr( {
|
2010-09-15 03:08:35 +00:00
|
|
|
'class' : 'page page-' + id,
|
|
|
|
'rel' : id
|
|
|
|
} );
|
|
|
|
switch ( page.layout ) {
|
|
|
|
case 'table':
|
|
|
|
$page.addClass( 'page-table' );
|
2011-07-02 09:09:05 +00:00
|
|
|
html =
|
2010-09-15 03:08:35 +00:00
|
|
|
'<table cellpadding=0 cellspacing=0 ' + 'border=0 width="100%" class="table table-' + id + '">';
|
|
|
|
if ( 'headings' in page ) {
|
2011-06-06 23:18:13 +00:00
|
|
|
html += $.wikiEditor.modules.toolbar.fn.buildHeading( context, page.headings );
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
|
|
|
if ( 'rows' in page ) {
|
2011-07-02 09:09:05 +00:00
|
|
|
for ( var i = 0; i < page.rows.length; i++ ) {
|
|
|
|
html += $.wikiEditor.modules.toolbar.fn.buildRow( context, page.rows[i] );
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-20 15:02:20 +00:00
|
|
|
$page.html( html + '</table>');
|
2010-09-15 03:08:35 +00:00
|
|
|
break;
|
|
|
|
case 'characters':
|
|
|
|
$page.addClass( 'page-characters' );
|
2011-07-02 08:54:01 +00:00
|
|
|
var $characters = $( '<div/>' ).data( 'context', context ).data( 'actions', {} );
|
2010-09-15 03:08:35 +00:00
|
|
|
var actions = $characters.data( 'actions' );
|
|
|
|
if ( 'language' in page ) {
|
|
|
|
$characters.attr( 'lang', page.language );
|
|
|
|
}
|
|
|
|
if ( 'direction' in page ) {
|
|
|
|
$characters.attr( 'dir', page.direction );
|
2012-05-20 20:27:06 +00:00
|
|
|
} else {
|
|
|
|
// By default it should be explicit ltr for all scripts.
|
|
|
|
// Without this some conjoined ltr characters look
|
|
|
|
// weird in rtl wikis.
|
|
|
|
$characters.attr( 'dir', 'ltr' );
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
|
|
|
if ( 'characters' in page ) {
|
2011-07-02 09:09:05 +00:00
|
|
|
html = '';
|
2010-09-15 03:08:35 +00:00
|
|
|
for ( var i = 0; i < page.characters.length; i++ ) {
|
|
|
|
html += $.wikiEditor.modules.toolbar.fn.buildCharacter( page.characters[i], actions );
|
|
|
|
}
|
|
|
|
$characters
|
|
|
|
.html( html )
|
|
|
|
.children()
|
|
|
|
.mousedown( function( e ) {
|
2010-10-05 20:54:38 +00:00
|
|
|
context.fn.saveCursorAndScrollTop();
|
2010-09-15 03:08:35 +00:00
|
|
|
// 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' )],
|
|
|
|
$(this)
|
|
|
|
);
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
$page.append( $characters );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $page;
|
|
|
|
},
|
|
|
|
buildHeading : function( context, headings ) {
|
|
|
|
var html = '<tr>';
|
2011-07-02 09:09:05 +00:00
|
|
|
for ( var i = 0; i< headings.length; i++ ) {
|
|
|
|
html += '<th>' + $.wikiEditor.autoMsg( headings[i], ['html', 'text'] ) + '</th>';
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
2011-11-14 21:21:15 +00:00
|
|
|
return html + '</tr>';
|
2010-09-15 03:08:35 +00:00
|
|
|
},
|
|
|
|
buildRow : function( context, row ) {
|
|
|
|
var html = '<tr>';
|
2011-06-06 23:18:13 +00:00
|
|
|
for ( var cell in row ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
html += '<td class="cell cell-' + cell + '" valign="top"><span>' +
|
|
|
|
$.wikiEditor.autoMsg( row[cell], ['html', 'text'] ) + '</span></td>';
|
|
|
|
}
|
2011-11-14 21:21:15 +00:00
|
|
|
return html + '</tr>';
|
2010-09-15 03:08:35 +00:00
|
|
|
},
|
|
|
|
buildCharacter : function( character, actions ) {
|
|
|
|
if ( typeof character == 'string' ) {
|
|
|
|
character = {
|
|
|
|
'label' : character,
|
|
|
|
'action' : {
|
|
|
|
'type' : 'replace',
|
|
|
|
'options' : {
|
|
|
|
'peri' : character,
|
|
|
|
'selectPeri': false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-10-11 00:10:09 +00:00
|
|
|
// In some cases the label for the character isn't the same as the
|
|
|
|
// character that gets inserted (e.g. Hebrew vowels)
|
2011-11-14 21:55:46 +00:00
|
|
|
} else if ( character && 0 in character && 1 in character ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
character = {
|
|
|
|
'label' : character[0],
|
|
|
|
'action' : {
|
|
|
|
'type' : 'replace',
|
|
|
|
'options' : {
|
|
|
|
'peri' : character[1],
|
|
|
|
'selectPeri': false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2011-11-14 21:55:46 +00:00
|
|
|
if ( character && 'action' in character && 'label' in character ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
actions[character.label] = character.action;
|
2012-10-11 00:10:09 +00:00
|
|
|
if ( character.titleMsg !== undefined ) {
|
|
|
|
return mw.html.element(
|
|
|
|
'span',
|
|
|
|
{ 'rel': character.label, 'title': mw.msg( character.titleMsg ) },
|
|
|
|
character.label
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return mw.html.element( 'span', { 'rel': character.label }, character.label );
|
|
|
|
}
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
2011-11-14 21:55:46 +00:00
|
|
|
mw.log( "A character for the toolbar was undefined. This is not supposed to happen. Double check the config." );
|
|
|
|
return ""; // bug 31673; also an additional fix for bug 24208...
|
2010-09-15 03:08:35 +00:00
|
|
|
},
|
|
|
|
buildTab : function( context, id, section ) {
|
|
|
|
var selected = $.cookie( 'wikiEditor-' + context.instance + '-toolbar-section' );
|
|
|
|
// Re-save cookie
|
2011-06-06 23:18:13 +00:00
|
|
|
if ( selected !== null ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
$.cookie( 'wikiEditor-' + context.instance + '-toolbar-section', selected, { expires: 30, path: '/' } );
|
|
|
|
}
|
2011-09-13 08:56:32 +00:00
|
|
|
var $link =
|
2011-06-06 23:18:13 +00:00
|
|
|
$( '<a/>' )
|
|
|
|
.addClass( selected == id ? 'current' : null )
|
|
|
|
.attr( 'href', '#' )
|
|
|
|
.text( $.wikiEditor.autoMsg( section, 'label' ) )
|
|
|
|
.data( 'context', context )
|
|
|
|
.mouseup( function( e ) {
|
|
|
|
$(this).blur();
|
|
|
|
} )
|
|
|
|
.mousedown( function( e ) {
|
|
|
|
// No dragging!
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} )
|
|
|
|
.click( function( e ) {
|
|
|
|
var $sections = $(this).data( 'context' ).$ui.find( '.sections' );
|
|
|
|
var $section =
|
|
|
|
$(this).data( 'context' ).$ui.find( '.section-' + $(this).parent().attr( 'rel' ) );
|
|
|
|
var show = $section.css( 'display' ) == 'none';
|
2011-07-02 08:54:01 +00:00
|
|
|
var $previousSections = $section.parent().find( '.section-visible' );
|
2011-06-06 23:18:13 +00:00
|
|
|
$previousSections.css( 'position', 'absolute' );
|
|
|
|
$previousSections.removeClass( 'section-visible' );
|
2011-11-12 09:08:00 +00:00
|
|
|
$previousSections.fadeOut( 'fast', function() { $(this).css( 'position', 'static' ); } );
|
2011-06-06 23:18:13 +00:00
|
|
|
$(this).parent().parent().find( 'a' ).removeClass( 'current' );
|
|
|
|
$sections.css( 'overflow', 'hidden' );
|
|
|
|
var animate = function( $that ) {
|
|
|
|
$sections
|
|
|
|
.css( 'display', 'block' )
|
|
|
|
.animate( { 'height': $section.outerHeight() }, $section.outerHeight() * 2, function() {
|
|
|
|
$that.css( 'overflow', 'visible' ).css( 'height', 'auto' );
|
|
|
|
context.fn.trigger( 'resize' );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
if ( show ) {
|
|
|
|
$section.addClass( 'section-visible' );
|
|
|
|
$section.fadeIn( 'fast' );
|
|
|
|
if ( $section.hasClass( 'loading' ) ) {
|
|
|
|
// Loading of this section was deferred, load it now
|
|
|
|
var $that = $(this);
|
|
|
|
$that.addClass( 'current loading' );
|
|
|
|
setTimeout( function() {
|
|
|
|
$section.trigger( 'loadSection' );
|
|
|
|
animate( $that );
|
|
|
|
$that.removeClass( 'loading' );
|
|
|
|
}, 1000 );
|
2010-09-15 03:08:35 +00:00
|
|
|
} else {
|
2011-06-06 23:18:13 +00:00
|
|
|
animate( $(this) );
|
|
|
|
$(this).addClass( 'current' );
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
2011-06-06 23:18:13 +00:00
|
|
|
} else {
|
|
|
|
$sections
|
|
|
|
.css( 'height', $section.outerHeight() )
|
|
|
|
.animate( { 'height': 'hide' }, $section.outerHeight() * 2, function() {
|
|
|
|
$(this).css( { 'overflow': 'visible', 'height': 0 } );
|
|
|
|
context.fn.trigger( 'resize' );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
// Save the currently visible section
|
|
|
|
$.cookie(
|
|
|
|
'wikiEditor-' + $(this).data( 'context' ).instance + '-toolbar-section',
|
|
|
|
show ? $section.attr( 'rel' ) : null,
|
|
|
|
{ expires: 30, path: '/' }
|
|
|
|
);
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
return $( '<span/>' )
|
|
|
|
.attr({
|
|
|
|
'class' : 'tab tab-' + id,
|
|
|
|
'rel' : id
|
|
|
|
})
|
|
|
|
.append( $link );
|
2010-09-15 03:08:35 +00:00
|
|
|
},
|
|
|
|
buildSection: function( context, id, section ) {
|
2011-06-06 23:18:13 +00:00
|
|
|
var $section = $( '<div/>' ).attr( { 'class': section.type + ' section section-' + id, 'rel': id } );
|
2010-09-15 03:08:35 +00:00
|
|
|
var selected = $.cookie( 'wikiEditor-' + context.instance + '-toolbar-section' );
|
|
|
|
var show = selected == id;
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2011-06-06 23:18:13 +00:00
|
|
|
if ( section.deferLoad !== undefined && section.deferLoad && id !== 'main' && !show ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
// This class shows the spinner and serves as a marker for the click handler in buildTab()
|
2011-06-06 23:18:13 +00:00
|
|
|
$section.addClass( 'loading' ).append( $( '<div/>' ).addClass( 'spinner' ) );
|
2010-09-15 03:08:35 +00:00
|
|
|
$section.bind( 'loadSection', function() {
|
|
|
|
$.wikiEditor.modules.toolbar.fn.reallyBuildSection( context, id, section, $section );
|
|
|
|
$section.removeClass( 'loading' );
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
$.wikiEditor.modules.toolbar.fn.reallyBuildSection( context, id, section, $section );
|
|
|
|
}
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2010-09-15 03:08:35 +00:00
|
|
|
// Show or hide section
|
|
|
|
if ( id !== 'main' ) {
|
|
|
|
$section.css( 'display', show ? 'block' : 'none' );
|
2011-06-06 23:18:13 +00:00
|
|
|
if ( show ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
$section.addClass( 'section-visible' );
|
2011-06-06 23:18:13 +00:00
|
|
|
}
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
|
|
|
return $section;
|
|
|
|
},
|
|
|
|
reallyBuildSection: function( context, id, section, $section ) {
|
|
|
|
context.$textarea.trigger( 'wikiEditor-toolbar-buildSection-' + $section.attr( 'rel' ), [section] );
|
|
|
|
switch ( section.type ) {
|
|
|
|
case 'toolbar':
|
|
|
|
if ( 'groups' in section ) {
|
2011-06-06 23:18:13 +00:00
|
|
|
for ( var group in section.groups ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
$section.append(
|
|
|
|
$.wikiEditor.modules.toolbar.fn.buildGroup( context, group, section.groups[group] )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'booklet':
|
2011-06-06 23:18:13 +00:00
|
|
|
var $pages = $( '<div/>' ).addClass( 'pages' );
|
|
|
|
var $index = $( '<div/>' ).addClass( 'index' );
|
2010-09-15 03:08:35 +00:00
|
|
|
if ( 'pages' in section ) {
|
2011-06-06 23:18:13 +00:00
|
|
|
for ( var page in section.pages ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
$pages.append(
|
|
|
|
$.wikiEditor.modules.toolbar.fn.buildPage( context, page, section.pages[page] )
|
|
|
|
);
|
|
|
|
$index.append(
|
|
|
|
$.wikiEditor.modules.toolbar.fn.buildBookmark( context, page, section.pages[page] )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$section.append( $index ).append( $pages );
|
|
|
|
$.wikiEditor.modules.toolbar.fn.updateBookletSelection( context, id, $pages, $index );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateBookletSelection : function( context, id, $pages, $index ) {
|
|
|
|
var cookie = 'wikiEditor-' + context.instance + '-booklet-' + id + '-page';
|
|
|
|
var selected = $.cookie( cookie );
|
|
|
|
// Re-save cookie
|
|
|
|
if ( selected != null ) {
|
|
|
|
$.cookie( cookie, selected, { expires: 30, path: '/' } );
|
|
|
|
}
|
2011-07-02 09:17:01 +00:00
|
|
|
var $selectedIndex = $index.find( '*[rel="' + selected + '"]' );
|
2011-06-06 23:18:13 +00:00
|
|
|
if ( $selectedIndex.size() === 0 ) {
|
2011-01-10 23:45:03 +00:00
|
|
|
$selectedIndex = $index.children().eq( 0 );
|
|
|
|
selected = $selectedIndex.attr( 'rel' );
|
2010-09-15 03:08:35 +00:00
|
|
|
$.cookie( cookie, selected, { expires: 30, path: '/' } );
|
|
|
|
}
|
|
|
|
$pages.children().hide();
|
2011-07-02 09:17:01 +00:00
|
|
|
$pages.find( '*[rel="' + selected + '"]' ).show();
|
2010-09-15 03:08:35 +00:00
|
|
|
$index.children().removeClass( 'current' );
|
|
|
|
$selectedIndex.addClass( 'current' );
|
|
|
|
},
|
|
|
|
build : function( context, config ) {
|
2011-06-06 23:18:13 +00:00
|
|
|
var $tabs = $( '<div/>' ).addClass( 'tabs' ).appendTo( context.modules.toolbar.$toolbar );
|
|
|
|
var $sections = $( '<div/>' ).addClass( 'sections' ).appendTo( context.modules.toolbar.$toolbar );
|
|
|
|
context.modules.toolbar.$toolbar.append( $( '<div/>' ).css( 'clear', 'both' ) );
|
2010-09-15 03:08:35 +00:00
|
|
|
var sectionQueue = [];
|
2011-06-06 23:18:13 +00:00
|
|
|
for ( var section in config ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
if ( section == 'main' ) {
|
|
|
|
context.modules.toolbar.$toolbar.prepend(
|
|
|
|
$.wikiEditor.modules.toolbar.fn.buildSection( context, section, config[section] )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
sectionQueue.push( {
|
|
|
|
'$sections' : $sections,
|
|
|
|
'context' : context,
|
|
|
|
'id' : section,
|
|
|
|
'config' : config[section]
|
|
|
|
} );
|
|
|
|
$tabs.append( $.wikiEditor.modules.toolbar.fn.buildTab( context, section, config[section] ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$.eachAsync( sectionQueue, {
|
|
|
|
'bulk' : 0,
|
|
|
|
'end' : function() {
|
|
|
|
// HACK: Opera doesn't seem to want to redraw after these bits
|
|
|
|
// are added to the DOM, so we can just FORCE it!
|
|
|
|
var oldValue = $( 'body' ).css( 'position' );
|
|
|
|
$( 'body' ).css( 'position', 'static' );
|
|
|
|
$( 'body' ).css( 'position', oldValue );
|
2013-01-29 04:02:42 +00:00
|
|
|
|
|
|
|
context.$textarea.trigger( 'wikiEditor-toolbar-doneInitialSections' );
|
2010-09-15 03:08:35 +00:00
|
|
|
},
|
|
|
|
'loop' : function( i, s ) {
|
|
|
|
s.$sections.append( $.wikiEditor.modules.toolbar.fn.buildSection( s.context, s.id, s.config ) );
|
|
|
|
var $section = s.$sections.find( '.section:visible' );
|
|
|
|
if ( $section.size() ) {
|
|
|
|
$sections.animate( { 'height': $section.outerHeight() }, $section.outerHeight() * 2, function( ) {
|
|
|
|
context.fn.trigger( 'resize' );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 00:10:09 +00:00
|
|
|
}; } )( mediaWiki, jQuery );
|