Skip to content

Commit 7030227

Browse files
committed
Semantic class refactoring
1 parent 19a73f1 commit 7030227

File tree

6 files changed

+123
-95
lines changed

6 files changed

+123
-95
lines changed

Ajax/Semantic.php

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,8 @@
33
namespace Ajax;
44

55
use Ajax\common\BaseGui;
6-
use Ajax\semantic\html\modules\HtmlDropdown;
7-
use Ajax\semantic\html\modules\HtmlPopup;
8-
use Ajax\common\html\BaseHtml;
9-
use Ajax\semantic\html\collections\menus\HtmlIconMenu;
10-
use Ajax\semantic\html\collections\menus\HtmlLabeledIconMenu;
11-
use Ajax\semantic\html\collections\HtmlBreadcrumb;
12-
use Ajax\semantic\html\modules\HtmlAccordion;
13-
use Ajax\semantic\components\Accordion;
14-
use Ajax\semantic\html\collections\menus\HtmlAccordionMenu;
156
use Ajax\semantic\traits\SemanticComponentsTrait;
167
use Ajax\semantic\traits\SemanticHtmlElementsTrait;
17-
use Ajax\semantic\html\modules\HtmlSticky;
188
use Ajax\semantic\traits\SemanticHtmlCollectionsTrait;
199
use Ajax\semantic\traits\SemanticHtmlModulesTrait;
2010
use Ajax\semantic\traits\SemanticHtmlViewsTrait;
@@ -31,85 +21,6 @@ public function __construct($autoCompile=true) {
3121
}
3222

3323

