1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\common\html; |
4
|
|
|
|
5
|
|
|
use Ajax\service\JArray; |
6
|
|
|
use Ajax\JsUtils; |
7
|
|
|
use Ajax\service\JReflection; |
8
|
|
|
use Ajax\service\JString; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Base class for Html collections |
12
|
|
|
* @author jc |
13
|
|
|
* @version 1.001 |
14
|
|
|
*/ |
15
|
|
|
abstract class HtmlCollection extends HtmlDoubleElement { |
16
|
|
|
|
17
|
|
|
public function __construct($identifier,$tagName="div"){ |
18
|
|
|
parent::__construct($identifier,$tagName); |
19
|
|
|
$this->content=array(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function addItems($items){ |
23
|
|
|
if(JArray::isAssociative($items)){ |
24
|
|
|
foreach ($items as $k=>$v){ |
25
|
|
|
$this->addItem([$k,$v]); |
26
|
|
|
} |
27
|
|
|
}else{ |
28
|
|
|
foreach ($items as $item){ |
29
|
|
|
$this->addItem($item); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function setItems($items){ |
36
|
|
|
$this->content=$items; |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getItems(){ |
41
|
|
|
return $this->content; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function getItemToAdd($item){ |
45
|
|
|
$itemO=$item; |
46
|
|
|
if($this->createCondition($item)===true){ |
47
|
|
|
$itemO=$this->createItem($item); |
48
|
|
|
} |
49
|
|
|
return $itemO; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function setItemIdentifier($item,$classname,$index){ |
53
|
|
|
if($item instanceof BaseWidget){ |
54
|
|
|
if(JString::isNull($item->getIdentifier())){ |
55
|
|
|
$item->setIdentifier($classname."-".$this->identifier."-".$index); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* adds and returns an item |
62
|
|
|
* @param HtmlDoubleElement|string|array $item |
63
|
|
|
* @return \Ajax\common\html\HtmlDoubleElement |
64
|
|
|
*/ |
65
|
|
|
public function addItem($item){ |
66
|
|
|
$itemO=$this->getItemToAdd($item); |
67
|
|
|
$this->addContent($itemO); |
68
|
|
|
return $itemO; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function insertItem($item,$position=0){ |
72
|
|
|
$itemO=$this->getItemToAdd($item); |
73
|
|
|
\array_splice( $this->content, $position, 0, array($itemO)); |
74
|
|
|
return $itemO; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return the item at index |
79
|
|
|
* @param int|string $index the index or the item identifier |
80
|
|
|
* @return \Ajax\common\html\HtmlDoubleElement |
81
|
|
|
*/ |
82
|
|
|
public function getItem($index) { |
83
|
|
|
if (is_int($index)&& isset($this->content[$index])) |
84
|
|
|
return $this->content[$index]; |
85
|
|
|
else { |
86
|
|
|
$elm=$this->getElementById($index, $this->content); |
87
|
|
|
return $elm; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setItem($index, $value) { |
92
|
|
|
$this->content[$index]=$value; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function removeItem($index){ |
97
|
|
|
return array_splice($this->content, $index, 1); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function count(){ |
101
|
|
|
return \sizeof($this->content); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/* (non-PHPdoc) |
105
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
106
|
|
|
*/ |
107
|
|
|
public function fromDatabaseObject($object, $function) { |
108
|
|
|
return $this->addItem($function($object)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function apply($callBack){ |
112
|
|
|
foreach ($this->content as $item){ |
113
|
|
|
$callBack($item); |
114
|
|
|
} |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/* |
119
|
|
|
* (non-PHPdoc) |
120
|
|
|
* @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray() |
121
|
|
|
*/ |
122
|
|
|
public function fromArray($array) { |
123
|
|
|
$this->addItems($array); |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
/** |
127
|
|
|
* The item factory |
128
|
|
|
* @param mixed $value |
129
|
|
|
*/ |
130
|
|
|
abstract protected function createItem($value); |
131
|
|
|
|
132
|
|
|
protected function createCondition($value){ |
133
|
|
|
return !($value instanceof BaseHtml); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function contentAs($tagName){ |
137
|
|
|
foreach ($this->content as $item){ |
138
|
|
|
$item->setTagName($tagName); |
139
|
|
|
} |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function setProperties($properties){ |
144
|
|
|
$i=0; |
145
|
|
|
foreach ($properties as $k=>$v){ |
146
|
|
|
$c=$this->content[$i++]; |
147
|
|
|
if(isset($c)) |
148
|
|
|
$c->setProperty($k,$v); |
149
|
|
|
else |
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Sets the values of a property for each item in the collection |
157
|
|
|
* @param string $property |
158
|
|
|
* @param array|mixed $values |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
|
|
public function setPropertyValues($property,$values){ |
162
|
|
|
if(\is_array($values)===false){ |
163
|
|
|
$values=\array_fill(0, $this->count(),$values); |
164
|
|
|
} |
165
|
|
|
foreach ($values as $i=>$value){ |
166
|
|
|
if(isset($this->content[$i])){ |
167
|
|
|
$this->content[$i]->setProperty($property,$value); |
168
|
|
|
} |
169
|
|
|
else{ |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Adds the values of a property for each item in the collection |
178
|
|
|
* @param string $property |
179
|
|
|
* @param array|mixed $values |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
|
public function addPropertyValues($property,$values){ |
183
|
|
|
if(\is_array($values)===false){ |
184
|
|
|
$values=\array_fill(0, $this->count(),$values); |
185
|
|
|
} |
186
|
|
|
foreach ($values as $i=>$value){ |
187
|
|
|
if(isset($this->content[$i])){ |
188
|
|
|
$this->content[$i++]->addToProperty($property,$value); |
189
|
|
|
} |
190
|
|
|
else{ |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function compile(JsUtils $js=NULL, &$view=NULL) { |
198
|
|
|
$index=0; |
199
|
|
|
$classname=\strtolower(JReflection::shortClassName($this)); |
200
|
|
|
foreach ($this->content as $item){ |
201
|
|
|
$this->setItemIdentifier($item,$classname,$index++); |
202
|
|
|
} |
203
|
|
|
return parent::compile($js,$view); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function getItemById($identifier){ |
207
|
|
|
return $this->getElementById($identifier, $this->content); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param array $hrefs |
212
|
|
|
* @param string $target |
213
|
|
|
* @return HtmlCollection |
214
|
|
|
*/ |
215
|
|
|
public function asLinks($hrefs=[],$target=NUll) { |
216
|
|
|
foreach ( $this->content as $index=>$item ) { |
217
|
|
|
if($item instanceof HtmlDoubleElement){ |
218
|
|
|
$href=""; |
219
|
|
|
if(isset($hrefs[$index])) |
220
|
|
|
$href=$hrefs[$index]; |
221
|
|
|
$item->asLink($href,$target); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Remove a portion of the items array and replace it with something else |
229
|
|
|
* @param int $offset |
230
|
|
|
* @param int $length If length is omitted, removes everything from offset to the end of the array. If length is specified and is positive, then that many elements will be removed. If length is specified and is negative then the end of the removed portion will be that many elements from the end of the array. If length is specified and is zero, no elements will be removed. Tip: to remove everything from offset to the end of the array when replacement is also specified, use count($input) for length. |
231
|
|
|
* @return $this |
232
|
|
|
*/ |
233
|
|
|
public function splice($offset,$length=null){ |
234
|
|
|
$this->content=array_slice($this->content, $offset,$length); |
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|