66
77/**
88 * @author jc
9- * @property Ajax\JsUtils $js
9+ * @property array $jquery_code_for_compile
1010 */
1111trait 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 /**
0 commit comments