Skip to content

Commit 5c7ff3c

Browse files
committed
JReflection added + DataTable checkboxes update
1 parent 8471858 commit 5c7ff3c

File tree

9 files changed

+69
-10
lines changed

9 files changed

+69
-10
lines changed

Ajax/common/Widget.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Ajax\JsUtils;
1717
use Ajax\semantic\html\collections\form\HtmlFormField;
1818
use Ajax\semantic\html\collections\form\traits\FormTrait;
19+
use Ajax\common\html\BaseWidget;
1920

2021
abstract class Widget extends HtmlDoubleElement {
2122
use FieldAsTrait,FormTrait;
@@ -181,6 +182,11 @@ public function getToolbar(){
181182
*/
182183
public function addInToolbar($element,$callback=NULL){
183184
$tb=$this->getToolbar();
185+
if($element instanceof BaseWidget){
186+
if($element->getIdentifier()===""){
187+
$element->setIdentifier("tb-item-".$this->identifier."-".$tb->count());
188+
}
189+
}
184190
if(isset($callback)){
185191
if(\is_callable($callback)){
186192
$callback($element);

Ajax/common/html/HtmlCollection.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Ajax\common\html\HtmlDoubleElement;
66
use Ajax\service\JArray;
7+
use Ajax\JsUtils;
8+
use Ajax\service\JReflection;
79

810
/**
911
* Base class for Html collections
@@ -47,6 +49,14 @@ protected function getItemToAdd($item){
4749
return $itemO;
4850
}
4951

52+
protected function setItemIdentifier($item,$classname,$index){
53+
if($item instanceof BaseWidget){
54+
if($item->getIdentifier()===""){
55+
$item->setIdentifier($classname."-".$this->identifier."-".$index);
56+
}
57+
}
58+
}
59+
5060
/**
5161
* adds and returns an item
5262
* @param HtmlDoubleElement|string|array $item
@@ -158,4 +168,13 @@ public function setPropertyValues($property,$values){
158168
}
159169
return $this;
160170
}
171+
172+
public function compile(JsUtils $js=NULL, &$view=NULL) {
173+
$index=0;
174+
$classname=\strtolower(JReflection::shortClassName($this));
175+
foreach ($this->content as $item){
176+
$this->setItemIdentifier($item,$classname,$index++);
177+
}
178+
return parent::compile($js,$view);
179+
}
161180
}

Ajax/php/ci/JsUtils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function controller($controller, $name = '', $db_conn = FALSE){
8888
if ( ! file_exists($mod_path.'controllers/'.$path.$controller.'.php')){
8989
continue;
9090
}
91-
if ($db_conn !== FALSE AND ! class_exists('CI_DB')){
91+
if ($db_conn !== FALSE && ! class_exists('CI_DB')){
9292
if ($db_conn === TRUE){
9393
$db_conn = '';
9494
}

Ajax/semantic/html/collections/form/traits/FormTrait.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
use Ajax\semantic\html\collections\HtmlMessage;
66
use Ajax\service\AjaxCall;
77
use Ajax\JsUtils;
8+
use Ajax\semantic\html\elements\HtmlButton;
89

10+
/**
11+
* trait used in Widget and HtmlForm
12+
* @author jc
13+
*
14+
*/
915
trait FormTrait{
1016

1117
/**
@@ -80,10 +86,10 @@ public function addSubmit($identifier,$value,$cssStyle=NULL,$url=NULL,$responseE
8086
return $this->_buttonAsSubmit($bt, "click",$url,$responseElement);
8187
}
8288

83-
protected function _buttonAsSubmit(&$button,$event,$url,$responseElement=NULL,$parameters=NULL){
89+
protected function _buttonAsSubmit(HtmlButton &$button,$event,$url,$responseElement=NULL,$parameters=NULL){
8490
$form=$this->getForm();
8591
if(isset($url) && isset($responseElement)){
86-
$button->addEvent($event, "$('#".$form->getIdentifier()."').form('validate form');");
92+
$button->addEvent($event, "$('#".$form->getIdentifier()."').form('validate form');",true,true);
8793
$params=["form"=>$form->getIdentifier(),"responseElement"=>$responseElement,"url"=>$url,"stopPropagation"=>true];
8894
if(\is_array($parameters))
8995
$params=\array_merge($params,$parameters);

Ajax/semantic/html/elements/HtmlButtonGroups.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct($identifier, $elements=array(), $asIcons=false) {
2121
$this->addElements($elements, $asIcons);
2222
}
2323
protected function createItem($value){
24-
return new HtmlButton("button-" . $this->identifier . "-" . \sizeof($this->content), $value);
24+
return new HtmlButton("", $value);
2525
}
2626

2727

@@ -40,7 +40,7 @@ public function addElements($elements, $asIcons=false) {
4040
}
4141

4242
public function insertOr($aferIndex=0, $or="or") {
43-
$orElement=new HtmlSemDoubleElement("or-" . $this->identifier, "div", "or");
43+
$orElement=new HtmlSemDoubleElement("", "div", "or");
4444
$orElement->setProperty("data-text", $or);
4545
array_splice($this->content, $aferIndex + 1, 0, array ($orElement ));
4646
return $this;

Ajax/semantic/widgets/base/FieldAsTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Ajax\service\JArray;
1919

2020
/**
21+
* trait used in Widget
2122
* @author jc
2223
* @property InstanceViewer $_instanceViewer
2324
* @property boolean $_edition
@@ -29,7 +30,7 @@ abstract protected function _getFieldIdentifier($prefix,$name="");
2930
abstract public function setValueFunction($index,$callback);
3031
abstract protected function _getFieldName($index);
3132
abstract protected function _getFieldCaption($index);
32-
abstract protected function _buttonAsSubmit(&$button,$event,$url,$responseElement=NULL,$parameters=NULL);
33+
abstract protected function _buttonAsSubmit(HtmlButton &$button,$event,$url,$responseElement=NULL,$parameters=NULL);
3334

3435
/**
3536
* @param HtmlFormField $element

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class DataTable extends Widget {
3434
protected $_hasCheckedMessage=false;
3535
protected $_targetSelector;
3636
protected $_checkedMessage;
37+
protected $_checkedClass;
3738

3839
public function __construct($identifier,$model,$modelInstance=NULL) {
3940
parent::__construct($identifier, $model,$modelInstance);
@@ -66,6 +67,9 @@ protected function _runCheckboxes(JsUtils $js){
6667
\$('#checked-count-".$this->identifier."').contents().filter(function() {return this.nodeType == 3;}).each(function(){this.textContent = msg.replace('{count}',count);});
6768
\$('#toolbar-{$this->identifier} .visibleOnChecked').toggle(count>0);}\$('#toolbar-".$this->identifier." .visibleOnChecked').hide();";
6869
$checkedMessageCall="updateChecked();";
70+
if(isset($this->_checkedClass)){
71+
$checkedMessageCall.="$(this).closest('tr').toggleClass('".$this->_checkedClass."',$(this).prop('checked'));";
72+
}
6973
$js->exec($checkedMessageFunction,true);
7074
}
7175
$js->execOn("change", "#".$this->identifier." [name='selection[]']", "
@@ -135,11 +139,20 @@ private function _generateMainCheckbox(&$captions){
135139
$checkedMessageCall="";
136140
if($this->_hasCheckedMessage)
137141
$checkedMessageCall="updateChecked();";
138-
$ck->setOnChecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',true);".$checkedMessageCall);
139-
$ck->setOnUnchecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',false);".$checkedMessageCall);
142+
143+
$ck->setOnChecked($this->_setAllChecked("true").$checkedMessageCall);
144+
$ck->setOnUnchecked($this->_setAllChecked("false").$checkedMessageCall);
140145
\array_unshift($captions, $ck);
141146
}
142147

148+
private function _setAllChecked($checked){
149+
$result="$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',".$checked.");";
150+
if(isset($this->_checkedClass)){
151+
$result.="$('#".$this->identifier." tr').toggleClass('".$this->_checkedClass."',".$checked.");";
152+
}
153+
return $result;
154+
}
155+
143156
protected function _generateContent($table){
144157
$objects=$this->_modelInstance;
145158
if(isset($this->_pagination)){
@@ -344,5 +357,8 @@ public function addCountCheckedInToolbar(array $checkedMessage=null,$callback=nu
344357
$this->addInToolbar($element,$callback);
345358
}
346359

347-
360+
public function setCheckedClass($_checkedClass) {
361+
$this->_checkedClass=$_checkedClass;
362+
return $this;
363+
}
348364
}

Ajax/semantic/widgets/datatable/DataTableFieldAsTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Ajax\semantic\html\base\HtmlSemDoubleElement;
66

77
/**
8+
* trait used in DataTable
89
* @author jc
910
* @property array $_deleteBehavior
1011
* @property array $_editBehavior
@@ -145,7 +146,7 @@ private function getDefaultButton($icon,$class=null,$visibleHover=true){
145146
* @param boolean $visibleHover
146147
* @param array $deleteBehavior
147148
* @param callable $callback
148-
* @return \Ajax\semantic\widgets\datatable\DataTableFieldAsTrait
149+
* @return DataTable
149150
*/
150151
public function addDeleteButton($visibleHover=true,$deleteBehavior=[],$callback=null){
151152
$this->_deleteBehavior=$deleteBehavior;

Ajax/service/JReflection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Ajax\service;
4+
5+
class JReflection {
6+
public static function shortClassName($object){
7+
$classNameWithNamespace = get_class($object);
8+
return substr($classNameWithNamespace, strrpos($classNameWithNamespace, '\\')+1);
9+
}
10+
}

0 commit comments

Comments
 (0)