Skip to content

Commit 163c98d

Browse files
committed
widget refactoring + fieldAsProgress
1 parent 72aa1af commit 163c98d

File tree

3 files changed

+71
-67
lines changed

3 files changed

+71
-67
lines changed

Ajax/common/Widget.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
use Ajax\semantic\html\elements\HtmlButton;
77
use Ajax\semantic\widgets\datatable\PositionInTable;
88
use Ajax\semantic\html\collections\menus\HtmlMenu;
9+
use Ajax\semantic\widgets\base\FieldAsTrait;
910

1011
abstract class Widget extends HtmlDoubleElement {
12+
use FieldAsTrait;
1113

1214
/**
1315
* @var string classname
@@ -36,6 +38,12 @@ public function __construct($identifier,$model,$modelInstance=NULL) {
3638
$this->show($modelInstance);
3739
}
3840

41+
protected function _getFieldIdentifier($prefix){
42+
return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
43+
}
44+
45+
protected abstract function _setToolbarPosition($table,$captions=NULL);
46+
3947
public function show($modelInstance){
4048
$this->_modelInstance=$modelInstance;
4149
}
@@ -64,6 +72,42 @@ public function setColor($color){
6472
return $this->getHtmlComponent()->setColor($color);
6573
}
6674

75+
76+
public function setCaptions($captions){
77+
$this->_instanceViewer->setCaptions($captions);
78+
return $this;
79+
}
80+
81+
public function setFields($fields){
82+
$this->_instanceViewer->setVisibleProperties($fields);
83+
return $this;
84+
}
85+
86+
public function addField($field){
87+
$this->_instanceViewer->addField($field);
88+
return $this;
89+
}
90+
91+
public function insertField($index,$field){
92+
$this->_instanceViewer->insertField($index, $field);
93+
return $this;
94+
}
95+
96+
public function insertInField($index,$field){
97+
$this->_instanceViewer->insertInField($index, $field);
98+
return $this;
99+
}
100+
101+
public function setValueFunction($index,$callback){
102+
$this->_instanceViewer->setValueFunction($index, $callback);
103+
return $this;
104+
}
105+
106+
public function setIdentifierFunction($callback){
107+
$this->_instanceViewer->setIdentifierFunction($callback);
108+
return $this;
109+
}
110+
67111
/**
68112
* @return \Ajax\semantic\html\collections\menus\HtmlMenu
69113
*/
@@ -78,10 +122,16 @@ public function getToolbar(){
78122
/**
79123
* Adds a new element in toolbar
80124
* @param mixed $element
125+
* @param callable $callback function to call on $element
81126
* @return \Ajax\common\html\HtmlDoubleElement
82127
*/
83-
public function addInToolbar($element){
128+
public function addInToolbar($element,$callback=NULL){
84129
$tb=$this->getToolbar();
130+
if(isset($callback)){
131+
if(\is_callable($callback)){
132+
$callback($element);
133+
}
134+
}
85135
return $tb->addItem($element);
86136
}
87137

@@ -91,9 +141,9 @@ public function addItemInToolbar($caption,$icon=NULL){
91141
return $result;
92142
}
93143

94-
public function addButtonInToolbar($caption){
144+
public function addButtonInToolbar($caption,$callback=NULL){
95145
$bt=new HtmlButton("",$caption);
96-
return $this->addInToolbar($bt);
146+
return $this->addInToolbar($bt,$callback);
97147
}
98148

99149
public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){

Ajax/semantic/widgets/base/FieldAsTrait.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Ajax\semantic\html\elements\HtmlImage;
88
use Ajax\semantic\html\modules\checkbox\HtmlRadio;
99
use Ajax\semantic\html\base\constants\Size;
10-
use Ajax\semantic\widgets\datatable\InstanceViewer;
1110
use Ajax\semantic\html\elements\HtmlLabel;
11+
use Ajax\semantic\html\modules\HtmlProgress;
1212

1313
/**
1414
* @author jc
@@ -25,21 +25,28 @@ private function _getLabelField($caption,$icon=NULL){
2525
return $label;
2626
}
2727

28+
29+
public function fieldAsProgress($index,$label=NULL, $attributes=array()){
30+
$this->setValueFunction($index,function($value) use($label,$attributes){
31+
$pb=new HtmlProgress($this->_getFieldIdentifier("pb"),$value,$label,$attributes);
32+
return $pb;
33+
});
34+
return $this;
35+
}
36+
2837
public function fieldAsLabel($index,$icon=NULL){
2938
$this->setValueFunction($index,function($caption) use($icon){
3039
$lbl=$this->_getLabelField($caption,$icon);
3140
return $lbl;
32-
}
33-
);
41+
});
3442
return $this;
3543
}
3644

3745
public function fieldAsImage($index,$size=Size::SMALL,$circular=false){
3846
$this->setValueFunction($index,function($img) use($size,$circular){
3947
$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
4048
return $image;
41-
}
42-
);
49+
});
4350
return $this;
4451
}
4552

@@ -55,8 +62,7 @@ public function fieldAsRadio($index,$name=NULL){
5562
}
5663
$radio=new HtmlRadio($this->_getFieldIdentifier("radio"),$name,$value,$value);
5764
return $radio;
58-
}
59-
);
65+
});
6066
return $this;
6167
}
6268

@@ -68,8 +74,7 @@ public function fieldAsInput($index,$name=NULL,$type="text",$placeholder=""){
6874
}
6975
$input->getField()->setProperty("name", $name);
7076
return $input;
71-
}
72-
);
77+
});
7378
return $this;
7479
}
7580

@@ -81,8 +86,8 @@ public function fieldAsCheckbox($index,$name=NULL){
8186
$name=$this->_instanceViewer->getCaption($index)."[]";
8287
}
8388
$checkbox->getField()->setProperty("name", $name);
84-
return $checkbox;}
85-
);
89+
return $checkbox;
90+
});
8691
return $this;
8792
}
8893

@@ -93,8 +98,8 @@ public function fieldAsDropDown($index,$elements=[],$multiple=false,$name=NULL){
9398
$name=$this->_instanceViewer->getCaption($index)."[]";
9499
}
95100
$dd->asSelect($name,$multiple);
96-
return $dd;}
97-
);
101+
return $dd;
102+
});
98103
return $this;
99104
}
100105
}

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
use Ajax\semantic\html\elements\HtmlButton;
1212
use Ajax\semantic\html\base\constants\Direction;
1313
use Ajax\service\JArray;
14-
use Ajax\semantic\widgets\base\FieldAsTrait;
1514
use Ajax\semantic\html\base\HtmlSemDoubleElement;
16-
use Ajax\semantic\widgets\base\InstanceViewerCaption;
1715
use Ajax\semantic\widgets\base\InstanceViewer;
1816

1917
/**
@@ -24,7 +22,6 @@
2422
*
2523
*/
2624
class DataTable extends Widget {
27-
use FieldAsTrait;
2825

2926
protected $_searchField;
3027
protected $_urls;
@@ -150,50 +147,6 @@ private function addToolbarRow($part,$table,$captions){
150147
$row->setValues([$this->_toolbar]);
151148
}
152149

153-
public function getInstanceViewer() {
154-
return $this->_instanceViewer;
155-
}
156-
157-
public function setInstanceViewer($_instanceViewer) {
158-
$this->_instanceViewer=$_instanceViewer;
159-
return $this;
160-
}
161-
162-
public function setCaptions($captions){
163-
$this->_instanceViewer->setCaptions($captions);
164-
return $this;
165-
}
166-
167-
public function setFields($fields){
168-
$this->_instanceViewer->setVisibleProperties($fields);
169-
return $this;
170-
}
171-
172-
public function addField($field){
173-
$this->_instanceViewer->addField($field);
174-
return $this;
175-
}
176-
177-
public function insertField($index,$field){
178-
$this->_instanceViewer->insertField($index, $field);
179-
return $this;
180-
}
181-
182-
public function insertInField($index,$field){
183-
$this->_instanceViewer->insertInField($index, $field);
184-
return $this;
185-
}
186-
187-
public function setValueFunction($index,$callback){
188-
$this->_instanceViewer->setValueFunction($index, $callback);
189-
return $this;
190-
}
191-
192-
public function setIdentifierFunction($callback){
193-
$this->_instanceViewer->setIdentifierFunction($callback);
194-
return $this;
195-
}
196-
197150
public function getHtmlComponent(){
198151
return $this->content["table"];
199152
}
@@ -360,10 +313,6 @@ public function setSortable($colIndex=NULL) {
360313
return $this;
361314
}
362315

363-
protected function _getFieldIdentifier($prefix){
364-
return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
365-
}
366-
367316
/**
368317
* The callback function called after the insertion of each row when fromDatabaseObjects is called
369318
* callback function takes the parameters $row : the row inserted and $object: the instance of model used

0 commit comments

Comments
 (0)