Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
  • Opera: Strg+F5
//Dokumentation siehe [[Benutzer:Schnark/js/wikieditor]] <nowiki>
/*global mediaWiki*/

(function ($, mw, libs) {
"use strict";

var standardbar, wikieditor = {
	version: '2.3',
	sig: '--~~~~',

	additional: {
		preview: false, //kann auf true gesetzt werden, Reiter für Live-Vorschau bzw. Diff
		publish: false //verbuggt?
	}
},

functionCodes = {
	section: {},
	group: {},
	chars: {},
	table: {},
	button: {},
	select: {}
};

/**********************************
*         interal functions       *
**********************************/

//create an id from a given name (probably not necessary) or use default
function makeId (name, defaultVal) {
	return (name !== '') ? name.replace(/ /g, '_').replace(/[^\-_a-zA-Z0-9]/g, 'x') : defaultVal;
}

//put things together to an action, opt and func are optional
function makeAction (preText, sampleText, postText, opt, func) {
	var action = {};
	if (opt === true) { //backward compatibility
		opt = {ownline: true};
	}
	if (!opt) {
		opt = {};
	}
	opt.pre = preText;
	opt.peri = sampleText;
	opt.post = postText;
	if (func) {
		if (preText === '' && sampleText === '' && postText === '') { //only a function
			action = {type: 'callback', execute: func};
		} else {
			action = {
				type: 'callback',
				execute: function () {
					wikieditor.$textarea.textSelection('encapsulateSelection', opt);
					func();
				}
			};
		}
	} else {
		action = {type: 'encapsulate', options: opt};
	}
	return action;
}

//append CSS for a select
//list = [[item1, icon1], [item2, icon2], ...]
function cssSelect (where, name, list) {
	if (where.indexOf('/') === -1) {
		where += '/';
	}
	var css1 = '.wikiEditor-ui-toolbar .section[rel=' + makeId(where.split('/')[0], 'main') +
			'] .group[rel=' + makeId(where.split('/')[1], 'insert') +
			'] .tool-select[rel=' + makeId(name) +
			'] .options .option[rel=',
		css2 = ']:before {content:url(',
		css3 = ')" "; }',
		css = '', i;
	for (i = 0; i < list.length; i++) {
		css += css1 + makeId(list[i][0]) + css2 + list[i][1] + css3;
	}
	mw.util.addCSS(css);
}

//insert a comment
function addComment (comment) {
	return function () {
		$('#wpSummary').val($('#wpSummary').val() + comment);
	};
}

/*****************************************************
*                  addToToolbar                      *
*****************************************************/

//add a section
//type: true - toolbar, false - booklet
function addMySection (name, type) {
	var id = makeId(name),
		newSection = {sections: {}},
		newGroup;
	newSection.sections[id] = {
		type: type ? 'toolbar' : 'booklet',
		label: name
	};
	if (!type) { //HACK
		newSection.sections[id].pages = {x: {}};
	}
	wikieditor.$textarea.wikiEditor('addToToolbar', newSection);
	if (!type) { //HACK
		remove(name + '/x', false);
	}
	if (type) { //insert empty group
		newGroup = {section: id, groups: {'insert': {}}};
		wikieditor.$textarea.wikiEditor('addToToolbar', newGroup);
	}
}

function addSection (code, name) {
	if (functionCodes.section[code]) {
		return functionCodes.section[code](name);
	}
	return false;
}

//add a group to a toolbar (section with type = true)
function addMyGroup (where, name) {
	var id = makeId(name),
		newGroup = {section: makeId(where, 'main'), groups: {}};
	if (name.charAt(0) === ' ') { //if name starts with a space don't show the name
		name = '';
	}
	newGroup.groups[id] = {label: name};
	wikieditor.$textarea.wikiEditor('addToToolbar', newGroup);
}

function addGroup (where, code, name) {
	if (functionCodes.group[code]) {
		return functionCodes.group[code](where, name);
	}
	return false;
}

//add a table with characters to a booklet (section with type = false)
//or into an existing characters table
//rtl is optional
//chars = [z1, z2, ...]
//z_i = 'x' or = ['show', 'insert'] or = ['pre', 'peri', 'post']
function addMyChars (where, name, chars, rtl) {
	var id = makeId(name), c = [], i, newChars;
	for (i = 0; i < chars.length; i++) {
		if ($.isArray(chars[i]) && chars[i].length === 3) {
			c.push({
				label: chars[i][0] + chars[i][1] + chars[i][2],
				action: {
					type: 'encapsulate',
					options: {pre: chars[i][0], peri: chars[i][1], post: chars[i][2]}
				}
			});
		} else {
			c.push(chars[i]);
		}
	}
	if (where.indexOf('/') === -1) {
		newChars = {section: makeId(where, 'characters'), pages: {}};
		newChars.pages[id] = {label: name, layout: 'characters', characters: c};
		if (rtl) {
			newChars.pages[id].direction = 'rtl';
		}
	} else {
		newChars = {
			section: makeId(where.split('/')[0], 'characters'),
			page: makeId(where.split('/')[1], 'symbols'),
			characters: c
		};
	}
	wikieditor.$textarea.wikiEditor('addToToolbar', newChars);
}

function addChars (where, code, name) {
	if (functionCodes.chars[code]) {
		return functionCodes.chars[code](where, name);
	}
	return false;
}

//add a table to a booklet (section with type = false)
//table = [[head1, head2, ...], [1.1, 1.2, ...], ...]
function addMyTable (where, name, table) {
	var id = makeId(name), head = [], rows = [], row, i, j,
		newTable = {section: makeId(where, 'help'), pages: {}};
	for (i = 0; i < table[0].length; i++) {
		head.push({text: table[0][i]});
	}
	for (i = 1; i < table.length; i++) {
		row = {};
		for (j = 0; j < table[i].length; j++) {
			row['a' + j] = {html: table[i][j]};
		}
		rows.push(row);
	}
	newTable.pages[id] = {label: name, layout: 'table', headings: head, rows: rows};
	wikieditor.$textarea.wikiEditor('addToToolbar', newTable);
}

function addTable (where, code, name) {
	if (functionCodes.table[code]) {
		return functionCodes.table[code](where, name);
	}
	return false;
}

//add a button to a toolbar (section with type = true)
function addMyButton (where, icon, tooltip, pre, peri, post, ownline, func) {
	if (where.indexOf('/') === -1) {
		where += '/';
	}
	var id = makeId(tooltip),
		newButton = {
			section: makeId(where.split('/')[0], 'main'),
			group: makeId(where.split('/')[1], 'insert'),
			tools: {}
		};
	newButton.tools[id] = {
		label: tooltip,
		type: 'button',
		icon: icon,
		action: makeAction(pre, peri, post, ownline, func)
	};
	try {
		wikieditor.$textarea.wikiEditor('addToToolbar', newButton);
	} catch (e) {
		
	}
}

function addButton (where, code, name) {
	if (functionCodes.button[code]) {
		return functionCodes.button[code](where, name);
	}
	return false;
}

//add a select to a toolbar (section with type = true)
//list = [[name, pre, peri, post], ...]
//bzw.    [name, pre, peri, post, ownline, funktion]
function addMySelect (where, name, list, icons) {
	if (where.indexOf('/') === -1) {
		where += '/';
	}
	var id = makeId(name),
		newList = {
			section: makeId(where.split('/')[0], 'main'),
			group: makeId(where.split('/')[1], 'insert'),
			tools: {}
		},
		listObject = {}, i, item;
	for (i = 0; i < list.length; i++) {
		item = list[i];
		listObject[makeId(item[0])] = {
			label: item[0],
			action: makeAction(item[1], item[2], item[3], item[4], item[5])
		};
		if (icons) {
			icons[i] = [item[0], icons[i]];
		}
	}
	newList.tools[id] = {
		label: name,
		type: 'select',
		list: listObject
	};
	wikieditor.$textarea.wikiEditor('addToToolbar', newList);
	if (icons) {
		cssSelect(where, name, icons);
	}
}

function addSelect (where, code, name) {
	if (functionCodes.select[code]) {
		return functionCodes.select[code](where, name);
	}
	return false;
}

//add standard tool
function addInternalTool (where, section, group, name) {
	if (!standardbar) {
		standardbar = $.wikiEditor.modules.toolbar.config.getDefaultConfig();
	}
	if (where.indexOf('/') === -1) {
		where += '/';
	}
	var newtool = {
			section: makeId(where.split('/')[0], 'main'),
			group: makeId(where.split('/')[1], 'insert'),
			tools: {}
		},
		tool = standardbar.toolbar[section].groups[group].tools[name];
	if (tool.filters) { //remove filters
		tool.filters = [];
	}
	newtool.tools[name] = tool;
	wikieditor.$textarea.wikiEditor('addToToolbar', newtool);
	return true;
}

/*****************************************************
*               removeFromToolbar                    *
*****************************************************/

//remove something
//type = true for toolbar, = false for booklet
function remove (thing, type) {
	var obj = {section: makeId(thing.split('/')[0], type ? 'main' : 'characters')};
	if (thing.indexOf('/') > -1) {
		obj[type ? 'group' : 'page'] = makeId(thing.split('/')[1], 'insert');
	}
	if (type && thing.lastIndexOf('/') !== thing.indexOf('/')) {
		obj.tool = makeId(thing.split('/')[2], 'heading');
	}
	if (!type && thing.lastIndexOf('/') !== thing.indexOf('/')) {
		obj.character = thing.split('/')[2];
	}
	wikieditor.$textarea.wikiEditor('removeFromToolbar', obj);
}

/*************************************
*        configuration               *
*************************************/

function define (type, code, func, depr) {
	var c, f, ok = false;
	if (typeof code !== 'string') {
		for (c in code) {
			if (code.hasOwnProperty(c)) {
				ok = define(type, c, func, code[c]) || ok;
			}
		}
		return ok;
	}
	if (code in functionCodes[type]) {
		return false;
	}
	if (depr) {
		//jscs:disable requireFunctionDeclarations
		f = function (a, b, c) {
			mw.log.warn(depr);
			func(a, b, c);
		};
		//jscs:enable requireFunctionDeclarations
	} else {
		f = func;
	}
	functionCodes[type][code] = f;
	return true;
}

function init () {
	var additional = [], component, action = mw.config.get('wgAction'), dep;
	if (action !== 'edit' && action !== 'submit') {
		return;
	}
	if (window.usersignature !== undefined) { //user-configurable signature
		wikieditor.sig = window.usersignature;
	}
	if (additional.length) {
		mw.loader.load(additional);
	}
	dep = [
		'mediawiki.util', 'jquery.textSelection', 'ext.wikiEditor'];
	mw.loader.using(dep, function () {
		$(function () {
			//TODO if there are other places where WikiEditor can be this should change
			wikieditor.$textarea = $('#wpTextbox1');
			if (typeof wikieditor.config === 'function') {
				wikieditor.config(wikieditor);
			} else {
				wikieditor.standard();
			}
		});
	});
}

/*************************************
*          standard bar              *
*************************************/

define('button', 'bold', function (where) {
	return addInternalTool(where, 'main', 'format', 'bold');
});

define('button', 'italic', function (where) {
	return addInternalTool(where, 'main', 'format', 'italic');
});

define('button', 'xlink', function (where) {
	return addInternalTool(where, 'main', 'insert', 'xlink');
});

define('button', 'ilink', function (where) {
	return addInternalTool(where, 'main', 'insert', 'ilink');
});

define('button', 'file', function (where) {
	return addInternalTool(where, 'main', 'insert', 'file');
});

define('button', 'reference', function (where) {
	return addInternalTool(where, 'main', 'insert', 'reference');
});

define('button', 'signature', function (where) {
	return addInternalTool(where, 'main', 'insert', 'signature');
});

define('select', 'heading', function (where) {
	return addInternalTool(where, 'advanced', 'heading', 'heading');
});

define('button', 'ulist', function (where) {
	return addInternalTool(where, 'advanced', 'format', 'ulist');
});

define('button', 'olist', function (where) {
	return addInternalTool(where, 'advanced', 'format', 'olist');
});

define('button', 'nowiki', function (where) {
	return addInternalTool(where, 'advanced', 'format', 'nowiki');
});

define('button', 'newline', function (where) {
	return addInternalTool(where, 'advanced', 'format', 'newline');
});

define('button', 'big', function (where) {
	return addInternalTool(where, 'advanced', 'size', 'big');
});

define('button', 'small', function (where) {
	return addInternalTool(where, 'advanced', 'size', 'small');
});

define('button', 'superscript', function (where) {
	return addInternalTool(where, 'advanced', 'size', 'superscript');
});

define('button', 'subscript', function (where) {
	return addInternalTool(where, 'advanced', 'size', 'subscript');
});

define('button', 'gallery', function (where) {
	return addInternalTool(where, 'advanced', 'insert', 'gallery');
});

define('button', 'table', function (where) {
	return addInternalTool(where, 'advanced', 'insert', 'table');
});

define('button', 'redirect', function (where) {
	return addInternalTool(where, 'advanced', 'insert', 'redirect');
});

wikieditor.addComment = addComment;
wikieditor.addMySection = addMySection;
wikieditor.addSection = addSection;
wikieditor.addMyGroup = addMyGroup;
wikieditor.addGroup = addGroup;
wikieditor.addMyChars = addMyChars;
wikieditor.addChars = addChars;
wikieditor.addMyTable = addMyTable;
wikieditor.addTable = addTable;
wikieditor.addMyButton = addMyButton;
wikieditor.addButton = addButton;
wikieditor.addMySelect = addMySelect;
wikieditor.addSelect = addSelect;
wikieditor.remove = remove;
wikieditor.standard = $.noop;
wikieditor.init = init;
wikieditor.define = define;

$.extend(true, wikieditor, libs.wikieditor);
libs.wikieditor = wikieditor;

})(jQuery, mediaWiki, mediaWiki.libs);
//</nowiki>