Total Complexity | 43 |
Total Lines | 212 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like HtmlList often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HtmlList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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) { |
||
113 | } |
||
114 | |||
115 | public function setRelaxed() { |
||
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){ |
||
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 |