User:Yair rand/keyboards.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.


mw.loader.using("jquery.textSelection",function(){
  $(document).ready(function($){
try{
    var z = document.getElementById("p-search");
    z.style.marginLeft = "2.5em"
    z = z.insertBefore(newNode("a",newNode("img",{src:"//upload.wikimedia.org/wikipedia/commons/2/2e/Farm-Fresh_keyboard.png"}),{href:"#",style:"float:left;margin-left:-2.2em;margin-top:3px;"}), z.firstChild);
    var isOpen = false, targetl = $.cookie("searchkb") || 'he';
    function transliterate(e){
      e = (e || event).keyCode;
      if (!(e >= 33 && e <= 40) && e != 8 && e != 46 && e != 27 && e != 16){
        var thiz = this;
        setTimeout(function(){
          var c = $(s).textSelection('getCaretPosition'), newvalue = '', currentLetter, v, target = keys[targetl], source = keys.en, 
          enk = keys.en + keys.en.toUpperCase() + '{}:"<>?';
          // todo: rewrite this section
          for(var i = 0; i < thiz.value.length; i++){
            currentLetter = thiz.value[i];
            if(enk.indexOf(currentLetter) != -1){
              if(source.indexOf(currentLetter) == -1){
                v = (LC[currentLetter]?target[source.indexOf(LC[currentLetter])]:target[source.toUpperCase().indexOf(currentLetter)]).toUpperCase();
                newvalue += v != " " ? v : currentLetter;
              } else {
                v = target[source.indexOf(currentLetter)];
                newvalue += v != " " ? v : currentLetter;
              }
            } else {
              newvalue += currentLetter;
            }
          }
          thiz.value = newvalue;
          $(s).textSelection('setSelection', {start:c})
        },0)
      }
    }
    var LC = {"{":"[","}":"]",":":";",'"':"'","<":",",">":".","?":"/"};

    var keys = {
      en: "qwertyuiop[]asdfghjkl;'zxcvbnm,./",
      he: "  קראטוןםפ  שדגכעיחלךף זסבהנמצתץ ",
      eo: "ŝŭ   ĝ    ĵĥ            ĉ        ",
      ru: "йцукенгшщзхъфывапролджэячсмитьбю " // not actually sure if this is accurate, tbh
    }
    function fillkeyboard(){
      keyboard.appendChild(newNode('div',{style:"float:left;width:100%;border-bottom:1px solid #CCCCCC;"}))
      for(var i in keys){
        if(i=="en")continue; 
        (function(i){keyboard.firstChild.appendChild(newNode("a", i+' ', {href:"#",click:function(){
          targetl=i;
          $.cookie("searchkb", i, {expires:30,path:"/"});
          $(keyboard).empty();
          fillkeyboard();
          s.focus();
        }}))})(i)
      }
      for(var i = 0; i < keys[targetl].length; i++){
        (function(l){
          keyboard.appendChild(newNode('div',{class:'yrkey',style:((i==0||i==12||i==23)?'clear:left;margin-left:'+(i?i==12?5:10:0)+'px;':''), click:function(){if(l==' '){return}$("#searchInput").textSelection('encapsulateSelection',{'pre':l})}}, l))
        })(keys[targetl][i])
      }
    }

    var s = document.getElementById("searchInput"), keyboard = null;
    z.onmousedown = function(e){
      
      if(isOpen){
        $(s).unbind("keypress", transliterate)
        keyboard.style.display = 'none';
        isOpen = false;
        return false;
      }
      $(s).keypress(transliterate); // todo: don't mess up suggestbox
      if(!keyboard){
        mw.util.addCSS(".yrkey{float:left;width:15px;text-align:center;height:25px;border:1px solid #D3D3D3;cursor:pointer;border-top-width:0px;}.yrkey:hover{background-color:#EEEEEE;}") // todo: less ugly
        keyboard = document.getElementsByTagName("body")[0].appendChild(newNode('div', {style:'position:absolute;background-color:white;top:63px;right:200px;border:1px solid #000;'}));
        fillkeyboard()
      } else {
        keyboard.style.display = 'block';
      }
      isOpen = true;
      s.focus();
      return false;
    }

}catch(e){window.console&&console.log&&console.log(e)}
  })
}) //todo: don't break like crazy