MediaWiki:Common.js

From LBA File Information
Revision as of 07:22, 20 May 2013 by Zink (talk | contribs)
Jump to navigation Jump to search

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

function findIndexableTables()
{
  var tabs = document.getElementsByTagName("table");
  var res = new Array();
  for (a = 0; a < tabs.length; a++) {
    var cls = tabs[a].className.split(/\s+/);
    if (cls.indexOf("indexable") > -1)
      res.push(tabs[a]);
  }
  return res;
}

function reindexTable(id)
{
  var cb = document.getElementById('index1_' + id);
  if (cb && reid_tables[id]) {
    var delta;
    if (cb.checked)
      delta = 1;
    else
      delta = -1;
    for (a = 0; a < reid_tables[id].rows.length; a++) {
      var row = reid_tables[id].rows[a];   
      var cell = row.cells[0].innerHTML.trim();     
      
      if (cell != '')     
        if (!isNaN(cell)) //normal index
          row.cells[0].innerHTML = parseInt(cell) + delta;
        else { //range index
          var spl = cell.split("-");
          if (spl.length > 1) {
            spl[0] = spl[0].trim();
            spl[1] = spl[1].trim();
            if (!isNaN(spl[0]) && !isNaN(spl[1])) {
              row.cells[0].innerHTML = (parseInt(spl[0]) + delta) + " - " + (parseInt(spl[1]) + delta);
            }
          }
        }
      }    
  
      //repeated index
      if (row.cells.length > 1) {
        cell = row.cells[1].innerHTML.trim().substr(4);
        if (!isNaN(cell)) {
          var n = parseInt(cell);
          row.cells[1].innerHTML = row.cells[1].innerHTML.replace(n, n + delta);
        }
      }
    }
  }
}

var reid_tables = findIndexableTables();

for (a = 0; a < reid_tables.length; a++) {
  var tr = reid_tables[a].rows[0];
  if (reid_tables[a].caption == null)
    reid_tables[a].createCaption();
  else
    reid_tables[a].caption.innerHTML += '<br>';
  reid_tables[a].caption.innerHTML +=
    '<form><input type="checkbox" name="index1" value="1" id="index1_'+(a)+'"'
  + ' style="margin-left:0;vertical-align:middle"'
  + ' onclick="reindexTable('+a+')" />'
  + '<label for="index1_'+(a)+'" style="vertical-align:middle;font-weight:normal">Index from 1</label>'
  + '</form>';
}