Commit 71b5f599 authored by Vinicius Ruoso's avatar Vinicius Ruoso

Almost complete rewrite of sdi.js.

Main changes:
 - Added a global variable 'selected'
 - Added a loading state while javascript execute populate()
 - Added cookies support to save last table state
 - Added a table expand feature
 - Fixed a bug when saving the collumns
 - Fixed the program identation
 - Adaptation of 'create_list' function to the new columns menu
 - Added menu functions to show and hide it
 - Fixed identation
Signed-off-by: default avatarVinicius Ruoso <vkr07@c3sl.ufpr.br>
parent 20d20f4e
var selected;
function populate(){
var tbls = document.getElementsByTagName('table');
var row;
var cols;
for (var tb=0; tb<tbls.length; tb++) {
row = tbls[tb].getElementsByTagName('tr');
cols = row[0].getElementsByTagName('td');
var temp = new Array(cols.length)
for (var col=0; col<cols.length; col++){
temp[col] = cols[col].innerHTML;
}
var tbid = tbls[tb].id;
divID = tbid + '_cols';
create_list(tbid, temp, divID);
paint(tbid);
}
var tbls = document.getElementsByTagName('table');
var row;
var cols;
for (var tb=0; tb<tbls.length; tb++) {
row = tbls[tb].getElementsByTagName('tr');
cols = row[0].getElementsByTagName('td');
var all = new Array(cols.length)
for (var col=0; col<cols.length; col++){
all[col] = cols[col].innerHTML;
}
var tbid = tbls[tb].id;
colsID = tbid + '_cols';
var loadID = tbid + '_load';
document.getElementById(loadID).innerHTML = '<img ' +
'src="img/loader.gif" /> Carregando...';
var cookieName = location.href+'#'+tbid;
if (getCookie(cookieName)){
selected = getCookie(cookieName).split(',');
} else {
var colsSel = document.getElementById(colsID);
selected = colsSel.innerHTML.split(',');
setCookie(cookieName, selected.join(','), null);
}
create_list(tbid, all, selected, colsID);
paint(tbid);
expand(loadID);
expand(tbid);
}
}
function paint(table){
var tbl = document.getElementById(table);
var rows = tbl.getElementsByTagName('tr');
var tbl = document.getElementById(table);
var rows = tbl.getElementsByTagName('tr');
for (var row=1; row<rows.length; row++)
if (row%2==1)
rows[row].className="painted";
else
rows[row].className="";
for (var row=1; row<rows.length; row++)
if (row%2==1)
rows[row].className="painted";
else
rows[row].className="";
}
function expand(elementID){
elementID = elementID + '_div';
element = document.getElementById(elementID);
if (element.style.display=='none')
element.style.display='';
else
element.style.display='none';
element = document.getElementById(elementID);
if (element.style.display=='none')
element.style.display='';
else
element.style.display='none';
}
function table_expand(tbID){
element = document.getElementById(tbID);
image = tbID + '_expand';
image = document.getElementById(image);
if (element.style.display=='none'){
element.style.display='';
image.innerHTML='<img src="img/expandUP.jpg" />';
} else {
element.style.display='none';
image.innerHTML='<img src="img/expandDOWN.jpg" />';
}
}
function column(table, col, show){
var stl;
if (show) stl = ''
else stl = 'none';
var stl;
if (show){
stl = ''
selected.push(col);
} else {
stl = 'none';
var new_selected = selected.join(',');
selected = new_selected.replace(','+col,'').replace(col+',','').split();
}
var cookieName = location.href+'#'+table;
setCookie(cookieName, selected.join(','), null);
var tbl = document.getElementById(table);
var rows = tbl.getElementsByTagName('tr');
for (var row=0; row<rows.length; row++) {
var cels = rows[row].getElementsByTagName('td')
cels[col].style.display=stl;
}
var tbl = document.getElementById(table);
var rows = tbl.getElementsByTagName('tr');
// get col number
var cols = rows[0].getElementsByTagName('td');
var colnum = -1;
for (i=0; i<cols.length; i++)
if (cols[i].innerHTML==col)
colnum = i;
for (var row=0; row<rows.length; row++) {
var cels = rows[row].getElementsByTagName('td')
cels[colnum].style.display=stl;
}
}
function create_list(table, cols, elementID){
var i;
element = document.getElementById(elementID);
element.innerHTML = ''
for (i in cols){
element.innerHTML = element.innerHTML + '<input type="checkbox" id="'+table+i+'" onClick="javascript:column(\''+table+'\','+i+',this.checked);" checked="checked" /> <label for="'+table+i+'">'+cols[i]+'</label> ';
}
function create_list(table, cols, selected, elementID){
var i;
var html = '';
element = document.getElementById(elementID);
element.innerHTML = '<ul>';
for (i in cols){
html = html + '<li><input type="checkbox" id="'+table+i+'" ' +
'onClick="javascript:column(\''+table+'\',\''+cols[i]+'\', ' +
'this.checked);" ';
var j;
var haveIt = false;
for (j=0; j<cols.length; j++){
if (cols[i]==selected[j]){
haveIt = true;
break;
}
}
if (haveIt)
html = html + 'checked="checked"';
else
column(table, cols[i], false);
html = html + ' /> ' +
'<label for="'+table+i+'">'+cols[i]+'</label></li>';
}
element.innerHTML = html;
}
function hide_menu(e, element){
if (!e) var e = window.event;
var tg = (window.event) ? e.srcElement : e.target;
if (tg.nodeName != 'DIV') return;
var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
while (reltg != tg && reltg.nodeName != 'BODY')
reltg = reltg.parentNode
if (reltg== tg) return;
// Mouseout took place when mouse actually left layer
// Handle event
element.className='hide';
}
function show_menu(elementID){
document.getElementById(elementID).className='select_cols';
}
function setCookie(name, value, expireDays){
var exdate = new Date();
exdate.setDate(exdate.getDate()+expireDays);
document.cookie = name + '=' + escape(value) +
((expireDays==null) ? '' : '; expires='+exdate.toGMTString());
}
function getCookie(name){
if (document.cookie.length>0){
start = document.cookie.indexOf(name + "=");
if (start!=-1){
start = start + name.length+1;
end = document.cookie.indexOf(";",start);
if (end==-1) end = document.cookie.length;
return unescape(document.cookie.substring(start, end));
}
}
return "";
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment