MDL-21652 html_table rendering refactored

* class html_component does not exist any more
* class html_table rendered via html_writer::table()
* html_table, html_table_row and html_table_cell have public $attributes property to set their CSS classes
* dropped rotateheaders feature, should be added again after more research of possible ways (<svg> is not nice IMHO)
* dropped possibility to define CSS classes for table heading, body and footer - can be easily done and better done using just table class and context
This commit is contained in:
David Mudrak 2010-03-20 22:15:54 +00:00
parent ad70376ce2
commit 16be897441
106 changed files with 565 additions and 677 deletions

View file

@ -1802,173 +1802,6 @@ END;
return html_writer::tag('div', $output, array('class' => 'paging'));
}
/**
* Render a HTML table
*
* @param object $table {@link html_table} instance containing all the information needed
* @return string the HTML to output.
*/
public function table(html_table $table) {
$table = clone($table);
$table->prepare($this, $this->page, $this->target);
$attributes = array(
'id' => $table->id,
'width' => $table->width,
'summary' => $table->summary,
'cellpadding' => $table->cellpadding,
'cellspacing' => $table->cellspacing,
'class' => $table->get_classes_string());
$output = html_writer::start_tag('table', $attributes) . "\n";
$countcols = 0;
if (!empty($table->head)) {
$countcols = count($table->head);
$output .= html_writer::start_tag('thead', $table->headclasses) . "\n";
$output .= html_writer::start_tag('tr', array()) . "\n";
$keys = array_keys($table->head);
$lastkey = end($keys);
foreach ($table->head as $key => $heading) {
// Convert plain string headings into html_table_cell objects
if (!($heading instanceof html_table_cell)) {
$headingtext = $heading;
$heading = new html_table_cell();
$heading->text = $headingtext;
$heading->header = true;
}
if ($heading->header !== false) {
$heading->header = true;
}
$heading->add_classes(array('header', 'c' . $key));
if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) {
$heading->colspan = $table->headspan[$key];
$countcols += $table->headspan[$key] - 1;
}
if ($key == $lastkey) {
$heading->add_class('lastcol');
}
if (isset($table->colclasses[$key])) {
$heading->add_class($table->colclasses[$key]);
}
if ($table->rotateheaders) {
// we need to wrap the heading content
$heading->text = html_writer::tag('span', $heading->text);
}
$attributes = array(
'style' => $table->align[$key] . $table->size[$key] . $heading->style,
'class' => $heading->get_classes_string(),
'scope' => $heading->scope,
'colspan' => $heading->colspan);
$tagtype = 'td';
if ($heading->header === true) {
$tagtype = 'th';
}
$output .= html_writer::tag($tagtype, $heading->text, $attributes) . "\n";
}
$output .= html_writer::end_tag('tr') . "\n";
$output .= html_writer::end_tag('thead') . "\n";
if (empty($table->data)) {
// For valid XHTML strict every table must contain either a valid tr
// or a valid tbody... both of which must contain a valid td
$output .= html_writer::start_tag('tbody', array('class' => renderer_base::prepare_classes($table->bodyclasses).' empty'));
$output .= html_writer::tag('tr', html_writer::tag('td', '', array('colspan'=>count($table->head))));
$output .= html_writer::end_tag('tbody');
}
}
if (!empty($table->data)) {
$oddeven = 1;
$keys = array_keys($table->data);
$lastrowkey = end($keys);
$output .= html_writer::start_tag('tbody', array('class' => renderer_base::prepare_classes($table->bodyclasses))) . "\n";
foreach ($table->data as $key => $row) {
if (($row === 'hr') && ($countcols)) {
$output .= html_writer::tag('td', html_writer::tag('div', '', array('class' => 'tabledivider')), array('colspan' => $countcols)) . "\n";
} else {
// Convert array rows to html_table_rows and cell strings to html_table_cell objects
if (!($row instanceof html_table_row)) {
$newrow = new html_table_row();
foreach ($row as $unused => $item) {
$cell = new html_table_cell();
$cell->text = $item;
$newrow->cells[] = $cell;
}
$row = $newrow;
}
$oddeven = $oddeven ? 0 : 1;
if (isset($table->rowclasses[$key])) {
$row->add_classes(array_unique(html_component::clean_classes($table->rowclasses[$key])));
}
$row->add_class('r' . $oddeven);
if ($key == $lastrowkey) {
$row->add_class('lastrow');
}
$output .= html_writer::start_tag('tr', array('class' => $row->get_classes_string(), 'style' => $row->style, 'id' => $row->id)) . "\n";
$keys2 = array_keys($row->cells);
$lastkey = end($keys2);
foreach ($row->cells as $key => $cell) {
if (!($cell instanceof html_table_cell)) {
$mycell = new html_table_cell();
$mycell->text = $cell;
$cell = $mycell;
}
if (isset($table->colclasses[$key])) {
$cell->add_classes(array_unique(html_component::clean_classes($table->colclasses[$key])));
}
$cell->add_classes('cell');
$cell->add_classes('c' . $key);
if ($key == $lastkey) {
$cell->add_classes('lastcol');
}
$tdstyle = '';
$tdstyle .= isset($table->align[$key]) ? $table->align[$key] : '';
$tdstyle .= isset($table->size[$key]) ? $table->size[$key] : '';
$tdstyle .= isset($table->wrap[$key]) ? $table->wrap[$key] : '';
$tdattributes = array(
'style' => $tdstyle . $cell->style,
'colspan' => $cell->colspan,
'rowspan' => $cell->rowspan,
'id' => $cell->id,
'class' => $cell->get_classes_string(),
'abbr' => $cell->abbr,
'scope' => $cell->scope,
'title' => $cell->title);
$tagtype = 'td';
if ($cell->header === true) {
$tagtype = 'th';
}
$output .= html_writer::tag($tagtype, $cell->text, $tdattributes) . "\n";
}
}
$output .= html_writer::end_tag('tr') . "\n";
}
$output .= html_writer::end_tag('tbody') . "\n";
}
$output .= html_writer::end_tag('table') . "\n";
if ($table->rotateheaders && can_use_rotated_text()) {
$this->page->requires->yui2_lib('event');
$this->page->requires->js('/course/report/progress/textrotate.js');
}
return $output;
}
/**
* Output the place a skip link goes to.
* @param string $id The target name from the corresponding $PAGE->requires->skip_link_to($target) call.