Completed
Push — master ( 30b23e...72108a )
by Jean-Christophe
03:13
created

Jquery::_ajax()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 13
nc 16
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Ajax;
4
5
use Ajax\service\PhalconUtils;
6
use Ajax\service\JString;
7
8
/**
9
 * JQuery Phalcon library
10
 *
11
 * @author jcheron
12
 * @version 1.002
13
 * @license Apache 2 http://www.apache.org/licenses/
14
 */
15
/**
16
 * jQuery Class
17
 */
18
class Jquery {
19
	protected $_di;
20
	protected $_ui;
21
	protected $_bootstrap;
22
	protected $libraryFile;
23
	protected $_javascript_folder='js';
24
	protected $jquery_code_for_load=array ();
25
	protected $jquery_code_for_compile=array ();
26
	protected $jquery_corner_active=FALSE;
27
	protected $jquery_table_sorter_active=FALSE;
28
	protected $jquery_table_sorter_pager_active=FALSE;
29
	protected $ajaxLoader='<span></span><span></span><span></span><span></span><span></span>';
30
	protected $jquery_events=array (
31
			"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"
32
	);
33
34
	public function setDi($di) {
35
		$this->_di=$di;
36
	}
37
38
	public function ui($ui=NULL) {
39
		if ($ui!==NULL) {
40
			$this->_ui=$ui;
41
		}
42
		return $this->_ui;
43
	}
44
45
	public function bootstrap($bootstrap=NULL) {
46
		if ($bootstrap!==NULL) {
47
			$this->_bootstrap=$bootstrap;
48
		}
49
		return $this->_bootstrap;
50
	}
51
52
	public function __construct() {
53
	}
54
55
	// --------------------------------------------------------------------
56
57
	/**
58
	 * Inline
59
	 *
60
	 * Outputs a <script> tag
61
	 *
62
	 * @access public
63
	 * @param string $script
64
	 * @param boolean $cdata a CDATA section should be added
65
	 * @return string
66
	 */
67
	public function inline($script, $cdata=TRUE) {
68
		$str=$this->_open_script();
69
		$str.=($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n";
70
		$str.=$this->_close_script();
71
72
		return $str;
73
	}
74
75
	// --------------------------------------------------------------------
76
77
	/**
78
	 * Open Script
79
	 *
80
	 * Outputs an opening <script>
81
	 *
82
	 * @access private
83
	 * @param string $src
84
	 * @return string
85
	 */
86
	private function _open_script($src='') {
87
		$str='<script type="text/javascript" ';
88
		$str.=($src=='') ? '>' : ' src="'.$src.'">';
89
		return $str;
90
	}
91
92
	// --------------------------------------------------------------------
93
94
	/**
95
	 * Close Script
96
	 *
97
	 * Outputs an closing </script>
98
	 *
99
	 * @param string
100
	 * @return string
101
	 */
102
	private function _close_script($extra="\n") {
103
		return "</script>{$extra}";
104
	}
105
106
	public function getLibraryScript() {
107
		$assets=$this->_di->get('assets');
108
		$assets->addJs($this->libraryFile);
109
		return $assets->outputJs();
110
	}
111
112
	public function setLibraryFile($name) {
113
		$this->libraryFile=$name;
114
	}
115
116
	public function _setAjaxLoader($loader) {
117
		$this->ajaxLoader=$loader;
118
	}
119
120
	// --------------------------------------------------------------------
121
	// Event Code
122
	// --------------------------------------------------------------------
123
124
	/**
125
	 * Blur
126
	 *
127
	 * Outputs a jQuery blur event
128
	 *
129
	 * @param string The element to attach the event to
130
	 * @param string The code to execute
131
	 * @return string
132
	 */
133
	public function _blur($element='this', $js='') {
134
		return $this->_add_event($element, $js, 'blur');
135
	}
136
137
	// --------------------------------------------------------------------
138
139
	/**
140
	 * Change
141
	 *
142
	 * Outputs a jQuery change event
143
	 *
144
	 * @param string The element to attach the event to
145
	 * @param string The code to execute
146
	 * @return string
147
	 */
148
	public function _change($element='this', $js='') {
149
		return $this->_add_event($element, $js, 'change');
150
	}
151
152
	// --------------------------------------------------------------------
153
154
	/**
155
	 * Outputs a jQuery click event
156
	 *
157
	 * @param string $element The element to attach the event to
158
	 * @param mixed $js The code to execute
159
	 * @param boolean $ret_false whether or not to return false
160
	 * @return string
161
	 */
162
	public function _click($element='this', $js=array(), $ret_false=TRUE) {
163
		if (!is_array($js)) {
164
			$js=array (
165
					$js
166
			);
167
		}
168
169
		if ($ret_false) {
170
			$js[]="return false;";
171
		}
172
173
		return $this->_add_event($element, $js, 'click');
174
	}
175
	
176
	/**
177
	 * Outputs a jQuery contextmenu event
178
	 *
179
	 * @param string The element to attach the event to
180
	 * @param string The code to execute
181
	 * @return string
182
	 */
183
	public function _contextmenu($element='this', $js='') {
184
		return $this->_add_event($element, $js, 'contextmenu');
185
	}
186
187
	// --------------------------------------------------------------------
188
189
	/**
190
	 * Outputs a jQuery dblclick event
191
	 *
192
	 * @param string The element to attach the event to
193
	 * @param string The code to execute
194
	 * @return string
195
	 */
196
	public function _dblclick($element='this', $js='') {
197
		return $this->_add_event($element, $js, 'dblclick');
198
	}
199
200
	// --------------------------------------------------------------------
201
202
	/**
203
	 * Outputs a jQuery error event
204
	 *
205
	 * @param string The element to attach the event to
206
	 * @param string The code to execute
207
	 * @return string
208
	 */
209
	public function _error($element='this', $js='') {
210
		return $this->_add_event($element, $js, 'error');
211
	}
212
213
	// --------------------------------------------------------------------
214
215
	/**
216
	 * Outputs a jQuery focus event
217
	 *
218
	 * @param string The element to attach the event to
219
	 * @param string The code to execute
220
	 * @return string
221
	 */
222
	public function _focus($element='this', $js='') {
223
		return $this->_add_event($element, $js, 'focus');
224
	}
225
226
	// --------------------------------------------------------------------
227
228
	/**
229
	 * Outputs a jQuery hover event
230
	 *
231
	 * @param string - element
232
	 * @param string - Javascript code for mouse over
233
	 * @param string - Javascript code for mouse out
234
	 * @return string
235
	 */
236
	public function _hover($element='this', $over, $out) {
237
		$event="\n\t$(".$this->_prep_element($element).").hover(\n\t\tfunction()\n\t\t{\n\t\t\t{$over}\n\t\t}, \n\t\tfunction()\n\t\t{\n\t\t\t{$out}\n\t\t});\n";
238
239
		$this->jquery_code_for_compile[]=$event;
240
241
		return $event;
242
	}
243
244
	// --------------------------------------------------------------------
245
246
	/**
247
	 * Outputs a jQuery keydown event
248
	 *
249
	 * @param string The element to attach the event to
250
	 * @param string The code to execute
251
	 * @return string
252
	 */
253
	public function _keydown($element='this', $js='') {
254
		return $this->_add_event($element, $js, 'keydown');
255
	}
256
257
	// --------------------------------------------------------------------
258
259
	/**
260
	 * Outputs a jQuery keypress event
261
	 *
262
	 * @param string The element to attach the event to
263
	 * @param string The code to execute
264
	 * @return string
265
	 */
266
	public function _keypress($element='this', $js='') {
267
		return $this->_add_event($element, $js, 'keypress');
268
	}
269
270
	// --------------------------------------------------------------------
271
272
	/**
273
	 * Outputs a jQuery keydown event
274
	 *
275
	 * @param string The element to attach the event to
276
	 * @param string The code to execute
277
	 * @return string
278
	 */
279
	public function _keyup($element='this', $js='') {
280
		return $this->_add_event($element, $js, 'keyup');
281
	}
282
283
	// --------------------------------------------------------------------
284
285
	/**
286
	 * Outputs a jQuery load event
287
	 *
288
	 * @param string The element to attach the event to
289
	 * @param string The code to execute
290
	 * @return string
291
	 */
292
	public function _load($element='this', $js='') {
293
		return $this->_add_event($element, $js, 'load');
294
	}
295
296
	// --------------------------------------------------------------------
297
298
	/**
299
	 * Outputs a jQuery mousedown event
300
	 *
301
	 * @param string The element to attach the event to
302
	 * @param string The code to execute
303
	 * @return string
304
	 */
305
	public function _mousedown($element='this', $js='') {
306
		return $this->_add_event($element, $js, 'mousedown');
307
	}
308
309
	// --------------------------------------------------------------------
310
311
	/**
312
	 * Outputs a jQuery mouseout event
313
	 *
314
	 * @param string The element to attach the event to
315
	 * @param string The code to execute
316
	 * @return string
317
	 */
318
	public function _mouseout($element='this', $js='') {
319
		return $this->_add_event($element, $js, 'mouseout');
320
	}
321
322
	// --------------------------------------------------------------------
323
324
	/**
325
	 * Outputs a jQuery mouseover event
326
	 *
327
	 * @param string The element to attach the event to
328
	 * @param string The code to execute
329
	 * @return string
330
	 */
331
	public function _mouseover($element='this', $js='') {
332
		return $this->_add_event($element, $js, 'mouseover');
333
	}
334
335
	// --------------------------------------------------------------------
336
337
	/**
338
	 * Outputs a jQuery mouseup event
339
	 *
340
	 * @param string The element to attach the event to
341
	 * @param string The code to execute
342
	 * @return string
343
	 */
344
	public function _mouseup($element='this', $js='') {
345
		return $this->_add_event($element, $js, 'mouseup');
346
	}
347
348
	// --------------------------------------------------------------------
349
350
	/**
351
	 * Outputs script directly
352
	 *
353
	 * @param string The element to attach the event to
354
	 * @param string The code to execute
355
	 * @return string
356
	 */
357
	public function _output($array_js='') {
358
		if (!is_array($array_js)) {
359
			$array_js=array (
360
					$array_js
361
			);
362
		}
363
364
		foreach ( $array_js as $js ) {
365
			$this->jquery_code_for_compile[]="\t$js\n";
366
		}
367
	}
368
369
	// --------------------------------------------------------------------
370
371
	/**
372
	 * Outputs a jQuery resize event
373
	 *
374
	 * @param string The element to attach the event to
375
	 * @param string The code to execute
376
	 * @return string
377
	 */
378
	public function _resize($element='this', $js='') {
379
		return $this->_add_event($element, $js, 'resize');
380
	}
381
382
	// --------------------------------------------------------------------
383
384
	/**
385
	 * Outputs a jQuery scroll event
386
	 *
387
	 * @param string The element to attach the event to
388
	 * @param string The code to execute
389
	 * @return string
390
	 */
391
	public function _scroll($element='this', $js='') {
392
		return $this->_add_event($element, $js, 'scroll');
393
	}
394
395
	// --------------------------------------------------------------------
396
397
	/**
398
	 * Outputs a jQuery unload event
399
	 *
400
	 * @param string The element to attach the event to
401
	 * @param string The code to execute
402
	 * @return string
403
	 */
404
	public function _unload($element='this', $js='') {
405
		return $this->_add_event($element, $js, 'unload');
406
	}
407
408
	// --------------------------------------------------------------------
409
	// Effects
410
	// --------------------------------------------------------------------
411
412
	/**
413
	 * Insert content, specified by the parameter, after each element in the set of matched elements
414
	 * @param string $element
415
	 * @param string $value
416
	 * @param boolean $immediatly defers the execution if set to false
417
	 * @return string
418
	 */
419
	public function after($element='this', $value='', $immediatly=false){
420
		$element=$this->_prep_element($element);
421
		$value=$this->_prep_value($value);
422
		$str="$({$element}).after({$value});";
423
		if ($immediatly)
424
			$this->jquery_code_for_compile[]=$str;
425
		return $str;
426
	}
427
	
428
	/**
429
	 * Get or set the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.
430
	 * @param string $element
431
	 * @param string $attributeName
432
	 * @param string $value
433
	 * @param boolean $immediatly delayed if false
434
	 */
435
	public function _attr($element='this', $attributeName, $value="", $immediatly=false) {
436
		$element=$this->_prep_element($element);
437
		if (isset($value)) {
438
			$value=$this->_prep_value($value);
439
			$str="$({$element}).attr(\"$attributeName\",{$value});";
440
		} else
441
			$str="$({$element}).attr(\"$attributeName\");";
442
		if ($immediatly)
443
			$this->jquery_code_for_compile[]=$str;
444
		return $str;
445
	}
446
447
	/**
448
	 * Execute a generic jQuery call with a value.
449
	 * @param string $jQueryCall
450
	 * @param string $element
451
	 * @param string $param
452
	 * @param boolean $immediatly delayed if false
453
	 */
454
	public function _genericCallValue($jQueryCall,$element='this', $param="", $immediatly=false) {
455
		$element=$this->_prep_element($element);
456
		if (isset($param)) {
457
			$param=$this->_prep_value($param);
458
			$str="$({$element}).{$jQueryCall}({$param});";
459
		} else
460
			$str="$({$element}).{$jQueryCall}();";
461
			if ($immediatly)
462
				$this->jquery_code_for_compile[]=$str;
463
			return $str;
464
	}
465
	/**
466
	 * Execute a generic jQuery call with 2 elements.
467
	 * @param string $jQueryCall
468
	 * @param string $to
469
	 * @param string $element
470
	 * @param boolean $immediatly delayed if false
471
	 * @return string
472
	 */
473
	public function _genericCallElement($jQueryCall,$to='this', $element, $immediatly=false) {
474
		$to=$this->_prep_element($to);
475
		$element=$this->_prep_element($element);
476
		$str="$({$to}).{$jQueryCall}({$element});";
477
		if ($immediatly)
478
			$this->jquery_code_for_compile[]=$str;
479
			return $str;
480
	}
481
	// --------------------------------------------------------------------
482
483
	/**
484
	 * Outputs a jQuery animate event
485
	 *
486
	 * @param string $element element
487
	 * @param string|array $params One of 'slow', 'normal', 'fast', or time in milliseconds
488
	 * @param string $speed
489
	 * @param string $extra
490
	 * @param boolean $immediatly delayed if false
491
	 * @return string
492
	 */
493
	public function _animate($element='this', $params=array(), $speed='', $extra='', $immediatly=false) {
494
		$element=$this->_prep_element($element);
495
		$speed=$this->_validate_speed($speed);
496
497
		$animations="\t\t\t";
498
		if (is_array($params)) {
499
			foreach ( $params as $param => $value ) {
500
				$animations.=$param.': \''.$value.'\', ';
501
			}
502
		}
503
		$animations=substr($animations, 0, -2); // remove the last ", "
504
505
		if ($speed!='') {
506
			$speed=', '.$speed;
507
		}
508
509
		if ($extra!='') {
510
			$extra=', '.$extra;
511
		}
512
513
		$str="$({$element}).animate({\n$animations\n\t\t}".$speed.$extra.");";
514
515
		if ($immediatly)
516
			$this->jquery_code_for_compile[]=$str;
517
		return $str;
518
	}
519
520
	// --------------------------------------------------------------------
521
522
	/**
523
	 * Outputs a jQuery hide event
524
	 *
525
	 * @param string $element element
526
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
527
	 * @param string $callback Javascript callback function
528
	 * @param boolean $immediatly delayed if false
529
	 * @return string
530
	 */
531
	public function _fadeIn($element='this', $speed='', $callback='', $immediatly=false) {
532
		$element=$this->_prep_element($element);
533
		$speed=$this->_validate_speed($speed);
534
535
		if ($callback!='') {
536
			$callback=", function(){\n{$callback}\n}";
537
		}
538
539
		$str="$({$element}).fadeIn({$speed}{$callback});";
540
541
		if ($immediatly)
542
			$this->jquery_code_for_compile[]=$str;
543
		return $str;
544
	}
545
546
	// --------------------------------------------------------------------
547
548
	/**
549
	 * Outputs a jQuery hide event
550
	 *
551
	 * @param string $element element
552
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
553
	 * @param string $callback Javascript callback function
554
	 * @param boolean $immediatly delayed if false
555
	 * @return string
556
	 */
557
	public function _fadeOut($element='this', $speed='', $callback='', $immediatly=false) {
558
		$element=$this->_prep_element($element);
559
		$speed=$this->_validate_speed($speed);
560
561
		if ($callback!='') {
562
			$callback=", function(){\n{$callback}\n}";
563
		}
564
565
		$str="$({$element}).fadeOut({$speed}{$callback});";
566
567
		if ($immediatly)
568
			$this->jquery_code_for_compile[]=$str;
569
		return $str;
570
	}
571
572
	// --------------------------------------------------------------------
573
574
	/**
575
	 * Outputs a jQuery hide action
576
	 *
577
	 * @param string $element element
578
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
579
	 * @param string $callback Javascript callback function
580
	 * @param boolean $immediatly delayed if false
581
	 * @return string
582
	 */
583
	public function _hide($element='this', $speed='', $callback='', $immediatly=false) {
584
		$element=$this->_prep_element($element);
585
		$speed=$this->_validate_speed($speed);
586
587
		if ($callback!='') {
588
			$callback=", function(){\n{$callback}\n}";
589
		}
590
591
		$str="$({$element}).hide({$speed}{$callback});";
592
593
		if ($immediatly)
594
			$this->jquery_code_for_compile[]=$str;
595
		return $str;
596
	}
597
598
	// --------------------------------------------------------------------
599
600
	// --------------------------------------------------------------------
601
602
	/**
603
	 * Outputs a jQuery slideUp event
604
	 *
605
	 * @param string $element element
606
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
607
	 * @param string $callback Javascript callback function
608
	 * @param boolean $immediatly delayed if false
609
	 * @return string
610
	 */
611
	public function _slideUp($element='this', $speed='', $callback='', $immediatly=false) {
612
		$element=$this->_prep_element($element);
613
		$speed=$this->_validate_speed($speed);
614
615
		if ($callback!='') {
616
			$callback=", function(){\n{$callback}\n}";
617
		}
618
619
		$str="$({$element}).slideUp({$speed}{$callback});";
620
621
		if ($immediatly)
622
			$this->jquery_code_for_compile[]=$str;
623
		return $str;
624
	}
625
626
	// --------------------------------------------------------------------
627
628
	/**
629
	 * Outputs a jQuery slideDown event
630
	 *
631
	 * @param string $element element
632
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
633
	 * @param string $callback Javascript callback function
634
	 * @param boolean $immediatly delayed if false
635
	 * @return string
636
	 */
637
	public function _slideDown($element='this', $speed='', $callback='', $immediatly=false) {
638
		$element=$this->_prep_element($element);
639
		$speed=$this->_validate_speed($speed);
640
641
		if ($callback!='') {
642
			$callback=", function(){\n{$callback}\n}";
643
		}
644
645
		$str="$({$element}).slideDown({$speed}{$callback});";
646
647
		if ($immediatly)
648
			$this->jquery_code_for_compile[]=$str;
649
		return $str;
650
	}
651
652
	// --------------------------------------------------------------------
653
654
	/**
655
	 * Outputs a jQuery slideToggle event
656
	 *
657
	 * @param string $element element
658
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
659
	 * @param string $callback Javascript callback function
660
	 * @param boolean $immediatly delayed if false
661
	 * @return string
662
	 */
663
	public function _slideToggle($element='this', $speed='', $callback='', $immediatly=false) {
664
		$element=$this->_prep_element($element);
665
		$speed=$this->_validate_speed($speed);
666
667
		if ($callback!='') {
668
			$callback=", function(){\n{$callback}\n}";
669
		}
670
671
		$str="$({$element}).slideToggle({$speed}{$callback});";
672
673
		if ($immediatly)
674
			$this->jquery_code_for_compile[]=$str;
675
		return $str;
676
	}
677
678
	// --------------------------------------------------------------------
679
680
	/**
681
	 * Outputs a jQuery toggle event
682
	 *
683
	 * @param string $element element
684
	 * @param boolean $immediatly delayed if false
685
	 * @return string
686
	 */
687
	public function _toggle($element='this', $immediatly=false) {
688
		$element=$this->_prep_element($element);
689
		$str="$({$element}).toggle();";
690
691
		if ($immediatly)
692
			$this->jquery_code_for_compile[]=$str;
693
		return $str;
694
	}
695
696
	// --------------------------------------------------------------------
697
698
	/**
699
	 * Execute all handlers and behaviors attached to the matched elements for the given event.
700
	 * @param string $element
701
	 * @param string $event
702
	 * @param boolean $immediatly delayed if false
703
	 */
704
	public function _trigger($element='this', $event='click', $immediatly=false) {
705
		$element=$this->_prep_element($element);
706
		$str="$({$element}).trigger(\"$event\");";
707
708
		if ($immediatly)
709
			$this->jquery_code_for_compile[]=$str;
710
		return $str;
711
	}
712
713
	// --------------------------------------------------------------------
714
715
	/**
716
	 * Outputs a jQuery show event
717
	 *
718
	 * @param string $element element
719
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
720
	 * @param string $callback Javascript callback function
721
	 * @param boolean $immediatly delayed if false
722
	 * @return string
723
	 */
724
	public function _show($element='this', $speed='', $callback='', $immediatly=false) {
725
		$element=$this->_prep_element($element);
726
		$speed=$this->_validate_speed($speed);
727
728
		if ($callback!='') {
729
			$callback=", function(){\n{$callback}\n}";
730
		}
731
732
		$str="$({$element}).show({$speed}{$callback});";
733
734
		if ($immediatly)
735
			$this->jquery_code_for_compile[]=$str;
736
		return $str;
737
	}
738
739
	/**
740
	 * Places a condition
741
	 * @param string $condition
742
	 * @param string $jsCodeIfTrue
743
	 * @param string $jsCodeIfFalse
744
	 * @param boolean $immediatly delayed if false
745
	 * @return string
746
	 */
747
	public function _condition($condition, $jsCodeIfTrue, $jsCodeIfFalse=null, $immediatly=false) {
748
		$str="if(".$condition."){".$jsCodeIfTrue."}";
749
		if (isset($jsCodeIfFalse)) {
750
			$str.="else{".$jsCodeIfFalse."}";
751
		}
752
753
		if ($immediatly)
754
			$this->jquery_code_for_compile[]=$str;
755
		return $str;
756
	}
757
758
	// --------------------------------------------------------------------
759
	// Plugins
760
	// --------------------------------------------------------------------
761
762
	/**
763
	 * Creates a jQuery sortable
764
	 *
765
	 * @param string $element
766
	 * @param array $options
767
	 * @return void
768
	 */
769
	public function sortable($element, $options=array()) {
770
		if (count($options)>0) {
771
			$sort_options=array ();
772
			foreach ( $options as $k => $v ) {
773
				$sort_options[]="\n\t\t".$k.': '.$v."";
774
			}
775
			$sort_options=implode(",", $sort_options);
776
		} else {
777
			$sort_options='';
778
		}
779
780
		return "$(".$this->_prep_element($element).").sortable({".$sort_options."\n\t});";
781
	}
782
783
	// --------------------------------------------------------------------
784
785
	/**
786
	 * Table Sorter Plugin
787
	 *
788
	 * @param string $table table name
789
	 * @param string $options plugin location
790
	 * @return string
791
	 */
792
	public function tablesorter($table='', $options='') {
793
		$this->jquery_code_for_compile[]="\t$(".$this->_prep_element($table).").tablesorter($options);\n";
794
	}
795
796
	// --------------------------------------------------------------------
797
	// Class functions
798
	// --------------------------------------------------------------------
799
800
	/**
801
	 * Constructs the syntax for an event, and adds to into the array for compilation
802
	 *
803
	 * @param string $element The element to attach the event to
804
	 * @param string $js The code to execute
805
	 * @param string $event The event to pass
806
	 * @param boolean $preventDefault If set to true, the default action of the event will not be triggered.
807
	 * @param boolean $stopPropagation Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.
808
	 * @return string
809
	 */
810
	public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false) {
811
		if (is_array($js)) {
812
			$js=implode("\n\t\t", $js);
813
		}
814
		if ($preventDefault===true) {
815
			$js="event.preventDefault();\n".$js;
816
		}
817
		if ($stopPropagation===true) {
818
			$js="event.stopPropagation();\n".$js;
819
		}
820
		if (array_search($event, $this->jquery_events)===false)
821
			$event="\n\t$(".$this->_prep_element($element).").bind('{$event}',function(event){\n\t\t{$js}\n\t});\n";
822
		else
823
			$event="\n\t$(".$this->_prep_element($element).").{$event}(function(event){\n\t\t{$js}\n\t});\n";
824
		$this->jquery_code_for_compile[]=$event;
825
		return $event;
826
	}
827
828
	// --------------------------------------------------------------------
829
830
	/**
831
	 * As events are specified, they are stored in an array
832
	 * This function compiles them all for output on a page
833
	 * @param view $view
834
	 * @param string $view_var
835
	 * @param boolean $script_tags
836
	 * @return string
837
	 */
838
	public function _compile($view=NULL, $view_var='script_foot', $script_tags=TRUE) {
839
		// Components UI
840
		$ui=$this->ui();
841
		if ($this->ui()!=NULL) {
842
			if ($ui->isAutoCompile()) {
843
				$ui->compile(true);
844
			}
845
		}
846
847
		// Components UI
848
		$bootstrap=$this->bootstrap();
849
		if ($this->bootstrap()!=NULL) {
850
			if ($bootstrap->isAutoCompile()) {
851
				$bootstrap->compile(true);
852
			}
853
		}
854
855
		// External references
856
		$external_scripts=implode('', $this->jquery_code_for_load);
857
		extract(array (
858
				'library_src' => $external_scripts
859
		));
860
861
		if (count($this->jquery_code_for_compile)==0) {
862
			// no inline references, let's just return
863
			return;
864
		}
865
866
		// Inline references
867
		$script='$(document).ready(function() {'."\n";
868
		$script.=implode('', $this->jquery_code_for_compile);
869
		$script.='});';
870
871
		$output=($script_tags===FALSE) ? $script : $this->inline($script);
872
873
		if ($view!=NULL)
874
			$view->setVar($view_var, $output);
875
		return $output;
876
	}
877
878
	public function _addToCompile($jsScript) {
879
		$this->jquery_code_for_compile[]=$jsScript;
880
	}
881
882
	// --------------------------------------------------------------------
883
884
	/**
885
	 * Clears the array of script events collected for output
886
	 *
887
	 * @return void
888
	 */
889
	public function _clear_compile() {
890
		$this->jquery_code_for_compile=array ();
891
	}
892
893
	// --------------------------------------------------------------------
894
895
	/**
896
	 * A wrapper for writing document.ready()
897
	 *
898
	 * @return string
899
	 */
900
	public function _document_ready($js) {
901
		if (!is_array($js)) {
902
			$js=array (
903
					$js
904
			);
905
		}
906
907
		foreach ( $js as $script ) {
908
			$this->jquery_code_for_compile[]=$script;
909
		}
910
	}
911
912
	// --------------------------------------------------------------------
913
914
	/**
915
	 * Puts HTML element in quotes for use in jQuery code
916
	 * unless the supplied element is the Javascript 'this'
917
	 * object, in which case no quotes are added
918
	 *
919
	 * @param string $element
920
	 * @return string
921
	 */
922
	public function _prep_element($element) {
923
		if (strrpos($element, 'this')===false&&strrpos($element, 'event')===false&&strrpos($element, 'self')===false) {
924
			$element='"'.addslashes($element).'"';
925
		}
926
		return $element;
927
	}
928
929
	/**
930
	 * Puts HTML values in quotes for use in jQuery code
931
	 * unless the supplied value contains the Javascript 'this' or 'event'
932
	 * object, in which case no quotes are added
933
	 *
934
	 * @param string $value
935
	 * @return string
936
	 */
937
	public function _prep_value($value) {
938
		if (is_array($value)) {
939
			$value=implode(",", $value);
940
		}
941
		if (strrpos($value, 'this')===false&&strrpos($value, 'event')===false&&strrpos($value, 'self')===false) {
942
			$value='"'.$value.'"';
943
		}
944
		return $value;
945
	}
946
947
	// --------------------------------------------------------------------
948
949
	/**
950
	 * Ensures the speed parameter is valid for jQuery
951
	 *
952
	 * @param string|int $speed
953
	 * @return string
954
	 */
955
	private function _validate_speed($speed) {
956
		if (in_array($speed, array (
957
				'slow','normal','fast'
958
		))) {
959
			$speed='"'.$speed.'"';
960
		} elseif (preg_match("/[^0-9]/", $speed)) {
961
			$speed='';
962
		}
963
964
		return $speed;
965
	}
966
	// ------------------------------------------------------------------------
967
	protected function addLoading(&$retour, $responseElement) {
968
		$loading_notifier='<div class="ajax-loader">';
969
		if ($this->ajaxLoader=='') {
970
			$loading_notifier.="Loading...";
971
		} else {
972
			$loading_notifier.=$this->ajaxLoader;
973
		}
974
		$loading_notifier.='</div>';
975
		$retour.="$({$responseElement}).empty();\n";
976
		$retour.="\t\t$({$responseElement}).prepend('{$loading_notifier}');\n";
977
	}
978
979
	public function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) {
980
		return $this->_ajax("get", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$immediatly);
981
	}
