Difference between revisions of "MediaWiki:Common.js"

From Eco-Bible 1
Jump to: navigation, search
(Undo revision 1827 by Mwdennis (talk))
Line 1: Line 1:
The following javascript code was added to MediaWiki:Vector.js in the TNG Wiki with the upgrade to MediaWiki 1.16.2
+
/* Any JavaScript here will be loaded for all users on every page load. */
  
Return to [[Adding Buttons to WikiEditor]]
+
/** Collapsible tables *********************************************************
 +
  *
 +
  *  Description: Allows tables to be collapsed, showing only the header. See
 +
  *              [[Wikipedia:NavFrame]].
 +
  *  Maintainer on Wikipedia: [[User:R. Koot]]
 +
  */
 +
 +
var autoCollapse = 2;
 +
var collapseCaption = "hide";
 +
var expandCaption = "show";
 +
 +
function hasClass( element, className ) {
 +
  var Classes = element.className.split( " " );
 +
  for ( var i = 0; i < Classes.length; i++ ) {
 +
    if ( Classes[i] == className ) {
 +
      return ( true );
 +
    }
 +
  }
 +
  return ( false );
 +
}
 +
 +
function collapseTable( tableIndex )
 +
{
 +
    var Button = document.getElementById( "collapseButton" + tableIndex );
 +
    var Table = document.getElementById( "collapsibleTable" + tableIndex );
 +
 +
    if ( !Table || !Button ) {
 +
        return false;
 +
    }
 +
 +
    var Rows = Table.getElementsByTagName( "tr" );
 +
 +
    if ( Button.firstChild.data == collapseCaption ) {
 +
        for ( var i = 1; i < Rows.length; i++ ) {
 +
            Rows[i].style.display = "none";
 +
        }
 +
        Button.firstChild.data = expandCaption;
 +
    } else {
 +
        for ( var i = 1; i < Rows.length; i++ ) {
 +
            Rows[i].style.display = Rows[0].style.display;
 +
        }
 +
        Button.firstChild.data = collapseCaption;
 +
    }
 +
}
 +
 +
function createCollapseButtons()
 +
{
 +
    var tableIndex = 0;
 +
    var NavigationBoxes = new Object();
 +
    var Tables = document.getElementsByTagName( "table" );
 +
 +
    for ( var i = 0; i < Tables.length; i++ ) {
 +
        if ( hasClass( Tables[i], "collapsible" ) ) {
 +
            NavigationBoxes[ tableIndex ] = Tables[i];
 +
            Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
 +
 +
            var Button    = document.createElement( "span" );
 +
            var ButtonLink = document.createElement( "a" );
 +
            var ButtonText = document.createTextNode( collapseCaption );
 +
 +
            Button.style.styleFloat = "right";
 +
            Button.style.cssFloat = "right";
 +
            Button.style.fontWeight = "normal";
 +
            Button.style.textAlign = "right";
 +
            Button.style.width = "6em";
 +
 +
            ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
 +
            ButtonLink.setAttribute( "href", "javascript:collapseTable(" + tableIndex + ");" );
 +
            ButtonLink.appendChild( ButtonText );
 +
 +
            Button.appendChild( document.createTextNode( "[" ) );
 +
            Button.appendChild( ButtonLink );
 +
            Button.appendChild( document.createTextNode( "]" ) );
 +
 +
            var Header = Tables[i].getElementsByTagName( "tr" )[0].getElementsByTagName( "th" )[0];
 +
            /* only add button and increment count if there is a header row to work with */
 +
            if (Header) {
 +
                Header.insertBefore( Button, Header.childNodes[0] );
 +
                tableIndex++;
 +
            }
 +
        }
 +
    }
 +
 +
    for ( var i = 0;  i < tableIndex; i++ ) {
 +
        if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
 +
            collapseTable( i );
 +
        }
 +
    }
 +
}
 +
 +
addOnloadHook( createCollapseButtons );
  
<syntaxhighlight lang="javascript" enclose="div">
+
/* EDIT BUTTONS **************************************************************** */
/* Any JavaScript here will be loaded for users using the Vector skin */
+
  
// Check that the toolbar is available
 
