Skip to content

Commit f022dc6

Browse files
committed
add listenerOn for add event (dynamic events)
1 parent 6f80f95 commit f022dc6

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

Ajax/JsUtils.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,11 @@ public function _prep_args($result, $is_key=FALSE) {
424424
* @param string $event The event to pass
425425
* @param boolean $preventDefault If set to true, the default action of the event will not be triggered.
426426
* @param boolean $stopPropagation Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.
427+
* @param boolean $immediatly if true, execute this code immediatly
428+
* @param boolean|string false for an event associated with existing DOM elements, or a string selector and then the event is dynamically associated to all elements created in this selector
427429
* @return string
428430
*/
429-
public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true) {
431+
public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false, $immediatly=true, $listenerOn=false) {
430432
if (\is_array($js)) {
431433
$js=implode("\n\t\t", $js);
432434
}
@@ -436,14 +438,15 @@ public function _add_event($element, $js, $event, $preventDefault=false, $stopPr
436438
if ($stopPropagation===true) {
437439
$js=Javascript::$stopPropagation.$js;
438440
}
439-
if (array_search($event, $this->jquery_events)===false){
440-
$event="\n\t$(".Javascript::prep_element($element).").bind('{$event}',function(event){\n\t\t{$js}\n\t});\n";
441-
}
442-
else{
443-
/*if($event==="click"){
444-
$js="let self = $(this);if (self.hasClass('__clicked')){self.removeClass('__clicked');}else{self.addClass('__clicked');setTimeout(function() {if (self.hasClass('__clicked')){self.removeClass('__clicked');".$js."}}, 500);}";
445-
}*/
446-
$event="\n\t$(".Javascript::prep_element($element).").{$event}(function(event){\n\t\t{$js}\n\t});\n";
441+
if(\is_string($listenerOn)){
442+
$event="\n\t$(".Javascript::prep_element($listenerOn).").on('{$event}',".Javascript::prep_element($element).",function(event){\n\t\t{$js}\n\t});\n";
443+
}else{
444+
if (\array_search($event, $this->jquery_events)===false){
445+
$event="\n\t$(".Javascript::prep_element($element).").bind('{$event}',function(event){\n\t\t{$js}\n\t});\n";
446+
}
447+
else{
448+
$event="\n\t$(".Javascript::prep_element($element).").{$event}(function(event){\n\t\t{$js}\n\t});\n";
449+
}
447450
}
448451
if($immediatly)
449452
$this->jquery_code_for_compile[]=$event;

Ajax/common/traits/JsUtilsActionsTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
* @property array $jquery_code_for_compile_at_last
1212
*/
1313
trait JsUtilsActionsTrait {
14-
abstract public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true);
14+
15+
abstract public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true, $listenerOn=false);
1516

1617
/**
1718
* show or hide with effect

Ajax/common/traits/JsUtilsAjaxTrait.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ trait JsUtilsAjaxTrait {
1616
protected $ajaxTransition;
1717
protected $ajaxLoader = "<div class=\"ui active centered inline text loader\">Loading</div>";
1818
abstract public function getUrl($url);
19-
abstract public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true);
19+
abstract public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true, $listenerOn=false);
2020
abstract public function interval($jsCode, $time, $globalName = null, $immediatly = true);
2121
protected function _ajax($method, $url, $responseElement = "", $parameters = [ ]) {
2222
if (isset ( $this->params ["ajax"] )) {
@@ -349,7 +349,7 @@ public function json($url, $method = "get", $parameters = [ ]) {
349349
* @param string $method
350350
* default get
351351
* @param array $parameters
352-
* default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true,"before"=>null)
352+
* default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true,"before"=>null,"listenerOn"=>false)
353353
*/
354354
public function jsonOn($event, $element, $url, $method = "get", $parameters = array ()) {
355355
$this->setDefaultParameters ( $parameters, [
@@ -453,7 +453,7 @@ public function jsonArrayDeferred($maskSelector, $url, $method = "get", $paramet
453453
* @param string $method
454454
* Method used, default : get
455455
* @param array $parameters
456-
* default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true,"before"=>null)
456+
* default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true,"before"=>null,"listenerOn"=>false)
457457
*/
458458
public function jsonArrayOn($event, $element, $maskSelector, $url, $method = "get", $parameters = array ()) {
459459
$this->setDefaultParameters ( $parameters, [
@@ -493,7 +493,7 @@ public function getDeferred($url, $responseElement = "", $parameters = [ ]) {
493493
* @param string $responseElement
494494
* The selector of the HTML element displaying the answer
495495
* @param array $parameters
496-
* 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,"before"=>null)
496+
* 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,"before"=>null,"listenerOn"=>false)
497497
*/
498498
public function getOn($event, $element, $url, $responseElement = "", $parameters = array ()) {
499499
$this->setDefaultParameters ( $parameters, [
@@ -517,7 +517,7 @@ public function getOn($event, $element, $url, $responseElement = "", $parameters
517517
* @param string $responseElement
518518
* The selector of the HTML element displaying the answer
519519
* @param array $parameters
520-
* default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
520+
* default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null,"listenerOn"=>false)
521521
*/
522522
public function ajaxOn($event, $element, $url, $responseElement = "", $parameters = array ()) {
523523
$this->setDefaultParameters ( $parameters, [
@@ -540,7 +540,7 @@ public function ajaxOn($event, $element, $url, $responseElement = "", $parameter
540540
* @param string $responseElement
541541
* The selector of the HTML element displaying the answer
542542
* @param array $parameters
543-
* default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
543+
* default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null,"listenerOn"=>false)
544544
*/
545545
public function ajaxOnClick($element, $url, $responseElement = "", $parameters = array ()) {
546546
return $this->ajaxOn ( "click", $element, $url, $responseElement, $parameters );
@@ -557,7 +557,7 @@ public function ajaxOnClick($element, $url, $responseElement = "", $parameters =
557557
* @param string $responseElement
558558
* The selector of the HTML element displaying the answer
559559
* @param array $parameters
560-
* default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
560+
* default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null,"listenerOn"=>false)
561561
*/
562562
public function getOnClick($element, $url, $responseElement = "", $parameters = array ()) {
563563
return $this->getOn ( "click", $element, $url, $responseElement, $parameters );
@@ -663,7 +663,7 @@ public function postDeferred($url, $params = "{}", $responseElement = "", $param
663663
* @param string $responseElement
664664
* selector of the HTML element displaying the answer
665665
* @param array $parameters
666-
* default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
666+
* default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null,"listenerOn"=>false)
667667
*/
668668
public function postOn($event, $element, $url, $params = "{}", $responseElement = "", $parameters = array ()) {
669669
$this->setDefaultParameters ( $parameters, [
@@ -686,7 +686,7 @@ public function postOn($event, $element, $url, $params = "{}", $responseElement
686686
* @param string $responseElement
687687
* selector of the HTML element displaying the answer
688688
* @param array $parameters
689-
* default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null,"before"=>null)
689+
* default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null,"before"=>null,"listenerOn"=>false)
690690
*/
691691
public function postOnClick($element, $url, $params = "{}", $responseElement = "", $parameters = array ()) {
692692
return $this->postOn ( "click", $element, $url, $params, $responseElement, $parameters );
@@ -796,7 +796,7 @@ public function postFormDeferred($url, $form, $responseElement, $parameters = [
796796
* @param string $responseElement
797797
* selector of the HTML element displaying the answer
798798
* @param array $parameters
799-
* default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false,"before"=>null)
799+
* default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false,"before"=>null,"listenerOn"=>false)
800800
*/
801801
public function postFormOn($event, $element, $url, $form, $responseElement = "", $parameters = array ()) {
802802
$this->setDefaultParameters ( $parameters, [
@@ -817,7 +817,7 @@ public function postFormOn($event, $element, $url, $form, $responseElement = "",
817817
* @param string $responseElement
818818
* selector of the HTML element displaying the answer
819819
* @param array $parameters
820-
* default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false,"before"=>null)
820+
* default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false,"before"=>null,"listenerOn"=>false)
821821
*/
822822
public function postFormOnClick($element, $url, $form, $responseElement = "", $parameters = array ()) {
823823
return $this->postFormOn ( "click", $element, $url, $form, $responseElement, $parameters );

Ajax/common/traits/JsUtilsEventsTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ trait JsUtilsEventsTrait {
5050
"undelegate",
5151
"unload"
5252
);
53-
abstract public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true);
53+
54+
abstract public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true, $listenerOn=false);
5455

5556
/**
5657
* Outputs a javascript library blur event

0 commit comments

Comments
 (0)