982
	public function _post($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) {
983
		return $this->_ajax("post", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$immediatly);
984
	}
985
986
	protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) {
987
		if(JString::isNull($params)){$params="{}";}
988
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
989
		$retour=$this->_getAjaxUrl($url, $attr);
990
		$responseElement=$this->_getResponseElement($responseElement);
991
		$retour.="var self=this;\n";
992
		if($hasLoader===true){
993
			$this->addLoading($retour, $responseElement);
994
		}
995
		$retour.="$.".$method."(url,".$params.").done(function( data ) {\n";
996
		$retour.=$this->_getOnAjaxDone($responseElement, $jsCallback)."});\n";
997
		if ($immediatly)
998
			$this->jquery_code_for_compile[]=$retour;
999
		return $retour;
1000
	}
1001
1002
	protected function _getAjaxUrl($url,$attr){
1003
		$url=$this->_correctAjaxUrl($url);
1004
		$retour="url='".$url."';\n";
1005
		$slash="/";
1006
		if(PhalconUtils::endsWith($url, "/"))
1007
			$slash="";
1008
		if(JString::isNotNull($attr)){
1009
			if ($attr=="value")
1010
				$retour.="url=url+'".$slash."'+$(this).val();\n";
1011
			else if($attr!=null && $attr!=="")
1012
				$retour.="url=url+'".$slash."'+$(this).attr('".$attr."');\n";
1013
		}
1014
		return $retour;
1015
	}
