User:Manishearth/WikiTable.js

/*Copied and modified from. */

/* wiki-toolbox.js */ var parsedResult; var cellSettings=false; //Screws up templates if true, but screws up on cell settings if false; var donotappend=false;// disable appending of extra cells to short rows var exclmn="@@EXCL@@";

$(function{

if(document.getElementById('toolbar')!==null||document.getElementById('wikiEditor-ui-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 tb=document.getElementById("toolbar"); if(tb==null){ tb=document.getElementById("wikiEditor-ui-toolbar"); } tb.appendChild(editButton);

tb.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.getElementsByTagName("textarea")[0];

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);

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.getElementsByTagName("textarea")[0];

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(cellSettings){ 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);

}; }else{ this._content.settings=null; } if(cellText.charAt(cellText.length-1)!="\n"){

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.replace(exclmn,"!");

};

/* wiki-table-editor.js */

SimileWikiToolbox.WikiTableEditor=function(wikiTable,onDone,onCancel,row,column){

this._wikiTable=wikiTable;

this._undoList=[];

this._redoList=[];

this._redid=true;

var me=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{me.geocodeColumn});

this.addToolbarEntry("","Undo",function{me.undoAction});

this.addToolbarEntry("","Redo",function{me.redoAction});

this.addToolbarEntry("","Paste",function{me.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){

me.insertColumnFirst;

});

createButton(columnDiv,"Last",function(evt){

me.insertColumnLast;

});

columnDiv.appendChild(document.createTextNode(" "));

createButton(columnDiv,"Left",function(evt){

me.insertColumnLeft;

});

createButton(columnDiv,"Right",function(evt){

me.insertColumnRight;

});

columnDiv.appendChild(createButtonGroupLabel(" Move Column"));

createButton(columnDiv,"Left",function(evt){

me.moveColumnLeft;

});

columnDiv.appendChild(document.createTextNode(" "));

createButton(columnDiv,"Right",function(evt){

me.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){

me.changeTableSet;

});

createButton(settingsDiv,"Row",function(evt){

me.changeRowSet;

});

createButton(settingsDiv,"Column",function(evt){

me.changeColSet;

});

createButton(settingsDiv,"Cell",function(evt){

me.changeCellSet;

});

/*MY BUTTONS */ columnDiv.appendChild(createButtonGroupLabel("My Buttons"));

createButton(columnDiv,"Merge columns",function(evt){ var s=prompt("Give destination column, source column, and delimiter, separated by commas. Column numbering starts at 1.","1,2,;").split(","); me.mergeColumn(parseInt(s[0]),parseInt(s[1]),s[2]);

});

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){

me.insertRowFirst;

}).style.width="100%";

createButton(rowDiv,"Bottom",function(evt){

me.insertRowLast;

}).style.width="100%";

createButton(rowDiv,"Above",function(evt){

me.insertRowAbove;

}).style.width="100%";

createButton(rowDiv,"Below",function(evt){

me.insertRowBelow;

}).style.width="100%";

rowDiv.appendChild(createButtonGroupLabel(" Move Row"));

createButton(rowDiv,"Up",function(evt){

me.moveRowUp;

}).style.width="100%";

createButton(rowDiv,"Down",function(evt){

me.moveRowDown;

}).style.width="100%";

rowDiv.appendChild(createButtonGroupLabel(" Delete"));

createButton(rowDiv,"Row",function(evt){

me.deleteRow;

}).style.width="100%";

createButton(rowDiv,"Column",function(evt){

me.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 me=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{me.insertTabVals;};

var cancelButton=document.createElement("button");

cancelButton.innerHTML="Cancel";

cancelButton.onclick=function{me.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 me=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;i0){

me._wikiTable.insertRow(me._currentRow+1);

me._currentRow++;

};

var oneRow=pasteVals[j];

for(k=0;k<oneRow.length;k++){

if(!me._wikiTable.getRow(me._currentRow).getCell(me._currentCol)){

for(m=0;m<me._wikiTable.getRowCount;m++){

me._wikiTable.getRow(m).insertCell(me._currentCol);

};

};

me._wikiTable.getRow(me._currentRow).getCell(me._currentCol).setContent(oneRow[k]);

me._currentCol++;

};

};

me._currentCol=oldCol;

me._currentRow=newRow?newRow:oldRow;

me._removeCellEditingUI;

me._renderTable;

},

