HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbGridView.php
Go to the documentation of this file.
1 <?php
10 Yii::import('zii.widgets.grid.CGridView');
11 Yii::import('booster.widgets.TbDataColumn');
12 
20 class TbGridView extends CGridView
21 {
22  // Table types.
23  const TYPE_STRIPED = 'striped';
24  const TYPE_BORDERED = 'bordered';
25  const TYPE_CONDENSED = 'condensed';
26  const TYPE_HOVER = 'hover';
27 
32  public $type;
33 
37  public $pagerCssClass = 'no-class';
38 
43  public $pager = array('class' => 'booster.widgets.TbPager');
44 
49  public $cssFile = false;
50 
54  public $responsiveTable = false;
55 
59  public $extraParams = array();
60 
66  public function init() {
67 
68  parent::init();
69 
70  $classes = array('table');
71  if (isset($this->type)) {
72  if (is_string($this->type)) {
73  $this->type = explode(' ', $this->type);
74  }
75 
76  if (!empty($this->type)) {
77  $validTypes = array(self::TYPE_STRIPED, self::TYPE_BORDERED, self::TYPE_CONDENSED, self::TYPE_HOVER);
78 
79  foreach ($this->type as $type) {
80  if (in_array($type, $validTypes)) {
81  $classes[] = 'table-' . $type;
82  }
83  }
84  }
85  }
86 
87  if (!empty($classes)) {
88  $classes = implode(' ', $classes);
89  if (isset($this->itemsCssClass)) {
90  $this->itemsCssClass .= ' ' . $classes;
91  } else {
92  $this->itemsCssClass = $classes;
93  }
94  }
95 
96  $booster = Booster::getBooster();
97  $popover = $booster->popoverSelector;
98  $tooltip = $booster->tooltipSelector;
99 
100  $afterAjaxUpdate = "js:function() {
101  jQuery('.popover').remove();
102  jQuery('{$popover}').popover();
103  jQuery('.tooltip').remove();
104  jQuery('{$tooltip}').tooltip();
105  }";
106 
107  if (!isset($this->afterAjaxUpdate)) {
108  $this->afterAjaxUpdate = $afterAjaxUpdate;
109  }
110  }
111 
117  protected function initColumns()
118  {
119  foreach ($this->columns as $i => $column) {
120  if (is_array($column) && !isset($column['class'])) {
121  $this->columns[$i]['class'] = 'booster.widgets.TbDataColumn';
122  }
123  }
124 
126 
127  if ($this->responsiveTable) {
128  $this->writeResponsiveCss();
129  }
130  }
131 
142  protected function createDataColumn($text)
143  {
144  if (!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/', $text, $matches)) {
145  throw new CException(Yii::t(
146  'zii',
147  'The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'
148  ));
149  }
150 
151  $column = new TbDataColumn($this);
152  $column->name = $matches[1];
153 
154  if (isset($matches[3]) && $matches[3] !== '') {
155  $column->type = $matches[3];
156  }
157 
158  if (isset($matches[5])) {
159  $column->header = $matches[5];
160  }
161 
162  return $column;
163  }
164 
170  protected function writeResponsiveCss()
171  {
172  $cnt = 1;
173  $labels = '';
174  foreach ($this->columns as $column) {
176  ob_start();
177  $column->renderHeaderCell();
178  $name = strip_tags(ob_get_clean());
179 
180  $labels .= "#$this->id td:nth-of-type($cnt):before { content: '{$name}'; }\n";
181  $cnt++;
182  }
183 
184  $css = <<<EOD
185 @media
186  only screen and (max-width: 760px),
187  (min-device-width: 768px) and (max-device-width: 1024px) {
188 
189  /* Force table to not be like tables anymore */
190  #{$this->id} table,#{$this->id} thead,#{$this->id} tbody,#{$this->id} th,#{$this->id} td,#{$this->id} tr {
191  display: block;
192  }
193 
194  /* Hide table headers (but not display: none;, for accessibility) */
195  #{$this->id} thead tr {
196  position: absolute;
197  top: -9999px;
198  left: -9999px;
199  }
200 
201  #{$this->id} tr { border: 1px solid #ccc; }
202 
203  #{$this->id} td {
204  /* Behave like a "row" */
205  border: none;
206  border-bottom: 1px solid #eee;
207  position: relative;
208  padding-left: 50%;
209  }
210 
211  #{$this->id} td:before {
212  /* Now like a table header */
213  position: absolute;
214  /* Top/left values mimic padding */
215  top: 6px;
216  left: 6px;
217  width: 45%;
218  padding-right: 10px;
219  white-space: nowrap;
220  }
221  .grid-view .button-column {
222  text-align: left;
223  width:auto;
224  }
225  /*
226  Label the data
227  */
228  {$labels}
229  }
230 EOD;
231  Yii::app()->clientScript->registerCss(__CLASS__ . '#' . $this->id, $css);
232  }
233 }