Skip to content

Commit 62c434c

Browse files
committed
Items refactoring
1 parent b1fd353 commit 62c434c

File tree

7 files changed

+77
-46
lines changed

7 files changed

+77
-46
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace Ajax\semantic\html\content\view;
3+
use Ajax\semantic\html\base\HtmlSemDoubleElement;
4+
5+
/**
6+
* @author jc
7+
* @property mixed $content
8+
*/
9+
trait ContentPartTrait{
10+
public function addElementInPart($element,$partKey,$before=false,$force=false){
11+
$part=$this->getPart($partKey,null,$force);
12+
if($part instanceof HtmlSemDoubleElement){
13+
$this->content[$partKey]=$part;
14+
$part->addContent($element,$before);
15+
}
16+
return $this;
17+
}
18+
19+
public function getPart($partKey, $index=NULL,$force=false) {
20+
if (\array_key_exists($partKey, $this->content)) {
21+
if (isset($index))
22+
return $this->content[$partKey][$index];
23+
return $this->content[$partKey];
24+
}
25+
if($force){
26+
return new HtmlSemDoubleElement($partKey."-".$this->identifier,"div",$partKey);
27+
}
28+
return NULL;
29+
}
30+
}

Ajax/semantic/html/content/view/HtmlItem.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace Ajax\semantic\html\content\view;
44

55