1016
1017
	protected function _getOnAjaxDone($responseElement,$jsCallback){
1018
		$retour="";
1019
		if ($responseElement!=="") {
1020
			$retour="\t$({$responseElement}).html( data );\n";
1021
		}
1022
		$retour.="\t".$jsCallback."\n";
1023
		return $retour;
1024
	}
1025
	
1026
	protected function _getResponseElement($responseElement){
1027
		if ($responseElement!=="") {
1028
			$responseElement=$this->_prep_value($responseElement);
1029
		}
1030
		return $responseElement;
1031
	}
1032
1033
	protected function _correctAjaxUrl($url) {
1034
		if (PhalconUtils::endsWith($url, "/"))
1035
			$url=substr($url, 0, strlen($url)-1);
1036
		if (strncmp($url, 'http://', 7)!=0&&strncmp($url, 'https://', 8)!=0) {
1037
			$url=$this->_di->get("url")->get($url);
1038
		}
1039
		return $url;
1040
	}
1041
1042
	/**
1043
	 * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name
1044
	 * @param string $url the request address
1045
	 * @param string $params Paramètres passés au format JSON
1046
	 * @param string $method Method use
1047
	 * @param string $jsCallback javascript code to execute after the request
1048
	 */
1049
	public function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) {
1050
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
1051
		$retour=$this->_getAjaxUrl($url, $attr);
1052
		$retour.="$.{$method}(url,".$params.").done(function( data ) {\n";
1053
		$retour.="\tdata=$.parseJSON(data);for(var key in data){"
1054
				."if($('#'+key,".$context.").length){ if($('#'+key,".$context.").is('[value]')) { $('#'+key,".$context.").val(data[key]);} else { $('#'+key,".$context.").html(data[key]); }}};\n";
1055
		$retour.="\t".$jsCallback."\n
1056
		});\n";
