Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

layout.js 3.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. + Layout
  3. + wrapper
  4. + LayoutRow
  5. + main_head
  6. + main_body
  7. + header
  8. + body
  9. ..
  10. ..
  11. + subrows
  12. */
  13. function Layout(parent, width) {
  14. if(parent&&parent.substr) { parent = $i(parent); }
  15. this.wrapper = $a(parent, 'div', '', {display:'none'});
  16. if(width) {
  17. this.width = this.wrapper.style.width;
  18. }
  19. this.myrows = [];
  20. }
  21. Layout.prototype.addrow = function() {
  22. this.cur_row = new LayoutRow(this, this.wrapper);
  23. this.myrows[this.myrows.length] = this.cur_row;
  24. return this.cur_row
  25. }
  26. Layout.prototype.addsubrow = function() {
  27. this.cur_row = new LayoutRow(this, this.cur_row.main_body);
  28. this.myrows[this.myrows.length] = this.cur_row;
  29. return this.cur_row
  30. }
  31. Layout.prototype.addcell = function(width) {
  32. return this.cur_row.addCell(width);
  33. }
  34. Layout.prototype.setcolour = function(col) { $bg(cc,col); }
  35. Layout.prototype.show = function() { $ds(this.wrapper); }
  36. Layout.prototype.hide = function() { $dh(this.wrapper); }
  37. Layout.prototype.close_borders = function() {
  38. if(this.with_border) {
  39. this.myrows[this.myrows.length-1].wrapper.style.borderBottom = '1px solid #000';
  40. }
  41. }
  42. function LayoutRow(layout, parent) {
  43. this.layout = layout;
  44. this.wrapper = $a(parent,'div');
  45. // main head
  46. this.main_head = $a(this.wrapper, 'div');
  47. // main head
  48. this.main_body = $a(this.wrapper, 'div');
  49. if(layout.with_border) {
  50. this.wrapper.style.border = '1px solid #000';
  51. this.wrapper.style.borderBottom = '0px';
  52. }
  53. this.header = $a(this.main_body, 'div','',{padding:(layout.with_border ? '0px 8px' : '0px')});
  54. this.body = $a(this.main_body,'div');
  55. this.table = $a(this.body, 'table', '', {width:'100%', borderCollapse: 'collapse'});
  56. this.row = this.table.insertRow(0);
  57. this.mycells = [];
  58. }
  59. LayoutRow.prototype.hide = function() { $dh(this.wrapper); }
  60. LayoutRow.prototype.show = function() { $ds(this.wrapper); }
  61. LayoutRow.prototype.addCell = function(wid) {
  62. var lc = new LayoutCell(this.layout, this, wid);
  63. this.mycells[this.mycells.length] = lc;
  64. return lc;
  65. }
  66. function LayoutCell(layout, layoutRow, width) {
  67. if(width) { // add '%' if user has forgotten
  68. var w = width + '';
  69. if(w.substr(w.length-2, 2) != 'px') {
  70. if(w.substr(w.length-1, 1) != "%") {width = width + '%'};
  71. }
  72. }
  73. this.width = width;
  74. this.layout = layout;
  75. var cidx = layoutRow.row.cells.length;
  76. this.cell = layoutRow.row.insertCell(cidx);
  77. this.cell.style.verticalAlign = 'top';
  78. this.set_width(layoutRow.row, width);
  79. var h = $a(this.cell, 'div','',{padding:(layout.with_border ? '0px 8px' : '0px')});
  80. this.wrapper = $a(this.cell, 'div','',{padding:(layout.with_border ? '8px' : '8px 0px')});
  81. layout.cur_cell = this.wrapper;
  82. layout.cur_cell.header = h;
  83. }
  84. // evenly distribute columns
  85. LayoutCell.prototype.set_width = function(row, width) {
  86. var w = 100;
  87. var n_cells = row.cells.length;
  88. var cells_with_no_width = n_cells;
  89. // current cell
  90. if(width) {
  91. $y(row.cells[n_cells-1], {width: cint(width) + '%'})
  92. } else {
  93. row.cells[n_cells-1].estimated_width = 1;
  94. }
  95. // get user specified width
  96. for(var i=0; i<n_cells; i++) {
  97. if(!row.cells[i].estimated_width) {
  98. w = w - cint(row.cells[i].style.width);
  99. cells_with_no_width--;
  100. }
  101. }
  102. // evenly distribute all
  103. for(var i=0; i<n_cells; i++) {
  104. if(row.cells[i].estimated_width)
  105. $y(row.cells[i], {width:cint(w/cells_with_no_width) + '%'})
  106. }
  107. }
  108. LayoutCell.prototype.show = function() { $ds(this.wrapper); }
  109. LayoutCell.prototype.hide = function() { $dh(this.wrapper); }