/* ------------------------------------------------------------------------ *\ copyright: (C) 2010 Maciej Jaros (pl:User:Nux, en:User:EcceNux) license: GNU General Public License v2, http://opensource.org/licenses/gpl-license.php OR CC-BY-SA http://creativecommons.org/licenses/by-sa/3.0/deed.pl Libraries used: * jQuery (keypress, preventDefault) * sel_t * wikibits \* ------------------------------------------------------------------------ */ function nuxTBKeys() { // // VERSION [private:] this.ver = this.version = '0.5.3'; // // [public:] // this.strInput = 'wpTextbox1'; // input name // // [private:] // Colectors and helpers this.strChars = ''; // pressed chars buffer this.strInterestingChars = ''; // chars of interest within the script this.intMaxChars = 0; // max chars that might be interesting for the script this.isDisabled = false; // // [private:] // Replacements array // DO NOT CHANGE DIRECTLY!!! Use addReplacements instead this.arrReplacements = []; } // // Adds a single replacement rule, should be called before init() // // Note: rules added at the begining will be checked first. // Also: rule match will prevent any following from matching. // nuxTBKeys.prototype.escapeStr4RegExp = function(str) { return str.replace(/([\[\]\{\}\|\.\*\?\(\)\$\^\\])/g,'\\$1'); } nuxTBKeys.prototype.addReplacements = function(atr/*{from, to}*/) { // add the replacement rule with some precalculated attributes this.arrReplacements[this.arrReplacements.length] = { chLastChar : atr.from.charAt(atr.from.length-1), reMatch : new RegExp(this.escapeStr4RegExp(atr.from)+'$'), objReplaceAtrs : {from: atr.from.substr(0, atr.from.length-1), to: atr.to} } // re-calculte some main attributes if (this.intMaxCharsthis.intMaxChars) { this.strChars = this.strChars.substr(1); // cut out least important character } // unneeded char if (this.strInterestingChars.indexOf(ch)==-1) { this.strChars = ''; } } // // Replace text that was just put in the input from atr.from to atr.to // nuxTBKeys.prototype.replaceLatestStr = function(atr/*{from, to}*/, chLastChar) { // get current position var inp = document.getElementById(this.strInput); var startScroll = inp.scrollY; var bounds = sel_t.getSelBound(inp); // check if the selected text would be correct var strBeforeCursor = inp.value.substr(bounds.end-atr.from.length, atr.from.length); if (strBeforeCursor!=atr.from) // something went wrong (probably cursor jumped somehow) { return false; } // add last character to enable undo (note: it might replace selected text) sel_t.setSelStr(inp, chLastChar, false); //bounds = sel_t.getSelBound(inp); // re-read bounds // don't work as expected on Opera (Opera seems to reset bounds to the end of the whole text of the input) bounds.start += chLastChar.length; bounds.end = bounds.start; // prepare new bounds bounds.start = bounds.end - atr.from.length - chLastChar.length; if (bounds.start<0) // probably interpretation error { return false; } // select chars to be replaced sel_t.setSelBound(inp, bounds, false); // replace sel_t.setSelStr(inp, atr.to, false); // reset cursor to the end of inserted text //bounds = sel_t.getSelBound(inp); // don't work as expected on IE (IE seems to reset bounds to a start of the selection) bounds.start += atr.to.length; bounds.end = bounds.start; sel_t.setSelBound(inp, bounds, false); // reset position inp.scrollY = startScroll; return true; } // ---------------------------------------------------------------- // // Autokorekta (pl) // // // init object // var oAutokorekta = new nuxTBKeys(); // minus/dywiz na myślnik oAutokorekta.addReplacements ({from:' - ', to:' – '}); // arrows oAutokorekta.addReplacements ({from:' -> ', to:' → '}); oAutokorekta.addReplacements ({from:' <- ', to:' ← '}); // quotes oAutokorekta.addReplacements ({from:' "', to:' „'}); oAutokorekta.addReplacements ({from:'"', to:'”'}); // disable by default //oAutokorekta.toggleDisablance(); // // Button settings // oAutokorekta.oImgbtn = { on : { src : 'http://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Vista-clean.png/22px-Vista-clean.png', alt : 'włączona', title : 'Autokorekta jest włączona' }, off : { src : 'http://upload.wikimedia.org/wikipedia/commons/thumb/6/65/Gnome-window-close.svg/22px-Gnome-window-close.svg.png', alt : 'wyłączona', title : 'Autokorekta jest wyłączona' } } // // Button adder // oAutokorekta.addDisableButon = function() { // add a group in a standard way $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', { 'section': 'advanced', 'groups': { 'autorepcontrol': { 'label': 'Autokorekta' } } } ); // get added group var elGroup; try { var elAdv = getElementsByClassName(document.getElementById('wikiEditor-ui-toolbar'), "div", "section-advanced")[0]; elGroup = getElementsByClassName(elAdv, "div", "group-autorepcontrol")[0]; } catch (e) { return false; } // add button var nel = document.createElement('img'); if (!oAutokorekta.isDisabled) { nel.src = oAutokorekta.oImgbtn.on.src; nel.alt = oAutokorekta.oImgbtn.on.alt; nel.title = oAutokorekta.oImgbtn.on.title; } else { nel.src = oAutokorekta.oImgbtn.off.src; nel.alt = oAutokorekta.oImgbtn.off.alt; nel.title = oAutokorekta.oImgbtn.off.title; } nel.className = "tool tool-button"; nel.style.cssText = "width: 22px; height: 22px; padding-top:2px"; nel.onclick = oAutokorekta.onClickDisableBtn; elGroup.appendChild(nel); // show group elGroup.style.display = ''; // border elGroup.style.cssText = "border-left: 1px solid #DDDDDD"; // seems OK return true; } // // Button click // oAutokorekta.onClickDisableBtn = function() { oAutokorekta.toggleDisablance(); if (this.alt == oAutokorekta.oImgbtn.on.alt) { this.src = oAutokorekta.oImgbtn.off.src; this.alt = oAutokorekta.oImgbtn.off.alt; this.title = oAutokorekta.oImgbtn.off.title; jQuery.cookie("js_oAutokorekta_setup", "off", { expires: 7 }); } else { this.src = oAutokorekta.oImgbtn.on.src; this.alt = oAutokorekta.oImgbtn.on.alt; this.title = oAutokorekta.oImgbtn.on.title; jQuery.cookie("js_oAutokorekta_setup", "on", { expires: 7 }); } // re-set focus try { document.getElementById(this.strInput).focus(); } catch (e) {} } // // Cookie setup stuff // var js_oAutokorekta_setup = ''; if (document.cookie.search(/(^|[ ;])js_oAutokorekta_setup=/)!=-1) { document.cookie.replace(/(^|[ ;])js_oAutokorekta_setup=(.*?)(;|$)/, function(a, s, val, e) { js_oAutokorekta_setup = val; } ) // re-set cookie jQuery.cookie("js_oAutokorekta_setup", js_oAutokorekta_setup, { expires: 7 }); } // setup default state if (js_oAutokorekta_setup != 'on') // defaults to off { oAutokorekta.setDisablance(true); } // // Main hook // // Check if the new toolbar is available if (mw.config.get('wgAction') == 'edit' || mw.config.get('wgAction') == 'submit') { if (mw.config.get('wgNamespaceNumber') > -1) { if ( typeof $j != 'undefined' && typeof $j.fn.wikiEditor != 'undefined' // wyłączone przez użytkownika && !window.isAutokorektaWylaczona) { hookEvent("load", function() { if (oAutokorekta.addDisableButon() || window.isAutokorektaAlwaysOn) { oAutokorekta.init(); } }); } } }