User:Koavf/monobook.js

From translatewiki.net
Jump to: navigation, search
span class="nx">sr$r=$e('srReplace'); sr$re=$e('srRegexp'); sr$mc=$e('srCase'); } } function srShowHide() { if (sr$f.style.display=='none') { sr$f.style.display='block'; sr$i.accessKey="none"; sr$t.style.width='auto'; sr$s.focus(); } else { sr$f.style.display='none'; sr$t.style.width=sr$w; sr$i.accessKey="F"; } } addOnloadHook(srInit); //</nowiki></pre>
">=$e('SearchIcon'); sr$i.accessKey="F"; sr.firstChild.style.display='none'; var ep=$e('editform'); ep.parentNode.insertBefore(sr,ep); sr$f=$e('srForm'); sr$s=$e('srSearch'); start, end); if (yOffset > ta.clientHeight) { yOffset -= Math.floor(ta.clientHeight / 2); ta.scrollTop = yOffset; // Opera does not support setting the scrollTop property if (ta.scrollTop != yOffset) { // todo: Warn the user or apply a workaround } } else { ta.scrollTop = 0; } } else { // IE incorrectly counts '\r\n' as a signle character start -= ta.value.substring(0, start).split('\r').length - 1; end -= ta.value.substring(0, end).split('\r').length - 1; var range = ta.createTextRange(); range.collapse(true); range.moveStart('character', start); range.moveEnd('character', end - start); range.select(); } }; // getPosition(e), observe(e, x, f), stopObserving(e, x, f), // and stopEvent(event) are inspired by the prototype.js framework // http://prototypejs.org/ ct.getPosition = function (e) { var x = 0; var y = 0; do { x += e.offsetLeft || 0; y += e.offsetTop || 0; e = e.offsetParent; } while (e); return {x: x, y: y}; }; ct.observe = function (e, eventName, f) { if (e.addEventListener) { e.addEventListener(eventName, f, false); } else { e.attachEvent('on' + eventName, f); } }; ct.stopObserving = function (e, eventName, f) { if (e.removeEventListener) { e.removeEventListener(eventName, f, false); } else { e.detachEvent('on' + eventName, f); } }; ct.stopEvent = function (event) { if (event.preventDefault) { event.preventDefault(); event.stopPropagation(); } else { event.returnValue = false; event.cancelBubble = true; } }; // ct.anchor() is a shortcut to creating a link as a DOM node: ct.anchor = function (text, href, title) { var e = document.createElement('A'); e.href = href; e.appendChild(document.createTextNode(text)); e.title = title || ''; return e; }; // ct.link() produces the HTML for a link to a Wikipedia article as a string. // It is convenient to embed in a help popup. ct.hlink = function (toWhat, text) { var wgServer = window.wgServer || 'http://en.wikipedia.org'; var wgArticlePath = window.wgArticlePath || '/wiki/$1'; var url = (wgServer + wgArticlePath).replace('$1', toWhat); return '<a href="' + url + '" target="_blank">' + (text || toWhat) + '</a>'; }; // === Helpers a la functional programming === // A higher-order function---produces a cached version of a one-arg function. ct.makeCached = function (f) { var cache = {}; // a closure; the cache is private for f return function (x) { return (cache[x] != null) ? cache[x] : (cache[x] = f(x)); }; }; // === Regular expressions === // Regular expressions can sometimes become inconveniently large. // In order to make complex ones easier to read, we introduce // a set of macros. Tokens enclosed with ``{'' and ``}'' will be // replaced according to the hashtable below. // // To do the replacements, one must pass the RegExp object // through fixRegExp() and use the result instead, like this: // // var re = ct.fixRegExp(/It happened in {month}/); // // Also, for the sake of convenience, we add the "getAllMatches(re, s)" // method, which is a quick means to find all occurrences of a // regex in some text. It returns an array containing the results // of applying RegExp.exec(..). ct.REG_EXP_REPLACEMENTS = { '{letter}': // all Unicode letters // http://www.codeproject.com/dotnet/UnicodeCharCatHelper.asp '\\u0041-\\u005a\\u0061-\\u007a\\u00aa' + '\\u00b5\\u00ba\\u00c0-\\u00d6' + '\\u00d8-\\u00f6\\u00f8-\\u01ba\\u01bc-\\u01bf' + '\\u01c4-\\u02ad\\u0386\\u0388-\\u0481\\u048c-\\u0556' + '\\u0561-\\u0587\\u10a0-\\u10c5\\u1e00-\\u1fbc\\u1fbe' + '\\u1fc2-\\u1fcc\\u1fd0-\\u1fdb\\u1fe0-\\u1fec' + '\\u1ff2-\\u1ffc\\u207f\\u2102\\u2107\\u210a-\\u2113' + '\\u2115\\u2119-\\u211d\\u2124\\u2126\\u2128' + '\\u212a-\\u212d\\u212f-\\u2131\\u2133\\u2134\\u2139' + '\\ufb00-\\ufb17\\uff21-\\uff3a\\uff41-\\uff5a', '{month}': // English only '(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|' + 'January|February|March|April|June|July|August|September|' + 'October|November|December)', '{year}': '[12][0-9]{3}' }; ct.fixRegExp = function (re) { // : RegExp if (re.__fixedRE != null) { return re.__fixedRE; } var s = re.source; for (var alias in ct.REG_EXP_REPLACEMENTS) { s = s.replace( new RegExp(ct.escapeRegExp(alias), 'g'), ct.REG_EXP_REPLACEMENTS[alias] ); } re.__fixedRE = new RegExp(s); // the fixed copy is cached re.__fixedRE.global = re.global; re.__fixedRE.ignoreCase = re.ignoreCase; re.__fixedRE.multiline = re.multiline; return re.__fixedRE; }; ct.escapeRegExp = ct.makeCached(function (s) { // : RegExp var r = ''; for (var i = 0; i < s.length; i++) { var code = s.charCodeAt(i).toString(16); r += '\\u' + '0000'.substring(code.length) + code; } return r; }); ct.getAllMatches = function (re, s) { // : Match[] var p = 0; var a = []; while (true) { re.lastIndex = 0; var m = re.exec(s.substring(p)); if (m == null) { return a; } m.start = p + m.index; m.end = p + m.index + m[0].length; a.push(m); p = m.end; } }; // == Advisor core == // This is the basic functionality of showing and fixing suggestions. // === Global constants and variables === ct.DEFAULT_MAX_SUGGESTIONS = 8; ct.maxSuggestions = ct.DEFAULT_MAX_SUGGESTIONS; ct.suggestions; // : Suggestion[] ct.eSuggestions; // : Element; that's where suggestions are rendered ct.eAddToSummary; // : Element; the proposed edit summary appears there ct.eTextarea; // : Element; the one with id="wpTextbox1" ct.appliedSuggestions = {}; // : Map<String, int> ct.scannedText = null; // remember what we scan, to check if it is // still the same when we try to fix it ct.BIG_THRESHOLD = 100 * 1024; ct.isBigScanConfirmed = false; // is the warning about a big article confirmed ct.isTalkPageScanConfirmed = false; ct.scanTimeoutId = null; // a timeout is set after a keystroke and before // a scan, this variable tracks its id // === int main() === // This is the entry point ct.observe(window, 'load', function () { ct.eTextarea = document.getElementById('wpTextbox1'); if (ct.eTextarea == null) { // This is not an ``?action=edit'' page return; } ct.eSuggestions = document.createElement('DIV'); ct.eSuggestions.style.border = 'dashed #ccc 1px'; ct.eSuggestions.style.color = '#888'; var e = document.getElementById('editform'); while (true) { var p = e.previousSibling; if ( (p == null) || ((p.nodeType == 1) && (p.id != 'toolbar')) ) { break; } e = p; } e.parentNode.insertBefore(ct.eSuggestions, e); ct.eAddToSummary = document.createElement('DIV'); ct.eAddToSummary.style.border = 'dashed #ccc 1px'; ct.eAddToSummary.style.color = '#888'; ct.eAddToSummary.style.display = 'none'; var wpSummaryLabel = document.getElementById('wpSummaryLabel'); wpSummaryLabel.parentNode.insertBefore(ct.eAddToSummary, wpSummaryLabel); ct.scan(); // do a scan now ... ct.observeWikiText(ct.delayScan); // ... and every time the user pauses typing }); // === Internationalisation === // ct._() is a gettext-style internationalisation helper // (http://en.wikipedia.org/wiki/gettext) // If no translation is found for the parameter, it is returned as is. // Additionally, subsequent parameters are substituted for $1, $2, and so on. ct._ = function (s) { if (ct.translation && ct.translation[s]) { s = ct.translation[s]; } var index = 1; while (arguments[index]) { s = s.replace('$' + index, arguments[index]); // todo: replace all? index++; } return s; }; // === Editor compatibility layer === // Controlling access to wpTextbox1 helps abstract out compatibility // with editors like wikEd (http://en.wikipedia.org/wiki/User:Cacycle/wikEd) ct.getWikiText = function () { if (window.wikEdUseWikEd) { var obj = {sel: WikEdGetSelection()}; WikEdParseDOM(obj, wikEdFrameBody); return obj.plain; } return ct.eTextarea.value; }; ct.setWikiText = function (s) { if (window.wikEdUseWikEd) { // todo: wikEd compatibility alert(ct._('Changing text in wikEd is not yet supported.')); return; }; ct.eTextarea.value = s; }; ct.focusWikiText = function () { if (window.wikEdUseWikEd) { wikEdFrameWindow.focus(); return; } ct.eTextarea.focus(); }; ct.selectWikiText = function (start, end) { if (window.wikEdUseWikEd) { var obj = x = {sel: WikEdGetSelection(), changed: {}}; WikEdParseDOM(obj, wikEdFrameBody); var i = 0; while ((obj.plainStart[i + 1] != null) && (obj.plainStart[i + 1] <= start)) {