Skip to content

Commit 7a8d953

Browse files
committed
Add autoActiveLinks option
1 parent ccea9aa commit 7a8d953

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Ajax/JsUtils.php

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

170170
/**
171-
* @param array $params ['debug'=>true,'defer'=>false,'ajax'=>['ajaxTransition'=>null,'attr'=>'','historize'=>false,'attr'=>''],'beforeCompileHtml'=>null,'semantic'=>false,'bootstrap'=>false,'historize'=>false]
171+
* @param array $params ['debug'=>true,'defer'=>false,'ajax'=>['ajaxTransition'=>null,'attr'=>'','historize'=>false,'attr'=>''],'beforeCompileHtml'=>null,'semantic'=>false,'bootstrap'=>false,'historize'=>true,'autoActiveLinks'=>true]
172172
* @param mixed $injected optional param for Symfony/Ubiquity
173173
*/
174174
public function __construct($params=array(),$injected=NULL) {
175175
$defaults=['debug'=>true,'defer'=>false,'ajax'=>
176176
['ajaxTransition'=>null,'attr'=>'','historize'=>false,'jsCallback'=>null,'hasLoader'=>true,'jqueryDone'=>'html',
177177
'async'=>true,'params'=>null,'headers'=>null,'jsCondition'=>null,'ajaxLoader'=>null],
178-
'historize'=>true
178+
'historize'=>true,'autoActiveLinks'=>true
179179
];
180180
foreach ( $defaults as $key => $val ) {
181181
if (isset($params[$key])===false || $params[$key]==="") {
@@ -200,6 +200,9 @@ public function __construct($params=array(),$injected=NULL) {
200200
if($params["historize"]){
201201
$this->execAtLast($this->onPopstate());
202202
}
203+
if($params["autoActiveLinks"]){
204+
$this->exec($this->autoActiveLinks(),true);
205+
}
203206

204207
$this->params=$params;
205208
$this->injected=$injected;

Ajax/common/traits/JsUtilsAjaxTrait.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ protected function _getAjaxUrl($url,$attr){
9999
protected function onPopstate(){
100100
return "window.onpopstate = function(e){if(e.state){var target=e.state.jqueryDone;$(e.state.selector)[target](e.state.html);}};";
101101
}
102+
103+
protected function autoActiveLinks($previousURL="window.location.href"){
104+
$result= "\nfunction getHref(url) { return \$('a').filter(function(){return \$(this).prop('href') == url; });}";
105+
$result.="\nvar myurl={$previousURL};if(window._previousURL) getHref(window._previousURL).removeClass('active');getHref(myurl).addClass('active');window._previousURL=myurl;";
106+
return $result;
107+
}
102108

103109
protected function _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$jsCallback,$hasLoader=false,$history=null){
104110
$retour="";$call=null;
@@ -114,9 +120,10 @@ protected function _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$
114120
$retour="\t{$responseElement}.{$jqueryDone}( data );\n";
115121
}
116122
if(isset($history)){
117-
$retour.="\nfunction getHref(url) { return \$('a').filter(function(){return \$(this).prop('href') == url; });}";
118-
$retour.="\nif(window._previousURL) getHref(window._previousURL).removeClass('active');getHref(url).addClass('active');";
119-
$retour.="\nwindow.history.pushState({'html':data,'selector':".Javascript::prep_value($history).",'jqueryDone':'{$jqueryDone}'},'', url);window._previousURL=url;";
123+
if($this->params["autoActiveLinks"]){
124+
$retour.=$this->autoActiveLinks("url");
125+
}
126+
$retour.="\nwindow.history.pushState({'html':data,'selector':".Javascript::prep_value($history).",'jqueryDone':'{$jqueryDone}'},'', url);";
120127
}
121128
if($hasLoader==="internal"){
122129
$retour.="\n$(self).removeClass('loading');";

0 commit comments

Comments
 (0)