blob: 4b8f76a106064097c93b55eefa615e54dfac655f [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Jones811f4752010-03-02 18:13:59 -06002/**
3 * CodeIgniter
4 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05005 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
Derek Jones811f4752010-03-02 18:13:59 -060018 *
19 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
23 * @link http://codeigniter.com
Derek Jones811f4752010-03-02 18:13:59 -060024 * @since Version 1.0
25 * @filesource
26 */
27
Derek Jonesf4a4bd82011-10-20 12:18:42 -050028// ------------------------------------------------------------------------
29
Derek Jones811f4752010-03-02 18:13:59 -060030/**
31 * Jquery Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
Derek Jones811f4752010-03-02 18:13:59 -060035 * @category Loader
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
37 * @link http://codeigniter.com/user_guide/libraries/javascript.html
Derek Jones811f4752010-03-02 18:13:59 -060038 */
Derek Jones37f4b9c2011-07-01 17:56:50 -050039
Greg Akeraacfe482010-03-03 12:26:31 -060040class CI_Jquery extends CI_Javascript {
Derek Jones811f4752010-03-02 18:13:59 -060041
42 var $_javascript_folder = 'js';
43 var $jquery_code_for_load = array();
44 var $jquery_code_for_compile = array();
45 var $jquery_corner_active = FALSE;
46 var $jquery_table_sorter_active = FALSE;
47 var $jquery_table_sorter_pager_active = FALSE;
48 var $jquery_ajax_img = '';
49
Greg Akera9263282010-11-10 15:26:43 -060050 public function __construct($params)
Derek Jones811f4752010-03-02 18:13:59 -060051 {
Derek Jones37f4b9c2011-07-01 17:56:50 -050052 $this->CI =& get_instance();
Derek Jones811f4752010-03-02 18:13:59 -060053 extract($params);
54
55 if ($autoload === TRUE)
56 {
Derek Jones37f4b9c2011-07-01 17:56:50 -050057 $this->script();
Derek Jones811f4752010-03-02 18:13:59 -060058 }
Derek Jones37f4b9c2011-07-01 17:56:50 -050059
Derek Jones811f4752010-03-02 18:13:59 -060060 log_message('debug', "Jquery Class Initialized");
61 }
Derek Jones37f4b9c2011-07-01 17:56:50 -050062
63 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -060064 // Event Code
Derek Jones37f4b9c2011-07-01 17:56:50 -050065 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -060066
67 /**
68 * Blur
69 *
70 * Outputs a jQuery blur event
71 *
72 * @access private
73 * @param string The element to attach the event to
74 * @param string The code to execute
75 * @return string
76 */
77 function _blur($element = 'this', $js = '')
78 {
79 return $this->_add_event($element, $js, 'blur');
80 }
Derek Jones37f4b9c2011-07-01 17:56:50 -050081
Derek Jones811f4752010-03-02 18:13:59 -060082 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050083
Derek Jones811f4752010-03-02 18:13:59 -060084 /**
85 * Change
86 *
87 * Outputs a jQuery change event
88 *
89 * @access private
90 * @param string The element to attach the event to
91 * @param string The code to execute
92 * @return string
93 */
94 function _change($element = 'this', $js = '')
95 {
96 return $this->_add_event($element, $js, 'change');
97 }
Derek Jones37f4b9c2011-07-01 17:56:50 -050098
Derek Jones811f4752010-03-02 18:13:59 -060099 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500100
Derek Jones811f4752010-03-02 18:13:59 -0600101 /**
102 * Click
103 *
104 * Outputs a jQuery click event
105 *
106 * @access private
107 * @param string The element to attach the event to
108 * @param string The code to execute
109 * @param boolean whether or not to return false
110 * @return string
111 */
112 function _click($element = 'this', $js = '', $ret_false = TRUE)
113 {
114 if ( ! is_array($js))
115 {
116 $js = array($js);
117 }
118
119 if ($ret_false)
120 {
121 $js[] = "return false;";
122 }
123
124 return $this->_add_event($element, $js, 'click');
125 }
126
127 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500128
Derek Jones811f4752010-03-02 18:13:59 -0600129 /**
130 * Double Click
131 *
132 * Outputs a jQuery dblclick event
133 *
134 * @access private
135 * @param string The element to attach the event to
136 * @param string The code to execute
137 * @return string
138 */
139 function _dblclick($element = 'this', $js = '')
140 {
141 return $this->_add_event($element, $js, 'dblclick');
142 }
143
144 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500145
Derek Jones811f4752010-03-02 18:13:59 -0600146 /**
147 * Error
148 *
149 * Outputs a jQuery error event
150 *
151 * @access private
152 * @param string The element to attach the event to
153 * @param string The code to execute
154 * @return string
155 */
156 function _error($element = 'this', $js = '')
157 {
158 return $this->_add_event($element, $js, 'error');
159 }
160
161 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500162
Derek Jones811f4752010-03-02 18:13:59 -0600163 /**
164 * Focus
165 *
166 * Outputs a jQuery focus event
167 *
168 * @access private
169 * @param string The element to attach the event to
170 * @param string The code to execute
171 * @return string
172 */
173 function _focus($element = 'this', $js = '')
174 {
175 return $this->_add_event($element, $js, 'focus');
176 }
177
178 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500179
Derek Jones811f4752010-03-02 18:13:59 -0600180 /**
181 * Hover
182 *
183 * Outputs a jQuery hover event
184 *
185 * @access private
186 * @param string - element
187 * @param string - Javascript code for mouse over
188 * @param string - Javascript code for mouse out
Barry Mienydd671972010-10-04 16:33:58 +0200189 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600190 */
191 function _hover($element = 'this', $over, $out)
192 {
193 $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";
194
195 $this->jquery_code_for_compile[] = $event;
196
197 return $event;
198 }
199
200 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500201
Derek Jones811f4752010-03-02 18:13:59 -0600202 /**
203 * Keydown
204 *
205 * Outputs a jQuery keydown event
206 *
207 * @access private
208 * @param string The element to attach the event to
209 * @param string The code to execute
210 * @return string
211 */
212 function _keydown($element = 'this', $js = '')
213 {
214 return $this->_add_event($element, $js, 'keydown');
215 }
216
217 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500218
Derek Jones811f4752010-03-02 18:13:59 -0600219 /**
220 * Keyup
221 *
222 * Outputs a jQuery keydown event
223 *
224 * @access private
225 * @param string The element to attach the event to
226 * @param string The code to execute
227 * @return string
228 */
229 function _keyup($element = 'this', $js = '')
230 {
231 return $this->_add_event($element, $js, 'keyup');
Derek Jones37f4b9c2011-07-01 17:56:50 -0500232 }
Derek Jones811f4752010-03-02 18:13:59 -0600233
234 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500235
Derek Jones811f4752010-03-02 18:13:59 -0600236 /**
237 * Load
238 *
239 * Outputs a jQuery load event
240 *
241 * @access private
242 * @param string The element to attach the event to
243 * @param string The code to execute
244 * @return string
245 */
246 function _load($element = 'this', $js = '')
247 {
248 return $this->_add_event($element, $js, 'load');
Derek Jones37f4b9c2011-07-01 17:56:50 -0500249 }
250
Derek Jones811f4752010-03-02 18:13:59 -0600251 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500252
Derek Jones811f4752010-03-02 18:13:59 -0600253 /**
254 * Mousedown
255 *
256 * Outputs a jQuery mousedown event
257 *
258 * @access private
259 * @param string The element to attach the event to
260 * @param string The code to execute
261 * @return string
262 */
263 function _mousedown($element = 'this', $js = '')
264 {
265 return $this->_add_event($element, $js, 'mousedown');
266 }
267
268 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500269
Derek Jones811f4752010-03-02 18:13:59 -0600270 /**
271 * Mouse Out
272 *
273 * Outputs a jQuery mouseout event
274 *
275 * @access private
276 * @param string The element to attach the event to
277 * @param string The code to execute
278 * @return string
279 */
280 function _mouseout($element = 'this', $js = '')
281 {
282 return $this->_add_event($element, $js, 'mouseout');
283 }
284
285 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500286
Derek Jones811f4752010-03-02 18:13:59 -0600287 /**
288 * Mouse Over
289 *
290 * Outputs a jQuery mouseover event
291 *
292 * @access private
293 * @param string The element to attach the event to
294 * @param string The code to execute
295 * @return string
296 */
297 function _mouseover($element = 'this', $js = '')
298 {
299 return $this->_add_event($element, $js, 'mouseover');
300 }
301
302 // --------------------------------------------------------------------
303
304 /**
305 * Mouseup
306 *
307 * Outputs a jQuery mouseup event
308 *
309 * @access private
310 * @param string The element to attach the event to
311 * @param string The code to execute
312 * @return string
313 */
314 function _mouseup($element = 'this', $js = '')
315 {
316 return $this->_add_event($element, $js, 'mouseup');
317 }
318
319 // --------------------------------------------------------------------
320
321 /**
322 * Output
323 *
324 * Outputs script directly
325 *
326 * @access private
327 * @param string The element to attach the event to
328 * @param string The code to execute
329 * @return string
330 */
331 function _output($array_js = '')
332 {
333 if ( ! is_array($array_js))
334 {
335 $array_js = array($array_js);
336 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500337
Derek Jones811f4752010-03-02 18:13:59 -0600338 foreach ($array_js as $js)
339 {
340 $this->jquery_code_for_compile[] = "\t$js\n";
341 }
342 }
343
344 // --------------------------------------------------------------------
345
346 /**
347 * Resize
348 *
349 * Outputs a jQuery resize event
350 *
351 * @access private
352 * @param string The element to attach the event to
353 * @param string The code to execute
354 * @return string
355 */
356 function _resize($element = 'this', $js = '')
357 {
358 return $this->_add_event($element, $js, 'resize');
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * Scroll
365 *
366 * Outputs a jQuery scroll event
367 *
368 * @access private
369 * @param string The element to attach the event to
370 * @param string The code to execute
371 * @return string
372 */
373 function _scroll($element = 'this', $js = '')
374 {
375 return $this->_add_event($element, $js, 'scroll');
376 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500377
Derek Jones811f4752010-03-02 18:13:59 -0600378 // --------------------------------------------------------------------
379
380 /**
381 * Unload
382 *
383 * Outputs a jQuery unload event
384 *
385 * @access private
386 * @param string The element to attach the event to
387 * @param string The code to execute
388 * @return string
389 */
390 function _unload($element = 'this', $js = '')
391 {
392 return $this->_add_event($element, $js, 'unload');
393 }
394
Derek Jones37f4b9c2011-07-01 17:56:50 -0500395 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600396 // Effects
Derek Jones37f4b9c2011-07-01 17:56:50 -0500397 // --------------------------------------------------------------------
398
Derek Jones811f4752010-03-02 18:13:59 -0600399 /**
400 * Add Class
401 *
402 * Outputs a jQuery addClass event
403 *
404 * @access private
405 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200406 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600407 */
408 function _addClass($element = 'this', $class='')
409 {
410 $element = $this->_prep_element($element);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500411 $str = "$({$element}).addClass(\"$class\");";
Derek Jones811f4752010-03-02 18:13:59 -0600412 return $str;
413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Animate
419 *
420 * Outputs a jQuery animate event
421 *
422 * @access private
423 * @param string - element
424 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
425 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200426 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600427 */
428 function _animate($element = 'this', $params = array(), $speed = '', $extra = '')
429 {
430 $element = $this->_prep_element($element);
431 $speed = $this->_validate_speed($speed);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500432
Derek Jones811f4752010-03-02 18:13:59 -0600433 $animations = "\t\t\t";
Derek Jones37f4b9c2011-07-01 17:56:50 -0500434
Derek Jones811f4752010-03-02 18:13:59 -0600435 foreach ($params as $param=>$value)
436 {
437 $animations .= $param.': \''.$value.'\', ';
438 }
439
440 $animations = substr($animations, 0, -2); // remove the last ", "
441
442 if ($speed != '')
443 {
444 $speed = ', '.$speed;
445 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500446
Derek Jones811f4752010-03-02 18:13:59 -0600447 if ($extra != '')
448 {
449 $extra = ', '.$extra;
450 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500451
452 $str = "$({$element}).animate({\n$animations\n\t\t}".$speed.$extra.");";
453
Derek Jones811f4752010-03-02 18:13:59 -0600454 return $str;
455 }
456
457 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500458
Derek Jones811f4752010-03-02 18:13:59 -0600459 /**
460 * Fade In
461 *
462 * Outputs a jQuery hide event
463 *
464 * @access private
465 * @param string - element
466 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
467 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200468 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600469 */
470 function _fadeIn($element = 'this', $speed = '', $callback = '')
471 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500472 $element = $this->_prep_element($element);
Derek Jones811f4752010-03-02 18:13:59 -0600473 $speed = $this->_validate_speed($speed);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500474
Derek Jones811f4752010-03-02 18:13:59 -0600475 if ($callback != '')
476 {
477 $callback = ", function(){\n{$callback}\n}";
478 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500479
480 $str = "$({$element}).fadeIn({$speed}{$callback});";
481
Derek Jones811f4752010-03-02 18:13:59 -0600482 return $str;
483 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500484
Derek Jones811f4752010-03-02 18:13:59 -0600485 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500486
Derek Jones811f4752010-03-02 18:13:59 -0600487 /**
488 * Fade Out
489 *
490 * Outputs a jQuery hide event
491 *
492 * @access private
493 * @param string - element
494 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
495 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200496 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600497 */
498 function _fadeOut($element = 'this', $speed = '', $callback = '')
499 {
500 $element = $this->_prep_element($element);
501 $speed = $this->_validate_speed($speed);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500502
Derek Jones811f4752010-03-02 18:13:59 -0600503 if ($callback != '')
504 {
505 $callback = ", function(){\n{$callback}\n}";
506 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500507
508 $str = "$({$element}).fadeOut({$speed}{$callback});";
509
Derek Jones811f4752010-03-02 18:13:59 -0600510 return $str;
511 }
512
513 // --------------------------------------------------------------------
514
515 /**
516 * Hide
517 *
518 * Outputs a jQuery hide action
519 *
520 * @access private
521 * @param string - element
522 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
523 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200524 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600525 */
526 function _hide($element = 'this', $speed = '', $callback = '')
527 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500528 $element = $this->_prep_element($element);
Derek Jones811f4752010-03-02 18:13:59 -0600529 $speed = $this->_validate_speed($speed);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500530
Derek Jones811f4752010-03-02 18:13:59 -0600531 if ($callback != '')
532 {
533 $callback = ", function(){\n{$callback}\n}";
534 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500535
536 $str = "$({$element}).hide({$speed}{$callback});";
Derek Jones811f4752010-03-02 18:13:59 -0600537
538 return $str;
539 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500540
Derek Jones811f4752010-03-02 18:13:59 -0600541 // --------------------------------------------------------------------
542
543 /**
544 * Remove Class
545 *
546 * Outputs a jQuery remove class event
547 *
548 * @access private
549 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200550 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600551 */
552 function _removeClass($element = 'this', $class='')
553 {
554 $element = $this->_prep_element($element);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500555 $str = "$({$element}).removeClass(\"$class\");";
Derek Jones811f4752010-03-02 18:13:59 -0600556 return $str;
557 }
558
559 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500560
Derek Jones811f4752010-03-02 18:13:59 -0600561 /**
562 * Slide Up
563 *
564 * Outputs a jQuery slideUp event
565 *
566 * @access private
567 * @param string - element
568 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
569 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200570 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600571 */
572 function _slideUp($element = 'this', $speed = '', $callback = '')
573 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500574 $element = $this->_prep_element($element);
Derek Jones811f4752010-03-02 18:13:59 -0600575 $speed = $this->_validate_speed($speed);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500576
Derek Jones811f4752010-03-02 18:13:59 -0600577 if ($callback != '')
578 {
579 $callback = ", function(){\n{$callback}\n}";
580 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500581
582 $str = "$({$element}).slideUp({$speed}{$callback});";
583
Derek Jones811f4752010-03-02 18:13:59 -0600584 return $str;
585 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500586
Derek Jones811f4752010-03-02 18:13:59 -0600587 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500588
Derek Jones811f4752010-03-02 18:13:59 -0600589 /**
590 * Slide Down
591 *
592 * Outputs a jQuery slideDown event
593 *
594 * @access private
595 * @param string - element
596 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
597 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200598 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600599 */
600 function _slideDown($element = 'this', $speed = '', $callback = '')
601 {
602 $element = $this->_prep_element($element);
603 $speed = $this->_validate_speed($speed);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500604
Derek Jones811f4752010-03-02 18:13:59 -0600605 if ($callback != '')
606 {
607 $callback = ", function(){\n{$callback}\n}";
608 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500609
610 $str = "$({$element}).slideDown({$speed}{$callback});";
611
Derek Jones811f4752010-03-02 18:13:59 -0600612 return $str;
613 }
614
615 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500616
Derek Jones811f4752010-03-02 18:13:59 -0600617 /**
618 * Slide Toggle
619 *
620 * Outputs a jQuery slideToggle event
621 *
622 * @access public
623 * @param string - element
624 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
625 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200626 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600627 */
628 function _slideToggle($element = 'this', $speed = '', $callback = '')
629 {
630 $element = $this->_prep_element($element);
631 $speed = $this->_validate_speed($speed);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500632
Derek Jones811f4752010-03-02 18:13:59 -0600633 if ($callback != '')
634 {
635 $callback = ", function(){\n{$callback}\n}";
636 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500637
638 $str = "$({$element}).slideToggle({$speed}{$callback});";
639
Derek Jones811f4752010-03-02 18:13:59 -0600640 return $str;
641 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500642
Derek Jones811f4752010-03-02 18:13:59 -0600643 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500644
Derek Jones811f4752010-03-02 18:13:59 -0600645 /**
646 * Toggle
647 *
648 * Outputs a jQuery toggle event
649 *
650 * @access private
651 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200652 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600653 */
654 function _toggle($element = 'this')
655 {
656 $element = $this->_prep_element($element);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500657 $str = "$({$element}).toggle();";
Derek Jones811f4752010-03-02 18:13:59 -0600658 return $str;
659 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500660
Derek Jones811f4752010-03-02 18:13:59 -0600661 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500662
Derek Jones811f4752010-03-02 18:13:59 -0600663 /**
664 * Toggle Class
665 *
666 * Outputs a jQuery toggle class event
667 *
668 * @access private
669 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200670 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600671 */
672 function _toggleClass($element = 'this', $class='')
673 {
674 $element = $this->_prep_element($element);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500675 $str = "$({$element}).toggleClass(\"$class\");";
Derek Jones811f4752010-03-02 18:13:59 -0600676 return $str;
677 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500678
Derek Jones811f4752010-03-02 18:13:59 -0600679 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500680
Derek Jones811f4752010-03-02 18:13:59 -0600681 /**
682 * Show
683 *
684 * Outputs a jQuery show event
685 *
686 * @access private
687 * @param string - element
688 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
689 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200690 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600691 */
692 function _show($element = 'this', $speed = '', $callback = '')
693 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500694 $element = $this->_prep_element($element);
Derek Jones811f4752010-03-02 18:13:59 -0600695 $speed = $this->_validate_speed($speed);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500696
Derek Jones811f4752010-03-02 18:13:59 -0600697 if ($callback != '')
698 {
699 $callback = ", function(){\n{$callback}\n}";
700 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500701
702 $str = "$({$element}).show({$speed}{$callback});";
703
Derek Jones811f4752010-03-02 18:13:59 -0600704 return $str;
705 }
706
707 // --------------------------------------------------------------------
708
709 /**
710 * Updater
711 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500712 * An Ajax call that populates the designated DOM node with
Derek Jones811f4752010-03-02 18:13:59 -0600713 * returned content
714 *
715 * @access private
716 * @param string The element to attach the event to
717 * @param string the controller to run the call against
718 * @param string optional parameters
719 * @return string
720 */
Derek Jones37f4b9c2011-07-01 17:56:50 -0500721
Derek Jones811f4752010-03-02 18:13:59 -0600722 function _updater($container = 'this', $controller, $options = '')
Derek Jones37f4b9c2011-07-01 17:56:50 -0500723 {
Derek Jones811f4752010-03-02 18:13:59 -0600724 $container = $this->_prep_element($container);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500725
Derek Jones811f4752010-03-02 18:13:59 -0600726 $controller = (strpos('://', $controller) === FALSE) ? $controller : $this->CI->config->site_url($controller);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500727
Derek Jones811f4752010-03-02 18:13:59 -0600728 // ajaxStart and ajaxStop are better choices here... but this is a stop gap
729 if ($this->CI->config->item('javascript_ajax_img') == '')
730 {
731 $loading_notifier = "Loading...";
732 }
733 else
734 {
735 $loading_notifier = '<img src=\'' . $this->CI->config->slash_item('base_url') . $this->CI->config->item('javascript_ajax_img') . '\' alt=\'Loading\' />';
736 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500737
Derek Jones811f4752010-03-02 18:13:59 -0600738 $updater = "$($container).empty();\n"; // anything that was in... get it out
739 $updater .= "\t\t$($container).prepend(\"$loading_notifier\");\n"; // to replace with an image
740
741 $request_options = '';
742 if ($options != '')
743 {
744 $request_options .= ", {";
745 $request_options .= (is_array($options)) ? "'".implode("', '", $options)."'" : "'".str_replace(":", "':'", $options)."'";
746 $request_options .= "}";
747 }
748
749 $updater .= "\t\t$($container).load('$controller'$request_options);";
750 return $updater;
751 }
752
753
754 // --------------------------------------------------------------------
755 // Pre-written handy stuff
756 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500757
Derek Jones811f4752010-03-02 18:13:59 -0600758 /**
759 * Zebra tables
760 *
761 * @access private
762 * @param string table name
763 * @param string plugin location
764 * @return string
765 */
766 function _zebraTables($class = '', $odd = 'odd', $hover = '')
767 {
768 $class = ($class != '') ? '.'.$class : '';
Derek Jones37f4b9c2011-07-01 17:56:50 -0500769
770 $zebra = "\t\$(\"table{$class} tbody tr:nth-child(even)\").addClass(\"{$odd}\");";
Derek Jones811f4752010-03-02 18:13:59 -0600771
772 $this->jquery_code_for_compile[] = $zebra;
773
774 if ($hover != '')
775 {
776 $hover = $this->hover("table{$class} tbody tr", "$(this).addClass('hover');", "$(this).removeClass('hover');");
777 }
778
779 return $zebra;
780 }
781
782
783
784 // --------------------------------------------------------------------
785 // Plugins
786 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500787
Derek Jones811f4752010-03-02 18:13:59 -0600788 /**
789 * Corner Plugin
790 *
791 * http://www.malsup.com/jquery/corner/
792 *
793 * @access public
794 * @param string target
795 * @return string
796 */
797 function corner($element = '', $corner_style = '')
798 {
799 // may want to make this configurable down the road
800 $corner_location = '/plugins/jquery.corner.js';
801
802 if ($corner_style != '')
803 {
804 $corner_style = '"'.$corner_style.'"';
805 }
806
807 return "$(" . $this->_prep_element($element) . ").corner(".$corner_style.");";
808 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500809
Derek Jones811f4752010-03-02 18:13:59 -0600810 // --------------------------------------------------------------------
811
812 /**
813 * modal window
814 *
815 * Load a thickbox modal window
816 *
817 * @access public
818 * @return void
819 */
820 function modal($src, $relative = FALSE)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500821 {
Derek Jones811f4752010-03-02 18:13:59 -0600822 $this->jquery_code_for_load[] = $this->external($src, $relative);
823 }
824
825 // --------------------------------------------------------------------
826
827 /**
828 * Effect
829 *
830 * Load an Effect library
831 *
832 * @access public
833 * @return void
834 */
835 function effect($src, $relative = FALSE)
836 {
837 $this->jquery_code_for_load[] = $this->external($src, $relative);
838 }
839
840 // --------------------------------------------------------------------
841
842 /**
843 * Plugin
844 *
845 * Load a plugin library
846 *
847 * @access public
848 * @return void
849 */
850 function plugin($src, $relative = FALSE)
851 {
852 $this->jquery_code_for_load[] = $this->external($src, $relative);
853 }
854
855 // --------------------------------------------------------------------
856
857 /**
858 * UI
859 *
860 * Load a user interface library
861 *
862 * @access public
863 * @return void
864 */
865 function ui($src, $relative = FALSE)
866 {
867 $this->jquery_code_for_load[] = $this->external($src, $relative);
868 }
869 // --------------------------------------------------------------------
870
871 /**
872 * Sortable
873 *
874 * Creates a jQuery sortable
875 *
876 * @access public
877 * @return void
878 */
879 function sortable($element, $options = array())
880 {
881
882 if (count($options) > 0)
883 {
884 $sort_options = array();
885 foreach ($options as $k=>$v)
886 {
887 $sort_options[] = "\n\t\t".$k.': '.$v."";
888 }
889 $sort_options = implode(",", $sort_options);
890 }
891 else
892 {
893 $sort_options = '';
894 }
895
896 return "$(" . $this->_prep_element($element) . ").sortable({".$sort_options."\n\t});";
897 }
898
899 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Jones811f4752010-03-02 18:13:59 -0600901 /**
902 * Table Sorter Plugin
903 *
904 * @access public
905 * @param string table name
906 * @param string plugin location
907 * @return string
908 */
909 function tablesorter($table = '', $options = '')
910 {
911 $this->jquery_code_for_compile[] = "\t$(" . $this->_prep_element($table) . ").tablesorter($options);\n";
912 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500913
Derek Jones811f4752010-03-02 18:13:59 -0600914 // --------------------------------------------------------------------
915 // Class functions
916 // --------------------------------------------------------------------
917
918 /**
919 * Add Event
920 *
921 * Constructs the syntax for an event, and adds to into the array for compilation
922 *
923 * @access private
924 * @param string The element to attach the event to
925 * @param string The code to execute
926 * @param string The event to pass
927 * @return string
Derek Jones37f4b9c2011-07-01 17:56:50 -0500928 */
Derek Jones811f4752010-03-02 18:13:59 -0600929 function _add_event($element, $js, $event)
930 {
931 if (is_array($js))
932 {
933 $js = implode("\n\t\t", $js);
934
935 }
936
937 $event = "\n\t$(" . $this->_prep_element($element) . ").{$event}(function(){\n\t\t{$js}\n\t});\n";
938 $this->jquery_code_for_compile[] = $event;
939 return $event;
940 }
941
942 // --------------------------------------------------------------------
943
944 /**
945 * Compile
946 *
947 * As events are specified, they are stored in an array
948 * This funciton compiles them all for output on a page
949 *
950 * @access private
951 * @return string
952 */
953 function _compile($view_var = 'script_foot', $script_tags = TRUE)
954 {
955 // External references
956 $external_scripts = implode('', $this->jquery_code_for_load);
957 $this->CI->load->vars(array('library_src' => $external_scripts));
958
959 if (count($this->jquery_code_for_compile) == 0 )
960 {
961 // no inline references, let's just return
962 return;
963 }
964
965 // Inline references
966 $script = '$(document).ready(function() {' . "\n";
967 $script .= implode('', $this->jquery_code_for_compile);
968 $script .= '});';
Derek Jones37f4b9c2011-07-01 17:56:50 -0500969
Derek Jones811f4752010-03-02 18:13:59 -0600970 $output = ($script_tags === FALSE) ? $script : $this->inline($script);
971
972 $this->CI->load->vars(array($view_var => $output));
973
974 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500975
Derek Jones811f4752010-03-02 18:13:59 -0600976 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500977
Derek Jones811f4752010-03-02 18:13:59 -0600978 /**
979 * Clear Compile
980 *
981 * Clears the array of script events collected for output
982 *
983 * @access public
984 * @return void
985 */
986 function _clear_compile()
987 {
988 $this->jquery_code_for_compile = array();
989 }
990
991 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500992
Derek Jones811f4752010-03-02 18:13:59 -0600993 /**
994 * Document Ready
995 *
996 * A wrapper for writing document.ready()
997 *
998 * @access private
999 * @return string
1000 */
1001 function _document_ready($js)
1002 {
1003 if ( ! is_array($js))
1004 {
1005 $js = array ($js);
1006
1007 }
Derek Jones37f4b9c2011-07-01 17:56:50 -05001008
Derek Jones811f4752010-03-02 18:13:59 -06001009 foreach ($js as $script)
1010 {
1011 $this->jquery_code_for_compile[] = $script;
1012 }
1013 }
1014
1015 // --------------------------------------------------------------------
1016
1017 /**
1018 * Script Tag
1019 *
1020 * Outputs the script tag that loads the jquery.js file into an HTML document
1021 *
1022 * @access public
1023 * @param string
1024 * @return string
1025 */
1026 function script($library_src = '', $relative = FALSE)
1027 {
1028 $library_src = $this->external($library_src, $relative);
1029 $this->jquery_code_for_load[] = $library_src;
1030 return $library_src;
1031 }
Derek Jones37f4b9c2011-07-01 17:56:50 -05001032
Derek Jones811f4752010-03-02 18:13:59 -06001033 // --------------------------------------------------------------------
1034
1035 /**
1036 * Prep Element
1037 *
1038 * Puts HTML element in quotes for use in jQuery code
1039 * unless the supplied element is the Javascript 'this'
1040 * object, in which case no quotes are added
1041 *
1042 * @access public
1043 * @param string
1044 * @return string
1045 */
1046 function _prep_element($element)
1047 {
1048 if ($element != 'this')
1049 {
1050 $element = '"'.$element.'"';
1051 }
Derek Jones37f4b9c2011-07-01 17:56:50 -05001052
Derek Jones811f4752010-03-02 18:13:59 -06001053 return $element;
1054 }
Derek Jones37f4b9c2011-07-01 17:56:50 -05001055
Derek Jones811f4752010-03-02 18:13:59 -06001056 // --------------------------------------------------------------------
1057
1058 /**
1059 * Validate Speed
1060 *
1061 * Ensures the speed parameter is valid for jQuery
1062 *
1063 * @access private
1064 * @param string
1065 * @return string
Derek Jones37f4b9c2011-07-01 17:56:50 -05001066 */
Derek Jones811f4752010-03-02 18:13:59 -06001067 function _validate_speed($speed)
1068 {
1069 if (in_array($speed, array('slow', 'normal', 'fast')))
1070 {
1071 $speed = '"'.$speed.'"';
1072 }
1073 elseif (preg_match("/[^0-9]/", $speed))
1074 {
1075 $speed = '';
1076 }
Derek Jones37f4b9c2011-07-01 17:56:50 -05001077
Derek Jones811f4752010-03-02 18:13:59 -06001078 return $speed;
1079 }
1080
1081}
1082
1083/* End of file Jquery.php */
1084/* Location: ./system/libraries/Jquery.php */