Skip to content

Commit 69044f7

Browse files
committed
JsUtils corrections
1 parent 63bca8d commit 69044f7

File tree

5 files changed

+76
-131
lines changed

5 files changed

+76
-131
lines changed

Ajax/JsUtils.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Ajax\common\traits\JsUtilsAjaxTrait;
1010
use Ajax\common\traits\JsUtilsInternalTrait;
1111
use Ajax\service\JArray;
12+
use Ajax\service\Javascript;
1213

1314
/**
1415
* JQuery PHP library
@@ -349,6 +350,35 @@ public function _prep_args($result, $is_key=FALSE) {
349350
}
350351
}
351352

353+
/**
354+
* Constructs the syntax for an event, and adds to into the array for compilation
355+
*
356+
* @param string $element The element to attach the event to
357+
* @param string $js The code to execute
358+
* @param string $event The event to pass
359+
* @param boolean $preventDefault If set to true, the default action of the event will not be triggered.
360+
* @param boolean $stopPropagation Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.
361+
* @return string
362+
*/
363+
public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true) {
364+
if (\is_array($js)) {
365+
$js=implode("\n\t\t", $js);
366+
}
367+
if ($preventDefault===true) {
368+
$js=Javascript::$preventDefault.$js;
369+
}
370+
if ($stopPropagation===true) {
371+
$js=Javascript::$stopPropagation.$js;
372+
}
373+
if (array_search($event, $this->jquery_events)===false)
374+
$event="\n\t$(".Javascript::prep_element($element).").bind('{$event}',function(event){\n\t\t{$js}\n\t});\n";
375+
else
376+
$event="\n\t$(".Javascript::prep_element($element).").{$event}(function(event){\n\t\t{$js}\n\t});\n";
377+
if($immediatly)
378+
$this->jquery_code_for_compile[]=$event;
379+
return $event;
380+
}
381+
352382
public function getInjected() {
353383
return $this->injected;
354384
}

Ajax/common/traits/JsUtilsActionsTrait.php

Lines changed: 34 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,32 @@
66

77
/**
88
* @author jc
9-
* @property Ajax\JsUtils $js
9+
* @property array $jquery_code_for_compile
1010
*/
1111
trait JsUtilsActionsTrait {
1212

13-
13+
abstract public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true);
14+
/**
15+
* show or hide with effect
16+
*
17+
* @param string $action
18+
* @param string - element
19+
* @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
20+
* @param string - Javascript callback function
21+
* @param boolean $immediatly defers the execution if set to false
22+
* @return string
23+
*/
24+
protected function _showHideWithEffect($action,$element='this', $speed='', $callback='', $immediatly=false) {
25+
$element=Javascript::prep_element($element);
26+
$speed=$this->_validate_speed($speed);
27+
if ($callback!='') {
28+
$callback=", function(){\n{$callback}\n}";
29+
}
30+
$str="$({$element}).{$action}({$speed}{$callback});";
31+
if ($immediatly)
32+
$this->jquery_code_for_compile[]=$str;
33+
return $str;
34+
}
1435
/**
1536
* Ensures the speed parameter is valid for jQuery
1637
* @param string|int $speed
@@ -205,18 +226,7 @@ public function prepend($to, $element, $immediatly=false) {
205226
* @return string
206227
*/
207228
public function fadeIn($element='this', $speed='', $callback='', $immediatly=false) {
208-
$element=Javascript::prep_element($element);
209-
$speed=$this->_validate_speed($speed);
210-
211-
if ($callback!='') {
212-
$callback=", function(){\n{$callback}\n}";
213-
}
214-
215-
$str="$({$element}).fadeIn({$speed}{$callback});";
216-
217-
if ($immediatly)
218-
$this->jquery_code_for_compile[]=$str;
219-
return $str;
229+
return $this->_showHideWithEffect("fadeIn",$element,$speed,$callback,$immediatly);
220230
}
221231

222232
/**
@@ -229,18 +239,7 @@ public function fadeIn($element='this', $speed='', $callback='', $immediatly=fal
229239
* @return string
230240
*/
231241
public function fadeOut($element='this', $speed='', $callback='', $immediatly=false) {
232-
$element=Javascript::prep_element($element);
233-
$speed=$this->_validate_speed($speed);
234-
235-
if ($callback!='') {
236-
$callback=", function(){\n{$callback}\n}";
237-
}
238-
239-
$str="$({$element}).fadeOut({$speed}{$callback});";
240-
241-
if ($immediatly)
242-
$this->jquery_code_for_compile[]=$str;
243-
return $str;
242+
return $this->_showHideWithEffect("fadeOut",$element,$speed,$callback,$immediatly);
244243
}
245244

