Skip to content

Commit

Permalink
MDL-20204 started refactoring of html_table + more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Feb 17, 2010
1 parent dd72b30 commit 8cea545
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 213 deletions.
2 changes: 1 addition & 1 deletion blog/external_blogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
$action = new confirm_action(get_string('externalblogdeleteconfirm', 'blog'));
$deleteicon = $OUTPUT->action_icon($deletelink, new pix_icon('t/delete', get_string('deleteexternalblog', 'blog')), $action);

$table->data[] = html_table_row::make(array($blog->name, $blog->url, userdate($blog->timefetched), $validicon, $editicon . $deleteicon));
$table->data[] = new html_table_row(array($blog->name, $blog->url, userdate($blog->timefetched), $validicon, $editicon . $deleteicon));
}
echo $OUTPUT->table($table);
}
Expand Down
6 changes: 3 additions & 3 deletions grade/edit/tree/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function build_html_tree($element, $totals, $parents, $level, &$row_count
$cell->colspan = 12;
$cell->add_classes(array($element['type'], 'moving'));
$cell->text = $object->name.' ('.get_string('move').')';
return array(html_table_row::make(array($cell)));
return array(new html_table_row(array($cell)));
}

if ($element['type'] == 'category') {
Expand Down Expand Up @@ -217,7 +217,7 @@ public function build_html_tree($element, $totals, $parents, $level, &$row_count
$cell->colspan = 12;
$cell->text = $OUTPUT->action_icon($aurl, $strmovehere, new pix_icon(array('class'=>'movetarget'), 'movehere'));

$moveto = html_table_row::make(array($cell));
$moveto = new html_table_row(array($cell));
}

$newparents = $parents;
Expand Down Expand Up @@ -306,7 +306,7 @@ public function build_html_tree($element, $totals, $parents, $level, &$row_count
$endcell->colspan = (19 - $level);
$endcell->add_classes(array('colspan', $levelclass));

$returnrows[] = html_table_row::make(array($endcell));;
$returnrows[] = new html_table_row(array($endcell));;

} else { // Dealing with a grade item

Expand Down
2 changes: 1 addition & 1 deletion grade/report/grader/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ public function get_left_rows() {
$fillercell->add_classes(array('fixedcolumn', 'cell', 'topleft'));
$fillercell->text = ' ';
$fillercell->colspan = $colspan;
$row = html_table_row::make(array($fillercell));
$row = new html_table_row(array($fillercell));
$rows[] = $row;
}

Expand Down
151 changes: 20 additions & 131 deletions lib/outputcomponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ public function event_handler($selector, $event, $function, array $arguments = n


// ===============================================================================================
// TODO: Following components will be refactored soon
// TODO: Following HTML components still need some refactoring

/**
* Base class for classes representing HTML elements.
Expand Down Expand Up @@ -1120,15 +1120,6 @@ class html_component {
*/
protected $actions = array();

/**
* Compoment constructor.
* @param array $options image attributes such as title, id, alt, style, class
*/
public function __construct(array $options = null) {
// not implemented in this class because we want to set only public properties of this component
renderer_base::apply_component_options($this, $options);
}

/**
* Ensure some class names are an array.
* @param mixed $classes either an array of class names or a space-separated
Expand Down Expand Up @@ -1186,102 +1177,14 @@ public function get_classes_string() {
* Perform any cleanup or final processing that should be done before an
* instance of this class is output. This method is supposed to be called
* only from renderers.
*
* @param renderer_base $output output renderer
* @param moodle_page $page
* @param string $target rendering target
* @return void
*/
public function prepare(renderer_base $output, moodle_page $page, $target) {
public function prepare() {
$this->classes = array_unique(self::clean_classes($this->classes));
}

/**
* This checks developer do not try to assign a property directly
* if we have a setter for it. Otherwise, the property is set as expected.
* @param string $name The name of the variable to set
* @param mixed $value The value to assign to the variable
* @return void
*/
public function __set($name, $value) {
if ($name == 'class') {
debugging('this way of setting css class has been deprecated. use set_classes() method instead.');
$this->set_classes($value);
} else {
$this->{$name} = $value;
}
}

/**
* Adds a JS action to this component.
* Note: the JS function you write must have only two arguments: (string)event and (object|array)args
* If you want to add an instantiated component_action (or one of its subclasses), give the object as the only parameter
*
* @param mixed $event a DOM event (click, mouseover etc.) or a component_action object
* @param string $jsfunction The name of the JS function to call. required if argument 1 is a string (event)
* @param array $jsfunctionargs An optional array of JS arguments to pass to the function
*/
public function add_action($event, $jsfunction=null, $jsfunctionargs=array()) {
if (empty($this->id)) {
$this->generate_id();
}

if ($event instanceof component_action) {
$this->actions[] = $event;
} else {
if (empty($jsfunction)) {
throw new coding_exception('html_component::add_action requires a JS function argument if the first argument is a string event');
}
$this->actions[] = new component_action($event, $jsfunction, $jsfunctionargs);
}
}

/**
* Internal method for generating a unique ID for the purpose of event handlers.
*/
protected function generate_id() {
$this->id = uniqid(get_class($this));
}

/**
* Returns the array of component_actions.
* @return array Component actions
*/
public function get_actions() {
return $this->actions;
}

/**
* Shortcut for adding a JS confirm dialog when the component is clicked.
* The message must be a yes/no question.
* @param string $message The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
* @param string $callback The name of a JS function whose scope will be set to the simpleDialog object and have this
* function's arguments set as this.args.
* @return void
*/
public function add_confirm_action($message, $callback=null) {
$this->add_action(new component_action('click', 'M.util.show_confirm_dialog', array('message' => $message, 'callback' => $callback)));
}

/**
* Returns true if this component has an action of the requested type (component_action by default).
* @param string $class The class of the action we are looking for
* @return boolean True if action is found
*/
public function has_action($class='component_action') {
foreach ($this->actions as $action) {
if (get_class($action) == $class) {
return true;
}
}
return false;
}
}


/// Components representing HTML elements


/**
* Holds all the information required to render a <table> by
* {@see core_renderer::table()} or by an overridden version of that
Expand Down Expand Up @@ -1437,7 +1340,7 @@ class html_table extends html_component {
* @see html_component::prepare()
* @return void
*/
public function prepare(renderer_base $output, moodle_page $page, $target) {
public function prepare() {
if (!empty($this->align)) {
foreach ($this->align as $key => $aa) {
if ($aa) {
Expand Down Expand Up @@ -1490,21 +1393,7 @@ public function prepare(renderer_base $output, moodle_page $page, $target) {
} else {
$this->rotateheaders = false; // Makes life easier later.
}
parent::prepare($output, $page, $target);
}
/**
* @param string $name The name of the variable to set
* @param mixed $value The value to assign to the variable
* @return void
*/
public function __set($name, $value) {
if ($name == 'rowclass') {
debugging('rowclass[] has been deprecated for html_table ' .
'and should be replaced with rowclasses[]. please fix the code.');
$this->rowclasses = $value;
} else {
parent::__set($name, $value);
}
parent::prepare();
}
}

Expand All @@ -1526,27 +1415,23 @@ class html_table_row extends html_component {
* @see lib/html_component#prepare()
* @return void
*/
public function prepare(renderer_base $output, moodle_page $page, $target) {
parent::prepare($output, $page, $target);
public function prepare() {
parent::prepare();
}

/**
* Shortcut method for creating a row with an array of cells. Converts cells to html_table_cell objects.
* Constructor
* @param array $cells
* @return html_table_row
*/
public static function make($cells=array()) {
$row = new html_table_row();
foreach ($cells as $celltext) {
if (!($celltext instanceof html_table_cell)) {
$cell = new html_table_cell();
$cell->text = $celltext;
$row->cells[] = $cell;
*/
public function __construct(array $cells=null) {
$cells = (array)$cells;
foreach ($cells as $cell) {
if ($cell instanceof html_table_cell) {
$this->cells[] = $cell;
} else {
$row->cells[] = $celltext;
$this->cells[] = new html_table_cell($cell);
}
}
return $row;
}
}

Expand Down Expand Up @@ -1588,11 +1473,15 @@ class html_table_cell extends html_component {
* @see lib/html_component#prepare()
* @return void
*/
public function prepare(renderer_base $output, moodle_page $page, $target) {
public function prepare() {
if ($this->header && empty($this->scope)) {
$this->scope = 'col';
}
parent::prepare($output, $page, $target);
parent::prepare();
}

public function __construct($text = null) {
$this->text = $text;
}
}

Expand Down
46 changes: 0 additions & 46 deletions lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,44 +129,6 @@ public static function prepare_classes($classes) {
public function pix_url($imagename, $component = 'moodle') {
return $this->page->theme->pix_url($imagename, $component);
}

/**
* A helper function that takes a html_component subclass as param.
* If that component has an id attribute and an array of valid component_action objects,
* it sets up the appropriate event handlers.
*
* @param html_component $component
* @return void;
*/
protected function prepare_event_handlers(html_component $component) {
//TODO: to be deleted soon
$actions = $component->get_actions();
if (!empty($actions) && is_array($actions) && $actions[0] instanceof component_action) {
foreach ($actions as $action) {
if (!empty($action->jsfunction)) {
$this->page->requires->event_handler("#$component->id", $action->event, $action->jsfunction, $action->jsfunctionargs);
}
}
}
}

/**
* Helper function for applying of html_component options
* @param html_component $component
* @param array $options
* @return void
*/
public static function apply_component_options(html_component $component, array $options = null) {
//TODO: to be deleted soon
$options = (array)$options;
foreach ($options as $key => $value) {
if ($key === 'class' or $key === 'classes') {
$component->add_classes($value);
} else if (array_key_exists($key, $component)) {
$component->$key = $value;
}
}
}
}


Expand Down Expand Up @@ -1770,8 +1732,6 @@ public function table(html_table $table) {
$heading->header = true;
}

$this->prepare_event_handlers($heading);

$heading->add_classes(array('header', 'c' . $key));
if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) {
$heading->colspan = $table->headspan[$key];
Expand Down Expand Up @@ -1831,14 +1791,11 @@ public function table(html_table $table) {
foreach ($row as $unused => $item) {
$cell = new html_table_cell();
$cell->text = $item;
$this->prepare_event_handlers($cell);
$newrow->cells[] = $cell;
}
$row = $newrow;
}

$this->prepare_event_handlers($row);

$oddeven = $oddeven ? 0 : 1;
if (isset($table->rowclasses[$key])) {
$row->add_classes(array_unique(html_component::clean_classes($table->rowclasses[$key])));
Expand All @@ -1860,9 +1817,6 @@ public function table(html_table $table) {
$cell = $mycell;
}

// Prepare all events handlers for this cell
$this->prepare_event_handlers($cell);

if (isset($table->colclasses[$key])) {
$cell->add_classes(array_unique(html_component::clean_classes($table->colclasses[$key])));
}
Expand Down
2 changes: 1 addition & 1 deletion mod/forum/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function subscriber_selection_form(user_selector_base $existinguc, user_s

$table = new html_table();
$table->set_classes(array('subscribertable','boxaligncenter'));
$table->data = array(html_table_row::make(array($existingcell, $actioncell, $potentialcell)));
$table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
$output .= $this->output->table($table);

$output .= html_writer::end_tag('form');
Expand Down
2 changes: 1 addition & 1 deletion mod/lesson/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2596,7 +2596,7 @@ public function display_answers(html_table $table) {
$cells = array();
$cells[] = "<span class=\"label\">".get_string("jump", "lesson")." $i<span>: ";
$cells[] = $this->get_jump_name($answer->jumpto);
$table->data[] = html_table_row::make($cells);
$table->data[] = new html_table_row($cells);
if ($i === 1){
$table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
}
Expand Down
4 changes: 2 additions & 2 deletions mod/lesson/pagetypes/branchtable.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ public function display_answers($table) {
$cells = array();
$cells[] = "<span class=\"label\">".get_string("branch", "lesson")." $i<span>: ";
$cells[] = format_text($answer->answer, FORMAT_MOODLE, $options);
$table->data[] = html_table_row::make($cells);
$table->data[] = new html_table_row($cells);

$cells = array();
$cells[] = "<span class=\"label\">".get_string("jump", "lesson")." $i<span>: ";
$cells[] = $this->get_jump_name($answer->jumpto);
$table->data[] = html_table_row::make($cells);
$table->data[] = new html_table_row($cells);

if ($i === 1){
$table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
Expand Down
Loading

0 comments on commit 8cea545

Please sign in to comment.