User:Karl Dickman/tablebutton.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

//============================================================
// Table generator
//============================================================

/**
 *
 * English: Generate an array using Mediawiki syntax
 *
 * @author: fr:user:dake (language conversion and  new options added by en:user:Voice of All)
 * @version: 0.1
 */
 
function generateTable(nbCol, nbRow, exfield)
{
        code = '\n{| class="wikitable"\n';
        code += "|+ TABLE CAPTION <!-- if required -->\n";
        if (exfield==true) code += '!\n';
        
        for (var i=1;i<nbCol+1;i++) code += '! FIELD ' + i + '\n';

        var items = 0;
        for (var j=0;j<nbRow;j++) 
        {
                   if (exfield==true) { 
                        items++;        
                        code += '|-\n'
                        code += '! ITEM ' + items + '\n';
                   }  else if (exfield==false) {                
                        code += '|-\n'
                   }    
                for (var i=0;i<nbCol;i++) code += '| element\n';
        }
        code += '|}\n';
        insertTags('','', code); 
}

/**
 *
 * English: Open a popup with parameters to generate an array. 
 * The number of rows/columns can be modified. Some additional
 * parameters are related to templates available on :fr
 *
 * @author: fr:user:dake
 * @version: 0.1
 */
 
function popupTable()
{
  var popup = window.open('about:blank','WPtable','height=350,width=400,scrollbars=yes');
  
  javaCode = '<script type="text\/javascript">function insertCode(){';
  javaCode += 'var row = parseInt(document.paramForm.inputRow.value); ';
  javaCode += 'var col = parseInt(document.paramForm.inputCol.value); ';
  javaCode += 'var exfield = document.paramForm.inputItems.checked; ';
  javaCode += 'window.opener.generateTable(col,row,exfield);';
  javaCode += 'window.close(); '
  javaCode += '}<\/script>';
  
  popup.document.write('<html><head><title>Make table</title>');
  popup.document.write('<script type="text\/javascript" src="\/skins-1.5\/common\/wikibits.js"><!-- wikibits js --><\/script>');
  popup.document.write('<style type="text\/css" media="screen,projection">/*<![CDATA[*/ @import "\/skins-1.5\/wikistandard\/main.css?5"; /*]]>*/<\/style>');
  popup.document.write(javaCode); 
  popup.document.write('</head><body>');
  popup.document.write('<p>Enter the table parameters below: </p>');
  popup.document.write('<form name="paramForm">');
  popup.document.write('Number of rows: <input type="text" name="inputRow" value="3" ><p>');
  popup.document.write('Number of columns: <input type="text" name="inputCol" value="3" ><p>');
  popup.document.write('Item column: <input type="checkbox" name="inputItems" ><p>');
  popup.document.write('</form">');
  popup.document.write('<i>The default table contains one row of fields.</i><p>');
  popup.document.write('Check "Item column" to give the table a field row <i>and</i> an item column.</i><p>');
  popup.document.write('<p><a href="javascript:insertCode()"> Insert table</a>     |');
  popup.document.write('    <a href="javascript:self.close()">Cancel</a></p>');
  popup.document.write('</body></html>');
  popup.document.close();
}

//Ressemble à la fonction de /skins-1.5/commons/wikibits.js pour insérer un autre lien que insertTags
function table_button()
{
 var toolbar = document.getElementById('toolbar');
 if (!toolbar) return false;
 
 var textbox = document.getElementById('wpTextbox1');
 if (!textbox) return false;
 
 if (!document.selection && textbox.selectionStart == null)
 return false;
 
 var image = document.createElement("img");
 image.width = 23;
 image.height = 22;
 image.src = 'http://upload.wikimedia.org/wikipedia/commons/0/04/Button_array.png';
 image.border = 0;
 image.alt = 'Table';
 image.title = 'Make table';
 image.style.cursor = "pointer";
 image.onclick = function() {
   popupTable();
   return false;
 }
 toolbar.appendChild(image);
}

// Changes made on Sat, 30 Sep 2006 20:28 UTC, seems to work on IE
addOnloadHook(table_button);