option.js:
var main_tabs = new Tabs(’#main_tabs’);
document.addEventListener(‘optionsPageReady’, function () {
document.addEventListener('optionSaved', function (event) {
// reload extension after toggle session watcher option
console.log("option saved");
}, false);
var input = document.getElementById("jsondata");
var bt_del = document.getElementById('bt_del');
var bt_add = document.getElementById('bt_add');
var bt_buildTable = document.getElementById('bt_buildTable');
var p = document.getElementById("table_here");
var data = JSON.parse(input.value);
p.appendChild(buildHtmlTable(data));
//p.innerHTML = “
”;
var table = document.getElementById(“table_regex”);
table.addEventListener(“blur”, function(){
//alert(“table onblur 1”);
var a = table2json(this);
input.value = a;
}, true);
var currentRow;
var row;
console.log("nbre de ligne : " + table.rows.length);
add_onClickListener(table);
bt_add_boo.onclick = function (){
var pos = (0 ? this.rows.length : currentRow+1);
var tr = table.insertRow(pos);
tr.onclick = function(){currentRow = this.rowIndex; };
//var colset = ["new label"];
for (var j=0, maxj= _columns_.length; j < maxj ; ++j) {
build_row(tr, newvalue_boo, 0,j );
}
};
bt_add_str.onclick = function (){
var pos = (0 ? this.rows.length : currentRow+1);
var tr = table.insertRow(pos);
tr.onclick = function(){currentRow = this.rowIndex; };
//var colset = ["new label"];
for (var j=0, maxj= _columns_.length; j < maxj ; ++j) {
build_row(tr, newvalue_str, 0,j );
}
};
bt_del.onclick = function () {
if (currentRow > 0 ) {
//alert("deleting " + currentRow);
table.deleteRow(currentRow);
input.value = table2json(table);
} else {
alert("click in a row before deleting it");
}
};
bt_buildTable.onclick = function (){
var data ;
try {
data = JSON.parse(input.value);
} catch (e) {
alert (e);
return;
}
var table = document.getElementById("table_regex");
table.parentNode.removeChild(table);
p.appendChild(buildHtmlTable(data));
add_onClickListener(table);
table.addEventListener("blur", function(){
//alert("table onblur 2");
var a = table2json(this);
input.value = a;
}, true);
};
function add_onClickListener(table) {
for (var i=0 ; i < table.rows.length; i++ ){
//console.log(i);
table.rows[i].onclick = function() {
// var t = document.getElementById(“table_regex”);
//alert(this.rowIndex);
currentRow = this.rowIndex;
//console.log(currentRow);
};
}
}
}, false);
// fusionner keepmatch et subs :
// nom: cmd
// valeur: true (keepmatch : 1), false (keepmatch : 0) string: string to substitute
var newvalue_boo = [
{
“name”:“new label”,
“regex”:“new regex”,
“regexmodif”:“gi”,
“casemodif”:“L”,
“cmd”:true,
“keep”:false,
“shortcut”:“one”
}
];
var newvalue_str = [
{
“name”:“new label”,
“regex”:“new regex”,
“regexmodif”:“gi”,
“casemodif”:“L”,
“cmd”:“xxxx”,
“keep”:false,
“shortcut”:“z”
}
];
var colw = [10, 15, 3, 3, 15, 15, 5];
var jsondata = [
{
“name”:">4",
“regex”:"[\w\u00c0-\u010f]{4,}",
“regexmodif”:“gi”,
“casemodif”:“L”,
“cmd”:true,
“keep”:false,
“shortcut”:“one”
},
{
“name”:"-dr",
“regex”:"(?:Dr. )|(?:Prof. )",
“regexmodif”:“gi”,
“casemodif”:"",
“cmd”:false,
“keep”:false,
“shortcut”:“p”
},
{
“name”:“doi”,
“regex”:"(.+)",
“regexmodif”:“i”,
“casemodif”:"",
“cmd”:“http://dx.doi.org/$1”,
“keep”:true,
“shortcut”:“o”
},
{
“name”:“newLine”,
“regex”:"(\r\n)+",
“regexmodif”:“gi”,
“casemodif”:"",
“cmd”:“xxx”,
“keep”:false,
“shortcut”:“a”
},
{
“name”:“AllUpperCase”,
“regex”:"(.+)",
“regexmodif”:“gi”,
“casemodif”:“U”,
“cmd”:"$1",
“keep”:false,
“shortcut”:“VK_F4”
},
{
“name”:“quote”,
“regex”:"(.+)",
“regexmodif”:“gi”,
“casemodif”:"",
“cmd”:"[quote]$1[\quote]",
“keep”:false,
“shortcut”:""
}
];
//http://stackoverflow.com/questions/5180382/convert-json-data-to-a-html-table
var table = document.createElement(‘table’),
tr = document.createElement(‘tr’),
th = document.createElement(‘th’),
td = document.createElement(‘td’),
input = document.createElement(‘input’);
var columns;
// Builds the HTML Table out of myList json data from Ivy restful service.
function buildHtmlTable(arr) {
var table = table.cloneNode(false);
columns = addAllColumnHeaders(arr, table);
table.id = “table_regex”;
for (var i=0, maxi=arr.length; i < maxi; ++i) {
var tr = tr.cloneNode(false);
for (var j=0, maxj=columns.length; j < maxj ; ++j) {
build_row(tr, arr, i, j);
}
table.appendChild(tr);
}
table.style.border =“thick solid #0000FF”;
return table ;
}
function build_row(tr, values, rowPos, colPos ){
var td = td.cloneNode(false);
var cellValue = values[rowPos][columns[colPos]];
var type = typeof cellValue;
var ip = input.cloneNode(false);
td.appendChild(ip);
if (type === “boolean”){
console.log(“boolean type found”);
ip.setAttribute(“type”, “checkbox”);
//ip.setAttribute(“style”, “visibility:visible;”);
ip.checked= cellValue;
} else {
ip.setAttribute(“type”, “text”);
ip.value = cellValue || ‘’;
//ip.size = 10;
ip.size = colw[colPos];
}
// td.appendChild( document.createTextNode(arr[i][columns[j]] || ‘’));
tr.appendChild(td);
}
// Adds a header row to the table and returns the set of columns.
// Need to do union of keys from all records as some records may not contain
// all records
function addAllColumnHeaders(arr, table)
{
var columnSet = [],
tr = tr.cloneNode(false);
for (var i=0, l=arr.length; i < l; i++) {
for (var key in arr[i]) {
if (arr[i].hasOwnProperty(key) && columnSet.indexOf(key)===-1) {
var label = chrome.i18n.getMessage(‘col_’ + key)
columnSet.push(key);
console.log("key: " + key + " : " + label);
var th = th.cloneNode(false);
th.appendChild(document.createTextNode(label));
tr.appendChild(th);
}
}
}
table.appendChild(tr);
return columnSet;
}
//http://johndyer.name/html-table-to-json/
function table2json(table){
var data = [];
// first row needs to be headers
var headers = [];
var lastcol = table.rows[0].cells.length;
for (var i=0; i< lastcol; i++) {
//headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi,’’);
headers[i] = columns[i];
console.log("table2json: header " + i + " : " + headers[i]);
}
// go through cells
for (var i=1; i<table.rows.length; i++) {
var tableRow = table.rows[i];
var rowData = {};
for (var j=0; j<tableRow.cells.length; j++) {
var cell = tableRow.cells[j].firstChild;
var type =cell.getAttribute(“type”);
//console.log(type);
var value;
if ( type === “checkbox”) {
value =cell.checked ;
} else {
value = cell.value;
}
rowData[ headers[j] ] = value;
}
data.push(rowData);
}
return JSON.stringify(data);
}
window.addEventListener(“load”, function() {
console.log(“onInstalled called”);
var key = 'jsondata';
storage.area.get(key, function (items){
if (!items[key]){
//var val = JSON.stringify(storage.default_options[key]);
items[key] = storage.default_options[key];
alert("retrieve from default " + items[key].substr(0,10) + " ...." );
storage.area.set({key: items[key]});
//chrome.tabs.create({'url': 'options.html#regex'});
//chrome.runtime.openOptionsPage();
}
// buildContextMenu(JSON.parse(items[key]));
});// storage.area.get
}, false); //add listener