1057
		if ($immediatly)
1058
			$this->jquery_code_for_compile[]=$retour;
1059
		return $retour;
1060
	}
1061
	
1062
	/**
1063
	 * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element
1064
	 * @param string $element
1065
	 * @param string $event
1066
	 * @param string $url the request address
1067
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get")
1068
	 */
1069
	public function _jsonOn($event,$element, $url,$parameters=array()) {
1070
		$preventDefault=true;
1071
		$stopPropagation=true;
1072
		$jsCallback=null;
1073
		$attr="id";
1074
		$method="get";
1075
		$context="document";
1076
		$params="{}";
1077
		extract($parameters);
1078
		return $this->_add_event($element, $this->_json($url,$method, $params,$jsCallback, $attr,$context), $event, $preventDefault, $stopPropagation);
1079
	}
1080
1081
	/**
1082
	 * Makes an ajax request and receives a JSON array data types by copying and assigning them to the DOM elements with the same name
1083
	 * @param string $url the request address
1084
	 * @param string $params Paramètres passés au format JSON
1085
	 * @param string $method Method use
1086
	 * @param string $jsCallback javascript code to execute after the request
1087
	 * @param string $context jquery DOM element, array container.
1088
	 */
1089
	public function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context=null,$immediatly=false) {
1090
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
1091
		$retour=$this->_getAjaxUrl($url, $attr);
1092
		if($context===null){
1093
			$appendTo="\t\tnewElm.appendTo($('".$maskSelector."').parent());\n";
1094
			$newElm = "$('#'+newId)";
1095
		}else{
1096
			$appendTo="\t\tnewElm.appendTo(".$context.");\n";
1097
			$newElm = $context.".find('#'+newId)";
1098
		}
1099
		$retour.="var self = $(this);\n$.{$method}(url,".$params.").done(function( data ) {\n";
1100
		$retour.="\tdata=$.parseJSON(data);$.each(data, function(index, value) {\n"."\tvar created=false;var maskElm=$('".$maskSelector."').first();maskElm.hide();"."\tvar newId=(maskElm.attr('id') || 'mask')+'-'+index;"."\tvar newElm=".$newElm.";\n"."\tif(!newElm.length){\n"."\t\tnewElm=maskElm.clone();newElm.attr('id',newId);\n";
1101
		$retour.= $appendTo;
1102
		$retour.="\t}\n"."\tfor(var key in value){\n"."\t\t\tvar html = $('<div />').append($(newElm).clone()).html();\n"."\t\t\tif(html.indexOf('[['+key+']]')>-1){\n"."\t\t\t\tcontent=$(html.split('[['+key+']]').join(value[key]));\n"."\t\t\t\t$(newElm).replaceWith(content);newElm=content;\n"."\t\t\t}\n"."\t\tvar sel='[data-id=\"'+key+'\"]';if($(sel,newElm).length){\n"."\t\t\tvar selElm=$(sel,newElm);\n"."\t\t\t if(selElm.is('[value]')) { selElm.attr('value',value[key]);selElm.val(value[key]);} else { selElm.html(value[key]); }\n"."\t\t}\n"."}\n"."\t$(newElm).show(true);"."\n"."\t$(newElm).removeClass('hide');"."});\n";
1103
		$retour.="\t$(document).trigger('jsonReady',[data]);\n"
1104
		$retour.="\t".$jsCallback."\n"."});\n";
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE
Loading history...
1105
		if ($immediatly)
1106
			$this->jquery_code_for_compile[]=$retour;
1107
		return $retour;
1108
	}
