Passed
Push — master ( 42337f...468e47 )
by Jean-Christophe
02:24
created

HtmlList::addList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\semantic\html\elements;
4
5
use Ajax\semantic\html\base\HtmlSemCollection;
6
use Ajax\semantic\html\content\HtmlListItem;
7
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
8
use Ajax\JsUtils;
9
use Ajax\semantic\html\modules\checkbox\AbstractCheckbox;
10
use Ajax\semantic\html\elements\html5\HtmlDatalist;
11
use Ajax\semantic\components\Visibility;
12
13
class HtmlList extends HtmlSemCollection {
14
	protected $_hasCheckedList;
15
	protected $_fireOnInit=true;
16
	protected $_ckItemChange="";
17
	protected $_maxVisible;
18
	protected $_dataList;
19
20
	public function __construct($identifier, $items=array()) {
21
		parent::__construct($identifier, "div", "ui list");
22
		$this->addItems($items);
23
		$this->_hasCheckedList=false;
24
	}
25
26
	protected function createItem($value) {
27
		$count=$this->count();
28
		$item=new HtmlListItem("item-" . $this->identifier . "-" . $count, $value);
29
		return $item;
30
	}
31
32
	public function addHeader($niveau, $content) {
33
		$header=new HtmlHeader("header-" . $this->identifier, $niveau, $content, "page");
34
		$this->wrap($header);
35
		return $header;
36
	}
37
38
	public function getItemPart($index,$partName="header"){
39
		return $this->getItem($index)->getPart($partName);
40
	}
41
42
	public function itemsAs($tagName) {
43
		return $this->contentAs($tagName);
44
	}
45
46
	public function asLinks($hrefs=[],$target=NUll) {
47
		$this->addToPropertyCtrl("class", "link", array ("link" ));
48
		return parent::asLinks($hrefs,$target);
49
	}
50
51
	public function addList($items=array()) {
52
		$list=new HtmlList("", $items);
53
		$list->setClass("list");
54
		return $this->addItem($list);
55
	}
56
	
57
	/**
58
	 * @return HtmlListItem
59
	 */
60
	public function getItem($index){
61
		return parent::getItem($index);
62
	}
63
64
	protected function getItemToAdd($item){
65
		$itemO=parent::getItemToAdd($item);
66
		if($itemO instanceof AbstractCheckbox)
67
			$itemO->addClass("item");
68
		return $itemO;
69
	}
70
71
	public function setCelled() {
72
		return $this->addToProperty("class", "celled");
73
	}
74
75
	public function setBulleted() {
76
		return $this->addToProperty("class", "bulleted");
77
	}
78
79
	public function setOrdered() {
80
		return $this->addToProperty("class", "ordered");
81
	}
82
83
	public function run(JsUtils $js) {
84
		if ($this->_hasCheckedList === true) {
85
			$jsCode=include dirname(__FILE__) . '/../../components/jsTemplates/tplCheckedList.php';
86
			$jsCode=\str_replace("%identifier%", "#" . $this->identifier, $jsCode);
87
			$jsCode=\str_replace("%fireOnInit%", $this->_fireOnInit, $jsCode);
88
			$jsCode=\str_replace("%onChange%", $this->_ckItemChange, $jsCode);
89
			$this->executeOnRun($jsCode);
90
		}
91
		return parent::run($js);
92
	}
93
	
94
	protected function addFollowPoints(){
95
		$count=$this->count();
96
		for ($i=$this->_maxVisible;$i<$count;$i++){
97
			$this->getItem($i)->addClass("notVisible")->setProperty("style", "display: none;");
98
		}
99
		$item=$this->addItem("...");
100
		$item->addClass("points");
101
		$item->onClick('$(this).hide();$("#'.$this->identifier.' .notVisible").show();');
102
	}
103
	
104
	public function onCkItemChange($jsCode){
105
		$this->_ckItemChange=$jsCode;
106
	}
107
108
	/**
109
	 * @param boolean $fireOnInit
110
	 */
111
	public function setFireOnInit($fireOnInit) {
112
		$this->_fireOnInit = $fireOnInit;
113
	}
114
115
	public function setRelaxed() {
116
		return $this->addToProperty("class", "relaxed");
117
	}
118
119
	public function setSelection() {
120
		return $this->addToProperty("class", "selection");
121
	}
122
123
	public function setDivided() {
124
		return $this->addToProperty("class", "divided");
125
	}
126
127
	public function setHorizontal() {
128
		return $this->addToProperty("class", "horizontal");
129
	}
130
131
	/**
132
	 * {@inheritDoc}
133
	 * @see \Ajax\common\html\HtmlCollection::compile()
134
	 */
135
	public function compile(JsUtils $js = NULL, &$view = NULL) {
136
		if(isset($this->_maxVisible) && $this->_maxVisible<$this->count()){
137
			$this->addFollowPoints();
138
			if(isset($js)){
139
				$visibility=new Visibility($js);
140
				$visibility->attach("#".$this->identifier);
141
				$visibility->setOnTopVisible("$(this).children('.notVisible').hide();$(this).find('.points').show();");
142
				$visibility->compile($js);
143
			}
144
		}
145
		return parent::compile ($js,$view);
146
	}
147
148
	/**
149
	 * Adds a grouped checked box to the list
150
	 * @param array $items
151
	 * @param string|array|null $masterItem
152
	 * @param array $values
153
	 * @param string $notAllChecked
154
	 * @return HtmlList
155
	 */
156
	public function addCheckedList($items=array(), $masterItem=NULL, $values=array(),$notAllChecked=false,$name=null) {
157
		$count=$this->count();
158
		$identifier=$this->identifier . "-" . $count;
159
		if (isset($masterItem)) {
160
			if(\is_array($masterItem)){
161
				$masterO=new HtmlFormCheckbox("master-" . $identifier, @$masterItem[0],@$masterItem[1]);
162
				if(isset($name))
163
					$masterO->setName($name);
164
				if(isset($masterItem[1])){
165
					if(\array_search($masterItem[1], $values)!==false){
166
						$masterO->getDataField()->setProperty("checked", "");
167
					}
168
				}
169
			}else{
170
				$masterO=new HtmlFormCheckbox("master-" . $identifier, $masterItem);
171
			}
172
			if($notAllChecked){
173
				$masterO->getDataField()->addClass("_notAllChecked");
174
			}
175
			$masterO->getHtmlCk()->addToProperty("class", "master");
176
			$masterO->setClass("item");
177
			$this->addItem($masterO);
178
		}
179
		$fields=array ();
180
		$i=0;
181
		foreach ( $items as $val => $caption ) {
182
			$itemO=new HtmlFormCheckbox($identifier . "-" . $i++, $caption, $val, "child");
183
			if (\array_search($val, $values) !== false) {
184
				$itemO->getDataField()->setProperty("checked", "");
185
			}
186
			if(isset($name))
187
				$itemO->setName($name);
188
			$itemO->setClass("item");
189
			$fields[]=$itemO;
190
		}
191
		if (isset($masterO) === true) {
192
			$list=new HtmlList("", $fields);
193
			$list->setClass("list");
194
			$masterO->addContent($list);
195
		} else {
196
			$this->addList($fields);
197
		}
198
		$this->_hasCheckedList=true;
199
		return $this;
200
	}
201
202
	public function setIcons($icons){
203
		if(!\is_array($icons)){
204
			$icons=\array_fill(0, \sizeof($this->content), $icons);
205
		}
206
		$max=\min(\sizeof($icons),\sizeof($this->content));
207
		for($i=0;$i<$max;$i++){
208
			$this->content[$i]->addIcon($icons[$i]);
209
		}
210
		return $this;
211
	}
212
	/**
213
	 * @return mixed
214
	 */
215
	public function getMaxVisible() {
216
		return $this->_maxVisible;
217
	}
218
219
	/**
220
	 * @param mixed $_maxVisible
221
	 */
222
	public function setMaxVisible($_maxVisible) {
223
		$this->_maxVisible = $_maxVisible;
224
		return $this;
225
	}
226
227
}
228