Skip to content

Commit a4feba4

Browse files
committed
Row table events
1 parent da7bd7b commit a4feba4

File tree

15 files changed

+126
-36
lines changed

15 files changed

+126
-36
lines changed

Ajax/common/Widget.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ protected function _init($instanceViewer,$contentKey,$content,$edition){
7575
* @return int|string
7676
*/
7777
protected function _getIndex($fieldName){
78-
return $fieldName;
78+
$index=$fieldName;
79+
if(\is_string($fieldName)){
80+
$fields=$this->_instanceViewer->getVisibleProperties();
81+
$index=\array_search($fieldName, $fields);
82+
}
83+
return $index;
7984
}
8085

8186
protected function _getFieldIdentifier($prefix,$name=""){

Ajax/common/components/SimpleComponent.php

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

55

66
use Ajax\JsUtils;
7+
use Ajax\service\JString;
8+
use Ajax\bootstrap\components\Splitbutton;
79
/**
810
* Base component for JQuery UI visuals components
911
* @author jc
@@ -22,20 +24,26 @@ public function __construct(JsUtils $js=NULL) {
2224

2325
protected function compileEvents() {
2426
foreach ( $this->events as $event => $jsCode ) {
27+
$itemSelector=JString::getValueBetween($jsCode);
2528
if($event=="execute"){
2629
$this->jquery_code_for_compile []=$jsCode;
2730
}else if($event=="beforeExecute"){
2831
\array_unshift($this->jquery_code_for_compile, $jsCode);
2932
}else{
30-
$selector=$this->attachTo;
31-
if(isset($this->itemSelector)){
32-
$selector.=" ".$this->itemSelector;
33-
}
33+
$selector=$this->_createSelector($itemSelector, $this->attachTo);
3434
$this->jquery_code_for_compile []="$( \"".$selector."\" ).on(\"".$event."\" , function( event, data ) {".$jsCode."});";
3535
}
3636
}
3737
}
3838

39+
protected function _createSelector($itemSelector,$selector){
40+
if(!isset($itemSelector))
41+
$itemSelector=$this->itemSelector;
42+
if(isset($itemSelector) && $itemSelector!=="")
43+
$selector.=" ".$itemSelector;
44+
return $selector;
45+
}
46+
3947
protected function compileJQueryCode() {
4048
$result=implode("\n", $this->jquery_code_for_compile);
4149
$result=str_ireplace("\"%", "", $result);

Ajax/common/html/BaseHtml.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ abstract class BaseHtml extends BaseWidget {
2121
protected $_wrapBefore=array ();
2222
protected $_wrapAfter=array ();
2323
protected $_bsComponent;
24+
protected $_compiled=false;
2425

2526
/**
2627
*
@@ -186,7 +187,13 @@ public function setBsComponent($bsComponent) {
186187
return $this;
187188
}
188189

190+
protected function compile_once(JsUtils $js=NULL, &$view=NULL) {
191+
}
189192
public function compile(JsUtils $js=NULL, &$view=NULL) {
193+
if(!$this->_compiled){
194+
$this->compile_once($js,$view);
195+
$this->_compiled=true;
196+
}
190197
$result=$this->getTemplate($js);
191198
foreach ( $this as $key => $value ) {
192199
if (JString::startswith($key, "_") === false && $key !== "events") {

Ajax/common/html/HtmlSingleElement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public function setTitle($value) {
3838
return $this;
3939
}
4040

41-
/*
42-
* (non-PHPdoc)
43-
* @see \Ajax\bootstrap\html\base\HtmlSingleElement::run()
41+
/**
42+
* {@inheritDoc}
43+
* @see \Ajax\common\html\BaseHtml::run()
4444
*/
4545
public function run(JsUtils $js) {
4646

Ajax/semantic/components/Checkbox.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@ public function setOnChecked($value) {
1919
public function setOnUnchecked($value) {
2020
$this->params["onUnchecked"]="%function(){".$value."}%";
2121
}
22+
23+
public function setOnChange($value){
24+
$this->params["onChange"]="%function(){".$value."}%";
25+
}
2226
//TODO other events implementation
2327
}

Ajax/semantic/html/collections/table/HtmlTable.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class HtmlTable extends HtmlSemDoubleElement {
2323
private $_compileParts;
2424
private $_footer;
2525
private $_afterCompileEvents;
26+
private $_activeClass="warning";
27+
private $_activeRowEvent="click";
2628

2729
public function __construct($identifier, $rowCount, $colCount) {
2830
parent::__construct($identifier, "table", "ui table");
@@ -241,15 +243,20 @@ public function compile(JsUtils $js=NULL, &$view=NULL) {
241243
if(\sizeof($this->_compileParts)<3){
242244
$this->_template="%content%";
243245
$this->refresh();
244-
}else{
245-
if ($this->propertyContains("class", "sortable")) {
246-
$this->addEvent("execute", "$('#" . $this->identifier . "').tablesort();");
247-
}
248246
}
249247
$this->content=JArray::sortAssociative($this->content, $this->_compileParts);
250248
return parent::compile($js, $view);
251249
}
252250

251+
protected function compile_once(JsUtils $js=NULL, &$view=NULL) {
252+
if ($this->propertyContains("class", "sortable")) {
253+
$this->addEvent("execute", "$('#" . $this->identifier . "').tablesort().data('tablesort').sort($('th.default-sort'));");
254+
}
255+
if(isset($this->_activeClass)){
256+
$this->onRow($this->_activeRowEvent, "$(this).toggleClass('".$this->_activeClass."');");
257+
}
258+
}
259+
253260
/**
254261
*
255262
* {@inheritDoc}
@@ -301,4 +308,16 @@ public function onNewRow($callback) {
301308
$this->_afterCompileEvents["onNewRow"]=$callback;
302309
return $this;
303310
}
311+
312+
public function setActiveClass($_activeClass) {
313+
$this->_activeClass=$_activeClass;
314+
return $this;
315+
}
316+
317+
public function setActiveRowEvent($_activeRowEvent) {
318+
$this->_activeRowEvent=$_activeRowEvent;
319+
return $this;
320+
}
321+
322+
304323
}

Ajax/semantic/html/collections/table/traits/TableTrait.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ trait TableTrait{
66
* @return HtmlTable
77
*/
88
abstract protected function getTable();
9+
abstract public function addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false);
10+
abstract public function getOn($event, $url, $responseElement="", $parameters=array());
911

1012
protected function addToPropertyTable($property,$value){
1113
return $this->getTable()->addToProperty($property, $value);
@@ -64,4 +66,17 @@ public function setSelectable() {
6466
public function setStriped() {
6567
return $this->addToPropertyTable("class", "striped");
6668
}
69+
70+
public function onRowClick($jsCode, $stopPropagation=false, $preventDefault=false){
71+
$this->onRowClick($jsCode,$stopPropagation,$preventDefault);
72+
}
73+
74+
public function onRow($event,$jsCode, $stopPropagation=false, $preventDefault=false){
75+
$this->getTable()->addEvent($event,"{{tr}}".$jsCode,$stopPropagation,$preventDefault);
76+
}
77+
78+
public function getOnRow($event, $url, $responseElement="", $parameters=array()){
79+
$parameters=\array_merge($parameters,["eventItemSelector"=>"tbody tr","stopPropagation"=>false,"preventDefault"=>false]);
80+
return $this->getTable()->getOn($event, $url,$responseElement,$parameters);
81+
}
6782
}

Ajax/semantic/html/content/table/HtmlTableContent.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct($identifier, $tagName="tbody", $rowCount=NULL, $colC
3030
*
3131
* @param int $rowCount
3232
* @param int $colCount
33-
* @return \Ajax\semantic\html\content\table\HtmlTableContent
33+
* @return HtmlTableContent
3434
*/
3535
public function setRowCount($rowCount, $colCount) {
3636
$count=$this->count();
@@ -86,7 +86,7 @@ public function _addRow($row) {
8686
* Returns the cell (HtmlTD) at position $row,$col
8787
* @param int $row
8888
* @param int $col
89-
* @return \Ajax\semantic\html\content\HtmlTD
89+
* @return HtmlTD
9090
*/
9191
public function getCell($row, $col) {
9292
$row=$this->getItem($row);
@@ -99,7 +99,7 @@ public function getCell($row, $col) {
9999
/**
100100
*
101101
* @param int $index
102-
* @return \Ajax\semantic\html\content\HtmlTR
102+
* @return HtmlTR
103103
*/
104104
public function getRow($index) {
105105
return $this->getItem($index);
@@ -110,7 +110,7 @@ public function getRow($index) {
110110
* @param int $row
111111
* @param int $col
112112
* @param mixed $value
113-
* @return \Ajax\semantic\html\content\table\HtmlTableContent
113+
* @return HtmlTableContent
114114
*/
115115
public function setCellValue($row, $col, $value="") {
116116
$cell=$this->getCell($row, $col);
@@ -234,7 +234,7 @@ public function getColCount() {
234234
* Removes the cell at position $rowIndex,$colIndex
235235
* @param int $rowIndex
236236
* @param int $colIndex
237-
* @return \Ajax\semantic\html\content\table\HtmlTableContent
237+
* @return HtmlTableContent
238238
*/
239239
public function delete($rowIndex, $colIndex=NULL) {
240240
if (isset($colIndex)) {
@@ -261,13 +261,14 @@ public function setFullWidth() {
261261
}
262262

263263
public function sort($colIndex) {
264-
$this->content[0]->getItem($colIndex)->addToProperty("class", "sorted ascending");
264+
$this->content[0]->getItem($colIndex)->addToProperty("class", "default-sort");
265+
return $this;
265266
}
266267

267268
/**
268269
* @param mixed $callback
269270
* @param string $format
270-
* @return \Ajax\semantic\html\content\table\HtmlTableContent
271+
* @return HtmlTableContent
271272
*/
272273
public function conditionalCellFormat($callback, $format) {
273274
$rows=$this->content;
@@ -280,7 +281,7 @@ public function conditionalCellFormat($callback, $format) {
280281
/**
281282
* @param mixed $callback
282283
* @param string $format
283-
* @return \Ajax\semantic\html\content\table\HtmlTableContent
284+
* @return HtmlTableContent
284285
*/
285286
public function conditionalRowFormat($callback, $format) {
286287
$rows=$this->content;
@@ -292,7 +293,7 @@ public function conditionalRowFormat($callback, $format) {
292293

293294
/**
294295
* @param mixed $callback
295-
* @return \Ajax\semantic\html\content\table\HtmlTableContent
296+
* @return HtmlTableContent
296297
*/
297298
public function applyCells($callback) {
298299
$rows=$this->content;
@@ -304,7 +305,7 @@ public function applyCells($callback) {
304305

305306
/**
306307
* @param mixed $callback
307-
* @return \Ajax\semantic\html\content\table\HtmlTableContent
308+
* @return HtmlTableContent
308309
*/
309310
public function applyRows($callback) {
310311
$rows=$this->content;

Ajax/semantic/html/modules/checkbox/AbstractCheckbox.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Ajax\JsUtils;
88

99
abstract class AbstractCheckbox extends HtmlSemDoubleElement {
10-
protected $_params=array ();
10+
protected $_params=[];
1111

1212
public function __construct($identifier, $name=NULL, $label=NULL, $value=NULL, $inputType="checkbox", $type="checkbox") {
1313
parent::__construct("ck-".$identifier, "div", "ui ".$type);
@@ -113,10 +113,17 @@ public function setFitted() {
113113

114114
public function setOnChecked($jsCode){
115115
$this->_params["onChecked"]=$jsCode;
116+
return $this;
116117
}
117118

118119
public function setOnUnchecked($jsCode){
119120
$this->_params["onUnchecked"]=$jsCode;
121+
return $this;
122+
}
123+
124+
public function setOnChange($jsCode){
125+
$this->_params["onChange"]=$jsCode;
126+
return $this;
120127
}
121128

122129
public function run(JsUtils $js) {

Ajax/semantic/widgets/base/FieldAsTrait.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ public function fieldAsAvatar($index,$attributes=NULL){
156156
}
157157

158158
public function fieldAsRadio($index,$attributes=NULL){
159-
return $this->_fieldAs(function($id,$name,$value){
159+
return $this->_fieldAs(function($id,$name,$value) use($attributes){
160160
$input= new HtmlRadio($id,$name,$value,$value);
161-
return $input;
161+
return $this->_prepareFormFields($input, $name, $attributes);
162162
}, $index,$attributes,"radio");
163163
}
164164

@@ -176,10 +176,9 @@ public function fieldAsInput($index,$attributes=NULL){
176176
}
177177

178178
public function fieldAsTextarea($index,$attributes=NULL){
179-
return $this->_fieldAs(function($id,$name,$value,$caption){
179+
return $this->_fieldAs(function($id,$name,$value,$caption) use ($attributes){
180180
$textarea=new HtmlFormTextarea($id,$caption,$value);
181-
$textarea->setName($name);
182-
return $textarea;
181+
return $this->_prepareFormFields($textarea, $name, $attributes);
183182
}, $index,$attributes,"textarea");
184183
}
185184

@@ -192,19 +191,18 @@ public function fieldAsHidden($index,$attributes=NULL){
192191
}
193192

194193
public function fieldAsCheckbox($index,$attributes=NULL){
195-
return $this->_fieldAs(function($id,$name,$value,$caption){
194+
return $this->_fieldAs(function($id,$name,$value,$caption) use($attributes){
196195
$input=new HtmlFormCheckbox($id,$caption,$this->_instanceViewer->getIdentifier());
197196
$input->setChecked(JString::isBooleanTrue($value));
198-
$input->setName($name);
199-
return $input;
197+
return $this->_prepareFormFields($input, $name, $attributes);
200198
}, $index,$attributes,"ck");
201199
}
202200

203201
public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
204-
return $this->_fieldAs(function($id,$name,$value,$caption) use($elements,$multiple){
202+
return $this->_fieldAs(function($id,$name,$value,$caption) use($elements,$multiple,$attributes){
205203
$dd=new HtmlFormDropdown($id,$elements,$caption,$value);
206204
$dd->asSelect($name,$multiple);
207-
return $dd;
205+
return $this->_prepareFormFields($dd, $name, $attributes);
208206
}, $index,$attributes,"dd");
209207
}
210208

Ajax/semantic/widgets/base/InstanceViewer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,9 @@ public function setDefaultValueFunction($defaultValueFunction) {
330330
$this->defaultValueFunction=$defaultValueFunction;
331331
return $this;
332332
}
333+
334+
public function getVisibleProperties() {
335+
return $this->visibleProperties;
336+
}
337+
333338
}

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,20 @@ protected function _generateRow($instance,&$table,$checkedClass=null){
152152
$this->_instanceViewer->setInstance($instance);
153153
InstanceViewer::$index++;
154154
$values= $this->_instanceViewer->getValues();
155+
$id=$this->_instanceViewer->getIdentifier();
155156
if($this->_hasCheckboxes){
156-
$ck=new HtmlCheckbox("ck-".$this->identifier,"");
157+
$ck=new HtmlCheckbox("ck-".$this->identifier."-".$id,"");
158+
$ck->setOnChange("event.stopPropagation();");
157159
$field=$ck->getField();
158-
$field->setProperty("value",$this->_instanceViewer->getIdentifier());
160+
$field->setProperty("value",$id);
159161
$field->setProperty("name", "selection[]");
160162
if(isset($checkedClass))
161163
$field->setClass($checkedClass);
162164
\array_unshift($values, $ck);
163165
}
164166
$result=$table->newRow();
165-
$result->setIdentifier($this->identifier."-tr-".$this->_instanceViewer->getIdentifier());
167+
$result->setIdentifier($this->identifier."-tr-".$id);
168+
$result->setProperty("data-ajax",$id);
166169
$result->setValues($values);
167170
$result->addToProperty("class",$this->_rowClass);
168171
return $result;

Ajax/semantic/widgets/datatable/JsonDataTable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
use Ajax\JsUtils;
1010
use Ajax\semantic\html\collections\menus\HtmlMenu;
1111

12+
/**
13+
* @author jc
14+
* a DataTable refreshed with JSON
15+
* @since 2.2.2
16+
*/
1217
class JsonDataTable extends DataTable {
1318
protected $_modelClass="_jsonArrayModel";
1419

0 commit comments

Comments
 (0)