if ( typeof $j !== 'undefined' && typeof $j.fn.wikiEditor !== 'undefined' ) {
 
    // Execute on load
 
    $j( function() {
 
   
 
        // To add a group to an existing toolbar section:
 
        // Added hdr = Page group on Main toolbar for TNG Wiki
 
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 
            'section': 'main',
 
            'groups': {
 
                'hdr': {
 
                    'labelMsg': 'wikieditor-toolbar-group-page' // or 'label': 'Text' when localized label no needed
 
                    }
 
                }
 
            }
 
        );
 
       
 
        // To add a group to an existing toolbar section:
 
        // Added style = Style group on Main toolbar for TNG Wiki
 
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 
            'section': 'main',
 
            'groups': {
 
                'style': {
 
                    'labelMsg': 'wikieditor-toolbar-group-style' // or use labelMsg for a localized label, see above
 
                    }
 
                }
 
            }
 
        );
 
       
 
        // To add a group to an existing toolbar section:
 
        // Added code = Code group on Advanced toolbar for TNG Wiki
 
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 
            'section': 'advanced',
 
            'groups': {
 
                'code': {
 
                    'labelMsg': 'wikieditor-toolbar-group-code' // or use labelMsg for a localized label, see above
 
                    }
 
                }
 
            }
 
        );
 
       
 
        // To add a button to an existing toolbar group:
 
        // Added Strikethrough button to WikiEditor main toolbar format group:
 
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 
            'section': 'main',
 
            'group': 'format',
 
                'tools': {
 
                    'strikethrough': {
 
                        labelMsg: 'wikieditor-toolbar-tool-strike',
 
                        type: 'button',
 
                        icon: 'Button-strike.png',
 
                        action: {
 
                            type: 'encapsulate',
 
                            options: {
 
                                pre: "<s>",
 
                                periMsg: 'wikieditor-toolbar-tool-strike-example',
 
                                post: "</s>"
 
                            }
 
                        }
 
                    }
 
                }
 
            }
 
        );
 
  
        // To add a button to an existing toolbar group:
+
var customizeToolbar = function() {
        // Added ZipFile button to WikiEditor main toolbar insert group:
+
/* Your code goes here */
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
+
            'section': 'main',
+
            'group': 'insert',
+
                'tools': {
+
                    'zipfile': {
+
                        labelMsg: 'wikieditor-toolbar-tool-zipfile',
+
                        type: 'button',
+
                        icon: 'Button-zipfile.png',
+
                        action: {
+
                            type: 'encapsulate',
+
                            options: {
+
                                preMsg: [ 'wikieditor-toolbar-tool-zipfile-pre', '[[Media:' ],
+
                                periMsg: 'wikieditor-toolbar-tool-zipfile-example',
+
                                post: "]]"
+
                            }
+
                        }
+
                    }
+
                }
+
            }
+
        );
+
  
        // To add a button to an existing toolbar group:
 
        // Added ZipFile button to WikiEditor main toolbar insert group:
 
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 
            'section': 'main',
 
            'group': 'insert',
 
                'tools': {
 
                    'image': {
 
                        labelMsg: 'wikieditor-toolbar-tool-imagefile',
 
                        type: 'button',
 
                        icon: 'Button-add-media.png',
 
                        action: {
 
                            type: 'encapsulate',
 
                            options: {
 
                                preMsg: [ 'wikieditor-toolbar-tool-imagefile-pre', '[[Image:' ],
 
                                periMsg: 'wikieditor-toolbar-tool-imagefile-example',
 
                                post: "]]"
 
                            }
 
                        }
 
                    }
 
                }
 
            }
 
        );
 
  
        // To add a button to an existing toolbar group:
 
        // Added Definition List button to WikiEditor advanced toolbar format group:
 
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 
            'section': 'advanced',
 
            'group': 'format',
 
                'tools': {
 
                    'definition': {
 
                    labelMsg: 'wikieditor-toolbar-tool-definition',
 
                        type: 'button',
 
                        icon: 'Button-list-definition.png',
 
                        action: {
 
                            type: 'encapsulate',
 
                            options: {
 
                                pre: "\n; Definition\n: Item 1\n: Item 2",
 
                                post: "\n"
 
                            }
 
                        }
 
                    }
 
                }
 
            }
 
        );
 
  
        // To add a button to an existing toolbar group:
 
        // Added BlockQuote button to TNG main toolbar style group:
 
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 
            'section': 'main',
 
            'group': 'style',
 
                'tools': {
 
                    'blockquote': {
 
                        labelMsg: 'wikieditor-toolbar-tool-blockquote',
 
                        type: 'button',
 
                        icon: 'Button-blockquote.png',
 
                        action: {
 
                            type: 'encapsulate',
 
                            options: {
 
                                pre: "<blockquote>\n",
 
                                periMsg: 'wikieditor-toolbar-tool-blockquote-example',
 
                                post: "\n</blockquote>"
 
                            }
 
                        }
 
                    }
 
                }
 
            }
 
        );
 
  
        // To add a button to an existing toolbar group:
+
$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
        // Added Font Color button to TNG main toolbar style group:
+
'sections': {
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
+
'emoticons': {
            'section': 'main',
+
'type': 'toolbar',  
            'group': 'style',
+
'label': 'Emoticons'
                'tools': {
+
'groups': {
                    'fontcolor': {
+
'faces': {
                        labelMsg: 'wikieditor-toolbar-tool-fontcolor',
+
'label': 'Faces'
                        type: 'button',
+
'tools': {
                        icon: 'Button-font-color.png',
+
'smile': {
                        action: {
+
label: 'Smile!',  
                        type: 'encapsulate',
+
type: 'button',
                            options: {
+
icon: 'http://www.journal33.com/eco-bible.org/images/2/2b/BP_Button.png',
                                pre: "<span style=\"color: ColourName\">",
+
action: {
                                periMsg: 'wikieditor-toolbar-tool-fontcolor-example',
+
type: 'encapsulate',
                                post: "</span>"
+
options: {
                            }
+
pre: ":)"  
                        }
+
}
                    }
+
}
                }
+
}
            }
+
}
        );
+
  
        // To add a button to an existing toolbar group:
+
}
        // Added RightTOC button to TNG main toolbar hdr group:
+
}
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
+
            'section': 'main',
+
            'group': 'hdr',
+
                'tools': {
+
                'RightTOC': {
+
                    labelMsg: 'wikieditor-toolbar-tool-rightTOC',
+
                    type: 'button',
+
                    icon: 'Button-toc.png',
+
                    action: {
+
                        type: 'encapsulate',
+
                        options: {
+
                            pre: "{| style=\"margin-right:0.5 em;\" align=\"right\" \n| __TOC__\n",
+
                            post: "|}\n"
+
                            }
+
                        }
+
                    }
+
                }
+
            }
+
        );
+
  
        // To add a button to an existing toolbar group:
 
        // Added Language Templage button to TNG main toolbar hdr group:
 
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 
            'section': 'main',
 
            'group': 'hdr',
 
                'tools': {
 
                'language': {
 
                    labelMsg: 'wikieditor-toolbar-tool-langtemplate',
 
                    type: 'button',
 
                    icon: 'Button-lang-template.png',
 
                    action: {
 
                        type: 'encapsulate',
 
                        options: {
 
                            pre: "\n{{Languages",
 
                            periMsg: 'wikieditor-toolbar-tool-langtemplate-example',
 
                            post: "}}\n"
 
                            }
 
                        }
 
                    }
 
                }
 
            }
 
        );
 
  
        // To add a button to an existing toolbar group:
+
}
        // Added Language Templage button to TNG main toolbar hdr group:
+
}
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
+
} );
            'section': 'main',
+
            'group': 'hdr',
+
                'tools': {
+
                'language': {
+
                    labelMsg: 'wikieditor-toolbar-tool-toggle',
+
                    type: 'button',
+
                    icon: 'Button-Toggle.png',
+
                    action: {
+
                        type: 'encapsulate',
+
                        options: {
+
                            preMsg: 'wikieditor-toolbar-tool-toggle-pre',
+
                            periMsg: 'wikieditor-toolbar-tool-toggle-example',
+
                            post: "</toggledisplay>\n"
+
                            }
+
                        }
+
                    }
+
                }
+
            }
+
        );