function{

me._removeCellEditingUI;

oldRow--;

for(n=0;n<pasteVals.length;n++){

me._wikiTable.removeRow(oldRow+1);

};

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.insertRowFirst=function{

var me=this;

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.insertRow(0);

me._currentRow++;

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable.removeRow(0);

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.insertRowLast=function{

var me=this;

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.insertRow(me._wikiTable.getRowCount);

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable.removeRow(me._wikiTable.getRowCount-1);

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.insertRowAbove=function{

var me=this;

var row=this._currentRow

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.insertRow(row);

me._currentRow++;

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable.removeRow(row);

me._currentRow--;

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.insertRowBelow=function{

var me=this;

var row=this._currentRow;

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.insertRow(row+1);

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable.removeRow(row+1);

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.insertColumnFirst=function{

var me=this;

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.insertColumn(0);

me._currentCol++;

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable.removeColumn(0);

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.insertColumnLast=function{

var me=this;

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.insertColumn(me._wikiTable.getColumnCount);

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable.removeColumn(me._wikiTable.getColumnCount-1);

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.insertColumnLeft=function{

var me=this;

var col=this._currentCol;

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.insertColumn(col);

me._currentCol++;

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable.removeColumn(col);

me._currentCol--;

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.insertColumnRight=function{

var me=this;

var col=this._currentCol;

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.insertColumn(col+1);

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable.removeColumn(col+1);

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.moveRowUp=function{

var me=this;

var row=this._currentRow;

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.moveRow(row,row-1);

me._currentRow=row-1;

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable.moveRow(row-1,row);

me._currentRow=row;

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.moveRowDown=function{

var me=this;

var row=this._currentRow;

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.moveRow(row,row+1);

me._currentRow=row+1;

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable.moveRow(row+1,row);

me._currentRow=row;

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.moveColumnLeft=function{

var me=this;

var col=this._currentCol;

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.moveColumn(col,col-1);

me._currentCol=col-1;

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable.moveColumn(col-1,col);

me._currentCol=col;

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.moveColumnRight=function{

var me=this;

var col=this._currentCol;

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.moveColumn(col,col+1);

me._currentCol=col+1;

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable.moveColumn(col+1,col);

me._currentCol=col;

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.deleteRow=function{

var me=this;

var row=this._currentRow

var rowVal=this._wikiTable.getRow(row);

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.removeRow(row);

me._currentRow--;

me._renderTable;

},

function{

me._removeCellEditingUI;

me._wikiTable._rows.splice(row,0,rowVal);

me._currentRow++;

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.deleteCol=function{

var me=this;

var col=this._currentCol;

var colVal=[];

this.doAction(

function{

me._removeCellEditingUI;

for(var i=0;i<me._wikiTable.getRowCount;i++){

if(me._wikiTable.getRow(i).getCell(col)){

colVal.push(me._wikiTable._rows[i]._row.cells.splice(col,1));

};

};

me._renderTable;

},

function{

me._removeCellEditingUI;

for(var j=0;j<me._wikiTable.getRowCount;j++){

if(me._wikiTable.getRow(j).getCell(col)){

var cellVal=colVal[j];

me._wikiTable._rows[j]._row.cells.splice(col,0,cellVal[0]);

};

};

me._renderTable;

}

);

};

SimileWikiToolbox.WikiTableEditor.prototype.changeTableSet=function{

var me=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 me=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 me=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<me._wikiTable.getRowCount;j++){

var cell=me._wikiTable.getRow(j).getCell(me._currentCol);

cell.setSettings(text.value);

};

text.value="saved";

};

};

};

SimileWikiToolbox.WikiTableEditor.prototype.changeCellSet=function{

var me=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 me=this;

var installHandlers=function(td,row,col){

td.onclick=function{

me._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 me=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"){

me._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 me=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{ me._removeCellEditingUI; me._renderTable; } } doNext; }

/* wiki-table-row.js */

SimileWikiToolbox.WikiTableRow=function(rowText){

if(rowText==null){

this._row={

isHead:false,

cells:[]

};

}else{

this._row={

isHead:rowText.indexOf("\n!")>=0,

cells:[]

};

if(rowText.search("[a-z]")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).replace("!",exclmn).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){ if(index>=this._row.cells.length){ return new SimileWikiToolbox.WikiTableCell(""); } 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"));};

};

var headerlen=0; SimileWikiToolbox.WikiTableRow.prototype.toWikiText=function{

if(this.getSettings!=null){

var setObj=this.getSettings;

var set=setObj.toWikiText;

}else{

var set="";

}; var undef; var c2=new SimileWikiToolbox.WikiTableCell(" "); if(this.isHeaderRow){ var headerArray=[]; for(var j=0;j<this.getCellCount;j++){ if(this.getCell(j)===undef){ headerArray.push(c2.toWikiText); continue; } headerArray.push(this.getCell(j).toWikiText);

};

return"|-"+set+"\n!"+headerArray.join("!");

}else{

var rowArray=[]; if(headerlen==0||donotappend||headerlen<this.getCellCount){ headerlen=this.getCellCount } for(var k=0;k<headerlen;k++){ if(this.getCell(k)===undef){ rowArray.push(c2.toWikiText); continue; } 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=0){ idChk=confirm("Found an id. Break table accordingly?"); } if(idChk){

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].split("|-")[0]; for(var i=1;i<rowTexts[0].split("|-").length;i++){ this._rows.push(new SimileWikiToolbox.WikiTableRow(rowTexts[0].split("|-")[i])) }

for(var i=1;i=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){ var me=this; for(var i=0;i<me.getRowCount;i++){

var movingCell=me.getRow(i).getCell(fromIndex);

me._rows[i]._row.cells.splice(fromIndex,1);

me._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=[]; headerlen=this.getRow(0).getCellCount; for(var i=0;i<this.getRowCount;i++){

arr.push(this.getRow(i).toWikiText);

};

return"{|"+set+this._tablePrologue+arr.join("")+"|}";

};

/* MY FUNCTIONS */

SimileWikiToolbox.WikiTable.prototype.mergeColumn=function(col1,col2,delimiter){ col1-- col2-- //As column number != array index for(var i=0;i<this.getRowCount;i++){ if(this.getRow(i).getCell(col1)&&this.getRow(i).getCell(col2)){ var arr =this._rows[i]._row.cells; var s1=arr[col1].toWikiText var s2=arr[col2].toWikiText if(s1.charAt(s1.length)=="\n"){ s1=s1.slice(0,-1); } if(s2.search(/(\{\{yes)|(\{\{no)|(\{\{partial)/ig)!=-1&&s1.search(/(\{\{yes)|(\{\{no)|(\{\{partial)/ig)==-1){

s1=s2.match(/(\{\{yes)|(\{\{no)|(\{\{partial)/ig)[0] s2=s2.replace(/(\{\{yes)|(\{\{no)|(\{\{partial)/g,""); } arr[col1]=new SimileWikiToolbox.WikiTableCell(s1+delimiter+s2) }; }; };

SimileWikiToolbox.WikiTableEditor.prototype.mergeColumn=function(col1,col2,delimiter){

var me=this;

this.doAction(

function{

me._removeCellEditingUI;

me._wikiTable.mergeColumn(col1,col2,delimiter);

me._renderTable;

},

function{

alert("Unable to undo");

}

);

};