Skip to content

Commit aa1d0c7

Browse files
committed
FieldAsHeader+captionCallback
1 parent bda9b8b commit aa1d0c7

File tree

7 files changed

+67
-16
lines changed

7 files changed

+67
-16
lines changed

Ajax/common/Widget.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Ajax\semantic\widgets\datatable\PositionInTable;
88
use Ajax\semantic\html\collections\menus\HtmlMenu;
99
use Ajax\semantic\widgets\base\FieldAsTrait;
10+
use Ajax\semantic\html\elements\HtmlButtonGroups;
1011

1112
abstract class Widget extends HtmlDoubleElement {
1213
use FieldAsTrait;
@@ -114,7 +115,7 @@ public function setIdentifierFunction($callback){
114115
public function getToolbar(){
115116
if(isset($this->_toolbar)===false){
116117
$this->_toolbar=new HtmlMenu("toolbar-".$this->identifier);
117-
$this->_toolbar->setSecondary();
118+
//$this->_toolbar->setSecondary();
118119
}
119120
return $this->_toolbar;
120121
}
@@ -135,8 +136,8 @@ public function addInToolbar($element,$callback=NULL){
135136
return $tb->addItem($element);
136137
}
137138

138-
public function addItemInToolbar($caption,$icon=NULL){
139-
$result=$this->addInToolbar($caption);
139+
public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
140+
$result=$this->addInToolbar($caption,$callback);
140141
$result->addIcon($icon);
141142
return $result;
142143
}
@@ -146,9 +147,25 @@ public function addButtonInToolbar($caption,$callback=NULL){
146147
return $this->addInToolbar($bt,$callback);
147148
}
148149

150+
public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){
151+
$bts=new HtmlButtonGroups("",$captions,$asIcon);
152+
return $this->addInToolbar($bts,$callback);
153+
}
154+
149155
public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
150156
$bt=new HtmlButton("",$caption);
151157
$bt->addIcon($icon,$before,$labeled);
152158
return $this->addInToolbar($bt);
153159
}
160+
161+
/**
162+
* Defines a callback function to call for modifying captions
163+
* function parameters are $captions: the captions to modify and $instance: the active model instance
164+
* @param callable $captionCallback
165+
* @return Widget
166+
*/
167+
public function setCaptionCallback($captionCallback) {
168+
$this->_instanceViewer->setCaptionCallback($captionCallback);
169+
return $this;
170+
}
154171
}

Ajax/semantic/html/collections/HtmlTable.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ public function setSelectable() {
253253
return $this->addToProperty("class", "selectable");
254254
}
255255

