MediaWiki:Common.js

From SMC Wiki
Revision as of 16:59, 29 July 2010 by Hrishikesh.kb (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */
 /*HERE STARTS THE WORKING-CODE OF "METABOXES"*/
 
 /* Funcionament de la Plantilla:Metacaixa
 Implementat per: Usuari:Peleguer.
 Actualitzat per Joanjoc seguint les indicacions d'en Martorell
 */
 
 function MetaCaixaInit(){
  //S'executa al carregar-se la pàgina, si hi ha metacaixes,
  // s'assignen els esdeveniments als botons
  //alert("MetaCaixaInit");
 
  var i=0       //Inicialitzem comptador de caixes
  for (i=0;i<=9;i++){
     var vMc = document.getElementById("mc"+i);
     if (!vMc) break;
     //alert("MetaCaixaInit, trobada Metacaixa mc"+i);
 
     var j=1    //Inicialitzem comptador de botons dins de la caixa
     var vPsIni = 0  //Pestanya visible inicial
     for (j=1;j<=9;j++){
        var vBt = document.getElementById("mc"+i+"bt"+j);
        if (!vBt) break;
        //alert("MetaCaixaInit, trobat botó mc"+i+"bt"+j);
        vBt.onclick = MetaCaixaMostraPestanya;          //A cada botó assignem l'esdeveniment onclick
        //alert (vBt.className);
        if (vBt.className=="mcBotoSel") vPsIni=j;  //Si tenim un botó seleccionat, en guardem l'index
     }
     //alert ("mc="+i+", ps="+j+", psini="+vPsIni );
     if (vPsIni == 0) { //Si no tenim cap botó seleccionat, n'agafem un aleatòriament
         vPsIni = 1+Math.floor((j-1)*Math.random()) ;
         //alert ("Activant Pestanya a l'atzar; _mc"+i+"bt"+vPsIni +"_");
         document.getElementById("mc"+i+"ps"+vPsIni).style.display = "block";
         document.getElementById("mc"+i+"ps"+vPsIni).style.visibility = "visible";
         document.getElementById("mc"+i+"bt"+vPsIni).className="mcBotoSel";
     } 
  }
 }
 
 function MetaCaixaMostraPestanya(){
  //S'executa al clicar una pestanya,
  //aquella es fa visible i les altres s'oculten
  var vMcNom = this.id.substr(0,3); //A partir del nom del botó, deduïm el nom de la caixa
  var vIndex = this.id.substr(5,1); //I l'index
 
  var i=1
  for (i=1;i<=9;i++){        //busquem totes les pestanyes d'aquella caixa
      //alert(vMcNom+"ps"+i);
        var vPsElem = document.getElementById(vMcNom+"ps"+i);
        if (!vPsElem) break;
        if (vIndex==i){ //Si és la pestanya bona la mostrem i canviem la classe de botó
                vPsElem.style.display = "block";
                vPsElem.style.visibility = "visible";
                document.getElementById(vMcNom+"bt"+i).className="mcBotoSel";
        } else {             //Sinó, l'ocultem i canviem la classe de botó
                vPsElem.style.display = "none";
                vPsElem.style.visibility = "hidden";
                document.getElementById(vMcNom+"bt"+i).className="mcBoto";
        }
  }
  return false; //evitem la recàrrega de la pàgina
 }
 
 addOnloadHook(MetaCaixaInit);
 
 /*HERE FINISHES THE WORKING-CODE OF "METABOXES"*/
 /************ Swanalekha code starts here **********************/
var pattern=null;
var tabCount=1;
function bindSwanalekha(widget){

if(widget.aBound){ 
widget.aBound=false;
disable();
return;  
} 

var INSCRIPT={
a:'\u0D4B',
b:'\u0D35',
c:'\u0D2E',
d:'\u0D4D',
e:'\u0D3E',
f:'\u0D3F',
g:'\u0D41',
h:'\u0D2A',
i:'\u0D17',
j:'\u0D30',
k:'\u0D15',
l:'\u0D24',
m:'\u0D38',
n:'\u0D32',
o:'\u0D26',
p:'\u0D1C',
q:'\u0D57',
r:'\u0D40',
s:'\u0D47',
t:'\u0D42',
u:'\u0D39',
v:'\u0D28',
w:'\u0D48',
x:'\u0D02',
y:'\u0D2C',
z:'\u0D46',
A:'\u0D13',
B:'\u0D34',
C:'\u0D23',
D:'\u0D05',
E:'\u0D06',
F:'\u0D07',
G:'\u0D09',
H:'\u0D2B',
I:'\u0D18',
J:'\u0D31',
K:'\u0D16',
L:'\u0D25',
M:'\u0D36',
N:'\u0D33',
O:'\u0D27',
P:'\u0D1D',
Q:'\u0D14',
R:'\u0D08',
S:'\u0D0F',
T:'\u0D0A',
U:'\u0D19',
V:'V',
W:'\u0D10',
X:'\u0D02',
Y:'\u0D2D',
Z:'\u0D0E',
'1':'\u0D67',
'2':'\u0D68',
'3':'\u0D69',
'4':'\u0D6A',
'5':'\u0D6B',
'6':'\u0D6C',
'7':'\u0D6D',
'8':'\u0D6E',
'9':'\u0D6F',
'0':'\u0D66',
'`':'\u0D4A',
'_':'\u0D03',
'~':'\u0D12',
'+':'\u0D0B',
'=':'\u0D43',
'[':'\u0D21',
']':'\u200D',
'{':'\u0D22',
'}':'\u0D1E',
':':'\u0D1B',
';':'\u0D1A',
'':'\u0D1A',
'<':'\u0D37',
'>':'\u200D',
'/':'\u0D2F',
'\"':'\u0D20',
'\'':'\u0D1F',
'\\':'\u200C'
    }; 


function isToggleEvent(event){
 event = (event) ? event : window.event;
  kCode = event.keyCode || event.which; 
  return   ((kCode == 32 && event.ctrlKey) || (kCode == 32 && event.ctrlKey));
};
function enable(){
widget.onkeypress=keypressEnabled;
widget.style.outline = 'dashed 1px red';
};
function disable(){
widget.onkeypress=keypressDisabled;
widget.style.outline = null;

};
function checkBoxListener(){
	if(widget.aBound){
		widget.aBound=false;
		disable();
	 }	
else{
		widget.aBound=true;
		enable();
	}
}
function keypressEnabled(event){
        if (event == undefined)
            event = window.event;
        if(isToggleEvent(event)){
            disable();
            document.getElementById("toggle").checked = false;
            return;
        }
        getWidgetSelectionStart(widget);
        kCode = event.keyCode || event.which; 
    
        if(event.ctrlKey||event. altKey||event.metaKey){
            return true;
        }
        var char=String.fromCharCode(kCode );
        var pos=widget.selectionStart;
        var stepback=0;
        if(!mal) {
            patternStart=widget.selectionStart;
            var mal=INSCRIPT[char];
            stepback=0;
        } 
        if(mal){
            if (isExplorer()) {
                var    range = document.selection.createRange();
                range.moveStart("character", -stepback);
                range.text = mal;
                range.collapse(false);
                range.select();
            }
            else{
                var scrollTop = widget.scrollTop;
                var cursorLoc =  widget.selectionStart;
                var stepback = cursorLoc-patternStart; 
                widget.value=  widget.value.substr(0,patternStart)+mal+widget.value.substr(widget.selectionEnd,widget.value.length); 
                widget.scrollTop=scrollTop ;
                widget.selectionStart = cursorLoc + mal.length  - stepback  ;
                widget.selectionEnd = cursorLoc + mal.length - stepback;
            }    
            return false;
        
        }
        if( kCode ==9){
            return false;
        }
        return true;
    }

function keypressDisabled(event){
if(isToggleEvent(event)){
enable();
document.getElementById("toggle"+widget.id).checked = true;
return false;
}
return true;
};
widget.aBound=false;
disable();
var checkbox = document.getElementById("toggle"+widget.id);
if (checkbox.addEventListener) 
                checkbox.addEventListener("click", checkBoxListener,false);
else if (checkbox.attachEvent) 
                checkbox.attachEvent("onclick", checkBoxListener);
};

 function addCheckbox(textBox) {
            if(textBox==null) return;
            try
            {
            var element = document.createElement("input");
            element.setAttribute("type","checkbox");
            element.setAttribute("id","toggle"+textBox.id);
            element.setAttribute("title","Write using Swanalekha transliteration scheme. You can use Control+Space to toggle.");
            var labelcheckBox = document.createTextNode('Transliterate');
          
            textBox.parentNode.insertBefore(element,textBox);
            document.getElementById("toggle"+textBox.id).checked = textBox.aBound;
            textBox.parentNode.insertBefore(labelcheckBox,textBox);
             var p = document.createElement("br");
            textBox.parentNode.insertBefore(p,textBox);
             }
             catch(ex)
             {
              alert(ex);
             }
}
function bindAllTextElements() {
	try
            {
            	
   
	        	
	var ta=document.getElementsByTagName('textarea');
 
	for(var i=0;i < ta.length;++i){
		addCheckbox(ta[i]);
		bindSwanalekha(ta[i]);
	}
	
	var tb=document.getElementById('wpSummary');
	if(tb){addCheckbox(tb);
	bindSwanalekha(tb);
	}
	var tb=document.getElementById('searchInput');
	if(tb){
	addCheckbox(tb);
	bindSwanalekha(tb); 
   }
   
	 
	
	
	var ifs = document.getElementsByTagName('iframe');	
    var len=ifs.length;
	for (var i=0;i < len; i++) {		
		bindAllTextElements(ifs[i].contentDocument.documentElement);
	}
	}
             catch(ex)
             {
              alert(ex);
             }
 };
function addLoadEvent(func) {
	 
            if (window.addEventListener) {
            	window.addEventListener("load", func, false);
			}
            else if (window.attachEvent) {
            	window.attachEvent("onload", func);
			}
}
 
 function getWidgetSelectionStart (widget) {
	 if( document.selection ){
	// The current selection
	var range = document.selection.createRange();
	// We'll use this as a 'dummy'
	var stored_range = range.duplicate();
	// Select all text
	stored_range.moveToElementText( widget );
	// Now move 'dummy' end point to end point of original range
	stored_range.setEndPoint( 'EndToEnd', range );
	// Now we can calculate start and end points
	widget.selectionStart = stored_range.text.length - range.text.length;
	widget.selectionEnd = widget.selectionStart + range.text.length;
}
 }

addLoadEvent(bindAllTextElements);
/************ Swanalekha code ends here **********************/