phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
JsUtilsActionsTrait.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
14 
15  abstract public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true, $listenerOn=false);
16 
31  protected function _showHideWithEffect($action, $element = 'this', $speed = '', $callback = '', $immediatly = false) {
32  $element = Javascript::prep_element ( $element );
33  $speed = $this->_validate_speed ( $speed );
34  if ($callback != '') {
35  $callback = ", function(){\n{$callback}\n}";
36  }
37  $str = "$({$element}).{$action}({$speed}{$callback});";
38  if ($immediatly)
39  $this->jquery_code_for_compile [] = $str;
40  return $str;
41  }
42 
49  private function _validate_speed($speed) {
50  if (in_array ( $speed, array (
51  'slow',
52  'normal',
53  'fast'
54  ) )) {
55  $speed = '"' . $speed . '"';
56  } elseif (preg_match ( "/[^0-9]/", $speed )) {
57  $speed = '';
58  }
59 
60  return $speed;
61  }
62 
72  public function _genericCallValue($jQueryCall, $element = 'this', $param = "", $immediatly = false) {
73  $element = Javascript::prep_element ( $element );
74  if (isset ( $param )) {
75  $param = Javascript::prep_value ( $param );
76  $str = "$({$element}).{$jQueryCall}({$param});";
77  } else
78  $str = "$({$element}).{$jQueryCall}();";
79  if ($immediatly)
80  $this->jquery_code_for_compile [] = $str;
81  return $str;
82  }
83 
94  public function _genericCallElement($jQueryCall, $to = 'this', $element = '', $immediatly = false) {
95  $to = Javascript::prep_element ( $to );
96  $element = Javascript::prep_element ( $element );
97  $str = "$({$to}).{$jQueryCall}({$element});";
98  if ($immediatly)
99  $this->jquery_code_for_compile [] = $str;
100  return $str;
101  }
102 
113  public function addClass($element = 'this', $class = '', $immediatly = false) {
114  return $this->_genericCallValue ( 'addClass', $element, $class, $immediatly );
115  }
116 
126  public function after($to, $element, $immediatly = false) {
127  return $this->_genericCallElement ( 'after', $to, $element, $immediatly );
128  }
129 
139  public function before($to, $element, $immediatly = false) {
140  return $this->_genericCallElement ( 'before', $to, $element, $immediatly );
141  }
142 
152  public function attr($element = 'this', $attributeName = 'id', $value = "", $immediatly = false) {
153  $element = Javascript::prep_element ( $element );
154  if (isset ( $value )) {
155  $value = Javascript::prep_value ( $value );
156  $str = "$({$element}).attr(\"$attributeName\",{$value});";
157  } else
158  $str = "$({$element}).attr(\"$attributeName\");";
159  if ($immediatly)
160  $this->jquery_code_for_compile [] = $str;
161  return $str;
162  }
163 
172  public function val($element = 'this', $value = '', $immediatly = false) {
173  return $this->_genericCallValue ( 'val', $element, $value, $immediatly );
174  }
175 
184  public function html($element = 'this', $value = '', $immediatly = false) {
185  return $this->_genericCallValue ( 'html', $element, $value, $immediatly );
186  }
187 
201  public function animate($element = 'this', $params = array (), $speed = '', $extra = '', $immediatly = false) {
202  $element = Javascript::prep_element ( $element );
203  $speed = $this->_validate_speed ( $speed );
204 
205  $animations = "\t\t\t";
206  if (\is_array ( $params )) {
207  foreach ( $params as $param => $value ) {
208  $animations .= $param . ': \'' . $value . '\', ';
209  }
210  }
211  $animations = substr ( $animations, 0, - 2 ); // remove the last ", "
212 
213  if ($speed != '') {
214  $speed = ', ' . $speed;
215  }
216 
217  if ($extra != '') {
218  $extra = ', ' . $extra;
219  }
220 
221  $str = "$({$element}).animate({\n$animations\n\t\t}" . $speed . $extra . ");";
222 
223  if ($immediatly)
224  $this->jquery_code_for_compile [] = $str;
225  return $str;
226  }
227 
237  public function append($to, $element, $immediatly = false) {
238  return $this->_genericCallElement ( 'append', $to, $element, $immediatly );
239  }
240 
250  public function prepend($to, $element, $immediatly = false) {
251  return $this->_genericCallElement ( 'prepend', $to, $element, $immediatly );
252  }
253 
267  public function fadeIn($element = 'this', $speed = '', $callback = '', $immediatly = false) {
268  return $this->_showHideWithEffect ( "fadeIn", $element, $speed, $callback, $immediatly );
269  }
270 
284  public function fadeOut($element = 'this', $speed = '', $callback = '', $immediatly = false) {
285  return $this->_showHideWithEffect ( "fadeOut", $element, $speed, $callback, $immediatly );
286  }
287 
301  public function slideUp($element = 'this', $speed = '', $callback = '', $immediatly = false) {
302  return $this->_showHideWithEffect ( "slideUp", $element, $speed, $callback, $immediatly );
303  }
304 
316  public function removeClass($element = 'this', $class = '', $immediatly = false) {
317  return $this->_genericCallValue ( 'removeClass', $element, $class, $immediatly );
318  }
319 
333  public function slideDown($element = 'this', $speed = '', $callback = '', $immediatly = false) {
334  return $this->_showHideWithEffect ( "slideDown", $element, $speed, $callback, $immediatly );
335  }
336 
350  public function slideToggle($element = 'this', $speed = '', $callback = '', $immediatly = false) {
351  return $this->_showHideWithEffect ( "slideToggle", $element, $speed, $callback, $immediatly );
352  }
353 
367  public function hide($element = 'this', $speed = '', $callback = '', $immediatly = false) {
368  return $this->_showHideWithEffect ( "hide", $element, $speed, $callback, $immediatly );
369  }
370 
384  public function toggle($element = 'this', $speed = '', $callback = '', $immediatly = false) {
385  return $this->_showHideWithEffect ( "toggle", $element, $speed, $callback, $immediatly );
386  }
387 
398  public function toggleClass($element = 'this', $class = '', $immediatly = false) {
399  return $this->_genericCallValue ( 'toggleClass', $element, $class, $immediatly );
400  }
401 
410  public function trigger($element = 'this', $event = 'click', $immediatly = false) {
411  $element = Javascript::prep_element ( $element );
412  $str = "$({$element}).trigger(\"$event\");";
413 
414  if ($immediatly)
415  $this->jquery_code_for_compile [] = $str;
416  return $str;
417  }
418 
432  public function show($element = 'this', $speed = '', $callback = '', $immediatly = false) {
433  return $this->_showHideWithEffect ( "show", $element, $speed, $callback, $immediatly );
434  }
435 
443  public function sortable($element, $options = array ()) {
444  if (count ( $options ) > 0) {
445  $sort_options = array ();
446  foreach ( $options as $k => $v ) {
447  $sort_options [] = "\n\t\t" . $k . ': ' . $v . "";
448  }
449  $sort_options = implode ( ",", $sort_options );
450  } else {
451  $sort_options = '';
452  }
453 
454  return "$(" . Javascript::prep_element ( $element ) . ").sortable({" . $sort_options . "\n\t});";
455  }
456 
466  public function tablesorter($table = '', $options = '') {
467  $this->jquery_code_for_compile [] = "\t$(" . Javascript::prep_element ( $table ) . ").tablesorter($options);\n";
468  }
469 
479  public function condition($condition, $jsCodeIfTrue, $jsCodeIfFalse = null, $immediatly = false) {
480  $str = "if(" . $condition . "){" . $jsCodeIfTrue . "}";
481  if (isset ( $jsCodeIfFalse )) {
482  $str .= "else{" . $jsCodeIfFalse . "}";
483  }
484 
485  if ($immediatly)
486  $this->jquery_code_for_compile [] = $str;
487  return $str;
488  }
489 
501  private function _doJQuery($element, $jqueryCall, $param = "", $jsCallback = "", $immediatly = false) {
502  $param = Javascript::prep_value ( $param );
503  $callback = "";
504  if ($jsCallback != "")
505  $callback = ", function(event){\n{$jsCallback}\n}";
506  $script = "$(" . Javascript::prep_element ( $element ) . ")." . $jqueryCall . "(" . $param . $callback . ");\n";
507  if ($immediatly)
508  $this->jquery_code_for_compile [] = $script;
509  return $script;
510  }
511 
525  public function doJQuery($element, $jqueryCall, $param = "", $jsCallback = "") {
526  return $this->_doJQuery ( $element, $jqueryCall, $param, $jsCallback, true );
527  }
528 
542  public function doJQueryDeferred($element, $jqueryCall, $param = "", $jsCallback = "") {
543  return $this->_doJQuery ( $element, $jqueryCall, $param, $jsCallback, false );
544  }
545 
560  private function _doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param = "", $preventDefault = false, $stopPropagation = false, $jsCallback = "", $immediatly = true) {
561  return $this->_add_event ( $element, $this->_doJQuery ( $elementToModify, $jqueryCall, $param, $jsCallback ), $event, $preventDefault, $stopPropagation, $immediatly );
562  }
563 
575  public function doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param = "", $parameters = array ()) {
576  $jsCallback = "";
577  $stopPropagation = false;
578  $preventDefault = false;
579  $immediatly = true;
580  extract ( $parameters );
581  return $this->_doJQueryOn ( $event, $element, $elementToModify, $jqueryCall, $param, $preventDefault, $stopPropagation, $jsCallback, $immediatly );
582  }
583 
593  public function exec($js, $immediatly = false) {
594  $script = $js . "\n";
595  if ($immediatly)
596  $this->jquery_code_for_compile [] = $script;
597  return $script;
598  }
599 
609  public function execAtLast($js) {
610  $script = $js . "\n";
611  $this->jquery_code_for_compile_at_last [] = $script;
612  return $script;
613  }
614 
626  public function execOn($event, $element, $js, $parameters = array ()) {
627  $stopPropagation = false;
628  $preventDefault = false;
629  $immediatly = true;
630  extract ( $parameters );
631  $script = $this->_add_event ( $element, $this->exec ( $js ), $event, $preventDefault, $stopPropagation, $immediatly );
632  return $script;
633  }
634  public function setJsonToElement($json, $elementClass = "_element", $immediatly = true) {
635  $retour = "var data={$json};" . "\n\tdata=($.isPlainObject(data))?data:JSON.parse(data);" . "\n\tvar pk=data['pk'];var object=data['object'];" . "\n\tfor(var field in object){" . "\n\tif($('[data-field='+field+']',$('._element[data-ajax='+pk+']')).length){" . "\n\t\t$('[data-field='+field+']',$('._element[data-ajax='+pk+']')).each(function(){" . "\n\t\t\tif($(this).is('[value]')) { $(this).val(object[field]);} else { $(this).html(object[field]); }" . "\n\t});" . "\n}};\n";
636  $retour .= "\t$(document).trigger('jsonReady',[data]);\n";
637  return $this->exec ( $retour, $immediatly );
638  }
639 
648  public function setDraggable($element, $parameters = [ ]) {
649  $attr = "id";
650  $preventDefault = false;
651  $stopPropagation = false;
652  $immediatly = true;
653  extract ( $parameters );
654  $script = $this->_add_event ( $element, Javascript::draggable ( $attr ), "dragstart", $preventDefault, $stopPropagation, $immediatly );
655  return $script;
656  }
657 
668  public function asDropZone($element, $jsCallback = "", $parameters = [ ]) {
669  $stopPropagation = false;
670  $immediatly = true;
671  $jqueryDone = "append";
672  $script = $this->_add_event ( $element, '', "dragover", true, $stopPropagation, $immediatly );
673  extract ( $parameters );
674  $script .= $this->_add_event ( $element, Javascript::dropZone ( $jqueryDone, $jsCallback ), "drop", true, $stopPropagation, $immediatly );
675  return $script;
676  }
677 
691  public function interval($jsCode, $time, $globalName = null, $immediatly = true) {
692  if (! Javascript::isFunction ( $jsCode )) {
693  $jsCode = "function(){\n" . $jsCode . "\n}";
694  }
695  if (isset ( $globalName )) {
696  $script = "if(window.{$globalName}){clearInterval(window.{$globalName});}\nwindow.{$globalName}=setInterval({$jsCode},{$time});";
697  } else {
698  $script = "setInterval({$jsCode},{$time});";
699  }
700  return $this->exec ( $script, $immediatly );
701  }
702 
712  public function clearInterval($globalName, $immediatly = true) {
713  return $this->exec ( "if(window.{$globalName}){clearInterval(window.{$globalName});}", $immediatly );
714  }
715 
734  public function counter($counterSelector, $value = 0, $limit = 0, $globalName = null, $countDown = true, $immediatly = true) {
735  $stop = "";
736  if ($countDown) {
737  $stop = "if (--timer < " . $limit . ") {clearInterval(interval);display.trigger({type:'counter-end',value: timer,limit:" . $limit . "});}";
738  } else {
739  if ($limit != 0) {
740  $stop = "if (++timer > " . $limit . ") {clearInterval(interval);display.trigger({type:'counter-end',value: timer,limit:" . $limit . "});}";
741  }
742  }
743  $global = "";
744  if (isset ( $globalName )) {
745  $global = "\nwindow.{$globalName}=interval;";
746  }
747  $timer = "var startTimer=function(duration, display) {var timer = duration, days, hours, minutes, seconds;
748  var sh=function(disp,v){if(disp.is('[value]')){disp.val(v);} else {disp.html(v);};};
749  var shHide=function(v,k,kBefore){if(v==0 && display.find(k).closest('.timer').is(':visible') && (!kBefore || !display.find(kBefore).closest('.timer').is(':visible'))){display.find(k).closest('.timer').hide();}else{sh(display.find(k),v);}};
750  var pl=function(v,text){return (v>1)?v+' '+text+'s':(v>0)?v+' '+text:'';};
751  var d0=function(v){return v < 10 ? '0' + v : v;};
752  var shortSh=function(d,h,m,s){sh(display,pl(d,'day')+' '+[h,m,s].join(':'));};
753  var longSh=function(d,h,m,s){shHide(d,'.day');shHide(h,'.hour','.day');shHide(m,'.minute','.hour');shHide(s,'.second','.minute');};
754  var mainSh=(display.find('.hour').first().length)?longSh:shortSh;
755  display.trigger('counter-start',timer);
756  display.show();
757  var interval=setInterval(function () {
758  days = parseInt(timer / 86400, 10);
759  hours = d0(parseInt((timer%86400) / 3600, 10));
760  minutes = d0(parseInt((timer%3600) / 60, 10));
761  seconds = d0(parseInt(timer%60, 10));
762  mainSh(days,hours,minutes,seconds);
763  " . $stop . "
764  }, 1000);
765  " . $global . "
766  }";
767  $element = '$("' . $counterSelector . '")';
768  return $this->exec ( $timer . "\nstartTimer(" . $value . "," . $element . ");", $immediatly );
769  }
770 
790  public function counterOn($element, $event, $counterSelector, $value = 0, $limit = 0, $globalName = null, $countDown = true) {
791  return $this->execOn ( $event, $element, $this->counter ( $counterSelector, $value, $limit, $globalName, $countDown, false ) );
792  }
793 
805  public function activateLink($target, $property = 'href', $href = null) {
806  return $this->execAtLast ( $this->_activateLink ( $target, $property, $href ) );
807  }
808 
820  public function _activateLink($target, $property = 'href', $href = null) {
821  $js = '$("' . $target . ' [' . $property . ']").removeClass("active");';
822  if (isset ( $href )) {
823  $js .= 'var href="' . $href . '";';
824  } else {
825  $js .= 'var href=window.location.href;';
826  }
827  $js .= '$("' . $target . ' [' . $property . ']").each(function(){if(href.includes($(this).attr("' . $property . '"))) $(this).addClass("active");});';
828  return $js;
829  }
830 }
removeClass($element='this', $class='', $immediatly=false)
Execute a javascript library removeClass action.
append($to, $element, $immediatly=false)
Insert content, specified by the parameter $element, to the end of each element in the set of matched...
prepend($to, $element, $immediatly=false)
Insert content, specified by the parameter $element, to the beginning of each element in the set of m...
_add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false, $immediatly=true, $listenerOn=false)
counter($counterSelector, $value=0, $limit=0, $globalName=null, $countDown=true, $immediatly=true)
Associates a counter to the element designated by $counterSelector Triggers the events counter-start ...
toggleClass($element='this', $class='', $immediatly=false)
Execute a javascript library toggle class action.
_validate_speed($speed)
Ensures the speed parameter is valid for jQuery.
after($to, $element, $immediatly=false)
Insert content, specified by the parameter, after each element in the set of matched elements...
val($element='this', $value='', $immediatly=false)
Get or set the value of the first element in the set of matched elements or set one or more attribute...
addClass($element='this', $class='', $immediatly=false)
add class to element
_showHideWithEffect($action, $element='this', $speed='', $callback='', $immediatly=false)
show or hide with effect
static prep_element($element)
Puts HTML element in quotes for use in jQuery code unless the supplied element is the Javascript &#39;thi...
Definition: Javascript.php:48
_genericCallElement($jQueryCall, $to='this', $element='', $immediatly=false)
Execute a generic jQuery call with 2 elements.
static prep_value($value)
Puts HTML values in quotes for use in jQuery code unless the supplied value contains the Javascript &#39;...
Definition: Javascript.php:63
ajax($method, $url, $responseElement='', $parameters=[])
Performs an ajax request.
click($element='this', $js='', $ret_false=TRUE, $preventDefault=false, $stopPropagation=false, $listenerOn=false)
Outputs a javascript library click event.
_genericCallValue($jQueryCall, $element='this', $param="", $immediatly=false)
Execute a generic jQuery call with a value.
attr($element='this', $attributeName='id', $value="", $immediatly=false)
Get or set the value of an attribute for the first element in the set of matched elements or set one ...
before($to, $element, $immediatly=false)
Insert content, specified by the parameter, before each element in the set of matched elements...
animate($element='this', $params=array(), $speed='', $extra='', $immediatly=false)
Outputs a javascript library animate event.
html($element='this', $value='', $immediatly=false)
Get or set the html of an attribute for the first element in the set of matched elements.