256+
public function setStriped() {
257+
return $this->addToProperty("class", "striped");
258+
}
259+
256260
/**
257261
*
258262
* {@inheritDoc}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function addSubmit($identifier,$value,$cssStyle=NULL,$url=NULL,$responseE
4848
return $this->_buttonAsSubmit($bt, "click",$url,$responseElement);
4949
}
5050

51-
protected function _buttonAsSubmit($button,$event,$url,$responseElement=NULL){
51+
protected function _buttonAsSubmit(&$button,$event,$url,$responseElement=NULL){
5252
$form=$this->getForm();
5353
if(isset($url) && isset($responseElement)){
5454
$button->addEvent($event, "$('#".$form->getIdentifier()."').form('validate form');");

Ajax/semantic/html/collections/menus/HtmlMenu.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Ajax\semantic\html\base\traits\AttachedTrait;
1717
use Ajax\semantic\html\content\HtmlMenuItem;
1818
use Ajax\JsUtils;
19+
use Ajax\semantic\html\elements\HtmlButtonGroups;
1920

2021
/**
2122
* Semantic Menu component
@@ -50,7 +51,7 @@ public function setActiveItem($index) {
5051
}
5152

5253
private function getItemToInsert($item) {
53-
if ($item instanceof HtmlInput || $item instanceof HtmlImg || $item instanceof HtmlIcon || $item instanceof HtmlButton || ($item instanceof HtmlDropdown && $this->propertyContains("class", "vertical")===false)) {
54+
if ($item instanceof HtmlInput || $item instanceof HtmlImg || $item instanceof HtmlIcon || $item instanceof HtmlButtonGroups || $item instanceof HtmlButton || ($item instanceof HtmlDropdown && $this->propertyContains("class", "vertical")===false)) {
5455
$itemO=new HtmlMenuItem("item-" . $this->identifier . "-" . \sizeof($this->content) , $item);
5556
$item=$itemO;
5657
}

Ajax/semantic/html/modules/HtmlDropdown.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function addMiniAvatarImageItem($caption,$image){
144144
}
145145

146146
public function addItems($items){
147-
if(JArray::isAssociative($items)){
147+
if(\is_array($items) && JArray::isAssociative($items)){
148148
foreach ($items as $k=>$v){
149149
$this->addItem($v)->setData($k);
150150
}

Ajax/semantic/widgets/base/FieldAsTrait.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Ajax\semantic\html\elements\HtmlLabel;
1111
use Ajax\semantic\html\modules\HtmlProgress;
1212
use Ajax\semantic\html\modules\HtmlRating;
13+
use Ajax\semantic\html\elements\HtmlHeader;
1314
/**
1415
* @author jc
1516
* @property InstanceViewer $_instanceViewer
@@ -66,7 +67,17 @@ public function fieldAsLabel($index,$icon=NULL){
6667
return $this;
6768
}
6869

69-
public function fieldAsImage($index,$size=Size::SMALL,$circular=false){
70+
public function fieldAsHeader($index,$niveau=1,$icon=NULL,$attributes=NULL){
71+
return $this->_fieldAs(function($id,$name,$value) use($niveau,$icon){
72+
$header=new HtmlHeader($id,$niveau,$value);
73+
if(isset($icon))
74+
$header->asIcon($icon, $value);
75+
return $header;
76+
}, $index,$attributes,"header");
77+
}
78+
79+
80+
public function fieldAsImage($index,$size=Size::MINI,$circular=false){
7081
$this->setValueFunction($index,function($img) use($size,$circular){
7182
$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
7283
return $image;

Ajax/semantic/widgets/base/InstanceViewer.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class InstanceViewer {
1111
protected $values;
1212
protected $afterCompile;
1313
protected $captions;
14+
protected $captionCallback;
1415

1516

1617
public static $index=0;
@@ -22,6 +23,7 @@ public function __construct($identifier,$instance=NULL,$captions=NULL){
2223
if(isset($instance))
2324
$this->setInstance($instance);
2425
$this->setCaptions($captions);
26+
$this->captionCallback=NULL;
2527
}
2628

2729
public function getValues(){
@@ -210,17 +212,21 @@ public function getCaption($index){
210212

211213
public function getCaptions(){
212214
if(isset($this->captions)){
213-
$result= $this->captions;
214-
for($i=\sizeof($result);$i<$this->count();$i++){
215-
$result[]="";
215+
$captions= $this->captions;
216+
for($i=\sizeof($captions);$i<$this->count();$i++){
217+
$captions[]="";
218+
}
219+
}else{
220+
$captions=[];
221+
$index=0;
222+
$count=$this->count();
223+
while($index<$count){
224+
$captions[]=$this->getCaption($index++);
216225
}
217-
return $result;
218226
}
219-
$captions=[];
220-
$index=0;
221-
$count=$this->count();
222-
while($index<$count){
223-
$captions[]=$this->getCaption($index++);
227+
if(isset($this->captionCallback) && \is_callable($this->captionCallback)){
228+
$callback=$this->captionCallback;
229+
$callback($captions,$this->instance);
224230
}
225231
return $captions;
226232
}
@@ -248,4 +254,16 @@ public function afterCompile($index,$callback){
248254
$this->afterCompile[$index]=$callback;
249255
return $this;
250256
}
257+
258+
/**
259+
* Defines a callback function to call for modifying captions
260+
* function parameters are $captions: the captions to modify and $instance: the active model instance
261+
* @param callable $captionCallback
262+
* @return \Ajax\semantic\widgets\base\InstanceViewer
263+
*/
264+
public function setCaptionCallback($captionCallback) {
265+
$this->captionCallback=$captionCallback;
266+
return $this;
267+
}
268+
251269
}

0 commit comments

Comments
 (0)