МедияУики:Common.js/Edit tools.js: Разлика между версии

ползване на файла от Уикипедия
(функции за падащи менюта)
(ползване на файла от Уикипедия)
document.write('<script type="text/javascript" src="http://bg.wikipedia.org/w/index.php?title=%D0%9C%D0%B5%D0%B4%D0%B8%D1%8F%D0%A3%D0%B8%D0%BA%D0%B8:Common.js/Edit_tools&action=raw&ctype=text/javascript"><\/scr'+'ipt>');
/* * * Extra buttons for text insertion * * */
 
/** добавя нови бутони и други играчки */
function setupCustomEditTools() {
var toolbar = document.getElementById("toolbar");
// ако няма съществуващи бутони, няма да добавяме и тези
if ( !toolbar ) { return; }
toolbar.className += " buttonlinks";
// падащите менюта с празни шаблони
appendDropDownMenus(toolbar, tplVarBaseName, insertIntoWikiText);
// бутоните
appendCustomButtons(toolbar);
}
 
hookEvent("load", setupCustomEditTools);
 
function appendCustomButtons(parent) {
var buts = document.createElement("div");
buts.id = "custombuttons";
for (var i in customInsButtons) {
var el = customInsButtons[i];
var title = el[3];
if ( title.charAt(0) == "+" ) {
title = "Вмъкване на " + title.substr(1);
}
appendCustomButton(buts,
{"href": "javascript:insertTags('"+el[0] +"','"+el[1]+"','"+ el[2]+"')",
"title": title, "innerHTML": el[4]});
}
for (var i in customMiscButtons) {
var el = customMiscButtons[i];
appendCustomButton(buts, {"href":"javascript:"+el[0], "title":el[1], "innerHTML":el[2]});
}
parent.appendChild(buts);
}
 
function appendCustomButton(box, item) {
var b = document.createElement("a");
for (var attr in item) { b[attr] = item[attr]; }
box.appendChild(b);
box.appendChild( document.createTextNode(" ") );
}
 
function appendDropDownMenus(parent, tplVarBaseName, callback) {
var tplVar = null;
for ( var i = 1; tplVar = tplVarBaseName + i,
eval("var tpl = typeof("+ tplVar +") == 'object' ? "+ tplVar +" : null"),
tpl != null; i++ ) {
appendDropDownMenu(parent, tpl, callback, "ddmenu_" + tplVar);
}
}
 
/** генерира падащо меню */
function appendDropDownMenu(parent, content, callback, id) {
var box = document.createElement("select");
box.id = id;
box.title = "Оттук можете да вмъкнете празен шаблон";
box.onchange = function() {
if (this.value != "-") {
callback(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"
? Creator.newOptgroup(i, opts[i])
: Creator.newOption(opts[i], i);
box.appendChild(child);
count++;
}
return count;
}
 
var Creator = {
newOptgroup: function(label, data) {
var g = document.createElement("optgroup");
g.label = label;
for (var i in data) {
g.appendChild( Creator.newOption(data[i], i) );
}
return g;
},
 
newOption: function(val, text) {
var o = document.createElement("option");
o.value = val;
o.appendChild( document.createTextNode(text) );
return o;
}
};
 
 
/* * * * * * * * * * Ajax functions * * * * * * * * * */
 
var prevReq;
var pageUrlTpl = wgScript + "?title=$1&action=raw&templates=expand";
var pageUrl = "";
var pageToFetch = "";
 
function loadPage(page) {
prevReq = sajax_init_object();
if ( !prevReq ) return false;
pageToFetch = page;
pageUrl = pageUrlTpl.replace(/\$1/, encodeURI(page));
showLoadIndicator();
prevReq.onreadystatechange = insertIntoWikiTextFromRequest;
prevReq.open("GET", pageUrl, true);
prevReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
prevReq.send(null);
return true;
}
 
function insertIntoWikiTextFromRequest() {
if ( prevReq.readyState != 4 ) {
return;
}
hideLoadIndicator();
if ( prevReq.status != 200 ) {
window.alert("Неуспешна връзка: " + prevReq.status + " " + prevReq.statusText +
"\nСтраница: "+ pageToFetch + "\nАдрес: "+pageUrl);
return;
}
insertIntoWikiText(prevReq.responseText);
}
 
function insertIntoWikiText(content) {
// изтриване на текст, отбелязан за невмъкване + <pre> и <nowiki>
var re = /<!--noinclude-->.*<!--\/noinclude-->|<\/?pre>|<\/?nowiki>/g;
content = content.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] +">");
}
 
// we can have >>|sample text|<< or >>|<< or just simple text
var parts = null;
var left, right, def = "";
content = escapeNl(content);
if ( ( parts = content.match(/(.*)>>\|(.*)\|<<(.*)/) ) ) {
left = parts[1];
def = parts[2];
right = parts[3];
} else { // no sample text: split at caret’s position
parts = content.split(">>|<<");
left = parts[0];
delete(parts[0]);
right = parts.join("");
}
insertTags(unescapeNl(left), unescapeNl(right), unescapeNl(def));
}
 
function escapeNl(s) { return s.replace(/\n/g, "\x01"); }
function unescapeNl(s) { return s.replace(/\x01/g, "\n"); }
 
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";
}
1646

редакции