1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\collections; |
4
|
|
|
|
5
|
|
|
use Ajax\common\html\HtmlCollection; |
6
|
|
|
use Ajax\semantic\html\content\HtmlGridRow; |
7
|
|
|
use Ajax\semantic\html\base\constants\Wide; |
8
|
|
|
use Ajax\semantic\html\base\constants\VerticalAlignment; |
9
|
|
|
use Ajax\semantic\html\base\HtmlSemCollection; |
10
|
|
|
use Ajax\semantic\html\base\traits\TextAlignmentTrait; |
11
|
|
|
use Ajax\semantic\html\content\HtmlGridCol; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Semantic Grid component |
15
|
|
|
* @see http://semantic-ui.com/collections/grid.html |
16
|
|
|
* @author jc |
17
|
|
|
* @version 1.001 |
18
|
|
|
*/ |
19
|
|
|
class HtmlGrid extends HtmlSemCollection{ |
20
|
|
|
use TextAlignmentTrait; |
21
|
|
|
private $_createCols; |
22
|
|
|
private $_colSizing=true; |
23
|
|
|
private $_implicitRows=false; |
24
|
|
|
public function __construct( $identifier,$numRows=1,$numCols=NULL,$createCols=true,$implicitRows=false){ |
25
|
|
|
parent::__construct( $identifier, "div","ui grid"); |
26
|
|
|
$this->_implicitRows=$implicitRows; |
27
|
|
|
$this->_createCols=$createCols; |
28
|
|
|
if(isset($numCols)){ |
29
|
|
|
//if($this->_createCols){ |
|
|
|
|
30
|
|
|
$this->_colSizing=false; |
31
|
|
|
//} |
32
|
|
|
$this->setWide($numCols); |
33
|
|
|
} |
34
|
|
|
$this->setRowsCount($numRows,$numCols); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setWide($wide){ |
38
|
|
|
$wide=Wide::getConstants()["W".$wide]; |
39
|
|
|
$this->addToPropertyCtrl("class", $wide, Wide::getConstants()); |
40
|
|
|
return $this->addToPropertyCtrl("class","column",array("column")); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function addRow($colsCount=NULL){ |
44
|
|
|
$rowCount=$this->rowCount()+1; |
45
|
|
|
$this->setRowsCount($rowCount,$colsCount,true); |
46
|
|
|
return $this->content[$rowCount-1]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function addCol($width=NULL){ |
50
|
|
|
$colCount=$this->colCount()+1; |
51
|
|
|
$this->setColsCount($colCount,true,$width); |
52
|
|
|
if($this->hasOnlyCols($this->count())) |
53
|
|
|
return $this->content[$colCount-1]; |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function addCols($sizes=array()){ |
58
|
|
|
foreach ($sizes as $size){ |
59
|
|
|
$this->addCol($size); |
60
|
|
|
} |
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create $rowsCount rows |
66
|
|
|
* @param int $rowsCount |
67
|
|
|
* @param int $colsCount |
68
|
|
|
* @return \Ajax\semantic\html\collections\HtmlGrid |
69
|
|
|
*/ |
70
|
|
|
public function setRowsCount($rowsCount,$colsCount=NULL,$force=false){ |
71
|
|
|
$count=$this->count(); |
72
|
|
|
if($rowsCount<2 && $force===false){ |
73
|
|
View Code Duplication |
for($i=$count;$i<$colsCount;$i++){ |
|
|
|
|
74
|
|
|
$this->addItem(new HtmlGridCol("col-".$this->identifier."-".$i)); |
75
|
|
|
} |
76
|
|
|
}else{ |
77
|
|
|
if($this->hasOnlyCols($count)){ |
78
|
|
|
$tmpContent=$this->content; |
79
|
|
|
$item=$this->addItem($colsCount); |
80
|
|
|
$item->setContent($tmpContent); |
81
|
|
|
$this->content=array(); |
82
|
|
|
$count=1; |
83
|
|
|
} |
84
|
|
|
for($i=$count;$i<$rowsCount;$i++){ |
85
|
|
|
$this->addItem($colsCount); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function hasOnlyCols($count){ |
92
|
|
|
return $count>0 && $this->content[0] instanceof HtmlGridCol; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setColsCount($numCols,$toCreate=true,$width=NULL){ |
96
|
|
|
if(isset($width)===false) |
97
|
|
|
$this->setWide($numCols); |
98
|
|
|
if($toCreate==true){ |
|
|
|
|
99
|
|
|
$count=$this->count(); |
100
|
|
|
if($count==0 || $this->hasOnlyCols($count)){ |
101
|
|
View Code Duplication |
for($i=$count;$i<$numCols;$i++){ |
|
|
|
|
102
|
|
|
$this->addItem(new HtmlGridCol("col-".$this->identifier."-".$i,$width)); |
103
|
|
|
} |
104
|
|
|
}else{ |
105
|
|
|
for($i=0;$i<$count;$i++){ |
106
|
|
|
$this->getItem($i)->setColsCount($numCols); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* return the row at $index |
115
|
|
|
* @param int $index |
116
|
|
|
* @return \Ajax\semantic\html\collections\HtmlGridRow |
117
|
|
|
*/ |
118
|
|
|
public function getRow($index){ |
119
|
|
|
return $this->getItem($index); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function rowCount(){ |
123
|
|
|
$count=$this->count(); |
124
|
|
|
if($this->hasOnlyCols($count)) |
125
|
|
|
return 0; |
126
|
|
|
return $count; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function colCount(){ |
130
|
|
|
$count=$this->count(); |
131
|
|
|
if($this->hasOnlyCols($count)) |
132
|
|
|
return $count; |
133
|
|
|
if($count>0) |
134
|
|
|
return $this->getItem(0)->count(); |
|
|
|
|
135
|
|
|
return 0; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param int $row |
140
|
|
|
* @param int $col |
141
|
|
|
* @return \Ajax\semantic\html\collections\HtmlGridCol |
142
|
|
|
*/ |
143
|
|
|
public function getCell($row,$col){ |
144
|
|
|
if($row<2 && $this->hasOnlyCols($this->count())) |
145
|
|
|
return $this->getItem($col); |
|
|
|
|
146
|
|
|
$row=$this->getItem($row); |
147
|
|
|
if(isset($row)){ |
148
|
|
|
$col=$row->getItem($col); |
149
|
|
|
} |
150
|
|
|
return $col; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Adds dividers between columns ($vertically=false) or between rows ($vertically=true) |
155
|
|
|
* @param boolean $vertically |
156
|
|
|
* @return \Ajax\semantic\html\collections\HtmlGrid |
157
|
|
|
*/ |
158
|
|
|
public function setDivided($vertically=false){ |
159
|
|
|
$value=($vertically===true)?"vertically divided":"divided"; |
160
|
|
|
return $this->addToPropertyCtrl("class", $value,array("divided")); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Divides rows into cells |
165
|
|
|
* @param boolean $internally true for internal cells |
166
|
|
|
* @return \Ajax\semantic\html\collections\HtmlGrid |
167
|
|
|
*/ |
168
|
|
|
public function setCelled($internally=false){ |
169
|
|
|
$value=($internally===true)?"internally celled":"celled"; |
170
|
|
|
return $this->addToPropertyCtrl("class", $value,array("celled","internally celled")); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* A grid can have its columns centered |
175
|
|
|
*/ |
176
|
|
|
public function setCentered(){ |
177
|
|
|
return $this->addToPropertyCtrl("class", "centered",array("centered")); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* automatically resize all elements to split the available width evenly |
182
|
|
|
* @return \Ajax\semantic\html\collections\HtmlGrid |
183
|
|
|
*/ |
184
|
|
|
public function setEqualWidth(){ |
185
|
|
|
return $this->addToProperty("class", "equal width"); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Adds vertical or/and horizontal gutters |
190
|
|
|
* @param string $value |
191
|
|
|
* @return \Ajax\semantic\html\collections\HtmlGrid |
192
|
|
|
*/ |
193
|
|
|
public function setPadded($value=NULL){ |
194
|
|
|
if(isset($value)) |
195
|
|
|
$this->addToPropertyCtrl("class", $value,array("vertically","horizontally")); |
196
|
|
|
return $this->addToProperty("class", "padded"); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param boolean $very |
201
|
|
|
* @return \Ajax\semantic\html\collections\HtmlGrid |
202
|
|
|
*/ |
203
|
|
|
public function setRelaxed($very=false){ |
204
|
|
|
$value=($very===true)?"very relaxed":"relaxed"; |
205
|
|
|
return $this->addToPropertyCtrl("class", $value,array("relaxed","very relaxed")); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function setVerticalAlignment($value=VerticalAlignment::MIDDLE){ |
209
|
|
|
return $this->addToPropertyCtrl("class", $value." aligned",VerticalAlignment::getConstantValues("aligned")); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritDoc} |
214
|
|
|
* @see \Ajax\common\html\HtmlCollection::createItem() |
215
|
|
|
*/ |
216
|
|
|
protected function createItem($value){ |
217
|
|
|
if($this->_createCols===false) |
218
|
|
|
$value=null; |
219
|
|
|
$item=new HtmlGridRow($this->identifier."-row-".($this->count()+1),$value,$this->_colSizing,$this->_implicitRows); |
220
|
|
|
return $item; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function setValues($values){ |
224
|
|
|
$count=$this->count(); |
225
|
|
|
if($this->_createCols===false){ |
226
|
|
|
for($i=$count;$i<\sizeof($values);$i++){ |
|
|
|
|
227
|
|
|
$colSize=\sizeof($values[$i]); |
228
|
|
|
$this->addItem(new HtmlGridRow($this->identifier."-row-".($this->count()+1),$colSize,$this->_colSizing,$this->_implicitRows)); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
$count=\min(array($this->count(),\sizeof($values))); |
232
|
|
|
for($i=0;$i<$count;$i++){ |
233
|
|
|
$this->content[$i]->setValues($values[$i],$this->_createCols===false); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* stretch the row contents to take up the entire column height |
239
|
|
|
* @return \Ajax\semantic\html\content\HtmlGridRow |
240
|
|
|
*/ |
241
|
|
|
public function setStretched(){ |
242
|
|
|
return $this->addToProperty("class", "stretched"); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.