User:Conrad.Irwin/navframes.js

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

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • 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.

/*</pre>
=== Configuration ===
<pre>*/

var nf_show = 'show';
var nf_hide = 'hide';

var nf_general_state=false; //Global
function nf_state(node){

  //Allow editors to mark them as open by default
  if( node.className.indexOf('Shown') >= 0 ){
    return nf_show;
  }
  if( nf_general_state==false ){
    //Check userprefs
    if( getCookie('WiktionaryPreferencesShowNav') == 'true' ) {
        return nf_general_state=nf_show;
    }else{
      var preview = document.getElementById('wikiPreview');
      if (preview != null) {
        var p = preview.getElementsByTagName('p');
        if (p != null && p.length >= 2 && p[1].firstChild.id == 'Translations') {
          return nf_general_state=nf_show;
        }
      }
      return nf_general_state=nf_hide;
    }
    //Check length of content?
  }
  return nf_general_state;
}
/*</pre>

=== Find NavFrames ===
<pre>*/
function nf_initialise(){

  var divs = document.getElementsByTagName('div');
  
  for( var i=0;i<divs.length;i++ ){
    if( divs[i].className.indexOf('NavFrame') >= 0 ){
      nf_create(divs[i],i,nf_state(divs[i]));
    }
  }
}
/*</pre>
=== Create a Toggle ===
<pre>*/
function nf_create(node,uid,state){

  var head = false;
  var content = false;

  //Find the NavHead and NavContent
  for( var i=0;i<node.childNodes.length;i++ ){
    var child = node.childNodes[i];
    if( child.nodeName.toLowerCase()=='div' ){
      if( child.className.indexOf('NavHead') >= 0 ){
        head = child;
      }else if( child.className.indexOf('NavContent') >= 0 ){
        content = child;
      }
    }
  }
  if(!head || !content) return false;
  
  //Make Nodes
  var span = document.createElement('span');
  span.className='NavToggle editsection'; //Gives consistency with [edit] links
  
  var toggle=document.createElement('a');
  
  //Register Event Handler
  try{
      toggle.addEventListener('click',function(){nf_toggle(toggle)},false);
  }catch(e){
      toggle.attachEvent('onclick',function(){nf_toggle(toggle)});
  }
  
  //Create DOM
  toggle.appendChild(document.createTextNode(state));
  
  span.appendChild(document.createTextNode('['));
  span.appendChild(toggle);
  span.appendChild(document.createTextNode(']'));
  
  head.appendChild(span);
  //Link the two together and go to initial state
  content.setAttribute('id','for_nftog_'+uid);  
  toggle.setAttribute('id','nftog_'+uid);
  nf_toggle(toggle);
}
/*</pre>
=== Click Handler ===
<pre>*/
function nf_toggle(toggle){
  
  var id = toggle.getAttribute('id');
  var state = toggle.childNodes[0].nodeValue;
  var content = document.getElementById('for_'+id);
  
  if(!content) return false;
  
  if( state==nf_hide ){
    content.style.display='none';
    toggle.childNodes[0].nodeValue=nf_show;
  }else{ //guess show
    content.style.display='block';
    toggle.childNodes[0].nodeValue=nf_hide;
  }
}

$(nf_initialise);

 function createNavigationBarToggleButton() {return false;}