+
  
        // To add a button to an existing toolbar group:
 
        // Added Category button to TNG main toolbar hdr group:
 
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 
            'section': 'main',
 
            'group': 'hdr',
 
                'tools': {
 
                'category': {
 
                    labelMsg: 'wikieditor-toolbar-tool-category',
 
                    type: 'button',
 
                    icon: 'Button-category.png',
 
                    action: {
 
                        type: 'encapsulate',
 
                        options: {
 
                            pre: "\n[[Category:",
 
                            periMsg: 'wikieditor-toolbar-tool-category-example',
 
                            post: "]]\n"
 
                            }
 
                        }
 
                    }
 
                }
 
            }
 
        );
 
  
        // To add a button to an existing toolbar group:
+
        // Added Pre-Formatted Text button to TNG advanced toolbar code group:
+
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
+
            'section': 'advanced',
+
            'group': 'code',
+
                'tools': {
+
                'preformat': {
+
                    labelMsg: 'wikieditor-toolbar-tool-preformat',
+
                    type: 'button',
+
                    icon: 'Button-pre.png',
+
                    action: {
+
                        type: 'encapsulate',
+
                        options: {
+
                            pre: "\n<pre>\n",
+
                            periMsg: 'wikieditor-toolbar-tool-preformat-example',
+
                            post: "</pre>\n"
+
                            }
+
                        }
+
                    }
+
                }
+
            }
+
        );
+
  
        // To add a button to an existing toolbar group:
 
        // Added Code button to TNG advanced toolbar code group:
 
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 
            'section': 'advanced',
 
            'group': 'code',
 
                'tools': {
 
                'codesnippet': {
 
                    labelMsg: 'wikieditor-toolbar-tool-codesnippet',
 
                    type: 'button',
 
                    icon: 'Button-code.png',
 
                    action: {
 
                        type: 'encapsulate',
 
                        options: {
 
                            pre: "\n<code>",
 
                            periMsg: 'wikieditor-toolbar-tool-codesnippet-example',
 
                            post: "</code>\n"
 
                            }
 
                        }
 
                    }
 
                }
 
            }
 
        );
 
  
        // To add a button to an existing toolbar group:
+
        // Added Syntax Highlight button to TNG advanced toolbar code group:
+
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
+
            'section': 'advanced',
+
            'group': 'code',
+
                'tools': {
+
                'syntaxhighlight': {
+
                    labelMsg: 'wikieditor-toolbar-tool-highlight',
+
                    type: 'button',
+
                    icon: 'Button-source.png',
+
                    action: {
+
                        type: 'encapsulate',
+
                        options: {
+
  
pre: "\n<syntaxhighlight lang=\"html4strict\" line start=\"100\"
 
highlight=\"5\" enclose=\"div\">",
 
                            periMsg: 'wikieditor-toolbar-tool-highlight-example',
 
                            post: "</syntaxhighlight>\n"
 
                            }
 
                        }
 
                    }
 
                }
 
            }
 
        );
 
  
        // To add a button to an existing toolbar group:
 
        // Added Comment button to TNG advanced toolbar code group:
 
        $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 
            'section': 'advanced',
 
            'group': 'code',
 
                'tools': {
 
                'comments': {
 
                    labelMsg: 'wikieditor-toolbar-tool-comments',
 
                    type: 'button',
 
                    icon: 'Button-hide-comment.png',
 
                    action: {
 
                        type: 'encapsulate',
 
                        options: {
 
                            pre: "<!-- ",
 
                            periMsg: 'wikieditor-toolbar-tool-comments-example',
 
                            post: " -->\n"
 
                            }
 
                        }
 
                    }
 
                }
 
            }
 
        );
 
  
    // Terminate onLoad action - Buttons or sections must be added before this line
 
  
    }
 
);
 
  
}
 
  
</syntaxhighlight>
 
  
 
+
};
See [http://commons.wikimedia.org/wiki/MediaWiki_edit_toolbar MediaWiki edit toolbar] for extra buttons.
+
/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */
 
+
if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
Return to [[Adding Buttons to WikiEditor]]
+
mw.loader.using( 'user.options', function () {
[[Category:Toolbar Buttons]]
+
// This can be the string "0" if the user disabled the preference ([[phab:T54542#555387]])
 +
if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) {
 +
$.when(
 +
mw.loader.using( 'ext.wikiEditor.toolbar' ), $.ready
 +
).then( customizeToolbar );
 +
}
 +
} );
 +
}