1109
	/**
1110
	 * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element
1111
	 * @param string $element
1112
	 * @param string $event
1113
	 * @param string $url the request address
1114
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get", "context"=>null)
1115
	 */
1116
	public function _jsonArrayOn($event,$element, $maskSelector,$url,$parameters=array()) {
1117
		$preventDefault=true;
1118
		$stopPropagation=true;
1119
		$jsCallback=null;
1120
		$attr="id";
1121
		$method="get";
1122
		$context = null;
1123
		$params="{}";
1124
		extract($parameters);
1125
		return $this->_add_event($element, $this->_jsonArray($maskSelector,$url,$method, $params,$jsCallback, $attr, $context), $event, $preventDefault, $stopPropagation);
1126
	}
1127
	
1128
	public function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) {
1129
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
1130
		$retour=$this->_getAjaxUrl($url, $attr);
1131
		$retour.="\nvar params=$('#".$form."').serialize();\n";
1132
		$responseElement=$this->_getResponseElement($responseElement);
1133
		$retour.="var self=this;\n";
1134
		if($hasLoader===true){
1135
			$this->addLoading($retour, $responseElement);
1136
		}
1137
		$retour.="$.post(url,params).done(function( data ) {\n";
1138
		$retour.=$this->_getOnAjaxDone($responseElement, $jsCallback)."});\n";
1139
1140
		if ($validation) {
1141
			$retour="$('#".$form."').validate({submitHandler: function(form) {
1142
			".$retour."
1143
			}});\n";
