Skip to content

Commit 6da0828

Browse files
committed
Fix checkedlist items count pb
1 parent e046eb2 commit 6da0828

File tree

2 files changed

+107
-75
lines changed

2 files changed

+107
-75
lines changed

Ajax/semantic/components/jsTemplates/tplCheckedList.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
<?php
2-
return '$("%identifier% .master")
2+
return '
3+
var deferredEach=function (arr, batchSize) {
4+
var deferred = $.Deferred();var index = 0;
5+
function chunk () {var lastIndex = Math.min(index + batchSize, arr.length);
6+
for(;index<lastIndex;index++){deferred.notify(index, arr[index]);}
7+
if (index >= arr.length) {deferred.resolve();} else {setTimeout(chunk, 0);}
8+
};
9+
setTimeout(chunk, 0);return deferred.promise();
10+
};
11+
$("%identifier% .master")
312
.checkbox({
4-
onChecked: function() {if(!$(this).hasClass("_notAllChecked")){$(this).closest(".checkbox").siblings(".list").find(".checkbox").checkbox("check");}},
5-
onUnchecked: function() {$(this).closest(".checkbox").siblings(".list").find(".checkbox").checkbox("uncheck");}
13+
onChecked: function() {if($(this).closest(".loading").length==0 && !$(this).hasClass("_notAllChecked")){$(this).closest(".checkbox").siblings(".list").find(".checkbox").checkbox("check");}},
14+
onUnchecked: function() {if($(this).closest(".loading").length==0){$(this).closest(".checkbox").siblings(".list").find(".checkbox").checkbox("uncheck");}}
615
})
716
;
817
$("%identifier% .list .child.checkbox")
@@ -14,14 +23,17 @@
1423
$checkbox = $listGroup.find(".checkbox"),
1524
allChecked = true,
1625
allUnchecked = true;
17-
$checkbox.each(function() {
18-
if( $(this).checkbox("is checked") ) {
26+
$listGroup.closest(".segment").addClass("loading");
27+
deferredEach($checkbox,5).progress(function(index, item){
28+
if( $(item).checkbox("is checked") ) {
1929
allUnchecked = false;
2030
}
2131
else {
2232
allChecked = false;
2333
}
24-
});
34+
if(!allUnchecked && !allChecked) return;
35+
}).done(function(){
36+
$listGroup.closest(".segment.loading").removeClass("loading");
2537
if(allChecked) {
2638
$parentCheckbox.checkbox("set checked");
2739
}
@@ -34,6 +46,7 @@
3446
$parentCheckbox.checkbox("set indeterminate");
3547
}
3648
%onChange%
49+
});
3750
}
3851
})
3952
;';
Lines changed: 88 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace Ajax\semantic\html\elements;
43

54
use Ajax\semantic\html\base\HtmlSemCollection;
@@ -8,62 +7,72 @@
87
use Ajax\JsUtils;
98
use Ajax\semantic\html\modules\checkbox\AbstractCheckbox;
109
use Ajax\semantic\components\Visibility;
10+
use Ubiquity\utils\base\UString;
1111

