User:Toru10/WEDI.js/basic.js

/**********************************************************************************************************
 * "Einfache" Konfiguration der neuen Toolbar, Dokumentation siehe Benutzer:Schnark/js/wikieditor    *
 * Autor: Benutzer:Schnark                          *
 * aufbauend auf commons:User:Krinkle/insertVectorButtons.js von Benutzer:Krinkle                *
 * "Simple" configuration for the new toolbar, German documentation on Benutzer:Schnark/js/wikieditor *
 * author: Benutzer:Schnark                         *
 * using ideas from commons:User:Krinkle/insertVectorButtons.js by Benutzer:Krinkle              *
 * using ideas from commons:User:Krinkle/insertVectorButtons.js by Benutzer:Krinkle              *

/*global mediaWiki */ /*jshint sub: true */

/*************************************** (function($, mw, libs){
 * Part 1: project independent things *

var wikieditor = { version: '1.7', standardbar: null, //filled on first use $textarea: null, sig: '--~', additional: { preview: false, //kann auf true gesetzt werden, Reiter für Live-Vorschau bzw. Diff previewDialog: false, //verbuggt publish: false //verbuggt },

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

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

//put things together to an action, opt and func are optional action: function (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], ...]	CSSSelect: function (where, name, list) {		if (where.indexOf('/') === -1) {			where += '/';		}		var	css1 = '.wikiEditor-ui-toolbar .section[rel=' + wikieditor.ID(where.split('/')[0], 'main') +				'] .group[rel=' + wikieditor.ID(where.split('/')[1], 'insert') +				'] .tool-select[rel=' + wikieditor.ID(name) +				'] .options .option[rel=',			css2 = ']:before {content:url(',			css3 = ')" "; }',			css = '', i;		for (i = 0; i < list.length; i++) {			css += css1 + wikieditor.ID(list[i][0]) + css2 + list[i][1] + css3;		}		mw.util.addCSS(css);	},

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

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

//add a section //type: true - toolbar, false - booklet addMySection: function (name, type) { var	id = wikieditor.ID(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 wikieditor.remove(name + '/x', false); }		if (type) { //insert empty group newGroup = {section: id, groups: {'insert': {}}}; wikieditor.$textarea.wikiEditor('addToToolbar', newGroup); }	},	addSection_fn: {}, addSection_fnd: function (/*code, name*/) { return false; },	addSection: function (code, name) { var fn = wikieditor.addSection_fn[code]; return fn ? fn(name) : wikieditor.addSection_fnd(code, name); },

//add a group to a toolbar (section with type = true) addMyGroup: function (where, name) { var	id = wikieditor.ID (name), newGroup = {section: wikieditor.ID(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); },	addGroup_fn: {}, addGroup_fnd: function (/*where, code, name*/) { return false; },	addGroup: function (where, code, name) { var fn = wikieditor.addGroup_fn[code]; return fn ? fn(where, name) : wikieditor.addGroup_fnd(where, code, name); },

//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'] addMyChars: function (where, name, chars, rtl) { var id = wikieditor.ID (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: wikieditor.ID(where, 'characters'), pages: {}}; newChars.pages[id] = {label: name, layout: 'characters', characters: c}; if (rtl) { newChars.pages[id].direction = 'rtl'; }		} else { newChars = { section: wikieditor.ID(where.split('/')[0], 'characters'), page: wikieditor.ID(where.split('/')[1], 'symbols'), characters: c			}; }		wikieditor.$textarea.wikiEditor('addToToolbar', newChars); },	addChars_fn: {}, addChars_fnd: function (/*where, code, name*/) { return false; },	addChars: function (where, code, name) { var fn = wikieditor.addChars_fn[code]; return fn ? fn(where, name) : wikieditor.addChars_fnd(where, code, name); },

//add a table to a booklet (section with type = false) //table = [[head1, head2, ...], [1.1, 1.2, ...], ...]	addMyTable: function (where, name, table) {		var	id = wikieditor.ID (name), head = [], rows = [], row, i, j,			newTable = {section: wikieditor.ID(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);	},	addTable_fn: {},	addTable_fnd: function (/*where, code, name*/) {		return false;	},	addTable: function (where, code, name) {		var fn = wikieditor.addTable_fn[code];		return fn ? fn(where, name) : wikieditor.addTable_fnd(where, code, name);	},

//add a button to a toolbar (section with type = true) addMyButton: function (where, icon, tooltip, pre, peri, post, ownline, func) { if (where.indexOf('/') === -1) { where += '/'; }		var	id = wikieditor.ID(tooltip), newButton = { section: wikieditor.ID(where.split('/')[0], 'main'), group: wikieditor.ID(where.split('/')[1], 'insert'), tools: {} };		newButton.tools[id] = { label: tooltip, type: 'button', icon: icon, action: wikieditor.action(pre, peri, post, ownline, func) };		wikieditor.$textarea.wikiEditor('addToToolbar', newButton); },	addButton_fn: {}, addButton_fnd: function (/*where, code, name*/) { return false; },	addButton: function (where, code, name) { var fn = wikieditor.addButton_fn[code]; return fn ? fn(where, name) : wikieditor.addButton_fnd(where, code, name); },

//add a select to a toolbar (section with type = true) //list = [[name, pre, peri, post], ...] // bzw.  [name, pre, peri, post, ownline, funktion]	addMySelect: function (where, name, list) {		if (where.indexOf('/') === -1) {			where += '/';		}		var	id = wikieditor.ID(name),			newList = {				section: wikieditor.ID(where.split('/')[0], 'main'),				group: wikieditor.ID(where.split('/')[1], 'insert'),				tools: {}			},			list_ = {}, i, item;		for (i = 0; i < list.length; i++) {			item = list[i];			list_[wikieditor.ID(item[0])] = {				label: item[0],				action: wikieditor.action(item[1], item[2], item[3], item[4], item[5])			};		}		newList.tools[id] = {			label: name,			type: 'select',			list: list_		};		wikieditor.$textarea.wikiEditor('addToToolbar', newList);	},	addSelect_fn: {},	addSelect_fnd: function (/*where, code, name*/) {		return false;	},	addSelect: function (where, code, name) {		var fn = wikieditor.addSelect_fn[code];		return fn ? fn(where, name) : wikieditor.addSelect_fnd(where, code, name);	},

//add standard tool addInternalTool: function (where, section, group, name) { if (wikieditor.standardbar === null) { wikieditor.standardbar = $.wikiEditor.modules.toolbar.config.getDefaultConfig; }		if (where.indexOf('/') === -1) { where += '/'; }		var	newtool = { section: wikieditor.ID(where.split('/')[0], 'main'), group: wikieditor.ID(where.split('/')[1], 'insert'), tools: {} },			tool = wikieditor.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 remove: function (thing, type) { var obj = {section: wikieditor.ID(thing.split('/')[0], type ? 'main' : 'characters')}; if (thing.indexOf('/') > -1) { obj[type ? 'group' : 'page'] = wikieditor.ID(thing.split('/')[1], 'insert'); }		if (type && thing.lastIndexOf('/') !== thing.indexOf('/')) { obj.tool = wikieditor.ID(thing.split('/')[2], wikieditor.ID('heading')); }		if (!type && thing.lastIndexOf('/') !== thing.indexOf('/')) { obj.character = thing.split('/')[2]; }		wikieditor.$textarea.wikiEditor('removeFromToolbar', obj); },

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

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

};

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

wikieditor.addButton_fn['bold'] = function (where) { return wikieditor.addInternalTool(where, 'main', 'format', 'bold'); };

wikieditor.addButton_fn['italic'] = function (where) { return wikieditor.addInternalTool(where, 'main', 'format', 'italic'); };

wikieditor.addButton_fn['xlink'] = function (where) { return wikieditor.addInternalTool(where, 'main', 'insert', 'xlink'); };

wikieditor.addButton_fn['ilink'] = function (where) { return wikieditor.addInternalTool(where, 'main', 'insert', 'ilink'); };

wikieditor.addButton_fn['file'] = function (where) { return wikieditor.addInternalTool(where, 'main', 'insert', 'file'); };

wikieditor.addButton_fn['reference'] = function (where) { return wikieditor.addInternalTool(where, 'main', 'insert', 'reference'); };

wikieditor.addButton_fn['signature'] = function (where) { return wikieditor.addInternalTool(where, 'main', 'insert', 'signature'); };

wikieditor.addSelect_fn['heading'] = function (where) { return wikieditor.addInternalTool(where, 'advanced', 'heading', 'heading'); };

wikieditor.addButton_fn['ulist'] = function (where) { return wikieditor.addInternalTool(where, 'advanced', 'format', 'ulist'); };

wikieditor.addButton_fn['olist'] = function (where) { return wikieditor.addInternalTool(where, 'advanced', 'format', 'olist'); };

wikieditor.addButton_fn['nowiki'] = function (where) { return wikieditor.addInternalTool(where, 'advanced', 'format', 'nowiki'); };

wikieditor.addButton_fn['newline'] = function (where) { return wikieditor.addInternalTool(where, 'advanced', 'format', 'newline'); };

wikieditor.addButton_fn['big'] = function (where) { return wikieditor.addInternalTool(where, 'advanced', 'size', 'big'); };

wikieditor.addButton_fn['small'] = function (where) { return wikieditor.addInternalTool(where, 'advanced', 'size', 'small'); };

wikieditor.addButton_fn['superscript'] = function (where) { return wikieditor.addInternalTool(where, 'advanced', 'size', 'superscript'); };

wikieditor.addButton_fn['subscript'] = function (where) { return wikieditor.addInternalTool(where, 'advanced', 'size', 'subscript'); };

wikieditor.addButton_fn['gallery'] = function (where) { return wikieditor.addInternalTool(where, 'advanced', 'insert', 'gallery'); };

wikieditor.addButton_fn['table'] = function (where) { return wikieditor.addInternalTool(where, 'advanced', 'insert', 'table'); };

wikieditor.addButton_fn['redirect'] = function (where) { return wikieditor.addInternalTool(where, 'advanced', 'insert', 'redirect'); };

if (window.usersignature !== undefined) { //user-configurable signature wikieditor.sig = window.usersignature; }

wikieditor = $.extend(libs.wikieditor, wikieditor); libs.wikieditor = wikieditor; mw.log.deprecate(window, 'wikieditor', wikieditor); //backward compatibility

})(jQuery, mediaWiki, mediaWiki.libs); //