Skip to content

Commit 82c0ffa

Browse files
committed
Adds dom counter
1 parent 92f62c8 commit 82c0ffa

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Ajax/common/traits/JsUtilsActionsTrait.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,58 @@ public function interval($jsCode,$time,$globalName=null,$immediatly=true){
602602
public function clearInterval($globalName,$immediatly=true){
603603
return $this->exec("if(window.{$globalName}){clearInterval(window.{$globalName});}",$immediatly);
604604
}
605+
606+
/**
607+
* Associates a counter to the element designated by $counterSelector
608+
* @param string $counterSelector Selector of the existing element wich display the counter
609+
* @param integer $value The initial value of the counter
610+
* @param integer $limit The limit of the counter (minimum if countDown is true, maximum if not)
611+
* @param string $globalName The global name of the counter, to use with the clearInterval method
612+
* @param boolean $countDown count down if true or elapse if false
613+
* @param boolean $immediatly delayed if false
614+
* @return string
615+
*/
616+
public function counter($counterSelector,$value=0,$limit=0,$globalName=null,$countDown=true,$immediatly=true){
617+
$stop="";
618+
if($countDown){
619+
$stop="if (--timer < ".$limit.") {clearInterval(interval);display.trigger('counter-end',timer);}";
620+
}else{
621+
if($limit!=0){
622+
$stop="if (++timer > ".$limit.") {clearInterval(interval);display.trigger('counter-end',timer);}";
623+
}
624+
}
625+
$global="";
626+
if(isset($globalName)){
627+
$global="\nwindow.{$globalName}=interval;";
628+
}
629+
$timer="var startTimer=function(duration, display) {var timer = duration, minutes, seconds;
630+
display.trigger('counter-start',timer);
631+
display.show();
632+
var interval=setInterval(function () {
633+
minutes = parseInt(timer / 60, 10);seconds = parseInt(timer % 60, 10);
634+
minutes = minutes < 10 ? '0' + minutes : minutes;
635+
seconds = seconds < 10 ? '0' + seconds : seconds;
636+
if(display.is('[value]')){display.val(minutes + ':' + seconds);} else {display.html(minutes + ':' + seconds);};
637+
".$stop."
638+
}, 1000);
639+
".$global."
640+
}";
641+
$element='$("'.$counterSelector.'")';
642+
return $this->exec($timer."\nstartTimer(".$value.",".$element.");",$immediatly);
643+
}
644+
645+
/**
646+
* Associates a counter to the element designated by $counterSelector when $event is triggered on $element
647+
* @param string $element The triggering element
648+
* @param string $event The triggering event
649+
* @param string $counterSelector Selector of the existing element wich display the counter
650+
* @param integer $value The initial value of the counter
651+
* @param integer $limit The limit of the counter (minimum if countDown is true, maximum if not)
652+
* @param string $globalName The global name of the counter, to use with the clearInterval method
653+
* @param boolean $countDown count down if true or elapse if false
654+
* @return string
655+
*/
656+
public function counterOn($element,$event,$counterSelector,$value=0,$limit=0,$globalName=null,$countDown=true){
657+
return $this->execOn($event, $element, $this->counter($counterSelector,$value,$limit,$globalName,$countDown,false));
658+
}
605659
}

Ajax/common/traits/JsUtilsAjaxTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ public function ajax($method,$url, $responseElement="", $parameters=[]) {
231231
}
232232

233233
/**
234+
* Executes an ajax query at regular intervals
234235
* @param string $method The http method (post, get...)
235236
* @param string $url The url of the request
236237
* @param int $interval The interval in milliseconds

0 commit comments

Comments
 (0)