34-
/**
35-
* Adds an icon menu
36-
* @param string $identifier
37-
* @param array $items icons
38-
*/
39-
public function htmlIconMenu($identifier, $items=array()) {
40-
return $this->addHtmlComponent(new HtmlIconMenu($identifier, $items));
41-
}
42-
43-
/**
44-
* Adds an labeled icon menu
45-
* @param string $identifier
46-
* @param array $items icons
47-
*/
48-
public function htmlLabeledIconMenu($identifier, $items=array()) {
49-
return $this->addHtmlComponent(new HtmlLabeledIconMenu($identifier, $items));
50-
}
51-
52-
/**
53-
*
54-
* @param string $identifier
55-
* @param string $value
56-
* @param array $items
57-
*/
58-
public function htmlDropdown($identifier, $value="", $items=array()) {
59-
return $this->addHtmlComponent(new HtmlDropdown($identifier, $value, $items));
60-
}
61-
62-
/**
63-
*
64-
* @param string $identifier
65-
* @param mixed $content
66-
* @return HtmlPopup
67-
*/
68-
public function htmlPopup(BaseHtml $container, $identifier, $content) {
69-
return $this->addHtmlComponent(new HtmlPopup($container, $identifier, $content));
70-
}
71-
72-
/**
73-
* Returns a new Semantic Html Breadcrumb
74-
* @param string $identifier
75-
* @param array $items
76-
* @param boolean $autoActive sets the last element's class to <b>active</b> if true. default : true
77-
* @param function $hrefFunction the function who generates the href elements. default : function($e){return $e->getContent()}
78-
* @return HtmlBreadcrumb
79-
*/
80-
public function htmlBreadcrumb($identifier, $items=array(), $autoActive=true, $startIndex=0, $hrefFunction=NULL) {
81-
return $this->addHtmlComponent(new HtmlBreadcrumb($identifier, $items, $autoActive, $startIndex, $hrefFunction));
82-
}
83-
84-
/**
85-
* Returns a new Semantic Accordion
86-
* @param string $identifier
87-
* @return HtmlAccordion
88-
*/
89-
public function htmlAccordion($identifier) {
90-
return $this->addHtmlComponent(new HtmlAccordion($identifier));
91-
}
92-
93-
/**
94-
* Return a new Semantic Menu Accordion
95-
* @param string $identifier
96-
* @return HtmlAccordion
97-
*/
98-
public function htmlAccordionMenu($identifier, $items=array()) {
99-
return $this->addHtmlComponent(new HtmlAccordionMenu($identifier, $items));
100-
}
101-
102-
103-
/**
104-
* Returns a new Semantic Sticky
105-
* @param string $identifier
106-
* @param array $content
107-
* @return HtmlSticky
108-
*/
109-
public function htmlSticky($identifier, $content=array()) {
110-
return $this->addHtmlComponent(new HtmlSticky($identifier, $content));
111-
}
112-
11324
public function setLanguage($language){
11425
if($language!==$this->language){
11526
$file=\realpath(dirname(__FILE__)."/semantic/components/validation/languages/".$language.".js");

Ajax/semantic/html/collections/form/HtmlFormDropdown.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
class HtmlFormDropdown extends HtmlFormField {
99

10-
public function __construct($identifier,$items=array(), $label=NULL,$value=NULL,$multiple=false) {
11-
parent::__construct("field-".$identifier, (new HtmlDropdown("dropdown-".$identifier,$value,$items))->asSelect($identifier,$multiple), $label);
10+
public function __construct($identifier,$items=array(), $label=NULL,$value=NULL,$multiple=false,$associative=true) {
11+
parent::__construct("field-".$identifier, (new HtmlDropdown("dropdown-".$identifier,$value,$items,$associative))->asSelect($identifier,$multiple), $label);
1212
}
1313

1414
public function setItems($items){
@@ -17,8 +17,8 @@ public function setItems($items){
1717
public function addItem($item,$value=NULL,$image=NULL){
1818
return $this->getField()->addItem($item,$value,$image);
1919
}
20-
public static function multipleDropdown($identifier,$items=array(), $label=NULL,$value=NULL){
21-
return new HtmlFormDropdown($identifier,$items,$label,$value,true);
20+
public static function multipleDropdown($identifier,$items=array(), $label=NULL,$value=NULL,$associative=true){
21+
return new HtmlFormDropdown($identifier,$items,$label,$value,true,$associative);
2222
}
2323

2424
public function getDataField(){

Ajax/semantic/html/modules/HtmlDropdown.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ class HtmlDropdown extends HtmlSemDoubleElement {
2222
protected $_params=array("action"=>"nothing","on"=>"hover");
2323
protected $input;
2424
protected $value;
25+
protected $_associative;
2526

26-
public function __construct($identifier, $value="", $items=array()) {
27+
public function __construct($identifier, $value="", $items=array(),$associative=true) {
2728
parent::__construct($identifier, "div");
2829
$this->_template=include dirname(__FILE__).'/../templates/tplDropdown.php';
2930
$this->setProperty("class", "ui dropdown");
@@ -33,6 +34,7 @@ public function __construct($identifier, $value="", $items=array()) {
3334
$content->wrap("",new HtmlIcon("", "dropdown"));
3435
$this->content=array($content);
3536
$this->tagName="div";
37+
$this->_associative=$associative;
3638
$this->addItems($items);
3739
}
3840

@@ -144,7 +146,7 @@ public function addMiniAvatarImageItem($caption,$image){
144146
}
145147

146148
public function addItems($items){
147-
if(\is_array($items) && JArray::isAssociative($items)){
149+
if(\is_array($items) && $this->_associative){
148150
foreach ($items as $k=>$v){
149151
$this->addItem($v)->setData($k);
150152
}

Ajax/semantic/traits/SemanticHtmlCollectionsTrait.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use Ajax\semantic\html\collections\menus\HtmlMenu;
77
use Ajax\semantic\html\collections\form\HtmlForm;
88
use Ajax\semantic\html\collections\HtmlGrid;
9+
use Ajax\semantic\html\collections\HtmlBreadcrumb;
10+
use Ajax\semantic\html\collections\menus\HtmlIconMenu;
11+
use Ajax\semantic\html\collections\menus\HtmlLabeledIconMenu;
912

1013

1114
trait SemanticHtmlCollectionsTrait {
@@ -46,6 +49,36 @@ public function htmlMenu($identifier, $items=array()) {
4649
return $this->addHtmlComponent(new HtmlMenu($identifier, $items));
4750
}
4851

52+
/**
53+
* Adds an icon menu
54+
* @param string $identifier
55+
* @param array $items icons
56+
*/
57+
public function htmlIconMenu($identifier, $items=array()) {
58+
return $this->addHtmlComponent(new HtmlIconMenu($identifier, $items));
59+
}
60+
61+
/**
62+
* Adds an labeled icon menu
63+
* @param string $identifier
64+
* @param array $items icons
65+
*/
66+
public function htmlLabeledIconMenu($identifier, $items=array()) {
67+
return $this->addHtmlComponent(new HtmlLabeledIconMenu($identifier, $items));
68+
}
69+
70+
/**
71+
* Returns a new Semantic Html Breadcrumb
72+
* @param string $identifier
73+
* @param array $items
74+
* @param boolean $autoActive sets the last element's class to <b>active</b> if true. default : true
75+
* @param function $hrefFunction the function who generates the href elements. default : function($e){return $e->getContent()}
76+
* @return HtmlBreadcrumb
77+
*/
78+
public function htmlBreadcrumb($identifier, $items=array(), $autoActive=true, $startIndex=0, $hrefFunction=NULL) {
79+
return $this->addHtmlComponent(new HtmlBreadcrumb($identifier, $items, $autoActive, $startIndex, $hrefFunction));
80+
}
81+
4982

5083
/**
5184
* Returns a new Semantic Form

Ajax/semantic/traits/SemanticHtmlModulesTrait.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
use Ajax\semantic\html\modules\checkbox\HtmlCheckbox;
1212
use Ajax\semantic\html\modules\HtmlTab;
1313
use Ajax\semantic\html\modules\HtmlShape;
14+
use Ajax\semantic\html\modules\HtmlPopup;
15+
use Ajax\semantic\html\modules\HtmlDropdown;
16+
use Ajax\common\html\BaseHtml;
17+
use Ajax\semantic\html\modules\HtmlAccordion;
18+
use Ajax\semantic\html\collections\menus\HtmlAccordionMenu;
19+
use Ajax\semantic\html\modules\HtmlSticky;
1420

1521
trait SemanticHtmlModulesTrait {
1622

@@ -102,4 +108,55 @@ public function htmlTab($identifier, $tabs=array()) {
102108
public function htmlShape($identifier, $slides=array()) {
103109
return $this->addHtmlComponent(new HtmlShape($identifier, $slides));
104110
}
111+
112+
/**
113+
*
114+
* @param string $identifier
115+
* @param string $value
116+
* @param array $items
117+
* @param boolean $associative
118+
* @return HtmlDropdown
119+
*/
120+
public function htmlDropdown($identifier, $value="", $items=array(),$associative=true) {
121+
return $this->addHtmlComponent(new HtmlDropdown($identifier, $value, $items,$associative));
122+
}
123+
124+
/**
125+
*
126+
* @param string $identifier
127+
* @param mixed $content
128+
* @return HtmlPopup
129+
*/
130+
public function htmlPopup(BaseHtml $container, $identifier, $content) {
131+
return $this->addHtmlComponent(new HtmlPopup($container, $identifier, $content));
132+
}
133+
134+
/**
135+
* Returns a new Semantic Accordion
136+
* @param string $identifier
137+
* @return HtmlAccordion
138+
*/
139+
public function htmlAccordion($identifier) {
140+
return $this->addHtmlComponent(new HtmlAccordion($identifier));
141+
}
142+
143+
/**
144+
* Return a new Semantic Menu Accordion
145+
* @param string $identifier
146+
* @return HtmlAccordionMenu
147+
*/
148+
public function htmlAccordionMenu($identifier, $items=array()) {
149+
return $this->addHtmlComponent(new HtmlAccordionMenu($identifier, $items));
150+
}
151+
152+
153+
/**
154+
* Returns a new Semantic Sticky
155+
* @param string $identifier
156+
* @param array $content
157+
* @return HtmlSticky
158+
*/
159+
public function htmlSticky($identifier, $content=array()) {
160+
return $this->addHtmlComponent(new HtmlSticky($identifier, $content));
161+
}
105162
}

Ajax/service/JArray.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,29 @@ public static function sortAssociative($array, $sortedKeys=array()) {
7474
}
7575
return $newArray;
7676
}
77+
78+
public static function modelArray($objects,$identifierFunction=NULL,$modelFunction=NULL){
79+
$result=[];
80+
if(isset($modelFunction)===false){
81+
$modelFunction="__toString";
82+
}
83+
if(isset($identifierFunction)===false){
84+
foreach ($objects as $object){
85+
$result[]=self::callFunction($object, $modelFunction);
86+
}
87+
}else{
88+
foreach ($objects as $object){
89+
$result[self::callFunction($object, $identifierFunction)]=self::callFunction($object, $modelFunction);
90+
}
91+
}
92+
return $result;
93+
}
94+
95+
private static function callFunction($object,$callback){
96+
if(\is_string($callback))
97+
return \call_user_func(array($object, $callback),[]);
98+
else if (\is_callable($callback)){
99+
return $callback($object);
100+
}
101+
}
77102
}

0 commit comments

Comments
 (0)