1144
			$retour.="$('#".$form."').submit();\n";
1145
		}
1146
		if ($immediatly)
1147
			$this->jquery_code_for_compile[]=$retour;
1148
		return $retour;
1149
	}
1150
1151
	/**
1152
	 * Effectue un get vers $url sur l'évènement $event de $element en passant les paramètres $params
1153
	 * puis affiche le résultat dans $responseElement
1154
	 * @param string $element
1155
	 * @param string $event
1156
	 * @param string $url
1157
	 * @param string $params queryString parameters (JSON format). default : {}
1158
	 * @param string $responseElement
1159
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true)
1160
	 */
1161
	public function _getOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) {
1162
		$preventDefault=true;
1163
		$stopPropagation=true;
1164
		$jsCallback=null;
1165
		$attr="id";
1166
		$hasLoader=true;
1167
		extract($parameters);
1168
		return $this->_add_event($element, $this->_get($url, $params, $responseElement, $jsCallback, $attr,$hasLoader), $event, $preventDefault, $stopPropagation);
1169
	}
1170
1171
	/**
1172
	 * Effectue un post vers $url sur l'évènement $event de $element en passant les paramètres $params
1173
	 * puis affiche le résultat dans $responseElement
1174
	 * @param string $element
1175
	 * @param string $event
1176
	 * @param string $url
1177
	 * @param string $params queryString parameters (JSON format). default : {}
1178
	 * @param string $responseElement
1179
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true)
1180
	 */
