User:Karlfk/fixconverttosvg.js

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
 * SORT CONVERT-TO-SVG TAGS
 * @created 2006-02-21
 * @source [[c:Template:Convert to SVG]]
 * @revision 13:48, 25 August 2014 (UTC)
 * @author [[User:Ilmari Karonen]], 2006, 2010
 * @author [[c:User:Perhelion]], 2010, 2014
 * Commons: [[Category:User scripts]] <nowiki>
 * @license released in the public domain
 */
/* jshint undef: false, unused: false */
/*global jQuery, mediaWiki; convertToSVGTypes*/
(function ($, mw) {
	"use strict";
	function replaceTag ( eForm ) {
		var re = /\{\{(Convert[_ ]?to|To|2|Should[_ ]?Be)?[_ ]?(?:SVG|Vectorize|In SVG konvertieren)[^}]*?\}\}\s*/i,
			c, par = "",
			txt = eForm.wpTextbox1.value;
		while ( re.test(txt) )
			txt = txt.replace(RegExp.lastMatch, ""),
			par = /\|[^|}]*\s*(\|[^|}]+)/.exec(RegExp.lastMatch) || par;
		par = par[1] || par;
		return [txt, par];
	}

	function toSVGfile ( e ) {
		var type = e,
			eForm = document.eForm;
		if (!eForm) return;
		if (!e) e = window.Event;
		if (e && typeof(e) !== 'string') {
			e = e.target;
			type = e.innerText;
			//if (e.nodeName == "A") return;
			if (e.preventDefault) e.preventDefault();
		}
		//console.log(e, type, e.nodeName);  //TEST
		var txt = replaceTag(eForm);
		eForm.wpTextbox1.value = "{{Convert to SVG|"+ type + txt[1] +"}}\n"+ txt[0];
		eForm.wpSummary.value = "sorting tag Convert to SVG|" + type +"+ ([[User:Perhelion/fixconverttosvg.js|using gadget]])";
		eForm.wpMinoredit.checked = true;
		if (typeof(e) === 'string')
			eForm.wpSave.click(); // I'm feeling lucky!
		return false;
	}

	function createSection () {
		var types = makeTypes();
		$('#wpTextbox1').wikiEditor('addToToolbar', {
			sections : {
				'ToSVG' : {
					label: 'ToSVG',
					type : 'booklet',
					pages : {
						'tosvg' : {
							label : 'Convert to SVG:',
							layout : 'characters',
							characters : types
						}
					}
				}
			}
		});
		$('.wikiEditor-ui-toolbar .page-tosvg span').off("click").click( toSVGfile );
	}

	function makeTypes () {
		var types = ["alphabet", "art", "biology", "chemical", "chemistry", "circuit", "coat of arms", "diagram", "flag", "graph", "icon", "logo", "map", "math", "military insignia", "musical notation", "physical", "signature", "sport", "symbol", "technology", "text"],
			typec = "", t;
		if (typeof(convertToSVGTypes) !== 'undefined') {
			typec = convertToSVGTypes;
			if (typeof(typec) == 'string')
				typec = typec.split(/,\s*/);
			else if ($.isArray(typec)) types = []; // reset default
		}
		//console.log(types, typec, typec.length)
		// append default values
		for (t=0; t<typec.length; t++)
			if (types.indexOf(typec[t]) === -1)
				types.push(typec[t]);
		return types.sort();
	}

	function toSVGcategory () {
		var types = makeTypes(),
			url, frag, link, t;
		if (/(\S*) images that should use vector graphics$/.exec(wgTitle)) // remove current type from sub-cat
			types.splice(types.indexOf(RegExp.$1),1);
		var tLen = types.length; // performance?
		$('#mw-category-media li.gallerybox').width("250").children('div').width("250").find('div.gallerytext>a').after(function(){
			url = wgScript +"?title=File:"+ (/\/wiki\/File:([^\n]*?$)/.exec(this.href)[1])+"&action=edit&fixconverttosvg=";
			frag = [document.createElement('br')];
			for (t = 0; t < tLen; t++) { // this is faster than jQuery
				link = document.createElement('a');
				link.href = url+types[t];
				link.text = types[t];
				link.target = "_tab";
				link.onclick = toSVGfile;
				frag.push(document.createTextNode(" ["));
				frag.push(link);
				frag.push(document.createTextNode("]"));
			}
			return frag;
		});
	}
	mw.hook( 'wikipage.content' ).add(
		function () {
			if (wgNamespaceNumber === 14 && wgTitle.indexOf('mages that should use vector graphics') !== -1)
				toSVGcategory();
			else if (wgNamespaceNumber === 6 && ["edit", "submit"].indexOf(wgAction) !== -1 && !/SVG/i.test(wgTitle.slice(-3))) {
				if (/[?&]fixconverttosvg=([^&]*)/.test(window.location.search) && wgAction === 'edit')
					toSVGfile(decodeURI(RegExp.$1));
				else
					mw.loader.using('user.options', function () {
						if (mw.user.options.get( 'usebetatoolbar' ) && mw.user.options.get( 'showtoolbar' ))
							$.when(mw.loader.using(['ext.wikiEditor']), $.ready).then(createSection);
					});
			}
		});
})(jQuery, mediaWiki);