МедияУики:Common.js/Edit tools.js

От БГ-Фантастика
Направо към: навигация, търсене
// добавя нови бутони и други играчки
function setupCustomEditTools() {
	var toolbar = document.getElementById("toolbar");
	// ако няма съществуващи бутони, няма да добавяме и тези
	if ( !toolbar ) { return; }
	toolbar.className += " buttonlinks";
	var tools = document.createElement("div");
	tools.id = "custombuttons";
	for (var i in customInsButtons) {
		var el = customInsButtons[i];
		var title = el[3];
		if ( title.charAt(0) == "+" ) {
			title = "Вмъкване на " + title.substr(1);
		}
		addCustomButton(tools,
			{"href": "javascript:insertTags('"+el[0] +"','"+el[1]+"','"+ el[2]+"')",
			"title": title, "innerHTML": el[4]});
	}
	for (var i in customMiscButtons) {
		var el = customMiscButtons[i];
		addCustomButton(tools, {"href":"javascript:"+el[0], "title":el[1], "innerHTML":el[2]});
	}
	addDropDownMenus(toolbar); // падащите менюта
	toolbar.appendChild(tools);
}

hookEvent("load", setupCustomEditTools);

function addCustomButton(box, item) {
	var b = document.createElement("a");
	for (var attr in item) { b[attr] = item[attr]; }
	box.appendChild(b);
	box.appendChild( document.createTextNode(" ") );
}

function addDropDownMenus(parent) {
	var tplVar = null;
	for ( var i = 1; tplVar = tplVarBaseName + i,
			eval("var tpl = typeof("+ tplVar +") == 'object' ? "+ tplVar +" : null"),
			tpl != null; i++ ) {
		addDropDownMenu(parent, tpl, tplDescriptions[i]);
	}
}

/** генерира падащо меню */
function addDropDownMenu(parent, content, title) {
	var box = document.createElement("select");
	box.title = "Оттук можете да вмъкнете " + title;
	box.onchange = function() {
		if (this.value != "-") {
			loadPage(this.value);
			this.selectedIndex = 0;
		}
		return;
	};
	if ( appendOptions(box, content) > 1 ) {
		parent.appendChild(box);
	}
}


function appendOptions(box, opts) {
	var count = 0;
	for (var i in opts) {
		if (opts[i] == "") {
			continue; // skip emtpy entries
		}
		var child = typeof(opts[i]) == "object"
			? newOptgroup(i, opts[i])
			: newOption(opts[i], i);
		box.appendChild(child);
		count++;
	}
	return count;
}

function newOptgroup(label, data) {
	var g = document.createElement("optgroup");
	g.label = label;
	for (var i in data) {
		g.appendChild( newOption(data[i], i) );
	}
	return g;
}

function newOption(val, text) {
	var o = document.createElement("option");
	o.value = val;
	o.appendChild( document.createTextNode(text) );
	return o;
}


/* скрива/показва елемент */
function toggleElemDisplay(elemId) {
	var elem = document.getElementById(elemId);
	elem.style.display = elem.style.display == 'none' ? '' : 'none';
}


/* * * * * * * * * *   Ajax functions   * * * * * * * * * */

var prevReq;
if ( typeof(wgScript) == "undefined" ) {
	var wgScript = "/index.php";
}
var pageUrl = wgScript + "?title=$1&action=raw";
var pageToFetch = "";

function loadPage(page) {
	prevReq = sajax_init_object();
	if( !prevReq ) return false;
	pageToFetch = page;
	var getUrl = pageUrl.replace(/\$1/, encodeURI(page));
	showLoadIndicator();
	prevReq.onreadystatechange = insertIntoWikiText;
	prevReq.open("GET", getUrl, true);
	prevReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	prevReq.send(null);
	return true;
}

function insertIntoWikiText() {
	if( prevReq.readyState != 4 ) {
		return;
	}
	hideLoadIndicator();
	if ( prevReq.status != 200 ) {
		window.alert("Неуспешна връзка: " + prevReq.status +
			" “" + prevReq.statusText + "”\nСтраница: "+ pageToFetch);
		return;
	}
	// изтриване на текст, отбелязан за невмъкване + <pre> и <nowiki>
	var re = /<!--noinclude-->.*<!--\/noinclude-->|<\/?pre>|<\/?nowiki>/g;
	var content = prevReq.responseText.replace(re, "");
	// replace escaped tags
	var specials = ["pre", "nowiki"];
	for (var i in specials) {
		re = new RegExp("\\[(\/?)"+ specials[i] +"\\]", "g");
		content = content.replace(re, "<$1"+ specials[i] +">");
	}
	// split at caret’s position
	var parts = content.split(">>|<<");
	var left = parts[0];
	delete(parts[0]);
	var right = parts.join("");
	insertTags(left, right, "");
}

var loadIndicator;
function showLoadIndicator() {
	if ( typeof(loadIndicator) != "undefined" ) {
		loadIndicator.style.display = "block";
		return;
	}
	var content = document.getElementById("content");
	if ( !content ) { // no "content" element
		return;
	}
	loadIndicator = document.createElement("div");
	loadIndicator.id = "loadIndicator";
	loadIndicator.appendChild( document.createTextNode("Шаблонът се зарежда…") );
	content.appendChild(loadIndicator);
}

function hideLoadIndicator() {
	loadIndicator.style.display = "none";
}