6+
/** Semantic html item use in Items component
7+
* @author jc
8+
* @since 2.2.2
9+
*/
610
class HtmlItem extends HtmlViewItem {
711

812
public function __construct($identifier) {

Ajax/semantic/html/content/view/HtmlViewContent.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
use Ajax\semantic\html\elements\HtmlIcon;
88
use Ajax\semantic\html\elements\html5\HtmlImg;
99
use Ajax\JsUtils;
10-
use Ajax\service\JReflection;
1110
use Ajax\service\JArray;
11+
use Ajax\semantic\html\elements\HtmlButtonGroups;
1212

1313
class HtmlViewContent extends HtmlSemDoubleElement {
14-
14+
use ContentPartTrait;
1515
public function __construct($identifier, $content=array()) {
1616
parent::__construct($identifier, "div", "content",[]);
1717
$this->setContent($content);
@@ -32,7 +32,7 @@ public function setContent($value){
3232
$this->addContent($value);
3333
}
3434

35-
public function addElement($content, $baseClass="") {
35+
public function addElement($content, $baseClass="",$part=NULL) {
3636
$count=\sizeof($this->content);
3737
$result=new HtmlViewContent("element-" . $count . "-" . $this->identifier, $content);
3838
$result->setClass($baseClass);
@@ -68,6 +68,19 @@ public function addImage($src="", $alt="", $size=NULL) {
6868
return $image;
6969
}
7070

71+
/**
72+
* @param array $elements
73+
* @param boolean $asIcons
74+
* @param string $part
75+
* @param boolean $before
76+
* @return HtmlButtonGroups
77+
*/
78+
public function addContentButtons($elements=array(), $asIcons=false,$part="extra",$before=false){
79+
$buttons=new HtmlButtonGroups("buttons-".$this->identifier,$elements,$asIcons);
80+
$this->addElementInPart($buttons,$part, $before,true);
81+
return $buttons;
82+
}
83+
7184
public function addMetas($metas) {
7285
if (\is_array($metas)) {
7386
foreach ( $metas as $meta ) {
@@ -126,18 +139,6 @@ public function addHeaderContent($header, $metas=array(), $description=NULL,$ext
126139
return $this;
127140
}
128141

129-
public function getPart($part, $index=NULL) {
130-
if($this->content instanceof HtmlViewContent){
131-
return $this->content->getPart($part,$index);
132-
}
133-
if (\array_key_exists($part, $this->content)) {
134-
if (isset($index))
135-
return $this->content[$part][$index];
136-
return $this->content[$part];
137-
}
138-
return NULL;
139-
}
140-
141142
public function compile(JsUtils $js=NULL, &$view=NULL) {
142143
//$this->content=JArray::sortAssociative($this->content, [ "header","meta","description","extra" ]);
143144
return parent::compile($js, $view);

Ajax/semantic/html/content/view/HtmlViewItem.php

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
use Ajax\semantic\html\base\constants\RevealType;
1313
use Ajax\semantic\html\elements\HtmlButtonGroups;
1414
use Ajax\semantic\html\content\view\HtmlViewContent;
15-
use Ajax\service\JReflection;
1615

1716
abstract class HtmlViewItem extends HtmlSemDoubleElement {
18-
19-
protected $_sortContentBy=[];
17+
use ContentPartTrait;
2018

2119
public function __construct($identifier,$baseClass,$content=NULL) {
2220
parent::__construct($identifier, "div", $baseClass);
@@ -47,9 +45,9 @@ private function createContent($content, $baseClass="content") {
4745
return $result;
4846
}
4947

50-
private function addElementInContent($key, $element) {
48+
private function addElementIn($key, $element) {
5149
if (\array_key_exists($key, $this->content) === false) {
52-
$this->content[$key]=array ();
50+
$this->content[$key]=[];
5351
}
5452
if($this->content[$key] instanceof HtmlViewContent)
5553
$this->content[$key]->addElement($element);
@@ -58,23 +56,12 @@ private function addElementInContent($key, $element) {
5856
return $element;
5957
}
6058

61-
private function getPart($part, $index=NULL) {
62-
if($this->content instanceof HtmlViewContent){
63-
return $this->content->getPart($part,$index);
64-
}
65-
if (\array_key_exists($part, $this->content)) {
66-
if (isset($index))
67-
return $this->content[$part][$index];
68-
return $this->content[$part];
69-
}
70-
return NULL;
71-
}
7259

7360
public function addHeader($header, $niveau=4, $type="page") {
7461
if (!$header instanceof HtmlHeader) {
7562
$header=new HtmlHeader("header-" . $this->identifier, $niveau, $header, $type);
7663
}
77-
return $this->addElementInContent("header",$this->createContent($header));
64+
return $this->addElementIn("header",$this->createContent($header));
7865
}
7966

8067
public function addImage($image, $title="") {
@@ -85,12 +72,12 @@ public function addImage($image, $title="") {
8572
return $this->content["image"]= $image;
8673
}
8774

88-
public function addReveal($visibleContent, $hiddenContent=NULL, $type=RevealType::FADE, $attributeType=NULL) {
75+
public function addReveal($visibleContent, $hiddenContent=NULL, $type=RevealType::FADE, $attributeType=NULL,$key="extra-content") {
8976
$reveal=$visibleContent;
9077
if (!$visibleContent instanceof HtmlReveal) {
9178
$reveal=new HtmlReveal("reveral-" . $this->identifier, $visibleContent, $hiddenContent, $type, $attributeType);
9279
}
93-
return $this->content["image"]= $reveal;
80+
return $this->content[$key]= $reveal;
9481
}
9582

9683
public function addRevealImage($visibleContent, $hiddenContent=NULL, $type=RevealType::FADE, $attributeType=NULL) {
@@ -105,23 +92,23 @@ public function addExtraContent($content=array()) {
10592
return $this->content["extra-content"]= $this->createContent($content, "extra content");
10693
}
10794

108-
public function addContent($content=array(), $before=false) {
95+
/*public function addContent($content=array(), $before=false) {
10996
if (!$content instanceof HtmlViewContent) {
11097
$content=$this->createContent($content);
11198
}
11299
$this->content["content"]->addElement($content);
113100
return $content;
114-
}
101+
}*/
115102

116103
/**
117104
* @param array $elements
118105
* @param boolean $asIcons
119-
* @return \Ajax\semantic\html\elements\HtmlButtonGroups
106+
* @param string $part
107+
* @param boolean $before
108+
* @return HtmlButtonGroups
120109
*/
121-
public function addButtons($elements=array(), $asIcons=false,$key="extra-content"){
122-
$buttons=new HtmlButtonGroups("buttons-".$this->identifier,$elements,$asIcons);
123-
$this->addElementInContent($key, $buttons);
124-
return $buttons;
110+
public function addContentButtons($elements=array(), $asIcons=false,$part="extra",$before=false){
111+
return $this->content["content"]->addContentButtons($elements,$asIcons,$part, $before);
125112
}
126113

127114

@@ -132,11 +119,11 @@ public function addItemHeaderContent($header, $metas=array(), $description=NULL,
132119

133120
public function addItemContent($content=array()) {
134121
$count=\sizeof($this->content);
135-
return $this->addElementInContent("content", new HtmlViewContent("content-" . $count . "-" . $this->identifier, $content));
122+
return $this->addElementIn("content", new HtmlViewContent("content-" . $count . "-" . $this->identifier, $content));
136123
}
137124

138-
public function getItemContent($index=NULL) {
139-
return $this->content["content"]->getPart($index);
125+
public function getItemContent() {
126+
return $this->getPart("content",null,true);
140127
}
141128

142129
public function getItemExtraContent() {

Ajax/semantic/html/views/HtmlCardGroups.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
use Ajax\semantic\html\content\view\HtmlViewGroups;
77

8+
/**
9+
* Semantic html Cards group
10+
* @author jc
11+
*/
812
class HtmlCardGroups extends HtmlViewGroups {
913

1014
public function __construct($identifier, $cards=array()) {

Ajax/semantic/html/views/HtmlItems.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
use Ajax\semantic\html\content\view\HtmlViewGroups;
77
use Ajax\semantic\html\content\view\HtmlItem;
88

9+
/**
10+
* Semantic html Items component
11+
* @author jc
12+
* @since 2.2.2
13+
*/
914
class HtmlItems extends HtmlViewGroups {
1015

1116
public function __construct($identifier, $items=[]) {

Ajax/service/JReflection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public static function getterName($propertyName,$prefix="get"){
3939

4040
public static function callMethodFromAssociativeArray($object,$array,$methodPrefix="add"){
4141
foreach ($array as $key=>$value){
42-
if(\method_exists($object, "add".\ucfirst($key))){
43-
\call_user_func([$object,"add".\ucfirst($key)],$value);
42+
if(\method_exists($object, $methodPrefix.\ucfirst($key))){
43+
\call_user_func([$object,$methodPrefix.\ucfirst($key)],$value);
4444
}
4545
}
4646
}

0 commit comments

Comments
 (0)