1010use Ajax \semantic \html \elements \HtmlLabel ;
1111use Ajax \semantic \html \modules \HtmlProgress ;
1212use Ajax \semantic \html \modules \HtmlRating ;
13- use Ajax \semantic \html \base \HtmlSemDoubleElement ;
14-
1513/**
1614 * @author jc
1715 * @property InstanceViewer $_instanceViewer
@@ -27,19 +25,20 @@ private function _getLabelField($caption,$icon=NULL){
2725 return $ label ;
2826 }
2927
30- /**
31- * @param HtmlSemDoubleElement $element
32- * @param array $attributes
33- */
34- protected function _applyAttributes ($ element ,&$ attributes ,$ index ){
35- if (isset ($ attributes ["callback " ])){
36- $ callback =$ attributes ["callback " ];
37- if (\is_callable ($ callback )){
38- $ callback ($ element ,$ this ->_modelInstance ,$ index );
39- unset($ attributes ["callback " ]);
28+ protected function _addRules ($ element ,$ attributes ){}
29+
30+ protected function _fieldAs ($ elementCallback ,$ index ,$ attributes =NULL ,$ prefix =null ){
31+ $ this ->setValueFunction ($ index ,function ($ value )use ($ index ,&$ attributes ,$ elementCallback ,$ prefix ){
32+ $ name =$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
33+ if (isset ($ attributes ["name " ])===true ){
34+ $ name =$ attributes ["name " ];
4035 }
41- }
42- $ element ->fromArray ($ attributes );
36+ $ element =$ elementCallback ($ this ->_getFieldIdentifier ($ prefix ),$ value ,$ name );
37+ if (\is_array ($ attributes ))
38+ $ this ->_applyAttributes ($ element , $ attributes ,$ index );
39+ return $ element ;
40+ });
41+ return $ this ;
4342 }
4443
4544
@@ -75,61 +74,43 @@ public function fieldAsImage($index,$size=Size::SMALL,$circular=false){
7574 return $ this ;
7675 }
7776
78- public function fieldAsAvatar ($ index ){
79- $ this ->setValueFunction ($ index ,function ($ img ){return (new HtmlImage ("" ,$ img ))->asAvatar ();});
80- return $ this ;
77+ public function fieldAsAvatar ($ index ,$ attributes =NULL ){
78+ return $ this ->_fieldAs (function ($ id ,$ value ,$ name ){
79+ $ img = (new HtmlImage ($ id ,$ value ))->asAvatar ();
80+ return $ img ;
81+ }, $ index ,$ attributes ,"avatar " );
8182 }
8283
8384
8485 public function fieldAsRadio ($ index ,$ attributes =NULL ){
85- $ this ->setValueFunction ($ index ,function ($ value )use ($ index ,$ attributes ){
86- if (isset ($ attributes ["name " ])===false ){
87- $ attributes ["name " ]=$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
88- }
89- $ radio =new HtmlRadio ($ this ->_getFieldIdentifier ("radio " ),$ attributes ["name " ],$ value ,$ value );
90- $ this ->_applyAttributes ($ radio , $ attributes , $ index );
91- return $ radio ;
92- });
93- return $ this ;
86+ return $ this ->_fieldAs (function ($ id ,$ value ,$ name ){
87+ $ input = new HtmlRadio ($ id ,$ name ,$ value ,$ value );
88+ return $ input ;
89+ }, $ index ,$ attributes ,"radio " );
9490 }
9591
9692 public function fieldAsInput ($ index ,$ attributes =NULL ){
97- $ this ->setValueFunction ($ index ,function ($ value ) use ($ index ,$ attributes ){
98- $ input =new HtmlInput ($ this ->_getFieldIdentifier ("input " ),"text " ,$ value );
99- if (isset ($ attributes ["name " ])===false ){
100- $ attributes ["name " ]=$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
101- }
102- $ input ->getField ()->setProperty ("name " , $ attributes ["name " ]);
103- $ this ->_applyAttributes ($ input , $ attributes , $ index );
93+ return $ this ->_fieldAs (function ($ id ,$ value ,$ name ){
94+ $ input = new HtmlInput ($ id ,"text " ,$ value );
95+ $ input ->getField ()->setProperty ("name " , $ name );
10496 return $ input ;
105- });
106- return $ this ;
97+ }, $ index ,$ attributes ,"input " );
10798 }
10899
109100 public function fieldAsCheckbox ($ index ,$ attributes =NULL ){
110- $ this ->setValueFunction ($ index ,function ($ value ) use ($ index ,$ attributes ){
111- $ checkbox =new HtmlCheckbox ($ this ->_getFieldIdentifier ("ck " ),"" ,$ value );
112- $ checkbox ->setChecked (JString::isBooleanTrue ($ value ));
113- if (isset ($ attributes ["name " ])===false ){
114- $ attributes ["name " ]=$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
115- }
116- $ checkbox ->getField ()->setProperty ("name " , $ attributes ["name " ]);
117- $ this ->_applyAttributes ($ checkbox , $ attributes , $ index );
118- return $ checkbox ;
119- });
120- return $ this ;
101+ return $ this ->_fieldAs (function ($ id ,$ value ,$ name ){
102+ $ input =new HtmlCheckbox ($ id ,"" ,$ this ->_instanceViewer ->getIdentifier ());
103+ $ input ->setChecked (JString::isBooleanTrue ($ value ));
104+ $ input ->getField ()->setProperty ("name " , $ name );
105+ return $ input ;
106+ }, $ index ,$ attributes ,"ck " );
121107 }
122108
123109 public function fieldAsDropDown ($ index ,$ elements =[],$ multiple =false ,$ attributes =NULL ){
124- $ this ->setValueFunction ($ index ,function ($ value ) use ($ index ,$ elements ,$ multiple ,$ attributes ){
125- $ dd =new HtmlDropdown ($ this ->_getFieldIdentifier ("dd " ),$ value ,$ elements );
126- if (isset ($ attributes ["name " ])===false ){
127- $ attributes ["name " ]=$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
128- }
129- $ dd ->asSelect ($ attributes ["name " ],$ multiple );
130- $ this ->_applyAttributes ($ dd , $ attributes , $ index );
110+ return $ this ->_fieldAs (function ($ id ,$ value ,$ name ) use ($ elements ,$ multiple ){
111+ $ dd =new HtmlDropdown ($ id ,$ value ,$ elements );
112+ $ dd ->asSelect ($ name ,$ multiple );
131113 return $ dd ;
132- });
133- return $ this ;
114+ }, $ index ,$ attributes ,"dd " );
134115 }
135116}
0 commit comments