User:Gryllida/js/addCatButton.js

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins

/*
<!--
Author : Svetlana Tkachenko svetlana@members.fsf.org
Licence: GPLv3+
Description: [beta] button to prepend '[[Category:' and append ']]'
TODO: 
  [ ] suggest titles (autocomplete?)
See also <https://en.wikinews.org/wiki/User:Gryllida/Tasks>.
-->
*/ 

mw.loader.using(['mediawiki.api'], function () {
	"use strict";
	// == WikiEditor codes ==
	var customizeToolbar = function () {
		// add a category button
		$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
			'section': 'main',
			'group': 'insert',
			'tools': {
				'addCategory': {
					label: 'Add as a category',
					type: 'button',
					icon: '//upload.wikimedia.org/wikipedia/commons/thumb/6/64/Add_category_toolbar_button.svg/22px-Add_category_toolbar_button.svg.png',
					action: {
						type: 'encapsulate',
			            'options': {
			                'pre': '[[Category:',
			                'post': ']]'
			            }
					}
				}
			}
		} );
	};
	/* 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' ).then( 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' ), $.ready
				).then( customizeToolbar );
			}
		} );
	}
	// == WikiEditor codes end ==

});