X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=plekit%2Fphp%2Ftable.php;h=dc5bd8e67f46ed565105fb7924766834a4528be4;hb=a38b7921e68e9f0b6cd290457302ef744f3be1fe;hp=7adcb5f3c5a0d5cce6fbf109f0c9b931dfa63c13;hpb=f99cfc8401815836073effa34c5cda024e77a3a9;p=plewww.git diff --git a/plekit/php/table.php b/plekit/php/table.php index 7adcb5f..dc5bd8e 100644 --- a/plekit/php/table.php +++ b/plekit/php/table.php @@ -7,6 +7,7 @@ require_once 'prototype.php'; drupal_set_html_head(' + @@ -14,56 +15,81 @@ drupal_set_html_head(' //////////////////////////////////////// // table_id: 's id tag - WARNING : do not use '-' in table ids as it's used for generating javascript code -// headers: an associative array "label"=>"type" -// column_sort: the column to sort on at load-time +// headers: an associative array; the values can take several forms +// simple/legacy form is "label"=>"type" +// more advanced form is "label"=>options, it self a dict with the following known keys +// (*) 'type': the type for sorting; this is passed to the javascript layer for custom sorting +// default is to use 'text', custom sort functions can be specified with e.g. 'type'=>'sortAlphaNumericBottom' +// a special case is for type to be 'date-format' like e.g. 'type'=>'date-dmy' +// setting type to 'none' gives an non-sortable column +// (*) 'title': if set, this is used in the "Sort on ``''" bubble +// sort_column: the column to sort on at load-time - set to negative number for no onload- sorting // options : an associative array to override options +// - bullets1 : set to true if you want decorative bullets in column 1 (need white background) +// - stripes : use diferent colors for odd and even rows +// - caption : a caption for the table -- never used I'm afraid // - search_area : boolean (default true) // - pagesize_area : boolean (default true) // - notes_area : boolean (default true) // - search_width : size in chars of the search text dialog -// - notes : an array of additional notes // - pagesize: the initial pagination size // - pagesize_def: the page size when one clicks the pagesize reset button // - max_pages: the max number of pages to display in the paginator +// - notes : an array of additional notes +// - debug: enables debug callbacks (prints out on console.log) class PlekitTable { // mandatory var $table_id; var $headers; - var $column_sort; + var $sort_column; // options + var $bullets1; // boolean - default false - display decorative bullets in column 1 + var $stripes; // boolean - default true - use different colors for odd and even rows + var $caption; // string - never used so far var $search_area; // boolean (default true) var $pagesize_area; // boolean (default true) var $notes_area; // boolean (default true) var $search_width; // size in chars of the search text dialog - var $pagesize; // the initial pagination size + var $pagesize; // the initial pagination size var $pagesize_def; // the page size when one clicks the pagesize reset button var $max_pages; // the max number of pages to display in the paginator var $notes; // an array of additional notes + var $debug; // set to true for enabling various log messages on console.log + + // internal var $has_tfoot; - function PlekitTable ($table_id,$headers,$column_sort,$options=NULL) { + function PlekitTable ($table_id,$headers,$sort_column,$options=NULL) { $this->table_id = $table_id; $this->headers = $headers; - $this->column_sort = $column_sort; - - $this->has_tfoot=false; + $this->sort_column = $sort_column; + $this->bullets1 = true; + $this->stripes=true; + $this->caption=''; $this->search_area = true; $this->pagesize_area = true; $this->notes_area = true; $this->search_width = 40; $this->pagesize = 25; - $this->pagesize_def = 999; + $this->pagesize_def = 9999; $this->max_pages = 10; $this->notes = array(); + $this->debug = false; $this->set_options ($options); + + // internal + $this->has_tfoot=false; } function set_options ($options) { if ( ! $options) return; + if (array_key_exists('bullets1',$options)) $this->bullets1=$options['bullets1']; + if (array_key_exists('stripes',$options)) $this->stripes=$options['stripes']; + if (array_key_exists('caption',$options)) $this->caption=$options['caption']; if (array_key_exists('search_area',$options)) $this->search_area=$options['search_area']; if (array_key_exists('pagesize_area',$options)) $this->pagesize_area=$options['pagesize_area']; if (array_key_exists('notes_area',$options)) $this->notes_area=$options['notes_area']; @@ -71,8 +97,8 @@ class PlekitTable { if (array_key_exists('pagesize',$options)) $this->pagesize=$options['pagesize']; if (array_key_exists('pagesize_def',$options)) $this->pagesize_def=$options['pagesize_def']; if (array_key_exists('max_pages',$options)) $this->max_pages=$options['max_pages']; - if (array_key_exists('notes',$options)) $this->notes=array_merge($this->notes,$options['notes']); + if (array_key_exists('debug',$options)) $this->debug=$options['debug']; } public function columns () { @@ -85,42 +111,59 @@ class PlekitTable { $classname="paginationcallback-".$paginator; $classname.=" max-pages-" . $this->max_pages; $classname.=" paginate-" . $this->pagesize; - // instantiate paginator callback - print <<< EOF -<script type="text/javascript"> -function $paginator (opts) { plekit_table_paginator (opts,"$this->table_id"); } -</script> -<br/> -<table id="$this->table_id" cellpadding="0" cellspacing="0" border="0" -class="plekit_table sortable-onload-$this->column_sort rowstyle-alt colstyle-alt no-arrow $classname"> -<thead> -EOF; + if ($this->bullets1) { $classname .= " bullets1"; } + if ($this->stripes) { $classname .= " rowstyle-alt"; } + if ($this->sort_column >= 0) { $classname .= " sortable-onload-$this->sort_column"; } + + // instantiate paginator callback + print "<script type='text/javascript'> function $paginator (opts) { plekit_table_paginator (opts,'$this->table_id'); } </script>\n"; + + // instantiate debug hooks if needed + if ($this->debug) { + $cb_init = $this->table_id."_init"; + print "<script type='text/javascript'> function $cb_init () { plc_message ('sorting table $this->table_id'); } </script>\n"; + $classname .= " sortinitiatedcallback-$cb_init"; + $cb_comp = $this->table_id."_comp"; + print "<script type='text/javascript'> function $cb_comp () { plc_message ('table $this->table_id sorted'); } </script>\n"; + $classname .= " sortcompletecallback-$cb_comp"; + } + // start actual table + print "<table id='$this->table_id' class='plekit_table colstyle-alt no-arrow $classname'><thead>\n"; - if ($this->pagesize_area) - print $this->pagesize_area_html (); - if ($this->search_area) - print $this->search_area_html (); - - print "<tr>"; - foreach ($this->headers as $label => $type) { - if ($type == "none" ) { - $class=""; - } else { - if ($type == "string") $type=""; - if ($type == "int") $type=""; - if ($type == "float") $type=""; - $class="sortable"; - if ( ! empty($type)) $class .= "-sort" . $type; + if ($this->pagesize_area) + print $this->pagesize_area_html (); + if ($this->search_area) + print $this->search_area_html (); + + if ($this->caption) + print "<caption> $this->caption </caption>"; + print "<tr>"; + foreach ($this->headers as $label => $colspec) { + // which form is being used + if (is_array($colspec)) { + $type=$colspec['type']; + $title=ucfirst($colspec['title']); + } else { + // simple/legacy form + $type=$colspec; + $title=NULL; + } + switch ($type) { + case "none" : + $class=""; break; + case "string": case "int": case "float": + $class="sortable"; break; + case ( strpos($type,"date-") == 0): + $class="sortable-" . $type; break; + default: + $class="sortable-sort" . $type; break; + } + $title_part=$title ? "title=\"$title\"" : ""; + print ("<th class=\"$class plekit_table\" $title_part>$label</th>\n"); } - printf ('<th class="%s plekit_table">%s</th>',$class,$label); - } - print <<< EOF -</tr> -</thead> -<tbody> -EOF; -} + print "</tr></thead><tbody>"; + } //////////////////// // for convenience, the options that apply to the bottom area can be passed here @@ -142,9 +185,9 @@ EOF; <form class='pagesize' action='satisfy_xhtml_validator'><fieldset> <input class='pagesize_input' type='text' id="$pagesize_text_id" value='$this->pagesize' onkeyup='plekit_pagesize_set("$this->table_id","$pagesize_text_id", $this->pagesize);' - size='3' maxlength='3' /> + size='3' maxlength='4' /> <label class='pagesize_label'> items/page </label> - <img class='table_reset' src="/planetlab/icons/clear.png" alt="reset visible size" + <img class='reset' src="/planetlab/icons/clear.png" alt="reset visible size" onmousedown='plekit_pagesize_reset("$this->table_id","$pagesize_text_id",$this->pagesize_def);' /> </fieldset></form></td></tr> EOF; @@ -159,18 +202,18 @@ EOF; $search_and_id = $this->table_id . "_search_and"; $result = <<< EOF <tr class='search_area'><td class='search_area' colspan='$width'> -<form class='table_search' action='satisfy_xhtml_validator'><fieldset> - <label class='table_search_label'> Search </label> - <input class='table_search_input' type='text' id='$search_text_id' +<div class='search'><fieldset> + <label class='search_label'> Search </label> + <input class='search_input' type='text' id='$search_text_id' onkeyup='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");' size='$this->search_width' maxlength='256' /> <label>and</label> - <input id='$search_and_id' class='table_search_and' + <input id='$search_and_id' class='search_and' type='checkbox' checked='checked' onchange='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");' /> - <img class='table_reset' src="/planetlab/icons/clear.png" alt="reset search" + <img class='reset' src="/planetlab/icons/clear.png" alt="reset search" onmousedown='plekit_table_filter_reset("$this->table_id","$search_text_id","$search_and_id");' /> -</fieldset></form></td></tr> +</fieldset></div></td></tr> EOF; return $result; } @@ -195,19 +238,22 @@ EOF; //////////////////////////////////////// function notes_area_html () { - $default_notes = array( - "Enter & or | in the search area to switch between <span class='bold'>AND</span> and <span class='bold'>OR</span> search modes", - "Hold down the shift key to select multiple columns to sort"); + $search_notes = + array("Enter & or | in the search area to switch between <span class='bold'>AND</span> and <span class='bold'>OR</span> search modes"); + $sort_notes = + array ("Hold down the shift key to select multiple columns to sort"); if ($this->notes) $notes=$this->notes; else $notes=array(); - $notes=array_merge($notes,$default_notes); + $notes=array_merge($notes,$sort_notes); + if ($this->search_area) + $notes=array_merge($notes,$search_notes); if (! $notes) return ""; $result = ""; - $result .= "<p class='plekit_table_note'> <span class='plekit_table_note_title'>Notes</span>\n"; + $result .= "<p class='table_note'> <span class='table_note_title'>Notes</span>\n"; foreach ($notes as $note) $result .= "<br/>$note\n"; $result .= "</p>"; @@ -227,14 +273,24 @@ EOF; } //////////////////// - public function cell ($text,$colspan=0,$align=NULL) { print $this->cell_html ($text,$colspan,$align); } - public function cell_html ($text,$colspan=0,$align=NULL) { - $result=""; - $result .= "<td"; - if ($colspan) $result .= " colspan='$colspan'"; - if ($align) $result .= " style='text-align:$align'"; - $result .= ">$text</td>"; - return $result; + // supported options: + // (*) only-if : if set and false, then print 'n/a' instead of (presumably void) $text + // (*) class + // (*) columns + // (*) hfill + // (*) align + public function cell ($text,$options=NULL) { print $this->cell_html ($text,$options); } + public function cell_html ($text,$options=NULL) { + if (isset ($options['only-if']) && ! $options['only-if'] ) + $text="n/a"; + $html=""; + $html .= "<td"; + $option=$options['class']; if ($option) $html .= " class='$option'"; + $option=$options['columns'];if ($option) $html .= " colspan='$option'"; + $option=$options['hfill']; if ($option) $html .= " colspan='" . $this->columns() . "'"; + $option=$options['align']; if ($option) $html .= " style='text-align:$option'"; + $html .= ">$text</td>"; + return $html; } }