User:Inductiveload/TableEditor.js

/* wiki-toolbox.js */ //From http://en.wikipedia.org/User:Gadget850/wiki-toolbox.js (function {    if (document.getElementById('toolbar') !== null) {        var addCSSRule = function (sheet, selector, rule) {                sheet.insertRule(selector + " { " + rule + " }", sheet.cssRules.length);            };        if (document.getElementById("wiki-table-editor-button") == null) {            var editButton = document.createElement("img");            editButton.onclick = function  {                SimileWikiToolbox.editTableInPage;            };            editButton.alt = "Edit Table";            editButton.title = "Edit Table";            editButton.src = "http://simile.mit.edu/repository/wiki-toolbox/trunk/src/editImg.png";            editButton.style.cursor = "pointer";            editButton.id = "wiki-table-editor-button";            var insrtButton = document.createElement("img");            insrtButton.onclick = function  { SimileWikiToolbox.insertTableInPage; };           insrtButton.alt = "Insert Table"; insrtButton.title = "Insert Table"; insrtButton.src = "http://simile.mit.edu/repository/wiki-toolbox/trunk/src/insrtImg.png" insrtButton.style.cursor = "pointer"; insrtButton.id = "wiki-table-insert-button"; var toolbar = document.getElementById("toolbar"); toolbar.appendChild(editButton); toolbar.appendChild(insrtButton); }       var style = document.createElement("style"); document.getElementsByTagName("head")[0].appendChild(style); var sheet = style.sheet; addCSSRule(sheet, "div.wiki-toolbox-ui", "text-align: left"); addCSSRule(sheet, "div.wiki-toolbox-ui", "font-size: 10pt"); addCSSRule(sheet, "div.wiki-toolbox-ui", "z-index: 1000"); addCSSRule(sheet, "span.wiki-toolbox-button-group-label", "color: white"); } }); SimileWikiToolbox = {}; SimileWikiToolbox.editTableInPage = function {    var textarea = document.getElementById("wpTextbox1");    var text = textarea.value;    var cursor = textarea.selectionStart;    SimileWikiToolbox.editTableInText(text, cursor, function (text) { textarea.value = text; }); }; SimileWikiToolbox.editTableInText = function (text, cursor, onDone, onCancel) {   var tableLocation = SimileWikiToolbox.WikiTable.findTable(text, cursor);    var beforeTableText = text.substr(0, tableLocation.tableStart);    var afterTableText = text.substr(tableLocation.tableEnd);    var tableText = text.substring(tableLocation.tableStart, tableLocation.tableEnd);    var parsedResult = SimileWikiToolbox.WikiTable.parseTable(tableText, cursor - tableLocation.tableStart);    SimileWikiToolbox.editTable( parsedResult.wikiTable, function { if (onDone) { onDone(beforeTableText + parsedResult.wikiTable.toWikiText + afterTableText); }   }, function  { if (onCancel) { onCancel; }   }, parsedResult.row, parsedResult.column); }; SimileWikiToolbox.insertTableInPage = function  {    var textarea = document.getElementById("wpTextbox1");    var text = textarea.value;    var cursor = textarea.selectionStart;    SimileWikiToolbox.insertTableInText(text, cursor, function (text) { textarea.value = text; }); }; SimileWikiToolbox.insertTableInText = function (text, cursor, onDone, onCancel) {   var beforeCursorText = text.substr(0, cursor);    var afterCursorText = text.substr(cursor);    var wikiTable = new SimileWikiToolbox.WikiTable("{|\n|-\n!\n!\n|-\n|\n|\n|}");    SimileWikiToolbox.editTable(wikiTable, function  { if (onDone) { onDone(beforeCursorText + wikiTable.toWikiText + afterCursorText); }   }, function  { if (onCancel) { onCancel; }   }); }; var WikiTableEditor = null; SimileWikiToolbox.editTable = function (wikiTable, onDone, onCancel, row, column) {    WikiTableEditor = new SimileWikiToolbox.WikiTableEditor(wikiTable, onDone, onCancel, row, column); }; /* wiki-table-cell.js */ SimileWikiToolbox.WikiTableCell = function (cellText) {    this._content = {};    if (cellText.indexOf("|") > 0) {        var set = cellText.substring(0, cellText.indexOf("|"));        var cellText = cellText.substr(cellText.indexOf("|") + 1);        this._content.settings = new SimileWikiToolbox.WikiTableCellSettings(set);    };    if (cellText.indexOf("\n") < 0) {        cellText += "\n";    };    this._content.text = cellText; }; SimileWikiToolbox.WikiTableCell.prototype.getSettings = function  {    if (this._content.settings) {        return this._content.settings;    }; }; SimileWikiToolbox.WikiTableCell.prototype.setSettings = function (text) { this._content.settings = new SimileWikiToolbox.WikiTableCellSettings(text); }; SimileWikiToolbox.WikiTableCell.prototype.getContent = function { return this._content.text; }; SimileWikiToolbox.WikiTableCell.prototype.setContent = function (text) { this._content.text = text + "\n"; }; SimileWikiToolbox.WikiTableCell.prototype.toWikiText = function { if (this.getSettings != null) { var setObj = this.getSettings; var set = setObj.toWikiText + "|"; } else { var set = ""; };   return set + this._content.text.toString; }; /* wiki-table-editor.js */ SimileWikiToolbox.WikiTableEditor = function (wikiTable, onDone, onCancel, row, column) { this._wikiTable = wikiTable; this._undoList = []; this._redoList = []; this._redid = true; var self = this; var createUIDiv = function { var div = document.createElement("div"); div.className = "wiki-toolbox-ui"; return div; };   var createFixedUIDiv = function (left, right, top, bottom) { var div = createUIDiv; div.style.position = "fixed"; return div; };   var createEdgeBoundDiv = function (left, right, top, bottom) { var div = createFixedUIDiv; div.style.left = left; div.style.right = right; div.style.top = top; div.style.bottom = bottom; return div; };   var createButton = function (parentElmt, label, handler) { var button = document.createElement("button"); button.innerHTML = label; button.onclick = handler; parentElmt.appendChild(button); return button; };   var createInput = function (parentElmt, value, id) { var input = document.createElement("input"); input.type = "text"; input.value = value; input.id = id; parentElmt.appendChild(input); return input; }   var createButtonGroupLabel = function (label) { var span = document.createElement("span"); span.innerHTML = label; span.className = "wiki-toolbox-button-group-label"; return span; };   var createToolbarButton = function (image, label, handler) { if (image) { var button = document.createElement("img"); button.src = image; } else { var button = document.createElement("button"); button.innerHTML = label; button.style.marginBottom = "5px"; };           button.alt = label; button.title = label; button.onclick = handler; toolbarDiv.appendChild(button); };   this._toolbarEntries = []; SimileWikiToolbox.WikiTableEditor.prototype.addToolbarEntry = function (image, label, handler) { this._toolbarEntries.push({           image: image,            label: label,            handler: handler        }); };   this.addToolbarEntry("", "Make Lat/Lng Column", function  {        self.geocodeColumn    }); this.addToolbarEntry("", "Undo", function {        self.undoAction    }); this.addToolbarEntry("", "Redo", function {        self.redoAction    }); this.addToolbarEntry("", "Paste", function {        self.pasteInTextBox    }); var screen = createEdgeBoundDiv("0px", "0px", "0px", "0px"); screen.style.background = "black"; screen.style.opacity = "0.7"; document.body.appendChild(screen); var editingArea = this._editingArea = createEdgeBoundDiv("100px", "20px", "100px", "50px"); editingArea.style.background = "white"; editingArea.style.overflow = "auto"; document.body.appendChild(editingArea); var columnDiv = createFixedUIDiv; columnDiv.style.left = "100px"; columnDiv.style.right = "400px"; columnDiv.style.top = "50px"; columnDiv.style.padding = "5px"; document.body.appendChild(columnDiv); columnDiv.appendChild(createButtonGroupLabel("Insert Column")); createButton(columnDiv, "First", function (evt) {       self.insertColumnFirst;    }); createButton(columnDiv, "Last", function (evt) {       self.insertColumnLast;    }); columnDiv.appendChild(document.createTextNode(" ")); createButton(columnDiv, "Left", function (evt) {       self.insertColumnLeft;    }); createButton(columnDiv, "Right", function (evt) {       self.insertColumnRight;    }); columnDiv.appendChild(createButtonGroupLabel(" Move Column")); createButton(columnDiv, "Left", function (evt) {       self.moveColumnLeft;    }); columnDiv.appendChild(document.createTextNode(" ")); createButton(columnDiv, "Right", function (evt) {       self.moveColumnRight;    }); var settingsDiv = createFixedUIDiv; settingsDiv.style.right = "20px"; settingsDiv.style.top = "30px"; settingsDiv.style.padding = "0px"; settingsDiv.appendChild(createButtonGroupLabel("Change settings for: ")); createButton(settingsDiv, "Table", function (evt) {       self.changeTableSet;    }); createButton(settingsDiv, "Row", function (evt) {       self.changeRowSet;    }); createButton(settingsDiv, "Column", function (evt) {       self.changeColSet;    }); createButton(settingsDiv, "Cell", function (evt) {       self.changeCellSet;    }); settingsDiv.appendChild(document.createTextNode(" ")); var input = document.createElement("input"); input.id = "settings-input"; input.style.width = "300px"; input.value = "(press enter to save settings)"; input.onfocus = function (evt) { if (input.value == "saved" || input.value == "currently has no settings" || input.value == "(press enter to save settings)") { input.value = "" };   };    settingsDiv.appendChild(input); document.body.appendChild(settingsDiv); var toolbarDiv = createFixedUIDiv; toolbarDiv.style.right = "20px"; toolbarDiv.style.top = "77px"; toolbarDiv.style.padding = "0px"; document.body.appendChild(toolbarDiv); for (i = 0; i < this._toolbarEntries.length; i++) { var entry = this._toolbarEntries[i]; createToolbarButton(entry.image, entry.label, entry.handler); };   var rowDiv = createFixedUIDiv; rowDiv.style.left = "10px"; rowDiv.style.top = "100px"; rowDiv.style.width = "80px"; rowDiv.style.padding = "5px"; document.body.appendChild(rowDiv); rowDiv.appendChild(createButtonGroupLabel("Insert Row")); createButton(rowDiv, "Top", function (evt) {       self.insertRowFirst;    }).style.width = "100%"; createButton(rowDiv, "Bottom", function (evt) {       self.insertRowLast;    }).style.width = "100%"; createButton(rowDiv, "Above", function (evt) {       self.insertRowAbove;    }).style.width = "100%"; createButton(rowDiv, "Below", function (evt) {       self.insertRowBelow;    }).style.width = "100%"; rowDiv.appendChild(createButtonGroupLabel(" Move Row")); createButton(rowDiv, "Up", function (evt) {       self.moveRowUp;    }).style.width = "100%"; createButton(rowDiv, "Down", function (evt) {       self.moveRowDown;    }).style.width = "100%"; rowDiv.appendChild(createButtonGroupLabel(" Delete")); createButton(rowDiv, "Row", function (evt) {       self.deleteRow;    }).style.width = "100%"; createButton(rowDiv, "Column", function (evt) {       self.deleteCol;    }).style.width = "100%"; var finalDiv = createFixedUIDiv; finalDiv.style.left = "100px"; finalDiv.style.right = "20px"; finalDiv.style.bottom = "0px"; finalDiv.style.height = "40px"; finalDiv.style.textAlign = "center"; finalDiv.style.padding = "5px"; document.body.appendChild(finalDiv); var dismantle = function { document.body.removeChild(screen); document.body.removeChild(columnDiv); document.body.removeChild(settingsDiv); document.body.removeChild(toolbarDiv); document.body.removeChild(rowDiv); document.body.removeChild(editingArea); document.body.removeChild(finalDiv); };   createButton(finalDiv, "Save", function  {        dismantle;        if (onDone) {            onDone;        }    }); createButton(finalDiv, "Cancel", function {        dismantle;        if (onCancel) {            onCancel;        }    }); this._renderTable; }; SimileWikiToolbox.WikiTableEditor.prototype.pasteInTextBox = function { var self = this; this._removeCellEditingUI; var pasteDiv = document.createElement("div"); pasteDiv.id = "paste-div"; pasteDiv.style.top = "150px"; pasteDiv.style.bottom = "150px"; pasteDiv.style.left = "300px"; pasteDiv.style.right = "300px"; pasteDiv.style.position = "absolute"; pasteDiv.style.zIndex = "2000"; var pasteArea = document.createElement("textarea"); pasteArea.style.height = "200px"; pasteArea.id = "text-paste-area"; pasteArea.style.backgroundColor = "#ddd"; pasteArea.defaultValue = "Paste tab-separated values here." pasteArea.focus; var okButton = document.createElement("button"); okButton.innerHTML = "OK"; okButton.onclick = function { self.insertTabVals; };   var cancelButton = document.createElement("button"); cancelButton.innerHTML = "Cancel"; cancelButton.onclick = function { self.removePasteDiv; };   pasteDiv.appendChild(pasteArea); pasteDiv.appendChild(okButton); pasteDiv.appendChild(cancelButton); document.body.appendChild(pasteDiv); }; SimileWikiToolbox.WikiTableEditor.prototype.removePasteDiv = function { var div = document.getElementById("paste-div"); document.body.removeChild(div); }; SimileWikiToolbox.WikiTableEditor.prototype.insertTabVals = function { var self = this; var oldCol = this._currentCol; var oldRow = this._currentRow; var values = document.getElementById("text-paste-area").value; var pasteVals = []; var theRow = values.split("\n"); this.removePasteDiv; for (i = 0; i < theRow.length; i++) { pasteVals.push(theRow[i].split("\t")); };   this.doAction(