1181
	public function _postOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) {
1182
		$preventDefault=true;
1183
		$stopPropagation=true;
1184
		$jsCallback=null;
1185
		$attr="id";
1186
		$hasLoader=true;
1187
		extract($parameters);
1188
		return $this->_add_event($element, $this->_post($url, $params, $responseElement, $jsCallback, $attr,$hasLoader), $event, $preventDefault, $stopPropagation);
1189
	}
1190
1191
	/**
1192
	 * Effectue un post vers $url sur l'évènement $event de $element en passant les paramètres du formulaire $form
1193
	 * puis affiche le résultat dans $responseElement
1194
	 * @param string $element
1195
	 * @param string $event
1196
	 * @param string $url
1197
	 * @param string $form
1198
	 * @param string $responseElement
1199
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true)
1200
	 */
1201
	public function _postFormOn($event,$element, $url, $form, $responseElement="", $parameters=array()) {
1202
		$preventDefault=true;
1203
		$stopPropagation=true;
1204
		$validation=false;
1205
		$jsCallback=null;
1206
		$attr="id";
1207
		$hasLoader=true;
1208
		extract($parameters);
1209
		return $this->_add_event($element, $this->_postForm($url, $form, $responseElement, $validation, $jsCallback, $attr,$hasLoader), $event, $preventDefault, $stopPropagation);
1210
	}