246245
/**
@@ -253,18 +252,7 @@ public function fadeOut($element='this', $speed='', $callback='', $immediatly=fa
253252
* @return string
254253
*/
255254
public function slideUp($element='this', $speed='', $callback='', $immediatly=false) {
256-
$element=Javascript::prep_element($element);
257-
$speed=$this->_validate_speed($speed);
258-
259-
if ($callback!='') {
260-
$callback=", function(){\n{$callback}\n}";
261-
}
262-
263-
$str="$({$element}).slideUp({$speed}{$callback});";
264-
265-
if ($immediatly)
266-
$this->jquery_code_for_compile[]=$str;
267-
return $str;
255+
return $this->_showHideWithEffect("slideUp",$element,$speed,$callback,$immediatly);
268256
}
269257

270258
/**
@@ -289,18 +277,7 @@ public function removeClass($element='this', $class='', $immediatly=false) {
289277
* @return string
290278
*/
291279
public function slideDown($element='this', $speed='', $callback='', $immediatly=false) {
292-
$element=Javascript::prep_element($element);
293-
$speed=$this->_validate_speed($speed);
294-
295-
if ($callback!='') {
296-
$callback=", function(){\n{$callback}\n}";
297-
}
298-
299-
$str="$({$element}).slideDown({$speed}{$callback});";
300-
301-
if ($immediatly)
302-
$this->jquery_code_for_compile[]=$str;
303-
return $str;
280+
return $this->_showHideWithEffect("slideDown",$element,$speed,$callback,$immediatly);
304281
}
305282

306283
/**
@@ -313,18 +290,7 @@ public function slideDown($element='this', $speed='', $callback='', $immediatly=
313290
* @return string
314291
*/
315292
public function slideToggle($element='this', $speed='', $callback='', $immediatly=false) {
316-
$element=Javascript::prep_element($element);
317-
$speed=$this->_validate_speed($speed);
318-
319-
if ($callback!='') {
320-
$callback=", function(){\n{$callback}\n}";
321-
}
322-
323-
$str="$({$element}).slideToggle({$speed}{$callback});";
324-
325-
if ($immediatly)
326-
$this->jquery_code_for_compile[]=$str;
327-
return $str;
293+
return $this->_showHideWithEffect("slideToggle",$element,$speed,$callback,$immediatly);
328294
}
329295

330296
/**
@@ -337,34 +303,20 @@ public function slideToggle($element='this', $speed='', $callback='', $immediatl
337303
* @return string
338304
*/
339305
public function hide($element='this', $speed='', $callback='', $immediatly=false) {
340-
$element=Javascript::prep_element($element);
341-
$speed=$this->_validate_speed($speed);
342-
343-
if ($callback!='') {
344-
$callback=", function(){\n{$callback}\n}";
345-
}
346-
347-
$str="$({$element}).hide({$speed}{$callback});";
348-
349-
if ($immediatly)
350-
$this->jquery_code_for_compile[]=$str;
351-
return $str;
306+
return $this->_showHideWithEffect("hide",$element,$speed,$callback,$immediatly);
352307
}
353308

