Skip to content

Commit b7fff6d

Browse files
committed
jqueryDone + defer options
1 parent d6d0c3d commit b7fff6d

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

Ajax/Jquery.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,23 +229,22 @@ public function _add_event($element, $js, $event, $preventDefault=false, $stopPr
229229
* @return string
230230
*/
231231
public function _compile(&$view=NULL, $view_var='script_foot', $script_tags=TRUE) {
232-
// Components UI
233232
$this->_compileLibrary($this->ui());
234-
// Components BS
235233
$this->_compileLibrary($this->bootstrap());
236-
// Components Semantic
237234
$this->_compileLibrary($this->semantic());
238235

239236
if (\sizeof($this->jquery_code_for_compile)==0) {
240-
// no inline references, let's just return
241237
return;
242238
}
243239

244240
// Inline references
245241
$script='$(document).ready(function() {'."\n";
246242
$script.=implode('', $this->jquery_code_for_compile);
247-
$script.='});';
248-
243+
$script.='})';
244+
if($this->params["defer"]){
245+
$script=$this->defer($script);
246+
}
247+
$script.=";";
249248
$this->jquery_code_for_compile=array();
250249
if($this->params["debug"]===false){
251250
$script=$this->minify($script);
@@ -258,6 +257,12 @@ public function _compile(&$view=NULL, $view_var='script_foot', $script_tags=TRUE
258257
return $output;
259258
}
260259

260+
private function defer($script){
261+
$result="window.defer=function (method) {if (window.jQuery) method(); else setTimeout(function() { defer(method) }, 50);};";
262+
$result.="window.defer(function(){".$script."})";
263+
return $result;
264+
}
265+
261266
private function _compileLibrary($library){
262267
if ($library!=NULL) {
263268
if ($library->isAutoCompile()) {

Ajax/common/traits/JqueryAjaxTrait.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ protected function addLoading(&$retour, $responseElement) {
2828
$retour.="\t\t$({$responseElement}).prepend('{$loading_notifier}');\n";
2929
}
3030

31-
public function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) {
32-
return $this->_ajax("get", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$immediatly);
31+
public function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$immediatly=false) {
32+
return $this->_ajax("get", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$jqueryDone,$immediatly);
3333
}
34-
public function _post($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) {
35-
return $this->_ajax("post", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$immediatly);
34+
public function _post($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$immediatly=false) {
35+
return $this->_ajax("post", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$jqueryDone,$immediatly);
3636
}
3737

38-
protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) {
38+
protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$immediatly=false) {
3939
if(JString::isNull($params)){$params="{}";}
4040
$jsCallback=isset($jsCallback) ? $jsCallback : "";
4141
$retour=$this->_getAjaxUrl($url, $attr);
@@ -45,7 +45,7 @@ protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCal
4545
$this->addLoading($retour, $responseElement);
4646
}
4747
$retour.="$.".$method."(url,".$params.").done(function( data ) {\n";
48-
$retour.=$this->_getOnAjaxDone($responseElement, $jsCallback)."});\n";
48+
$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$jsCallback)."});\n";
4949
if ($immediatly)
5050
$this->jquery_code_for_compile[]=$retour;
5151
return $retour;
@@ -68,15 +68,10 @@ protected function _getAjaxUrl($url,$attr){
6868
return $retour;
6969
}
7070

71-
protected function _getOnAjaxDone($responseElement,$jsCallback){
71+
protected function _getOnAjaxDone($responseElement,$jqueryDone,$jsCallback){
7272
$retour="";
7373
if ($responseElement!=="") {
74-
if(JString::startswith($responseElement, "\"-")===true){
75-
$responseElement=\str_replace("\"-", "\"", $responseElement);
76-
$retour="\t$({$responseElement}).replaceWith( data );\n";
77-
}else{
78-
$retour="\t$({$responseElement}).html( data );\n";
79-
}
74+
$retour="\t$({$responseElement}).{$jqueryDone}( data );\n";
8075
}
8176
$retour.="\t".$jsCallback."\n";
8277
return $retour;
@@ -189,7 +184,7 @@ public function _jsonArrayOn($event,$element, $maskSelector,$url,$parameters=arr
189184
return $this->_add_event($element, $this->_jsonArray($maskSelector,$url,$method, $params,$jsCallback, $attr, $context), $event, $preventDefault, $stopPropagation,$immediatly);
190185
}
191186

192-
public function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) {
187+
public function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$immediatly=false) {
193188
$jsCallback=isset($jsCallback) ? $jsCallback : "";
194189
$retour=$this->_getAjaxUrl($url, $attr);
195190
$retour.="\nvar params=$('#".$form."').serialize();\n";
@@ -199,7 +194,7 @@ public function _postForm($url, $form, $responseElement, $validation=false, $jsC
199194
$this->addLoading($retour, $responseElement);
200195
}
201196
$retour.="$.post(url,params).done(function( data ) {\n";
202-
$retour.=$this->_getOnAjaxDone($responseElement, $jsCallback)."});\n";
197+
$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$jsCallback)."});\n";
203198

204199
if ($validation) {
205200
$retour="$('#".$form."').validate({submitHandler: function(form) {
@@ -220,7 +215,7 @@ public function _postForm($url, $form, $responseElement, $validation=false, $jsC
220215
* @param string $url
221216
* @param string $params queryString parameters (JSON format). default : {}
222217
* @param string $responseElement
223-
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true)
218+
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html")
224219
*/
225220
public function _getOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) {
226221
$preventDefault=true;
@@ -229,8 +224,9 @@ public function _getOn($event,$element, $url, $params="{}", $responseElement="",
229224
$attr="id";
230225
$hasLoader=true;
231226
$immediatly=true;
227+
$jqueryDone="html";
232228
extract($parameters);
233-
return $this->_add_event($element, $this->_get($url, $params, $responseElement, $jsCallback, $attr,$hasLoader), $event, $preventDefault, $stopPropagation,$immediatly);
229+
return $this->_add_event($element, $this->_get($url, $params, $responseElement, $jsCallback, $attr,$hasLoader,$jqueryDone), $event, $preventDefault, $stopPropagation,$immediatly);
234230
}
235231

236232
/**
@@ -250,8 +246,9 @@ public function _postOn($event,$element, $url, $params="{}", $responseElement=""
250246
$attr="id";
251247
$hasLoader=true;
252248
$immediatly=true;
249+
$jqueryDone="html";
253250
extract($parameters);
254-
return $this->_add_event($element, $this->_post($url, $params, $responseElement, $jsCallback, $attr,$hasLoader), $event, $preventDefault, $stopPropagation,$immediatly);
251+
return $this->_add_event($element, $this->_post($url, $params, $responseElement, $jsCallback, $attr,$hasLoader,$jqueryDone), $event, $preventDefault, $stopPropagation,$immediatly);
255252
}
256253

257254
/**
@@ -272,7 +269,8 @@ public function _postFormOn($event,$element, $url, $form, $responseElement="", $
272269
$attr="id";
273270
$hasLoader=true;
274271
$immediatly=true;
272+
$jqueryDone="html";
275273
extract($parameters);
276-
return $this->_add_event($element, $this->_postForm($url, $form, $responseElement, $validation, $jsCallback, $attr,$hasLoader), $event, $preventDefault, $stopPropagation,$immediatly);
274+
return $this->_add_event($element, $this->_postForm($url, $form, $responseElement, $validation, $jsCallback, $attr,$hasLoader,$jqueryDone), $event, $preventDefault, $stopPropagation,$immediatly);
277275
}
278276
}

Ajax/semantic/html/elements/HtmlButtonGroups.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Ajax\semantic\html\base\HtmlSemDoubleElement;
66
use Ajax\semantic\html\base\HtmlSemCollection;
7+
use Ajax\JsUtils;
78

89
/**
910
* Semantic UI Buttongroups component
@@ -113,4 +114,9 @@ public function addClasses($classes=array()) {
113114
public function fromDatabaseObject($object, $function) {
114115
$this->addElement($function($object));
115116
}
117+
118+
public function run(JsUtils $js){
119+
$result= parent::run($js);
120+
return $result->setItemSelector(".ui.button");
121+
}
116122
}

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
6969
if(isset($this->_compileParts))
7070
$table->setCompileParts($this->_compileParts);
7171
if(isset($this->_searchField) && isset($js)){
72-
$this->_searchField->postOn("change", $this->_urls,"{'s':$(this).val()}","-#".$this->identifier." tbody",["preventDefault"=>false]);
7372
}
7473

7574
$this->_generateContent($table);
@@ -123,7 +122,6 @@ private function _generatePagination($table){
123122
$menu->floatRight();
124123
$menu->setActiveItem($this->_pagination->getPage()-1);
125124
$footer->setValues($menu);
126-
$menu->postOnClick($this->_urls,"{'p':$(this).attr('data-page')}","-#".$this->identifier." tbody",["preventDefault"=>false]);
127125
}
128126

129127
protected function _setToolbarPosition($table,$captions=NULL){

0 commit comments

Comments
 (0)