MediaWiki:Common.js/Edit tools.js: различия между версиями
Перейти к навигации
Перейти к поиску
Karaby (обсуждение | вклад) Новая страница: «// добавя нови бутони и други играчкиfunction setupCustomEditTools() { var toolbar = document.getElementById("toolbar"); // ако н…» |
Karaby (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
// | /* * * Virtual keyboard characters * * */ | ||
var existChars = false; // are the extra chars already added | |||
/** generate and add extra chars in the element $charsElemId */ | |||
function addChars() { | |||
// if there are extra chars already, do nothing | |||
if ( existChars ) { return; } | |||
var cont = ''; | |||
var len = chars.length; | |||
for (var i in chars) { | |||
for (var j in chars[i]) { | |||
cont += "<a href=\"javascript:insertTags('"+chars[i][j]+"', '', '')\" "+ | |||
'title="' + gLang.msg("et-addchar", chars[i][j]) +'">'+chars[i][j]+'</a> '; | |||
} | |||
if (i != len-1) { cont += '<br/>'; } | |||
} | |||
document.getElementById(charsElemId).innerHTML = cont; | |||
existChars = true; | |||
} | |||
/* * * Extra buttons for text insertion * * */ | |||
/** add some buttons and drop-down menus */ | |||
function setupCustomEditTools() { | |||
var toolbar = document.getElementById("toolbar"); | |||
if ( !toolbar ) { | |||
if ( ! document.getElementById("editform") ) { | |||
return; | |||
} | |||
// check for the enhanced toolbar | |||
if ( getElementsByClassName(document, "div", "toolbar").length ) { | |||
toolbar = putToolbar(true); | |||
} else { | |||
return; | |||
} | |||
} | |||
toolbar.className += " buttonlinks"; | |||
if ( showMenus ) { | |||
// drop-down menus inserting text put direct in the javascript | |||
appendDropDownMenus(toolbar, tplVarBaseName, insertIntoWikiText); | |||
// drop-down menus inserting content from wiki pages | |||
appendDropDownMenus(toolbar, atplVarBaseName, loadPage); | |||
} | |||
if ( showButtons ) { | |||
appendCustomButtons(toolbar); | |||
appendExtraChars(toolbar); | |||
} | |||
} | |||
hookEvent("load", setupCustomEditTools); | |||
function setCustomInsButton(code, left, middle, right, shownText, title) { | |||
customInsButtons[code] = [left, middle, right, shownText, title]; | |||
} | |||
function setCustomMiscButton(code, codeToRun, shownText, title) { | |||
customMiscButtons[code] = [codeToRun, shownText, title]; | |||
} | |||
function rmCustomInsButtons(rmButtons) { | |||
rmCustomButtons(customInsButtons, rmButtons); | |||
} | |||
function rmCustomMiscButtons(rmButtons) { | |||
rmCustomButtons(customMiscButtons, rmButtons); | |||
} | |||
function rmCustomButtons(allButtons, rmButtons) { | |||
for (var i = rmButtons.length - 1; i >= 0; i--) { | |||
delete( allButtons[ rmButtons[i] ] ); | |||
} | |||
} | |||
function appendCustomButtons(parent) { | |||
var buts = document.createElement("div"); | |||
buts.id = "custombuttons"; | |||
for (var i in customInsButtons) { | |||
var el = customInsButtons[i]; | |||
var title = el[4]; | |||
if ( title.charAt(0) == "+" ) { | |||
title = gLang.msg("et-addpref") + title.substr(1); | |||
} | |||
appendCustomButton(buts, | |||
{"href": "javascript:insertTags('"+el[0] +"','"+el[2]+"','"+ el[1]+"')", | |||
"title": title, "innerHTML": el[3]}); | |||
} | |||
for (var i in customMiscButtons) { | |||
var el = customMiscButtons[i]; | |||
appendCustomButton(buts, {"href":"javascript:"+el[0], "title":el[2], "innerHTML":el[1]}); | |||
} | |||
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 appendExtraChars(parent) { | |||
if ( typeof(charsElemId) == "undefined" ) { | |||
return; | |||
} | |||
var chbox = document.createElement("div"); | |||
chbox.id = charsElemId; | |||
chbox.style.display = "none"; | |||
parent.appendChild(chbox); | |||
} | |||
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); | |||
} | |||
} | |||
/** generates a drop-down menu */ | |||
function appendDropDownMenu(parent, content, callback, id) { | |||
var box = document.createElement("select"); | |||
box.id = id; | |||
box.title = gLang.msg("et-ddmenutitle"); | |||
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.createOptgroup(i, opts[i]) | |||
: Creator.createOption(opts[i], i); | |||
box.appendChild(child); | |||
count++; | |||
} | |||
return count; | |||
} | |||
/** show/hide an element */ | |||
function toggleElemDisplay(elemId) { | |||
var elem = document.getElementById(elemId); | |||
elem.style.display = elem.style.display == 'none' ? '' : 'none'; | |||
} | |||
/** | |||
insert an edit toolbar before the textarea. | |||
useful for testing user script pages while previewing | |||
use it with: | |||
putToolbar(); | |||
put in your script page, e.g. User:Your_Name/monobook.js | |||
*/ | |||
function putToolbar(rightNow) { | |||
var toolbar = Creator.createElement("div", {"id" : "toolbar"}); | |||
var putIt = function() { | |||
var editform = document.getElementById("editform"); | |||
editform.parentNode.insertBefore(toolbar, editform); | |||
}; | |||
if ( typeof rightNow != "undefined" && rightNow ) { | |||
putIt(); | |||
} else { | |||
addOnloadHook(putIt); | |||
} | |||
return toolbar; | |||
} | |||
/* * * * * * * * * * 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(gLang.msg("et-ajaxerror", prevReq.status, prevReq.statusText, | |||
pageToFetch, pageUrl)); | |||
return; | |||
} | |||
insertIntoWikiText(prevReq.responseText); | |||
} | |||
function insertIntoWikiText(content) { | |||
// delete text marked for no inclusion + <pre> and <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 ( loadIndicator ) { | |||
loadIndicator.style.display = "block"; | |||
return; | |||
} | |||
loadIndicator = document.createElement("div"); | |||
loadIndicator.id = "loadIndicator"; | |||
loadIndicator.appendChild( document.createTextNode(gLang.msg("et-tplloading")) ); | |||
document.getElementsByTagName("body")[0].appendChild(loadIndicator); | |||
} | |||
function hideLoadIndicator() { | |||
if (loadIndicator) loadIndicator.style.display = "none"; | |||
} | |||
Версия от 12:23, 25 октября 2013
/* * * Virtual keyboard characters * * */
var existChars = false; // are the extra chars already added
/** generate and add extra chars in the element $charsElemId */
function addChars() {
// if there are extra chars already, do nothing
if ( existChars ) { return; }
var cont = '';
var len = chars.length;
for (var i in chars) {
for (var j in chars[i]) {
cont += "<a href=\"javascript:insertTags('"+chars[i][j]+"', '', '')\" "+
'title="' + gLang.msg("et-addchar", chars[i][j]) +'">'+chars[i][j]+'</a> ';
}
if (i != len-1) { cont += '<br/>'; }
}
document.getElementById(charsElemId).innerHTML = cont;
existChars = true;
}
/* * * Extra buttons for text insertion * * */
/** add some buttons and drop-down menus */
function setupCustomEditTools() {
var toolbar = document.getElementById("toolbar");
if ( !toolbar ) {
if ( ! document.getElementById("editform") ) {
return;
}
// check for the enhanced toolbar
if ( getElementsByClassName(document, "div", "toolbar").length ) {
toolbar = putToolbar(true);
} else {
return;
}
}
toolbar.className += " buttonlinks";
if ( showMenus ) {
// drop-down menus inserting text put direct in the javascript
appendDropDownMenus(toolbar, tplVarBaseName, insertIntoWikiText);
// drop-down menus inserting content from wiki pages
appendDropDownMenus(toolbar, atplVarBaseName, loadPage);
}
if ( showButtons ) {
appendCustomButtons(toolbar);
appendExtraChars(toolbar);
}
}
hookEvent("load", setupCustomEditTools);
function setCustomInsButton(code, left, middle, right, shownText, title) {
customInsButtons[code] = [left, middle, right, shownText, title];
}
function setCustomMiscButton(code, codeToRun, shownText, title) {
customMiscButtons[code] = [codeToRun, shownText, title];
}
function rmCustomInsButtons(rmButtons) {
rmCustomButtons(customInsButtons, rmButtons);
}
function rmCustomMiscButtons(rmButtons) {
rmCustomButtons(customMiscButtons, rmButtons);
}
function rmCustomButtons(allButtons, rmButtons) {
for (var i = rmButtons.length - 1; i >= 0; i--) {
delete( allButtons[ rmButtons[i] ] );
}
}
function appendCustomButtons(parent) {
var buts = document.createElement("div");
buts.id = "custombuttons";
for (var i in customInsButtons) {
var el = customInsButtons[i];
var title = el[4];
if ( title.charAt(0) == "+" ) {
title = gLang.msg("et-addpref") + title.substr(1);
}
appendCustomButton(buts,
{"href": "javascript:insertTags('"+el[0] +"','"+el[2]+"','"+ el[1]+"')",
"title": title, "innerHTML": el[3]});
}
for (var i in customMiscButtons) {
var el = customMiscButtons[i];
appendCustomButton(buts, {"href":"javascript:"+el[0], "title":el[2], "innerHTML":el[1]});
}
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 appendExtraChars(parent) {
if ( typeof(charsElemId) == "undefined" ) {
return;
}
var chbox = document.createElement("div");
chbox.id = charsElemId;
chbox.style.display = "none";
parent.appendChild(chbox);
}
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);
}
}
/** generates a drop-down menu */
function appendDropDownMenu(parent, content, callback, id) {
var box = document.createElement("select");
box.id = id;
box.title = gLang.msg("et-ddmenutitle");
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.createOptgroup(i, opts[i])
: Creator.createOption(opts[i], i);
box.appendChild(child);
count++;
}
return count;
}
/** show/hide an element */
function toggleElemDisplay(elemId) {
var elem = document.getElementById(elemId);
elem.style.display = elem.style.display == 'none' ? '' : 'none';
}
/**
insert an edit toolbar before the textarea.
useful for testing user script pages while previewing
use it with:
putToolbar();
put in your script page, e.g. User:Your_Name/monobook.js
*/
function putToolbar(rightNow) {
var toolbar = Creator.createElement("div", {"id" : "toolbar"});
var putIt = function() {
var editform = document.getElementById("editform");
editform.parentNode.insertBefore(toolbar, editform);
};
if ( typeof rightNow != "undefined" && rightNow ) {
putIt();
} else {
addOnloadHook(putIt);
}
return toolbar;
}
/* * * * * * * * * * 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(gLang.msg("et-ajaxerror", prevReq.status, prevReq.statusText,
pageToFetch, pageUrl));
return;
}
insertIntoWikiText(prevReq.responseText);
}
function insertIntoWikiText(content) {
// delete text marked for no inclusion + <pre> and <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 ( loadIndicator ) {
loadIndicator.style.display = "block";
return;
}
loadIndicator = document.createElement("div");
loadIndicator.id = "loadIndicator";
loadIndicator.appendChild( document.createTextNode(gLang.msg("et-tplloading")) );
document.getElementsByTagName("body")[0].appendChild(loadIndicator);
}
function hideLoadIndicator() {
if (loadIndicator) loadIndicator.style.display = "none";
}