User:George Orwell III/InsertWikiEditorButton.js

/** * Insert WikiEditor Button * @created 2011-06-05 * @source meta.wikimedia.org/wiki/User:Krinkle/Scripts/InsertWikiEditorButton * @version 0.3.1 (2015-02-15) * @author Krinkle, 2011 - 2012 * @author Locos epraix, 2012 * @license Released in the public domain */ /*jslint browser: true*/ /*global jQuery, mw*/ jQuery(document).ready(function ($) {	"use strict";

// Only on editpage with WikiEditor if ( mw.user.options.get( 'usebetatoolbar' ) && $.inArray(mw.config.get( 'wgAction' ), ['edit', 'submit']) !== -1 ) { /**		 * krInsertWikiEditorButton *		 * @param options {Object} An object with options: * - section {String} (optional) The name of the section in the WikiEditor. Defaults to 'main' * - group {String} (optional) The name of the group in the WikiEditor. Defaults to 'insert' * - id {String} (required) Unique id (ie. 'my-button') * - icon {String} (recommended) URL to the icon, should be square about 21 to 22px * - label {String} (required) Tooltip displayed when hovering button * - filters {Array} (optional) Include or Exclude namespaces in which button is generated (ie. [ 'body.ns-4, body.ns-talk' ] ) * - insertBefore {String} (optional) Wikitext to be inserted before the cursor on-click * - sampleText {String} (optional) Text inserted in place of the cursor if no text was selected * - insertAfter {String} (optional) Wikitext to be inserted after the cursor on-click * - ownline {Boolean} (optional) Specifies if the inserted text goes on it's own line. 'true' or 'false' * - splitlines {Boolean} (optional) Specifies if inserted text is applied to all highlighted lines. 'true' or 'false' * - callback {Function} (optional) Called when the button is clicked * - autoSummary {mixed} (optional) Null or an Object with the following properties: *  - summary {String} (required) Edit summary that should be used *  - position {String} (optional) 'append', 'prepend' or 'replace' *  - delimiter {String} (optional) delimiter between the (possibly) current summary and the to-be-inserted summary */		window.krInsertWikiEditorButton = function (options) { // Defaults options = $.extend({				'section': 'main',				'group': 'insert',				'id': null,				'filters': ,				'icon': '//upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Toolbaricon_bold_!.png/21px-Toolbaricon_bold_!.png',				'label': ,				'insertBefore': ,				'sampleText': ,				'insertAfter': '',				'ownline': null,				'splitlines': null,				'callback': null,				'autoSummary': {					'summary': null,					'position': 'append',					'delimiter': '; '				}			}, options); // Required if (!options.id || !options.label) { return false; }			var btnObj = { 'section': options.section, 'group': options.group, 'tools': {} };			btnObj.tools[options.id] = { label: options.label, filters: options.filters, type: 'button', icon: options.icon, action: { type: 'callback', execute: function { // encapsulateSelection $('#wpTextbox1').textSelection('encapsulateSelection', {							pre: options.insertBefore,							peri: options.sampleText,							post: options.insertAfter,							ownline: options.ownline,							splitlines: options.splitlines						}); // Auto summary if (options.autoSummary && options.autoSummary.summary) { var $summary = $('#wpSummary'), currentSum = $summary.val; if (!$.trim(currentSum)) { $summary.val(options.autoSummary.summary); } else { switch (options.autoSummary.position) { case 'prepend': $summary.val(										options.autoSummary.summary +											options.autoSummary.delimiter +											currentSum									); break; case 'replace': $summary.val(options.autoSummary.summary); break; default: // 'append' $summary.val(										currentSum +											options.autoSummary.delimiter +											options.autoSummary.summary									); }							}						}						// Callback if ($.isFunction(options.callback)) { options.callback; }					}				}			};			$('#wpTextbox1').wikiEditor('addToToolbar', btnObj); };	} else { // No-op function to avoid errors on other pages window.krInsertWikiEditorButton = function { }; } });