function { self._currentRow = oldRow; for (j = 0; j < pasteVals.length; j++) { self._currentCol = oldCol; if (j == 0 && self._wikiTable.getRow(self._currentRow).getCell(self._currentCol).getContent != "\n") { var newRow = oldRow++; self._currentRow++; self._wikiTable.insertRow(self._currentRow); };           if (j > 0) { self._wikiTable.insertRow(self._currentRow + 1); self._currentRow++; };           var oneRow = pasteVals[j]; for (k = 0; k < oneRow.length; k++) { if (!self._wikiTable.getRow(self._currentRow).getCell(self._currentCol)) { for (m = 0; m < self._wikiTable.getRowCount; m++) { self._wikiTable.getRow(m).insertCell(self._currentCol); };               };                self._wikiTable.getRow(self._currentRow).getCell(self._currentCol).setContent(oneRow[k]); self._currentCol++; };       };        self._currentCol = oldCol; self._currentRow = newRow ? newRow : oldRow; self._removeCellEditingUI; self._renderTable; }, function { self._removeCellEditingUI; oldRow--; for (n = 0; n < pasteVals.length; n++) { self._wikiTable.removeRow(oldRow + 1); };       self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.insertRowFirst = function {    var self = this;    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.insertRow(0); self._currentRow++; self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable.removeRow(0); self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.insertRowLast = function {    var self = this;    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.insertRow(self._wikiTable.getRowCount); self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable.removeRow(self._wikiTable.getRowCount - 1); self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.insertRowAbove = function {    var self = this;    var row = this._currentRow    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.insertRow(row); self._currentRow++; self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable.removeRow(row); self._currentRow--; self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.insertRowBelow = function {    var self = this;    var row = this._currentRow;    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.insertRow(row + 1); self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable.removeRow(row + 1); self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.insertColumnFirst = function {    var self = this;    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.insertColumn(0); self._currentCol++; self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable.removeColumn(0); self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.insertColumnLast = function {    var self = this;    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.insertColumn(self._wikiTable.getColumnCount); self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable.removeColumn(self._wikiTable.getColumnCount - 1); self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.insertColumnLeft = function {    var self = this;    var col = this._currentCol;    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.insertColumn(col); self._currentCol++; self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable.removeColumn(col); self._currentCol--; self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.insertColumnRight = function {    var self = this;    var col = this._currentCol;    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.insertColumn(col + 1); self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable.removeColumn(col + 1); self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.moveRowUp = function {    var self = this;    var row = this._currentRow;    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.moveRow(row, row - 1); self._currentRow = row - 1; self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable.moveRow(row - 1, row); self._currentRow = row; self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.moveRowDown = function {    var self = this;    var row = this._currentRow;    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.moveRow(row, row + 1); self._currentRow = row + 1; self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable.moveRow(row + 1, row); self._currentRow = row; self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.moveColumnLeft = function {    var self = this;    var col = this._currentCol;    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.moveColumn(col, col - 1); self._currentCol = col - 1; self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable.moveColumn(col - 1, col); self._currentCol = col; self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.moveColumnRight = function {    var self = this;    var col = this._currentCol;    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.moveColumn(col, col + 1); self._currentCol = col + 1; self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable.moveColumn(col + 1, col); self._currentCol = col; self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.deleteRow = function {    var self = this;    var row = this._currentRow    var rowVal = this._wikiTable.getRow(row);    this.doAction(

function { self._removeCellEditingUI; self._wikiTable.removeRow(row); self._currentRow--; self._renderTable; }, function { self._removeCellEditingUI; self._wikiTable._rows.splice(row, 0, rowVal); self._currentRow++; self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.deleteCol = function {    var self = this;    var col = this._currentCol;    var colVal = [];    this.doAction(

function { self._removeCellEditingUI; for (var i = 0; i < self._wikiTable.getRowCount; i++) { if (self._wikiTable.getRow(i).getCell(col)) { colVal.push(self._wikiTable._rows[i]._row.cells.splice(col, 1)); };       };        self._renderTable; }, function { self._removeCellEditingUI; for (var j = 0; j < self._wikiTable.getRowCount; j++) { if (self._wikiTable.getRow(j).getCell(col)) { var cellVal = colVal[j]; self._wikiTable._rows[j]._row.cells.splice(col, 0, cellVal[0]); };       };        self._renderTable; }); }; SimileWikiToolbox.WikiTableEditor.prototype.changeTableSet = function {    var self = this;    var text = document.getElementById("settings-input");    var table = this._wikiTable;    var settings = table.getSettings != null ? table.getSettings : "";    text.value = settings == "" ? "currently has no settings" : settings.toWikiText;    text.blur;    text.onkeydown = function (event) {        window.event ? keynum = event.keycode : keynum = event.which;        if (keynum == "13") {            table.setSettings(text.value);            text.value = "saved";        };    }; }; SimileWikiToolbox.WikiTableEditor.prototype.changeRowSet = function  {    var self = this;    var text = document.getElementById("settings-input");    if (!this._currentRow) {        text.value = "no row selected";    };    try {        var row = this._wikiTable.getRow(this._currentRow); var settings = row.getSettings != null ? row.getSettings : ""; text.value = settings == "" ? "currently has no settings" : settings.toWikiText; text.blur; } catch (e) {}; text.onkeydown = function (event) { window.event ? keynum = event.keycode : keynum = event.which; if (keynum == "13") { row.setSettings(text.value); text.value = "saved"; };   }; }; SimileWikiToolbox.WikiTableEditor.prototype.changeColSet = function  { var self = this; var text = document.getElementById("settings-input"); if (!this._currentCol) { text.value = "no column selected"; };   try { for (i = 0; i < this._wikiTable.getRowCount; i++) { var col = this._wikiTable.getRow(i).getCell(this._currentCol); var settings = col.getSettings != null ? col.getSettings : ""; if (settings == "") { break; };       };        text.value = settings == "" ? "currently has no settings" : settings.toWikiText; text.blur; } catch (e) {}; text.onkeydown = function (event) { window.event ? keynum = event.keycode : keynum = event.which; if (keynum == "13") { for (j = 0; j < self._wikiTable.getRowCount; j++) { var cell = self._wikiTable.getRow(j).getCell(self._currentCol); cell.setSettings(text.value); };           text.value = "saved"; };   }; }; SimileWikiToolbox.WikiTableEditor.prototype.changeCellSet = function  { var self = this; var text = document.getElementById("settings-input"); if (!this._currentCol) { text.value = "no cell selected"; };   try { var cell = this._wikiTable.getRow(this._currentRow).getCell(this._currentCol); var settings = cell.getSettings != null ? cell.getSettings : ""; text.value = settings == "" ? "currently has no settings" : settings.toWikiText; text.blur; } catch (e) {}; text.onkeydown = function (event) { window.event ? keynum = event.keycode : keynum = event.which; if (keynum == "13") { cell.setSettings(text.value); text.value = "saved"; };   }; }; SimileWikiToolbox.WikiTableEditor.prototype._renderTable = function  { var wikiTable = this._wikiTable; var div = this._editingArea; div.innerHTML = ""; var self = this; var installHandlers = function (td, row, col) { td.onclick = function { self._focusOnCell(td, row, col); };       };    var table = document.createElement("table"); table.border = "1"; table.style.textAlign = "left"; div.appendChild(table); for (var i = 0; i < wikiTable.getRowCount; i++) { var row = wikiTable.getRow(i); var tr = table.insertRow(i); for (var j = 0; j < wikiTable.getColumnCount; j++) { var td = document.createElement(row.isHead ? "th" : "td"); tr.appendChild(td); if (row.getCell(j) || row.getCell(j) == "") { this._renderCell(td, row.getCell(j)); installHandlers(td, i, j); } else { this._renderCell(td, ""); installHandlers(td, i, j); };           if (i == this._currentRow && j == this._currentCol) { this._focusOnCell(td, i, j); }       };    }; }; SimileWikiToolbox.WikiTableEditor.prototype._focusOnCell = function (td, row, col) { var self = this; this._removeCellEditingUI; this._currentRow = row; this._currentCol = col; this._currentTD = td; var cell = this._wikiTable.getRow(row).getCell(col); var textBox = document.createElement("input"); textBox.style.border = "none"; textBox.style.padding = "0"; textBox.style.margin = "0"; textBox.style.background = "#FFFFE0"; textBox.style.width = td.offsetWidth + "px"; textBox.style.height = td.offsetHeight + "px"; textBox.value = cell.getContent; var currentText = cell.getContent; textBox.onkeydown = function (event) { window.event ? keynum = event.keycode : keynum = event.which; if (keynum == "13") { self._removeCellEditingUI; };   };    td.innerHTML = ""; td.appendChild(textBox); textBox.focus; }; SimileWikiToolbox.WikiTableEditor.prototype._removeCellEditingUI = function { if (this._currentTD) { var cell = this._wikiTable.getRow(this._currentRow).getCell(this._currentCol); if (cell) { cell.setContent(this._currentTD.firstChild.value); }       this._renderCell(this._currentTD, cell); this._currentTD = null; } }; SimileWikiToolbox.WikiTableEditor.prototype._renderCell = function (td, cell) { if (cell) { var content = cell.getContent; if (content == "" || content == null || content == "\n") { var emptyDiv = document.createElement("div"); emptyDiv.style.width = "80px"; emptyDiv.style.height = "20px"; td.appendChild(emptyDiv); } else if (content.search(/.jpg/i) > 0 || content.search(/.png/i) > 0 || content.search(/.gif/i) > 0) { td.innerHTML = ""; } else { td.innerHTML = content; }   } else { td.innerHTML = ""; }; }; SimileWikiToolbox.WikiTableEditor.prototype.doAction = function (doFunc, undoFunc) { this._undoList.unshift({       doFunc: doFunc,        undoFunc: undoFunc    }); this._undoList[0].doFunc; }; SimileWikiToolbox.WikiTableEditor.prototype.undoAction = function { var undo = this._undoList; var redo = this._redoList; if (undo[0]) { if (undo[1] && this._redid == false) { undo.shift; };       var lastAction = undo[0]; this._redid = false; this._removeCellEditingUI; redo.unshift({           func: lastAction.doFunc        }); lastAction.undoFunc; this._renderTable; }; }; SimileWikiToolbox.WikiTableEditor.prototype.redoAction = function { var redo = this._redoList; if (redo[0]) { this._removeCellEditingUI; var action = redo.shift; action.func; this._redid = true; this._renderTable; }; }; /* wiki-table-geocoding.js */ SimileWikiToolbox.WikiTableEditor.prototype.geocodeColumn = function { if ((typeof GClientGeocoder == "undefined") && (document.getElementById('google-maps-iframe') == null)) { var iframe = document.createElement('iframe'); iframe.src = wikiURL + '/Special:GoogleMaps'; iframe.setAttribute('style', 'display: none'); iframe.id = 'google-maps-iframe'; document.getElementsByTagName('body')[0].appendChild(iframe); } else { this.doGeocode; } } SimileWikiToolbox.WikiTableEditor.prototype.doGeocode = function { if (typeof GClientGeocoder == "undefined") { var GClientGeocoder = document.getElementById('google-maps-iframe').contentWindow.GClientGeocoder; }   var addressToLatLng = {}; var badAddresses = {}; var lookupAddress = function (address, f) { if (address in addressToLatLng) { var latlng = addressToLatLng[address]; window.setTimeout(function {                    f(latlng);                }, 0); } else if (address in badAddresses) { window.setTimeout(function {                    f(null);                }, 0); } else { var geocoder = new GClientGeocoder; geocoder.getLocations(               address, function (json) {                    if ("Placemark" in json) {                        json.Placemark.sort(function (p1, p2) { return p2.AddressDetails.Accuracy - p1.AddressDetails.Accuracy; });                   }                    if ("Placemark" in json && json.Placemark.length > 0) {                        var coords = json.Placemark[0].Point.coordinates;                        var latlng = {                            lat: coords[1],                            lng: coords[0]                        };                        addressToLatLng[address] = latlng;                        f(latlng);                        return;                    }                    badAddresses[address] = true;                    f(null);                }); }       };    var wikiTable = this._wikiTable; var columnIndex = this._currentCol; this._wikiTable.insertColumn(this._currentCol + 1); wikiTable.getRow(0).getCell(columnIndex + 1).setContent('LatLng'); var currentRow = 1; var self = this; var doNext = function { if (currentRow < wikiTable.getRowCount) { var row = wikiTable.getRow(currentRow); lookupAddress(               row.getCell(columnIndex).getContent, function (latlng) {                    if ((latlng) && "lat" in latlng) {                        var latlngData = latlng.lat + ', ' + latlng.lng                        row.getCell(columnIndex + 1).setContent(latlngData);                    }                    currentRow++;                    doNext;                }); } else { self._removeCellEditingUI; self._renderTable; }       }    doNext; } /* wiki-table-row.js */ SimileWikiToolbox.WikiTableRow = function (rowText) { if (rowText == null) { this._row = { isHead: false, cells: [] };   } else { this._row = { isHead: rowText.indexOf("!") >= 0, cells: [] };       if (rowText.search("[a-z]") < rowText.indexOf("\n")) { var string = rowText.substring(rowText.search("[a-z]"), rowText.indexOf("\n")); rowText = rowText.substr(rowText.indexOf("\n")); this._row.settings = new SimileWikiToolbox.WikiTableRowSettings(string); };       if (this._row.isHead) { if (rowText.indexOf("!!") > 0) { var cellTexts = rowText.substr(2).split("!!"); } else { var cellTexts = rowText.substr(2).split("\n!"); };       } else { if (rowText.indexOf("||") > 0) { var cellTexts = rowText.substr(2).split("||"); } else { var cellTexts = rowText.substr(2).split("\n|"); };       };        for (var j = 0; j < cellTexts.length; j++) { this._row.cells.push(new SimileWikiToolbox.WikiTableCell(cellTexts[j])); };   }; }; SimileWikiToolbox.WikiTableRow.prototype.isHeaderRow = function  { return this._row.isHead; }; SimileWikiToolbox.WikiTableRow.prototype.setHeader = function (value) { this._row.isHead = value; }; SimileWikiToolbox.WikiTableRow.prototype.getSettings = function { if (this._row.settings) { return this._row.settings; }; }; SimileWikiToolbox.WikiTableRow.prototype.setSettings = function (text) { this._row.settings = new SimileWikiToolbox.WikiTableRowSettings(text); }; SimileWikiToolbox.WikiTableRow.prototype.getCells = function { return this._row.cells; }; SimileWikiToolbox.WikiTableRow.prototype.getCellCount = function { return this._row.cells.length; }; SimileWikiToolbox.WikiTableRow.prototype.getCell = function (index) { return this._row.cells[index]; }; SimileWikiToolbox.WikiTableRow.prototype.insertCell = function (index) { if (index || index == 0) { this._row.cells.splice(index, 0, new SimileWikiToolbox.WikiTableCell("\n")); } else { this._row.cells.push(new SimileWikiToolbox.WikiTableCell("\n")); }; }; SimileWikiToolbox.WikiTableRow.prototype.toWikiText = function { if (this.getSettings != null) { var setObj = this.getSettings; var set = setObj.toWikiText; } else { var set = ""; };   if (this.isHeaderRow) { var headerArray = []; for (var j = 0; j < this.getCellCount; j++) { headerArray.push(this.getCell(j).toWikiText); };       return "|-" + set + "\n!" + headerArray.join("!"); } else { var rowArray = []; for (var k = 0; k < this.getCellCount; k++) { rowArray.push(this.getCell(k).toWikiText); };       return "|-" + set + "\n|" + rowArray.join("|"); }; }; /* cell-settings.js */ SimileWikiToolbox.WikiTableCellSettings = function (settings) { this._cellSettings = { settings: [] };   var settingsArr = settings.split(";"); for (i = 0; i < settingsArr.length; i++) { this._cellSettings.settings.push(settingsArr[i]); }; }; SimileWikiToolbox.WikiTableCellSettings.prototype.toWikiText = function { return this._cellSettings.settings.join(";"); }; /* row-settings.js */ SimileWikiToolbox.WikiTableRowSettings = function (settings) { this._rowSettings = { settings: [] };   var settingsArr = settings.split(";"); for (i = 0; i < settingsArr.length; i++) { this._rowSettings.settings.push(settingsArr[i]); }; }; SimileWikiToolbox.WikiTableRowSettings.prototype.toWikiText = function { return this._rowSettings.settings.join(";"); }; /* table-settings.js */ SimileWikiToolbox.WikiTableSettings = function (settings) { this._tableSettings = { settings: [] };   var settingsArr = settings.split(";"); for (i = 0; i < settingsArr.length; i++) { this._tableSettings.settings.push(settingsArr[i]); }; }; SimileWikiToolbox.WikiTableSettings.prototype.toWikiText = function { return this._tableSettings.settings.join(";"); }; /* wiki-table.js */ SimileWikiToolbox.WikiTable = function (tableText) { this._rows = []; if (tableText) { if (tableText.indexOf("id=") >= 0) { var start = tableText.indexOf("id="); var idEnd = tableText.indexOf("\"", start + 4);           var end = tableText.indexOf("\n");            var beginning = tableText.substring(2, start);            var ending = tableText.substring(idEnd + 1, tableText.indexOf("\n"));            var settingStr = beginning.concat(ending);        } else if (tableText.search("[a-z]") < tableText.indexOf("\n")) {            var start = 2;            var end = tableText.indexOf("\n");            var settingStr = tableText.substring(start, end);        };        if (settingStr) {            this._settings = new SimileWikiToolbox.WikiTableSettings(settingStr);            if (idEnd) {                var rowTexts = tableText.substring(end + 3, tableText.length - 2).split("|-");                var idText = tableText.substring(start, idEnd + 2)                rowTexts.unshift(idText);            } else {                var rowTexts = tableText.substring(end + 3, tableText.length - 2).split("|-"); };       } else { var rowTexts = tableText.substring(2, tableText.length - 2).split("|-"); };       this._tablePrologue = rowTexts[0]; for (var i = 1; i < rowTexts.length; i++) { var row = new SimileWikiToolbox.WikiTableRow(rowTexts[i]); this._rows.push(row); };   } else { this._rows.push(new SimileWikiToolbox.WikiTableRow); }; }; SimileWikiToolbox.WikiTable.findTable = function (text, cursor) { var beforeCursor = text.substr(0, cursor); var afterCursor = text.substr(cursor); var tableStart = beforeCursor.lastIndexOf("{|"); if (tableStart < 0) { tableStart = afterCursor.indexOf("{|"); if (tableStart >= 0) { tableStart += beforeCursor.length; } else { alert("No table to edit"); return; }   }    var tableEnd = text.indexOf("|}"); if (tableEnd < 0) { alert("Table has no end"); return; } else { tableEnd += 2; }   return { tableStart: tableStart, tableEnd: tableEnd }; }; SimileWikiToolbox.WikiTable.parseTable = function (text, cursor) { var table = new SimileWikiToolbox.WikiTable(text); var cursorRow = 0; var cursorCol = 0; return { wikiTable: table, cursorRow: cursorRow, cursorCol: cursorCol }; }; SimileWikiToolbox.WikiTable.prototype.getSettings = function { if (this._settings) { return this._settings; }; }; SimileWikiToolbox.WikiTable.prototype.setSettings = function (text) { this._settings = new SimileWikiToolbox.WikiTableSettings(text); }; SimileWikiToolbox.WikiTable.prototype.getRowCount = function { return this._rows.length; }; SimileWikiToolbox.WikiTable.prototype.getColumnCount = function { return this._rows[0].getCellCount; }; SimileWikiToolbox.WikiTable.prototype.getRow = function (rowIndex) { return this._rows[rowIndex]; }; SimileWikiToolbox.WikiTable.prototype.setHeaderRow = function (row) { for (var i = 0; i < this.getRowCount; i++) { if (this.getRow(i).isHeaderRow) { this.getRow(i).setHeader(false); };   };    row.setHeader(true); }; SimileWikiToolbox.WikiTable.prototype.insertRow = function (index) { var newRow = new SimileWikiToolbox.WikiTableRow; for (var i = 0; i < this.getColumnCount; i++) { newRow.insertCell; };   if (index == 0) { this.setHeaderRow(newRow); };   this._rows.splice(index, 0, newRow); }; SimileWikiToolbox.WikiTable.prototype.insertColumn = function (index) { for (var i = 0; i < this.getRowCount; i++) { if (this.getRow(i).getCellCount < index) { for (j = this.getRow(i).getCellCount; j < index + 2; j++) { this.getRow(i).insertCell(j); };       } else { this.getRow(i).insertCell(index); };   }; }; SimileWikiToolbox.WikiTable.prototype.moveRow = function (fromIndex, toIndex) { if (fromIndex == 0) { this.setHeaderRow(this.getRow(1)); };   if (toIndex == 0) { this.setHeaderRow(this.getRow(fromIndex)); };   var movingRow = this.getRow(fromIndex); this._rows.splice(fromIndex, 1); this._rows.splice(toIndex, 0, movingRow); }; SimileWikiToolbox.WikiTable.prototype.moveColumn = function (fromIndex, toIndex) { for (var i = 0; i < self.getRowCount; i++) { var movingCell = self.getRow(i).getCell(fromIndex); self._rows[i]._row.cells.splice(fromIndex, 1); self._rows[i]._row.cells.splice(toIndex, 0, movingCell); }; }; SimileWikiToolbox.WikiTable.prototype.removeRow = function (index) { if (index == 0) { this.setHeaderRow(this.getRow(1)); };   this._rows.splice(index, 1); }; SimileWikiToolbox.WikiTable.prototype.removeColumn = function (index) { for (var i = 0; i < this.getRowCount; i++) { if (this.getRow(i).getCell(index)) { this._rows[i]._row.cells.splice(index, 1); };   }; }; SimileWikiToolbox.WikiTable.prototype.toWikiText = function  { if (this.getSettings != null) { var setObj = this.getSettings; var set = setObj.toWikiText + " "; } else { var set = ""; };   var arr = []; for (var i = 0; i < this.getRowCount; i++) { arr.push(this.getRow(i).toWikiText); };   return "{|" + set + this._tablePrologue + arr.join("") + "|}"; };