Skip to content

Commit 33a9023

Browse files
committed
Add historize for ajax requests
1 parent 52e3ac3 commit 33a9023

File tree

3 files changed

+112
-42
lines changed

3 files changed

+112
-42
lines changed

Ajax/JsUtils.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,15 @@ public function config($config=NULL) {
168168
}
169169

170170
/**
171-
* @param array $params ['debug'=>true,'defer'=>false,'ajaxTransition'=>null,'beforeCompileHtml'=>null,'semantic'=>false,'bootstrap'=>false]
171+
* @param array $params ['debug'=>true,'defer'=>false,'ajax'=>['ajaxTransition'=>null,'attr'=>'','historize'=>false,'attr'=>''],'beforeCompileHtml'=>null,'semantic'=>false,'bootstrap'=>false,'historize'=>false]
172172
* @param mixed $injected optional param for Symfony/Ubiquity
173173
*/
174174
public function __construct($params=array(),$injected=NULL) {
175-
$defaults=['debug'=>true,'defer'=>false,'ajaxTransition'=>null];
175+
$defaults=['debug'=>true,'defer'=>false,'ajax'=>
176+
['ajaxTransition'=>null,'attr'=>'','historize'=>false,'jsCallback'=>null,'hasLoader'=>true,'jqueryDone'=>'html',
177+
'async'=>true,'params'=>null,'headers'=>null,'jsCondition'=>null,'ajaxLoader'=>null],
178+
'historize'=>true
179+
];
176180
foreach ( $defaults as $key => $val ) {
177181
if (isset($params[$key])===false || $params[$key]==="") {
178182
$params[$key]=$defaults[$key];
@@ -185,9 +189,17 @@ public function __construct($params=array(),$injected=NULL) {
185189
if(\array_key_exists("bootstrap", $params)){
186190
$this->bootstrap(new Bootstrap());
187191
}
188-
189-
if(isset($params["ajaxTransition"]))
190-
$this->ajaxTransition=$this->setAjaxDataCall($params["ajaxTransition"]);
192+
if(isset($params["ajax"])){
193+
if(isset($params["ajax"]["ajaxTransition"])){
194+
$this->ajaxTransition=$this->setAjaxDataCall($params["ajax"]["ajaxTransition"]);
195+
}
196+
if($params["ajax"]["historize"]){
197+
$params["historize"]=true;
198+
}
199+
}
200+
if($params["historize"]){
201+
$this->execAtLast($this->onPopstate());
202+
}
191203

192204
$this->params=$params;
193205
$this->injected=$injected;

Ajax/common/html/traits/BaseHtmlEventsTrait.php

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ protected function _eventsOnCreate(JsUtils $js=NULL){
116116
}
117117

118118
/**
119-
* @param string $operation
120-
* @param string $event
121-
* @param string $url
122-
* @param string $responseElement
123-
* @param array $parameters
124-
* @return BaseHtml
119+
* @param string $operation http method get, post, postForm or json
120+
* @param string $event the event that triggers the request
121+
* @param string $url The url of the request
122+
* @param string $responseElement The selector of the HTML element displaying the answer
123+
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false)
124+
* @return $this
125125
*/
126126
public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
127127
$params=array ("url" => $url,"responseElement" => $responseElement );
@@ -130,14 +130,41 @@ public function _ajaxOn($operation, $event, $url, $responseElement="", $paramete
130130
return $this;
131131
}
132132

133+
/**
134+
* Performs a get to $url on the event $event on $element
135+
* and display it in $responseElement
136+
* @param string $event the event that triggers the get request
137+
* @param string $url The url of the request
138+
* @param string $responseElement The selector of the HTML element displaying the answer
139+
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false)
140+
* @return $this
141+
**/
133142
public function getOn($event, $url, $responseElement="", $parameters=array()) {
134143
return $this->_ajaxOn("get", $event, $url, $responseElement, $parameters);
135144
}
136145

146+
/**
147+
* Performs a get to $url on the click event on $element
148+
* and display it in $responseElement
149+
* @param string $url The url of the request
150+
* @param string $responseElement The selector of the HTML element displaying the answer
151+
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false)
152+
* @return $this
153+
**/
137154
public function getOnClick($url, $responseElement="", $parameters=array()) {
138155
return $this->getOn("click", $url, $responseElement, $parameters);
139156
}
140157

158+
/**
159+
* Performs a post to $url on the event $event on $element
160+
* and display it in $responseElement
161+
* @param string $event the event that triggers the post request
162+
* @param string $url The url of the request
163+
* @param string $params the request parameters in JSON format
164+
* @param string $responseElement The selector of the HTML element displaying the answer
165+
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false)
166+
* @return $this
167+
**/
141168
public function postOn($event, $url, $params="{}", $responseElement="", $parameters=array()) {
142169
$allParameters=[];
143170
if(isset($parameters["params"])){
@@ -149,16 +176,42 @@ public function postOn($event, $url, $params="{}", $responseElement="", $paramet
149176
$parameters["params"]=\implode("+'&'+", $allParameters);
150177
return $this->_ajaxOn("post", $event, $url, $responseElement, $parameters);
151178
}
152-
179+
180+
/**
181+
* Performs a post to $url on the click event on $element
182+
* and display it in $responseElement
183+
* @param string $url The url of the request
184+
* @param string $params the request parameters in JSON format
185+
* @param string $responseElement The selector of the HTML element displaying the answer
186+
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false)
187+
* @return $this
188+
**/
153189
public function postOnClick($url, $params="{}", $responseElement="", $parameters=array()) {
154190
return $this->postOn("click", $url, $params, $responseElement, $parameters);
155191
}
156192

193+
/**
194+
* Performs a post form with ajax
195+
* @param string $event the event that triggers the post request
196+
* @param string $url The url of the request
197+
* @param string $form The form HTML id
198+
* @param string $responseElement selector of the HTML element displaying the answer
199+
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
200+
* @return $this
201+
*/
157202
public function postFormOn($event, $url, $form, $responseElement="", $parameters=array()) {
158203
$parameters["form"]=$form;
159204
return $this->_ajaxOn("postForm", $event, $url, $responseElement, $parameters);
160205
}
161206

207+
/**
208+
* Performs a post form with ajax on click
209+
* @param string $url The url of the request
210+
* @param string $form The form HTML id
211+
* @param string $responseElement selector of the HTML element displaying the answer
212+
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
213+
* @return $this
214+
*/
162215
public function postFormOnClick($url, $form, $responseElement="", $parameters=array()) {
163216
return $this->postFormOn("click", $url, $form, $responseElement, $parameters);
164217
}

0 commit comments

Comments
 (0)