354309
/**
355310
* Execute a javascript library toggle action
356311
*
357312
* @param string - element
313+
* @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
314+
* @param string - Javascript callback function
358315
* @param boolean $immediatly defers the execution if set to false
359316
* @return string
360317
*/
361-
public function toggle($element='this', $immediatly=false) {
362-
$element=Javascript::prep_element($element);
363-
$str="$({$element}).toggle();";
364-
365-
if ($immediatly)
366-
$this->jquery_code_for_compile[]=$str;
367-
return $str;
318+
public function toggle($element='this', $speed='', $callback='', $immediatly=false) {
319+
return $this->_showHideWithEffect("toggle",$element,$speed,$callback,$immediatly);
368320
}
369321

370322
/**
@@ -403,18 +355,7 @@ public function trigger($element='this', $event='click', $immediatly=false) {
403355
* @return string
404356
*/
405357
public function show($element='this', $speed='', $callback='', $immediatly=false) {
406-
$element=Javascript::prep_element($element);
407-
$speed=$this->_validate_speed($speed);
408-
409-
if ($callback!='') {
410-
$callback=", function(){\n{$callback}\n}";
411-
}
412-
413-
$str="$({$element}).show({$speed}{$callback});";
414-
415-
if ($immediatly)
416-
$this->jquery_code_for_compile[]=$str;
417-
return $str;
358+
return $this->_showHideWithEffect("show",$element,$speed,$callback,$immediatly);
418359
}
419360

420361
/**

Ajax/common/traits/JsUtilsAjaxTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace Ajax\common\traits;
44

5-
use Ajax\service\JArray;
65
use Ajax\service\AjaxTransition;
76
use Ajax\service\Javascript;
87
use Ajax\service\JString;
98

109
/**
1110
* @author jc
11+
* @property array $jquery_code_for_compile
1212
*/
1313
trait JsUtilsAjaxTrait {
1414

@@ -325,8 +325,8 @@ public function getOn($event, $element, $url, $responseElement="", $parameters=a
325325
$immediatly=true;
326326
$jqueryDone="html";
327327
$ajaxTransition=null;
328+
$params="{}";
328329
extract($parameters);
329-
$params=JArray::getDefaultValue($parameters, "params", "{}");
330330
return $this->_add_event($element, $this->_get($url, $params,$responseElement,$jsCallback,$attr, $hasLoader,$jqueryDone,$ajaxTransition), $event, $preventDefault, $stopPropagation,$immediatly);
331331
}
332332

Ajax/common/traits/JsUtilsEventsTrait.php

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* @author jc
9+
* @property array $jquery_code_for_compile
910
*/
1011
trait JsUtilsEventsTrait {
1112

@@ -14,34 +15,7 @@ trait JsUtilsEventsTrait {
1415
"bind","blur","change","click","dblclick","delegate","die","error","focus","focusin","focusout","hover","keydown","keypress","keyup","live","load","mousedown","mousseenter","mouseleave","mousemove","mouseout","mouseover","mouseup","off","on","one","ready","resize","scroll","select","submit","toggle","trigger","triggerHandler","undind","undelegate","unload"
1516
);
1617

17-
/**
18-
* Constructs the syntax for an event, and adds to into the array for compilation
19-
*
20-
* @param string $element The element to attach the event to
21-
* @param string $js The code to execute
22-
* @param string $event The event to pass
23-
* @param boolean $preventDefault If set to true, the default action of the event will not be triggered.
24-
* @param boolean $stopPropagation Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.
25-
* @return string
26-
*/
27-
public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true) {
28-
if (\is_array($js)) {
29-
$js=implode("\n\t\t", $js);
30-
}
31-
if ($preventDefault===true) {
32-
$js=Javascript::$preventDefault.$js;
33-
}
34-
if ($stopPropagation===true) {
35-
$js=Javascript::$stopPropagation.$js;
36-
}
37-
if (array_search($event, $this->jquery_events)===false)
38-
$event="\n\t$(".Javascript::prep_element($element).").bind('{$event}',function(event){\n\t\t{$js}\n\t});\n";
39-
else
40-
$event="\n\t$(".Javascript::prep_element($element).").{$event}(function(event){\n\t\t{$js}\n\t});\n";
41-
if($immediatly)
42-
$this->jquery_code_for_compile[]=$event;
43-
return $event;
44-
}
18+
abstract public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true);
4519

4620
/**
4721
* Outputs a javascript library blur event
@@ -69,7 +43,7 @@ public function change($element='this', $js='') {
6943
* Outputs a javascript library click event
7044
*
7145
* @param string $element element to attach the event to
72-
* @param string $js code to execute
46+
* @param string|array $js code to execute
7347
* @param boolean $ret_false or not to return false
7448
* @return string
7549
*/

Ajax/common/traits/JsUtilsInternalTrait.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected function _addToCompile($jsScript) {
1414
* @param BaseGui $library
1515
* @param mixed $view
1616
*/
17-
private function _compileLibrary($library, &$view=NULL){
17+
protected function _compileLibrary($library, &$view=NULL){
1818
if ($library!=NULL) {
1919
if(isset($view))
2020
$library->compileHtml($this, $view);
@@ -24,19 +24,19 @@ private function _compileLibrary($library, &$view=NULL){
2424
}
2525
}
2626

27-
private function defer($script){
27+
protected function defer($script){
2828
$result="window.defer=function (method) {if (window.jQuery) method(); else setTimeout(function() { defer(method) }, 50);};";
2929
$result.="window.defer(function(){".$script."})";
3030
return $result;
3131
}
3232

33-
private function ready($script){
33+
protected function ready($script){
3434
$result='$(document).ready(function() {'."\n";
3535
$result.=$script.'})';
3636
return $result;
3737
}
3838

39-
private function minify($input) {
39+
protected function minify($input) {
4040
if(trim($input) === "") return $input;
4141
return preg_replace(
4242
array(
@@ -67,7 +67,7 @@ private function minify($input) {
6767
* @param string $src
6868
* @return string
6969
*/
70-
private function _open_script($src='') {
70+
protected function _open_script($src='') {
7171
$str='<script type="text/javascript" ';
7272
$str.=($src=='') ? '>' : ' src="'.$src.'">';
7373
return $str;
@@ -79,11 +79,11 @@ private function _open_script($src='') {
7979
* @param string $extra
8080
* @return string
8181
*/
82-
private function _close_script($extra="\n") {
82+
protected function _close_script($extra="\n") {
8383
return "</script>$extra";
8484
}
8585

86-
private function conflict() {
86+
protected function conflict() {
8787
$this->_addToCompile("var btn = $.fn.button.noConflict();$.fn.btn = btn;");
8888
}
8989

0 commit comments

Comments
 (0)