source: trunk/phpgwapi/js/venus/table.js @ 2

Revision 2, 15.1 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1/* Source: Venus Project
2   http://www.jbanana.org/venus/ 
3         License: LGPL-CC & Creative Commons */
4
5//** ROW TYPES ******
6var _TITLE_ROW = 0;
7var _FIRST_ROW = 1;
8var _SECOND_ROW = 2;
9//** CCS CLASS NAMES
10var _DEFAULT_TABLE_TITLE = "TableTitle";
11var _DEFAULT_TABLE_EXPAND_CELL = "TableExpandCell";
12var _DEFAULT_TABLE_ROW_1 = "TableRow1";
13var _DEFAULT_TABLE_ROW_2 = "TableRow2";
14var _DEFAULT_TABLE_ROW_1_HOVER = "TableRow1Hover";
15var _DEFAULT_TABLE_ROW_2_HOVER = "TableRow2Hover";
16var _DEFAULT_SUB_TABLE = "SubTable";
17var _DEFAULT_SUB_TABLE_TITLE = "SubTableTitle";
18var _DEFAULT_SUB_TABLE_ROW_1 = "SubTableRow1";
19var _DEFAULT_SUB_TABLE_ROW_2 = "SubTableRow2";
20var _DEFAULT_SUB_TABLE_ROW_1_HOVER = "SubTableRow1Hover";
21var _DEFAULT_SUB_TABLE_ROW_2_HOVER = "SubTableRow2Hover";
22//** CURSORS
23var _CURSOR_NORMAL = "default";
24var _CURSOR_TITLE = "n-resize";
25var _CURSOR_BODY = "pointer";
26//** OTHERS
27var _DEFAULT_SORT_COMPARE = "lexicalCompare";
28var _DEFAULT_TABLE_ROW_CLICK_FUNCTION_NAME = "tableRowClick";
29var _DEFAULT_EXPAND_ROW_CLICK_FUNCTION_NAME = "expandRowClick";
30//** objetos *****************************************
31
32function Table(id) {
33   //metodos
34   this.newRow = newRow;
35   this.newTitle = newTitle;
36   this.sort = sort;
37   this.setExpandColVisible = setExpandColVisible;
38   this.setRowClickEnable = setRowClickEnable;
39   this.setSelectionVisible = setSelectionVisible;
40   this.getRowsBySelection = getRowsBySelection;
41   this.removeRows = removeRows;
42   this.repaintRows = repaintRows;
43   //atributos
44   this.rows = null;
45   this.id = id;
46   this.DOMtable = document.createElement("table");
47   this.DOMtbody = document.createElement("tbody");
48   this.titleRow = null;
49   this.footerRow = null;
50   this.lastSort = - 1;
51   this.isRowClickEnable = false;
52   this.isTitleRowVisible = true;
53   this.isExpandColVisible = false;
54   this.isSelectionVisible = false;
55   this.DOMtable.style.cursor = _CURSOR_BODY;
56   this.tableRowClickFunctionName = _DEFAULT_TABLE_ROW_CLICK_FUNCTION_NAME;
57   this.expandRowClickFunctionName = _DEFAULT_EXPAND_ROW_CLICK_FUNCTION_NAME;
58   this._TABLE_TITLE = _DEFAULT_TABLE_TITLE;
59   this._TABLE_ROW_1 = _DEFAULT_TABLE_ROW_1;
60   this._TABLE_ROW_2 = _DEFAULT_TABLE_ROW_2;
61   this._TABLE_ROW_1_HOVER = _DEFAULT_TABLE_ROW_1_HOVER;
62   this._TABLE_ROW_2_HOVER = _DEFAULT_TABLE_ROW_2_HOVER;
63   this._TABLE_EXPAND_CELL = _DEFAULT_TABLE_EXPAND_CELL;
64   //outros
65   this.DOMtable.appendChild(this.DOMtbody);
66   if(this.isTitleRowVisible) {
67      this.titleRow = this.newRow();
68      if(this.isSelectionVisible) this.DOMtr.removeChild(this.DOMselection);
69      this.titleRow._setRowAs(_TITLE_ROW);
70      this.footerRow = this.newRow();
71      this.footerRow._setRowAs(_TITLE_ROW);
72      this.rows = new Array();
73   }
74}
75
76function Row(table) {
77   //metodos
78   this.newCell = newCell;
79   this._setRowAs = _setRowAs;
80   this._newSelectionCell = _newSelectionCell;
81   //atributos   
82   this.table = table;
83   this.cels = new Array();
84   this.DOMtr = document.createElement("tr");
85   this.DOMexpand = document.createElement("td");
86   this.DOMexpand.width = 10;
87   this.DOMexpand.align = "center";
88   this.DOMexpand.className = "TriangleCell";
89   this.DOMexpand.row = this;
90   this.DOMselection = this._newSelectionCell();
91   this.DOMsubrow = document.createElement("tr");
92   //outros
93   if(this.table.footerRow == null) table.DOMtbody.appendChild(this.DOMtr);
94   else table.DOMtbody.insertBefore(this.DOMtr, this.table.footerRow.DOMtr);
95   this.DOMtr.appendChild(this.DOMexpand);
96   this.DOMexpand.style.cursor = _CURSOR_NORMAL;
97   this.DOMselection.style.cursor = _CURSOR_NORMAL;
98   if(table.isExpandColVisible) {
99      var fp = new Function("var row = getJSObject('row', arguments[0]);_expandRow(row);");
100      this.DOMexpand.className = table._TABLE_EXPAND_CELL;
101      var DOMtriangle = getShape("TRIANGLE_RIGHT");
102      this.DOMexpand.onmousedown = fp;
103      DOMtriangle.nextTriangleOrientation = - 1;
104      DOMtriangle.row = this;
105      this.DOMexpand.appendChild(DOMtriangle);
106   }
107   if(table.isSelectionVisible) {
108      this.DOMtr.appendChild(this.DOMselection);
109   }
110   if(table.isRowClickEnable) {
111      this.DOMtr.onmouseover = getEnableHoverRow();
112      this.DOMtr.onmouseout = getDisableHoverRow();
113   }
114}
115
116function Cell(row, isVisible) {
117   //metodos
118   this.addText = addText;
119   //atributos   
120   this.colIndex = null;
121   this.compareFunctionName = null;
122   this.DOMcontent = null;
123   this.row = row;
124   this.isVisible = isVisible;
125   this.DOMtd = document.createElement("td");
126   this.DOMtd.cell = this;
127   //outros
128   if(this.isVisible) row.DOMtr.appendChild(this.DOMtd);
129   if(row.table.isSelectionVisible) {
130      row.DOMtr.removeChild(row.DOMselection);
131      row.DOMtr.appendChild(row.DOMselection);
132   }
133}
134//** construtores ***************
135
136function newTable(id) {
137   return new Table(id);
138}
139
140function newSubTable(id) {
141   var table = new Table(id);
142   table._TABLE_TITLE = _DEFAULT_SUB_TABLE_TITLE;
143   table._TABLE_ROW_1 = _DEFAULT_SUB_TABLE_ROW_1;
144   table._TABLE_ROW_2 = _DEFAULT_SUB_TABLE_ROW_2;
145   table._TABLE_ROW_1_HOVER = _DEFAULT_SUB_TABLE_ROW_1_HOVER;
146   table._TABLE_ROW_2_HOVER = _DEFAULT_SUB_TABLE_ROW_2_HOVER;
147   return table;
148}
149
150function newRow() {
151   var tmpRow = new Row(this);
152   if(this.rows != null) {
153      if(this.rows.length % 2) {
154         tmpRow._setRowAs(_SECOND_ROW);
155      }
156      else {
157         tmpRow._setRowAs(_FIRST_ROW);
158      }
159      this.rows[this.rows.length] = tmpRow;
160   }
161   return tmpRow;
162}
163
164function newCell(isVisible) {
165   var argumentsFunction;
166   var nArg = newCell.arguments.length;
167   if(nArg == 0) isVisible = true;
168   var tmpCell = new Cell(this, isVisible);
169   this.cels[this.cels.length] = tmpCell;
170   var fn = "var cell=getJSObject('cell', arguments[0]);";
171   fn += this.table.tableRowClickFunctionName + "(cell);";
172   if(this.table.isRowClickEnable) tmpCell.DOMtd.onmousedown = new Function(fn);
173   return tmpCell;
174}
175
176function newTitle(value, compareFunctionName, isVisible) {
177   var cell = this.titleRow.newCell();
178   var fcell = this.footerRow.newCell();
179   cell.colIndex = this.titleRow.cels.length - 1;
180   cell.DOMtd.className = this._TABLE_TITLE;
181   cell.row.DOMtr.className = null;
182   cell.addText(value);
183   cell.DOMtd.style.cursor = _CURSOR_NORMAL;
184   cell.DOMtd.onmousedown = null;
185   fcell.colIndex = this.titleRow.cels.length - 1;
186   fcell.DOMtd.className = this._TABLE_TITLE;
187   fcell.row.DOMtr.className = null;
188   fcell.DOMtd.style.cursor = _CURSOR_NORMAL;
189   fcell.DOMtd.onmousedown = null;
190   var nArg = newTitle.arguments.length;
191   if(nArg >= 2){
192      cell.compareFunctionName = compareFunctionName;
193      cell.DOMtd.style.cursor  = _CURSOR_TITLE;
194      cell.DOMtd.onmousedown   = _sortClick;
195      //cell.DOMtd.onmouseover = getEnableHoverCell();
196      //cell.DOMtd.onmouseout = getDisableHoverCell(); 
197      //cell.DOMtd.hoverClassName = this._TABLE_TITLE+"Hover";
198   }
199   if(nArg == 3) {
200      if(!isVisible) this.titleRow.DOMtr.removeChild(cell.DOMtd);
201   }
202}
203
204function addText(value) {
205   this.DOMcontent = document.createTextNode(value);
206   this.DOMtd.appendChild(this.DOMcontent);
207}
208//** metodos de objetos contextuais ************************
209
210function removeRows(rowsArray) {
211   var newArray = new Array();
212   var addRow = null;
213   for(var i = 0; i < this.rows.length; i++) {
214      addRow = true;
215      for(var ii = 0; ii < rowsArray.length; ii++) {
216         if(rowsArray[ii] == this.rows[i]) {
217            addRow = false;
218            this.DOMtbody.removeChild(this.rows[i].DOMtr);
219            break;
220         }
221      }
222      if(addRow) newArray[newArray.length] = this.rows[i];
223   }
224   this.rows = newArray;
225   if(this.rows.length > 0) this.repaintRows();
226}
227
228function sort(indexCol, compareFunction, invert) {
229   var nArg = sort.arguments.length;
230   if(nArg == 0) {
231      indexCol = 0;
232      compareFunction = _DEFAULT_SORT_COMPARE;
233      invert = false;
234   }
235   else if(nArg == 1) {
236      compareFunction = _DEFAULT_SORT_COMPARE;
237      invert = false;
238   }
239   else if(nArg == 2) {
240      invert = false;
241   }
242   if(invert) this.rows.sort(_invertedCompareHook(indexCol, compareFunction));
243   else this.rows.sort(_compareHook(indexCol, compareFunction));
244   this.repaintRows();
245}
246
247function repaintRows() {
248   this.DOMtable.removeChild(this.DOMtbody);
249   this.DOMtbody = document.createElement("tbody");
250   this.DOMtable.appendChild(this.DOMtbody);
251   this.DOMtbody.appendChild(this.titleRow.DOMtr);
252   var DOMexpand = null;
253   var DOMtriangle = null;
254   for(index = 0; index < this.rows.length; index++) {
255      if(index % 2) {
256         this.rows[index].DOMtr.className = this._TABLE_ROW_2;
257         this.rows[index].DOMtr.hoverClassName = this._TABLE_ROW_2_HOVER;
258      }
259      else {
260         this.rows[index].DOMtr.className = this._TABLE_ROW_1;
261         this.rows[index].DOMtr.hoverClassName = this._TABLE_ROW_1_HOVER;
262      }
263      this.rows[index].DOMtr.onmouseover = getEnableHoverRow();
264      this.rows[index].DOMtr.onmouseout = getDisableHoverRow();
265      this.DOMtbody.appendChild(this.rows[index].DOMtr);
266      DOMexpand = this.rows[index].DOMexpand;
267      if(DOMexpand.firstChild != null) DOMexpand.removeChild(DOMexpand.firstChild);
268      if(this.isExpandColVisible) {
269         DOMtriangle = getShape("TRIANGLE_RIGHT");
270         DOMexpand.appendChild(DOMtriangle);
271         DOMtriangle.nextTriangleOrientation = - 1;
272         DOMtriangle.row = this.rows[index];
273      }
274   }
275   this.DOMtbody.appendChild(this.footerRow.DOMtr);
276}
277
278function setExpandColVisible(isVisible) {
279   if(this.isExpandColVisible == isVisible)
280   return;
281   this.isExpandColVisible = isVisible;
282   var DOMexpand = null;
283   var index = 0;
284   var DOMtriangle = null;
285   var fp = new Function("var row=getJSObject('row', arguments[0]);_expandRow(row);");
286   if(isVisible) {
287      for(; index < this.rows.length; index++) {
288         DOMexpand = this.rows[index].DOMexpand;
289         DOMexpand.className = this._TABLE_EXPAND_CELL;
290         DOMtriangle = getShape("TRIANGLE_RIGHT");
291         DOMexpand.appendChild(DOMtriangle);
292         if(DOMexpand.onmousedown == null) {
293            DOMexpand.onmousedown = fp;
294            DOMtriangle.nextTriangleOrientation = - 1;
295            DOMtriangle.row = this.rows[index];
296         }
297      }
298   }else {
299      for(; index < this.rows.length; index++) {
300         DOMexpand = this.rows[index].DOMexpand;
301         DOMexpand.removeChild(DOMexpand.firstChild);
302      }
303   }
304}
305
306function setRowClickEnable(isEnable) {
307   this.isRowClickEnable = isEnable;
308   var fn = null;
309   if(isEnable) {
310      fn = "var cell=getJSObject('cell', arguments[0]);";
311      fn += this.tableRowClickFunctionName + "(cell);";
312      fn = new Function(fn);
313   }
314   var index = 0;
315   for(; index < this.rows.length; index++) {
316      for(ii = 0; ii < this.rows[index].cels.length; ii++) {
317         this.rows[index].cels[ii].DOMtd.onmousedown = fn;
318         this.rows[index].DOMtr.onmouseover = getEnableHoverRow();
319         this.rows[index].DOMtr.onmouseout = getDisableHoverRow();
320      }
321   }
322}
323
324function setSelectionVisible(isVisible) {
325   if(this.isSelectionVisible == isVisible)
326   return;
327   this.isSelectionVisible = isVisible;
328   var DOMexpand = null;
329   var index = 0;
330   var row = null;
331   if(isVisible) {
332      for(; index < this.rows.length; index++) {
333         row = this.rows[index];
334         row.DOMtr.appendChild(row.DOMselection);
335      }
336      row.table.titleRow.DOMtr.appendChild(row.table.titleRow.DOMselection);
337      row.table.footerRow.DOMtr.appendChild(row.table.footerRow.DOMselection);
338   } else {
339      for(; index < this.rows.length; index++) {
340         row = this.rows[index];
341         row.DOMtr.removeChild(row.DOMselection);
342      }
343      row.table.titleRow.DOMtr.removeChild(row.table.titleRow.DOMselection);
344      row.table.footerRow.DOMtr.removeChild(row.table.footerRow.DOMselection);
345   }
346}
347
348function getRowsBySelection(isSelected) {
349   if(!this.isSelectionVisible)
350   return new Array();
351   var DOMexpand = null;
352   var index = 0;
353   var row = null;
354   var tmpArray = new Array();
355   for(; index < this.rows.length; index++) {
356      row = this.rows[index];
357      if(row.DOMselection.firstChild.checked == isSelected) tmpArray[tmpArray.length] = row;
358   }
359   return tmpArray;
360}
361//** compare functions (sort)
362
363function lexicalCompare(indexCol, obj1, obj2) {
364   var v1 = obj1.cels[indexCol].DOMcontent.nodeValue;
365   var v2 = obj2.cels[indexCol].DOMcontent.nodeValue;
366   return(v1 < v2) ? - 1 :(v1 > v2) ? 1 : 0;
367}
368
369function numericCompare(indexCol, obj1, obj2) {
370   var v1 = eval(obj1.cels[indexCol].DOMcontent.nodeValue);
371   var v2 = eval(obj2.cels[indexCol].DOMcontent.nodeValue);
372   return(v1 < v2) ? - 1 :(v1 > v2) ? 1 : 0;
373}
374//** metodos privados ******************************
375
376function _newSelectionCell() {
377   var DOMcell = document.createElement("td");
378   var chk = document.createElement("input");
379   chk.type = "checkbox";
380   chk.id = "chk_" + this.table.id;
381   chk.name = "chk_" + this.table.id;
382   DOMcell.appendChild(chk);
383   return DOMcell;
384}
385
386function _setRowAs(type) {
387   var celsclass = null;
388   if(type == _TITLE_ROW) {
389      celsclass = this.table._TABLE_TITLE;
390      this.DOMtr.className = null;
391      this.DOMselection = document.createElement("td");
392      this.DOMselection.className = celsclass;
393      this.DOMselection.hoverClassName = celsclass + "Hover";
394   }
395   else if(type == _FIRST_ROW) {
396      this.DOMtr.className = this.table._TABLE_ROW_1;
397      this.DOMtr.hoverClassName = this.DOMtr.className + "Hover";   
398   }
399   else if(type == _SECOND_ROW) {
400      this.DOMtr.className = this.table._TABLE_ROW_2;
401      this.DOMtr.hoverClassName = this.DOMtr.className + "Hover";   
402   }
403   for(i = 0; i < this.cels.length; i++) {
404      this.cels[i].DOMtd.className = celsclass;
405      this.cels[i].DOMtd.hoverClassName = celsclass + "Hover";
406   }
407}
408
409function _sortClick(args0) {
410   var cell = getJSObject("cell", args0);
411   var table = cell.row.table;
412   if(table.lastSort == cell.colIndex) {
413      table.sort(cell.colIndex, cell.compareFunctionName, true);
414      table.lastSort = - 1;
415   }else {
416      table.sort(cell.colIndex, cell.compareFunctionName);
417      table.lastSort = cell.colIndex;
418   }
419}
420
421function _invertedCompareHook(indexCol, compareFunction) {
422   var result = "this.indexCol=" + indexCol + ";\n";
423   result += "return -1*(" + compareFunction + "(indexCol, arguments[0], arguments[1]));";
424   return new Function(result);
425}
426
427function _compareHook(indexCol, compareFunction) {
428   var result = "this.indexCol=" + indexCol + ";\n";
429   result += "return " + compareFunction + "(indexCol, arguments[0], arguments[1]);";
430   return new Function(result);
431}
432
433function _expandRow(row) {
434   var lastTriangle = row.DOMexpand.firstChild;
435   var name = "TRIANGLE_RIGHT";
436   if(lastTriangle.nextTriangleOrientation < 0) name = "TRIANGLE_DOWN";
437   var DOMtriangle = getShape(name);
438   DOMtriangle.nextTriangleOrientation = - 1 * lastTriangle.nextTriangleOrientation;
439   DOMtriangle.row = row;
440   row.DOMexpand.removeChild(lastTriangle);
441   row.DOMexpand.appendChild(DOMtriangle);
442   var DOMtr = row.DOMtr.nextSibling;
443   if(lastTriangle.nextTriangleOrientation < 0) {
444      if(DOMtr == null) row.table.DOMtbody.appendChild(row.DOMsubrow);
445      else row.table.DOMtbody.insertBefore(row.DOMsubrow, DOMtr);
446   }else {
447      row.table.DOMtbody.removeChild(row.DOMsubrow);
448   }
449   eval(row.table.expandRowClickFunctionName + "(row, " + lastTriangle.nextTriangleOrientation + ")");
450}
Note: See TracBrowser for help on using the repository browser.