User:Hippietrail/citetab.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.

importScript('User:Hippietrail/hippajax.js');
 
// TODO support Citations:xxx namespace and xxx/Ciations pseudo-subpage
// TODO use api.php instead of lookforlink() so we can look for several links at once:
// TODO  format=jsonfm&action=query&redirects&titles=pyramid|Talk:pyramid|Citations:pyramid|pyramid/Citations

// look for a wiki link with a specific title attribute and return the link's class name
// possible returns: null, '', 'new', 'new selected', 'selected new'
function lookforlink(tabli, title) {
  var foundclass = 'dunno';
 
  var bc, allas = -1; 
 
  if (bc = document.getElementById('bodyContent'))
    allas = bc.getElementsByTagName('a'); 
 
  if (allas != -1) {
    for (var i = 0; i < allas.length; i++) {
      if (allas[i].title == title) {
        foundclass = allas[i].className;
        break;
      }
    }
  }
 
  // no link in this document so use ajax
  if (foundclass == 'dunno') {
    var li = tabli;
    var url = wgScriptPath + '/index.php?title=' + title + '&action=raw';
    function on200(req) {
      li.className = null;
    };
    function on404() {
      li.className = 'new';
      li.firstChild.href = li.firstChild.href.replace(/\/wiki\/(.*)/, '/w/index.php?title=$1&action=edit');
    };
    ajax(url, on200, on404);
  }
 
  return foundclass;
}
 
// set or change the class and href of a tab
function fixtab(li, classes, title)
{
  if (classes && classes.match(/\bnew\b/))
    li.firstChild.href = wgScriptPath + '/index.php?title=' + title + '&action=edit';
  else
    li.firstChild.href = '/wiki/' + title;
 
  if (classes != null)
    li.className = classes;
  else
    //li.removeAttribute('className');
    li.className = null;
}
 
// add citations tab
function addCiteTab() {
  var currentpagetype = 'dunno';
 
  var maintitle;
  var talktitle;
  var citetitle;
 
  // make the correct article names for each tab
  switch (wgNamespaceNumber)
  {
  case 0:
    if (wgTitle.search(/\/Citations$/) != -1) {
      currentpagetype = 'cite';
      maintitle = wgTitle.replace(/^(.*)\/Citations$/, '$1');
      talktitle = 'Talk:' + maintitle;
      citetitle = wgTitle;
    } else {
      currentpagetype = 'article';
      maintitle = wgTitle;
      talktitle = 'Talk:' + maintitle;
      citetitle = wgTitle + '/Citations';
    }
    break;
  case 1:
    currentpagetype = 'talk';
    maintitle = wgTitle;
    talktitle = 'Talk:' + maintitle;
    // TODO xxx/Citations if it exists and Citations:xxx doesn't, Citations:xxx otherwise
    citetitle = wgTitle + '/Citations';
    break;
  case 114:
    currentpagetype = 'newcite';
    maintitle = wgTitle;
    talktitle = 'Talk:' + maintitle;
    citetitle = 'Citations:' + maintitle;
    break;
  default:
    return;
  }
 
  var maintabli = document.getElementById('ca-nstab-main');
  var talktabli = document.getElementById('ca-talk');
  var newcitetabli = document.getElementById('ca-nstab-citations');
  var citetabli = document.createElement('li');
  citetabli.id = 'ca-cite';
  citetabli.appendChild(document.createElement('a'));
  citetabli.firstChild.appendChild(document.createTextNode('citations'));
 
  if (maintabli == null && newcitetabli != null) maintabli = newcitetabli;

  if (currentpagetype == 'article' || currentpagetype == 'talk') {
    fixtab(citetabli, lookforlink(citetabli, citetitle), citetitle);
 
  } else if (currentpagetype == 'cite' || currentpagetype == 'newcite') {
    // move article tab href and classes to cite tab
    fixtab(citetabli, maintabli.className, citetitle);
 
    fixtab(maintabli, lookforlink(maintabli, maintitle), maintitle);
    fixtab(talktabli, lookforlink(talktabli, talktitle), talktitle);
  }
 
  // add the cite tab
  talktabli.parentNode.insertBefore(citetabli, talktabli.nextSibling);
 
  localisecitetab(citetabli);
}
 
//////////////////////////////////////////////////////////////////////
 
//importScript('User:Hippietrail/wfMsgCentre.js');
document.write('<script type="text/javascript" src="/w/index.php?title=User:Hippietrail/wfMsgCentre.js&action=raw&ctype=text/javascript"><\/script>');
 
var wgCiteTabMsgArray;
 
function localisecitetab(citetabli) {
  if (wgNamespaceNumber == 0 || wgNamespaceNumber == 1 || wgNamespaceNumber == 114) {
    var msgctr = wfMsgCentreFactory.create();
    wgCiteTabMsgArray = new Object();
 
    wgCiteTabMsgArray['Citations'] = { forcontent: true, obj: citetabli.firstChild.firstChild, attr: 'nodeValue' };
    wgCiteTabMsgArray['Tooltip-ca-citations'] = { forcontent: true, obj: citetabli.firstChild, attr: 'title' };
    // TODO accesskey
 
    msgctr.batch(wgCiteTabMsgArray);
  }
}