Revision as of 20:04, 5 August 2016

/* Any JavaScript here will be loaded for all users on every page load. */

 /** Collapsible tables *********************************************************
  *
  *  Description: Allows tables to be collapsed, showing only the header. See
  *               [[Wikipedia:NavFrame]].
  *  Maintainer on Wikipedia: [[User:R. Koot]]
  */
 
 var autoCollapse = 2;
 var collapseCaption = "hide";
 var expandCaption = "show";
 
 function hasClass( element, className ) {
  var Classes = element.className.split( " " );
  for ( var i = 0; i < Classes.length; i++ ) {
    if ( Classes[i] == className ) {
      return ( true );
    }
  }
  return ( false );
 }
 
 function collapseTable( tableIndex )
 {
     var Button = document.getElementById( "collapseButton" + tableIndex );
     var Table = document.getElementById( "collapsibleTable" + tableIndex );
 
     if ( !Table || !Button ) {
         return false;
     }
 
     var Rows = Table.getElementsByTagName( "tr" ); 
 
     if ( Button.firstChild.data == collapseCaption ) {
         for ( var i = 1; i < Rows.length; i++ ) {
             Rows[i].style.display = "none";
         }
         Button.firstChild.data = expandCaption;
     } else {
         for ( var i = 1; i < Rows.length; i++ ) {
             Rows[i].style.display = Rows[0].style.display;
         }
         Button.firstChild.data = collapseCaption;
     }
 }
 
 function createCollapseButtons()
 {
     var tableIndex = 0;
     var NavigationBoxes = new Object();
     var Tables = document.getElementsByTagName( "table" );
 
     for ( var i = 0; i < Tables.length; i++ ) {
         if ( hasClass( Tables[i], "collapsible" ) ) {
             NavigationBoxes[ tableIndex ] = Tables[i];
             Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
 
             var Button     = document.createElement( "span" );
             var ButtonLink = document.createElement( "a" );
             var ButtonText = document.createTextNode( collapseCaption );
 
             Button.style.styleFloat = "right";
             Button.style.cssFloat = "right";
             Button.style.fontWeight = "normal";
             Button.style.textAlign = "right";
             Button.style.width = "6em";
 
             ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
             ButtonLink.setAttribute( "href", "javascript:collapseTable(" + tableIndex + ");" );
             ButtonLink.appendChild( ButtonText );
 
             Button.appendChild( document.createTextNode( "[" ) );
             Button.appendChild( ButtonLink );
             Button.appendChild( document.createTextNode( "]" ) );
 
             var Header = Tables[i].getElementsByTagName( "tr" )[0].getElementsByTagName( "th" )[0];
             /* only add button and increment count if there is a header row to work with */
             if (Header) {
                 Header.insertBefore( Button, Header.childNodes[0] );
                 tableIndex++;
             }
         }
     }
 
     for ( var i = 0;  i < tableIndex; i++ ) {
         if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
             collapseTable( i );
         }
     }
 }
 
 addOnloadHook( createCollapseButtons );

/* EDIT BUTTONS **************************************************************** */


var customizeToolbar = function() {
	/* Your code goes here */
	
	




$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'sections': {
		'emoticons': {
			'type': 'toolbar', 
			'label': 'Emoticons'
		'groups': {
			'faces': {
			'label': 'Faces' 
'tools': {
		'smile': {
			label: 'Smile!', 
			type: 'button',
			icon: 'http://www.journal33.com/eco-bible.org/images/2/2b/BP_Button.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: ":)" 
				}
			}
		}
	}

		}
	}


		}
	}
} );


	


	







};
/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */
if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
	mw.loader.using( 'user.options', function () {
		// This can be the string "0" if the user disabled the preference ([[phab:T54542#555387]])
		if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) {
			$.when(
				mw.loader.using( 'ext.wikiEditor.toolbar' ), $.ready
			).then( customizeToolbar );
		}
	} );
}