1211
1212
	/**
1213
	 * Call the JQuery method $jqueryCall on $element with parameters $param
1214
	 * @param string $element
1215
	 * @param string $jqueryCall
1216
	 * @param mixed $param
1217
	 * @param string $jsCallback javascript code to execute after the jquery call
1218
	 * @return string
1219
	 */
1220
	public function _doJQuery($element, $jqueryCall, $param="", $jsCallback="", $immediatly=false) {
1221
		$param=$this->_prep_value($param);
1222
		$callback="";
1223
		if ($jsCallback!="")
1224
			$callback=", function(event){\n{$jsCallback}\n}";
1225
		$script="$(".$this->_prep_element($element).").".$jqueryCall."(".$param.$callback.");\n";
1226
		if ($immediatly)
1227
			$this->jquery_code_for_compile[]=$script;
1228
		return $script;
1229
	}
1230
1231
	/**
1232
	 *
1233
	 * @param string $event
1234
	 * @param string $element
1235
	 * @param string $elementToModify
1236
	 * @param string $jqueryCall
1237
	 * @param string|array $param
1238
	 * @param boolean $preventDefault
1239
	 * @param boolean $stopPropagation
1240
	 * @param string $jsCallback javascript code to execute after the jquery call
1241
	 * @return string
1242
	 */
1243
	public function _doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param="", $preventDefault=false, $stopPropagation=false, $jsCallback="") {
1244
		return $this->_add_event($element, $this->_doJQuery($elementToModify, $jqueryCall, $param, $jsCallback), $event, $preventDefault, $stopPropagation);
1245
	}
1246
1247
	/**
1248
	 * Execute the code $js
1249
	 * @param string $js Code to execute
1250
	 * @param boolean $immediatly diffère l'exécution si false
1251
	 * @return String
1252
	 */
1253
	public function _exec($js, $immediatly=false) {
1254
		$script=$js."\n";
1255
		if ($immediatly)
1256
			$this->jquery_code_for_compile[]=$script;
1257
		return $script;
1258
	}
1259
1260
	/**
1261
	 *
1262
	 * @param string $element
1263
	 * @param string $event
1264
	 * @param string $js Code to execute
1265
	 * @param boolean $preventDefault
1266
	 * @param boolean $stopPropagation
1267
	 * @return String
1268
	 */
1269
	public function _execOn($element, $event, $js, $preventDefault=false, $stopPropagation=false) {
1270
		return $this->_add_event($element, $this->_exec($js), $event, $preventDefault, $stopPropagation);
1271
	}
1272
}
1273
/* End of file Jquery.php */
1274