Skip to content

Commit 0ed595b

Browse files
committed
optimization
1 parent f4e97dc commit 0ed595b

File tree

4 files changed

+51
-44
lines changed

4 files changed

+51
-44
lines changed

Ajax/common/html/BaseHtml.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -213,35 +213,35 @@ public function setTagName($tagName) {
213213

214214
public function fromArray($array) {
215215
foreach ( $this as $key => $value ) {
216+
$this->_callSetter("set" . ucfirst($key), $key, $value, $array);
216217
if (array_key_exists($key, $array) && !JString::startswith($key, "_")) {
217218
$setter="set" . ucfirst($key);
218219
$this->$setter($array[$key]);
219220
unset($array[$key]);
220221
}
221222
}
222223
foreach ( $array as $key => $value ) {
223-
if (method_exists($this, $key)) {
224-
try {
225-
$this->$key($value);
226-
unset($array[$key]);
227-
} catch ( \Exception $e ) {
228-
// Nothing to do
229-
}
230-
} else {
231-
$setter="set" . ucfirst($key);
232-
if (method_exists($this, $setter)) {
233-
try {
234-
$this->$setter($value);
235-
unset($array[$key]);
236-
} catch ( \Exception $e ) {
237-
// Nothing to do
238-
}
239-
}
224+
if($this->_callSetter($key, $key, $value, $array)===false){
225+
$this->_callSetter("set" . ucfirst($key), $key, $value, $array);
240226
}
241227
}
242228
return $array;
243229
}
244230

231+
private function _callSetter($setter,$key,$value,&$array){
232+
$result=false;
233+
if (method_exists($this, $setter) && !JString::startswith($key, "_")) {
234+
try {
235+
$this->$setter($value);
236+
unset($array[$key]);
237+
$result=true;
238+
} catch ( \Exception $e ) {
239+
$result=false;
240+
}
241+
}
242+
return $result;
243+
}
244+
245245
public function fromDatabaseObjects($objects, $function) {
246246
if (isset($objects)) {
247247
foreach ( $objects as $object ) {
@@ -257,7 +257,6 @@ public function fromDatabaseObject($object, $function) {
257257
public function wrap($before, $after="") {
258258
if (isset($before)) {
259259
array_unshift($this->_wrapBefore, $before);
260-
// $this->_wrapBefore[]=$before;
261260
}
262261
$this->_wrapAfter[]=$after;
263262
return $this;

Ajax/common/html/traits/BaseHtmlEventsTrait.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,7 @@ public function onCreate($jsCode){
5959
}
6060

6161
public function addEventsOnRun(JsUtils $js) {
62-
if(isset($this->_events["_create"])){
63-
$create=$this->_events["_create"];
64-
if(\is_array($create)){
65-
$create=\implode("", $create);
66-
}
67-
if($create!=="")
68-
$js->exec($create,true);
69-
unset($this->_events["_create"]);
70-
}
62+
$this->_eventsOnCreate($js);
7163
if (isset($this->_bsComponent)) {
7264
foreach ( $this->_events as $event => $jsCode ) {
7365
$code=$jsCode;
@@ -89,6 +81,18 @@ public function addEventsOnRun(JsUtils $js) {
8981
}
9082
}
9183

84+
protected function _eventsOnCreate(JsUtils $js){
85+
if(isset($this->_events["_create"])){
86+
$create=$this->_events["_create"];
87+
if(\is_array($create)){
88+
$create=\implode("", $create);
89+
}
90+
if($create!=="")
91+
$js->exec($create,true);
92+
unset($this->_events["_create"]);
93+
}
94+
}
95+
9296
public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
9397
$params=array ("url" => $url,"responseElement" => $responseElement );
9498
$params=array_merge($params, $parameters);

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,20 @@ public function run(JsUtils $js) {
179179
if(isset($compo)===false){
180180
return parent::run($js);
181181
}
182-
if(isset($this->_validationParams["_ajaxSubmit"])){
183-
if($this->_validationParams["_ajaxSubmit"] instanceof AjaxCall){
184-
$compilation=$this->_validationParams["_ajaxSubmit"]->compile($js);
185-
$compilation=str_ireplace("\"","%quote%", $compilation);
186-
$this->onSuccess($compilation);
187-
unset($this->_validationParams["_ajaxSubmit"]);
188-
}
182+
$this->_runValidationParams($compo,$js);
183+
return $this->_bsComponent;
184+
}
185+
186+
private function _runValidationParams(&$compo,JsUtils $js=NULL){
187+
if(isset($this->_validationParams["_ajaxSubmit"]) && $this->_validationParams["_ajaxSubmit"] instanceof AjaxCall){
188+
$compilation=$this->_validationParams["_ajaxSubmit"]->compile($js);
189+
$compilation=str_ireplace("\"","%quote%", $compilation);
190+
$this->onSuccess($compilation);
191+
unset($this->_validationParams["_ajaxSubmit"]);
189192
}
190193
$compo->addParams($this->_validationParams);
191194
$this->_bsComponent=$compo;
192195
$this->addEventsOnRun($js);
193-
return $this->_bsComponent;
194196
}
195197

196198
public function addValidationParam($paramName,$paramValue){

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,20 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
6363
$table=$this->content["table"];
6464

6565
if($this->_hasCheckboxes){
66-
$ck=new HtmlCheckbox("main-ck-".$this->identifier,"");
67-
$ck->setOnChecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',true);");
68-
$ck->setOnUnchecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',false);");
69-
\array_unshift($captions, $ck);
66+
$this->_generateMainCheckbox($captions);
7067
}
7168

7269
$table->setRowCount(0, \sizeof($captions));
7370
$table->setHeaderValues($captions);
7471
if(isset($this->_compileParts))
7572
$table->setCompileParts($this->_compileParts);
76-
if(isset($this->_searchField)){
77-
if(isset($js))
78-
$this->_searchField->postOn("change", $this->_urls,"{'s':$(this).val()}","-#".$this->identifier." tbody",["preventDefault"=>false]);
73+
if(isset($this->_searchField) && isset($js)){
74+
$this->_searchField->postOn("change", $this->_urls,"{'s':$(this).val()}","-#".$this->identifier." tbody",["preventDefault"=>false]);
7975
}
8076

8177
$this->_generateContent($table);
8278

83-
if($this->_hasCheckboxes){
84-
if($table->hasPart("thead"))
79+
if($this->_hasCheckboxes && $table->hasPart("thead")){
8580
$table->getHeader()->getCell(0, 0)->addToProperty("class","no-sort");
8681
}
8782

@@ -95,6 +90,13 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
9590
return parent::compile($js,$view);
9691
}
9792

93+
private function _generateMainCheckbox(&$captions){
94+
$ck=new HtmlCheckbox("main-ck-".$this->identifier,"");
95+
$ck->setOnChecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',true);");
96+
$ck->setOnUnchecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',false);");
97+
\array_unshift($captions, $ck);
98+
}
99+
98100
protected function _generateContent($table){
99101
$objects=$this->_modelInstance;
100102
if(isset($this->_pagination)){

0 commit comments

Comments
 (0)