1212
class HtmlList extends HtmlSemCollection {
13+
1314
protected $_hasCheckedList;
14-
protected $_fireOnInit=true;
15-
protected $_ckItemChange="";
15+
16+
protected $_fireOnInit = false;
17+
18+
protected $_ckItemChange = "";
19+
1620
protected $_maxVisible;
21+
1722
protected $_dataList;
23+
1824
protected $_visibility;
1925

20-
public function __construct($identifier, $items=array()) {
26+
public function __construct($identifier, $items = array()) {
2127
parent::__construct($identifier, "div", "ui list");
2228
$this->addItems($items);
23-
$this->_hasCheckedList=false;
29+
$this->_hasCheckedList = false;
2430
}
2531

2632
protected function createItem($value) {
27-
$count=$this->count();
28-
$item=new HtmlListItem("item-" . $this->identifier . "-" . $count, $value);
33+
$count = $this->count();
34+
$item = new HtmlListItem("item-" . $this->identifier . "-" . $count, $value);
2935
return $item;
3036
}
3137

3238
public function addHeader($niveau, $content) {
33-
$header=new HtmlHeader("header-" . $this->identifier, $niveau, $content, "page");
39+
$header = new HtmlHeader("header-" . $this->identifier, $niveau, $content, "page");
3440
$this->wrap($header);
3541
return $header;
3642
}
3743

38-
public function getItemPart($index,$partName="header"){
44+
public function getItemPart($index, $partName = "header") {
3945
return $this->getItem($index)->getPart($partName);
4046
}
4147

4248
public function itemsAs($tagName) {
4349
return $this->contentAs($tagName);
4450
}
4551

46-
public function asLinks($hrefs=[],$target=NUll) {
47-
$this->addToPropertyCtrl("class", "link", array ("link" ));
48-
return parent::asLinks($hrefs,$target);
52+
public function asLinks($hrefs = [], $target = NUll) {
53+
$this->addToPropertyCtrl("class", "link", array(
54+
"link"
55+
));
56+
return parent::asLinks($hrefs, $target);
4957
}
5058

51-
public function addList($items=array()) {
52-
$list=new HtmlList("", $items);
59+
public function addList($items = array()) {
60+
$list = new HtmlList("", $items);
5361
$list->setClass("list");
5462
return $this->addItem($list);
5563
}
56-
64+
5765
/**
66+
*
5867
* @return HtmlListItem
5968
*/
60-
public function getItem($index){
69+
public function getItem($index) {
6170
return parent::getItem($index);
6271
}
6372

64-
protected function getItemToAdd($item){
65-
$itemO=parent::getItemToAdd($item);
66-
if($itemO instanceof AbstractCheckbox)
73+
protected function getItemToAdd($item) {
74+
$itemO = parent::getItemToAdd($item);
75+
if ($itemO instanceof AbstractCheckbox)
6776
$itemO->addClass("item");
6877
return $itemO;
6978
}
@@ -82,33 +91,38 @@ public function setOrdered() {
8291

8392
public function run(JsUtils $js) {
8493
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);
94+
$jsCode = include dirname(__FILE__) . '/../../components/jsTemplates/tplCheckedList.php';
95+
$jsCode = \str_replace("%identifier%", "#" . $this->identifier, $jsCode);
96+
$jsCode = \str_replace("%fireOnInit%", $this->_fireOnInit, $jsCode);
97+
$jsCode = \str_replace("%onChange%", $this->_ckItemChange, $jsCode);
8998
$this->executeOnRun($jsCode);
9099
}
91-
/*if(isset($this->_visibility)){
92-
$this->_visibility->run($js);
93-
}*/
100+
/*
101+
* if(isset($this->_visibility)){
102+
* $this->_visibility->run($js);
103+
* }
104+
*/
94105
return parent::run($js);
95106
}
96-
97-
protected function addFollowPoints(){
98-
$count=$this->count();
99-
for ($i=$this->_maxVisible;$i<$count;$i++){
100-
$this->getItem($i)->addClass("notVisible")->setProperty("style", "display: none;");
107+
108+
protected function addFollowPoints() {
109+
$count = $this->count();
110+
for ($i = $this->_maxVisible; $i < $count; $i ++) {
111+
$this->getItem($i)
112+
->addClass("notVisible")
113+
->setProperty("style", "display: none;");
101114
}
102-
$item=$this->addItem("...");
115+
$item = $this->addItem("...");
103116
$item->addClass("points");
104-
$item->onClick('$(this).hide();$("#'.$this->identifier.' .notVisible").show();');
117+
$item->onClick('$(this).hide();$("#' . $this->identifier . ' .notVisible").show();');
105118
}
106-
107-
public function onCkItemChange($jsCode){
108-
$this->_ckItemChange=$jsCode;
119+
120+
public function onCkItemChange($jsCode) {
121+
$this->_ckItemChange = $jsCode;
109122
}
110123

111124
/**
125+
*
112126
* @param boolean $fireOnInit
113127
*/
114128
public function setFireOnInit($fireOnInit) {
@@ -132,100 +146,105 @@ public function setHorizontal() {
132146
}
133147

134148
/**
135-
* {@inheritDoc}
149+
*
150+
* {@inheritdoc}
136151
* @see \Ajax\common\html\HtmlCollection::compile()
137152
*/
138153
public function compile(JsUtils $js = NULL, &$view = NULL) {
139-
if(isset($this->_maxVisible) && $this->_maxVisible<$this->count()){
154+
if (isset($this->_maxVisible) && $this->_maxVisible < $this->count()) {
140155
$this->addFollowPoints();
141-
if(isset($js)){
142-
$visibility=new Visibility($js);
143-
$visibility->attach("#".$this->identifier);
156+
if (isset($js)) {
157+
$visibility = new Visibility($js);
158+
$visibility->attach("#" . $this->identifier);
144159
$visibility->setOnTopVisible("$(this).children('.notVisible').hide();$(this).find('.points').show();");
145-
$visibility->compile($js,$view);
146-
$this->_visibility=$visibility;
160+
$visibility->compile($js, $view);
161+
$this->_visibility = $visibility;
147162
}
148163
}
149-
return parent::compile ($js,$view);
164+
return parent::compile($js, $view);
150165
}
151166

152167
/**
153168
* Adds a grouped checked box to the list
169+
*
154170
* @param array $items
155171
* @param string|array|null $masterItem
156172
* @param array $values
157173
* @param string $notAllChecked
158174
* @return HtmlList
159175
*/
160-
public function addCheckedList($items=array(), $masterItem=NULL, $values=array(),$notAllChecked=false,$name=null) {
161-
$count=$this->count();
162-
$identifier=$this->identifier . "-" . $count;
176+
public function addCheckedList($items = array(), $masterItem = NULL, $values = array(), $notAllChecked = false, $name = null) {
177+
$count = $this->count();
178+
$identifier = $this->identifier . "-" . $count;
163179
if (isset($masterItem)) {
164-
if(\is_array($masterItem)){
165-
$masterO=new HtmlFormCheckbox("master-" . $identifier, @$masterItem[0],@$masterItem[1]);
166-
if(isset($name))
180+
if (\is_array($masterItem)) {
181+
$masterO = new HtmlFormCheckbox("master-" . $identifier, @$masterItem[0], @$masterItem[1]);
182+
if (isset($name))
167183
$masterO->setName($name);
168-
if(isset($masterItem[1])){
169-
if(\array_search($masterItem[1], $values)!==false){
184+
if (isset($masterItem[1])) {
185+
if (\array_search($masterItem[1], $values) !== false) {
170186
$masterO->getDataField()->setProperty("checked", "");
171187
}
172188
}
173-
}else{
174-
$masterO=new HtmlFormCheckbox("master-" . $identifier, $masterItem);
189+
} else {
190+
$masterO = new HtmlFormCheckbox("master-" . $identifier, $masterItem);
175191
}
176-
if($notAllChecked){
192+
if ($notAllChecked) {
177193
$masterO->getDataField()->addClass("_notAllChecked");
178194
}
179195
$masterO->getHtmlCk()->addToProperty("class", "master");
180196
$masterO->setClass("item");
181197
$this->addItem($masterO);
182198
}
183-
$fields=array ();
184-
$i=0;
185-
foreach ( $items as $val => $caption ) {
186-
$itemO=new HtmlFormCheckbox($identifier . "-" . $i++, $caption, $val, "child");
199+
$fields = array();
200+
$i = 0;
201+
$this->setFireOnInit(UString::getBooleanStr($this->_fireOnInit || count($items) < 30));
202+
foreach ($items as $val => $caption) {
203+
$itemO = new HtmlFormCheckbox($identifier . "-" . $i ++, $caption, $val, "child");
187204
if (\array_search($val, $values) !== false) {
188205
$itemO->getDataField()->setProperty("checked", "");
189206
}
190-
if(isset($name))
207+
if (isset($name))
191208
$itemO->setName($name);
192209
$itemO->setClass("item");
193-
$fields[]=$itemO;
210+
$fields[] = $itemO;
194211
}
195212
if (isset($masterO) === true) {
196-
$list=new HtmlList("", $fields);
213+
$list = new HtmlList("", $fields);
197214
$list->setClass("list");
198215
$masterO->addContent($list);
199216
} else {
200217
$this->addList($fields);
201218
}
202-
$this->_hasCheckedList=true;
219+
$this->_hasCheckedList = true;
203220
return $this;
204221
}
205222

206-
public function setIcons($icons){
207-
if(!\is_array($icons)){
208-
$icons=\array_fill(0, \sizeof($this->content), $icons);
223+
public function setIcons($icons) {
224+
if (! \is_array($icons)) {
225+
$icons = \array_fill(0, \sizeof($this->content), $icons);
209226
}
210-
$max=\min(\sizeof($icons),\sizeof($this->content));
211-
for($i=0;$i<$max;$i++){
227+
$max = \min(\sizeof($icons), \sizeof($this->content));
228+
for ($i = 0; $i < $max; $i ++) {
212229
$this->content[$i]->addIcon($icons[$i]);
213230
}
214231
return $this;
215232
}
233+
216234
/**
235+
*
217236
* @return mixed
218237
*/
219238
public function getMaxVisible() {
220239
return $this->_maxVisible;
221240
}
222241

223242
/**
243+
*
224244
* @param mixed $_maxVisible
225245
*/
226246
public function setMaxVisible($_maxVisible) {
227247
$this->_maxVisible = $_maxVisible;
228248
return $this;
229249
}
230-
231250
}

0 commit comments

Comments
 (0)