blob: f30d7c6397a118bad6545ee7deea81f2f10f6946 [file] [log] [blame]
Andrey Andreevad47f942011-12-25 19:13:48 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Jones811f4752010-03-02 18:13:59 -06002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Jonesf4a4bd82011-10-20 12:18:42 -05006 *
7 * NOTICE OF LICENSE
Andrey Andreevad47f942011-12-25 19:13:48 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevad47f942011-12-25 19:13:48 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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 */
Andrey Andreevad47f942011-12-25 19:13:48 +020039
Greg Akeraacfe482010-03-03 12:26:31 -060040class CI_Jquery extends CI_Javascript {
Derek Jones811f4752010-03-02 18:13:59 -060041
Andrey Andreevad47f942011-12-25 19:13:48 +020042 protected $_javascript_folder = 'js';
43 public $jquery_code_for_load = array();
44 public $jquery_code_for_compile = array();
45 public $jquery_corner_active = FALSE;
46 public $jquery_table_sorter_active = FALSE;
47 public $jquery_table_sorter_pager_active = FALSE;
48 public $jquery_ajax_img = '';
Derek Jones811f4752010-03-02 18:13:59 -060049
Greg Akera9263282010-11-10 15:26:43 -060050 public function __construct($params)
Derek Jones811f4752010-03-02 18:13:59 -060051 {
Andrey Andreevad47f942011-12-25 19:13:48 +020052 $this->CI =& get_instance();
Derek Jones811f4752010-03-02 18:13:59 -060053 extract($params);
54
55 if ($autoload === TRUE)
56 {
Andrey Andreevad47f942011-12-25 19:13:48 +020057 $this->script();
Derek Jones811f4752010-03-02 18:13:59 -060058 }
Andrey Andreevad47f942011-12-25 19:13:48 +020059
Derek Jones811f4752010-03-02 18:13:59 -060060 log_message('debug', "Jquery Class Initialized");
61 }
Andrey Andreevad47f942011-12-25 19:13:48 +020062
63 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -060064 // Event Code
Andrey Andreevad47f942011-12-25 19:13:48 +020065 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -060066
67 /**
68 * Blur
69 *
70 * Outputs a jQuery blur event
71 *
Derek Jones811f4752010-03-02 18:13:59 -060072 * @param string The element to attach the event to
73 * @param string The code to execute
74 * @return string
75 */
Andrey Andreevad47f942011-12-25 19:13:48 +020076 protected function _blur($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -060077 {
78 return $this->_add_event($element, $js, 'blur');
79 }
Andrey Andreevad47f942011-12-25 19:13:48 +020080
Derek Jones811f4752010-03-02 18:13:59 -060081 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +020082
Derek Jones811f4752010-03-02 18:13:59 -060083 /**
84 * Change
85 *
86 * Outputs a jQuery change event
87 *
Derek Jones811f4752010-03-02 18:13:59 -060088 * @param string The element to attach the event to
89 * @param string The code to execute
90 * @return string
91 */
Andrey Andreevad47f942011-12-25 19:13:48 +020092 protected function _change($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -060093 {
94 return $this->_add_event($element, $js, 'change');
95 }
Andrey Andreevad47f942011-12-25 19:13:48 +020096
Derek Jones811f4752010-03-02 18:13:59 -060097 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +020098
Derek Jones811f4752010-03-02 18:13:59 -060099 /**
100 * Click
101 *
102 * Outputs a jQuery click event
103 *
Derek Jones811f4752010-03-02 18:13:59 -0600104 * @param string The element to attach the event to
105 * @param string The code to execute
106 * @param boolean whether or not to return false
107 * @return string
108 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200109 protected function _click($element = 'this', $js = '', $ret_false = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600110 {
111 if ( ! is_array($js))
112 {
113 $js = array($js);
114 }
115
116 if ($ret_false)
117 {
118 $js[] = "return false;";
119 }
120
121 return $this->_add_event($element, $js, 'click');
122 }
123
124 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200125
Derek Jones811f4752010-03-02 18:13:59 -0600126 /**
127 * Double Click
128 *
129 * Outputs a jQuery dblclick event
130 *
Derek Jones811f4752010-03-02 18:13:59 -0600131 * @param string The element to attach the event to
132 * @param string The code to execute
133 * @return string
134 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200135 protected function _dblclick($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600136 {
137 return $this->_add_event($element, $js, 'dblclick');
138 }
139
140 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200141
Derek Jones811f4752010-03-02 18:13:59 -0600142 /**
143 * Error
144 *
145 * Outputs a jQuery error event
146 *
Derek Jones811f4752010-03-02 18:13:59 -0600147 * @param string The element to attach the event to
148 * @param string The code to execute
149 * @return string
150 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200151 protected function _error($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600152 {
153 return $this->_add_event($element, $js, 'error');
154 }
155
156 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200157
Derek Jones811f4752010-03-02 18:13:59 -0600158 /**
159 * Focus
160 *
161 * Outputs a jQuery focus event
162 *
Derek Jones811f4752010-03-02 18:13:59 -0600163 * @param string The element to attach the event to
164 * @param string The code to execute
165 * @return string
166 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200167 protected function _focus($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600168 {
169 return $this->_add_event($element, $js, 'focus');
170 }
171
172 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200173
Derek Jones811f4752010-03-02 18:13:59 -0600174 /**
175 * Hover
176 *
177 * Outputs a jQuery hover event
178 *
Derek Jones811f4752010-03-02 18:13:59 -0600179 * @param string - element
180 * @param string - Javascript code for mouse over
181 * @param string - Javascript code for mouse out
Barry Mienydd671972010-10-04 16:33:58 +0200182 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600183 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200184 protected function _hover($element = 'this', $over, $out)
Derek Jones811f4752010-03-02 18:13:59 -0600185 {
186 $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";
187
188 $this->jquery_code_for_compile[] = $event;
189
190 return $event;
191 }
192
193 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200194
Derek Jones811f4752010-03-02 18:13:59 -0600195 /**
196 * Keydown
197 *
198 * Outputs a jQuery keydown event
199 *
Derek Jones811f4752010-03-02 18:13:59 -0600200 * @param string The element to attach the event to
201 * @param string The code to execute
202 * @return string
203 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200204 protected function _keydown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600205 {
206 return $this->_add_event($element, $js, 'keydown');
207 }
208
209 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200210
Derek Jones811f4752010-03-02 18:13:59 -0600211 /**
212 * Keyup
213 *
214 * Outputs a jQuery keydown event
215 *
Derek Jones811f4752010-03-02 18:13:59 -0600216 * @param string The element to attach the event to
217 * @param string The code to execute
218 * @return string
219 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200220 protected function _keyup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600221 {
222 return $this->_add_event($element, $js, 'keyup');
Andrey Andreevad47f942011-12-25 19:13:48 +0200223 }
Derek Jones811f4752010-03-02 18:13:59 -0600224
225 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200226
Derek Jones811f4752010-03-02 18:13:59 -0600227 /**
228 * Load
229 *
230 * Outputs a jQuery load event
231 *
Derek Jones811f4752010-03-02 18:13:59 -0600232 * @param string The element to attach the event to
233 * @param string The code to execute
234 * @return string
235 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200236 protected function _load($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600237 {
238 return $this->_add_event($element, $js, 'load');
Andrey Andreevad47f942011-12-25 19:13:48 +0200239 }
240
Derek Jones811f4752010-03-02 18:13:59 -0600241 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200242
Derek Jones811f4752010-03-02 18:13:59 -0600243 /**
244 * Mousedown
245 *
246 * Outputs a jQuery mousedown event
247 *
Derek Jones811f4752010-03-02 18:13:59 -0600248 * @param string The element to attach the event to
249 * @param string The code to execute
250 * @return string
251 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200252 protected function _mousedown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600253 {
254 return $this->_add_event($element, $js, 'mousedown');
255 }
256
257 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200258
Derek Jones811f4752010-03-02 18:13:59 -0600259 /**
260 * Mouse Out
261 *
262 * Outputs a jQuery mouseout event
263 *
Derek Jones811f4752010-03-02 18:13:59 -0600264 * @param string The element to attach the event to
265 * @param string The code to execute
266 * @return string
267 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200268 protected function _mouseout($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600269 {
270 return $this->_add_event($element, $js, 'mouseout');
271 }
272
273 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200274
Derek Jones811f4752010-03-02 18:13:59 -0600275 /**
276 * Mouse Over
277 *
278 * Outputs a jQuery mouseover event
279 *
Derek Jones811f4752010-03-02 18:13:59 -0600280 * @param string The element to attach the event to
281 * @param string The code to execute
282 * @return string
283 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200284 protected function _mouseover($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600285 {
286 return $this->_add_event($element, $js, 'mouseover');
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Mouseup
293 *
294 * Outputs a jQuery mouseup event
295 *
Derek Jones811f4752010-03-02 18:13:59 -0600296 * @param string The element to attach the event to
297 * @param string The code to execute
298 * @return string
299 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200300 protected function _mouseup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600301 {
302 return $this->_add_event($element, $js, 'mouseup');
303 }
304
305 // --------------------------------------------------------------------
306
307 /**
308 * Output
309 *
310 * Outputs script directly
311 *
Derek Jones811f4752010-03-02 18:13:59 -0600312 * @param string The element to attach the event to
313 * @param string The code to execute
314 * @return string
315 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200316 protected function _output($array_js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600317 {
318 if ( ! is_array($array_js))
319 {
320 $array_js = array($array_js);
321 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200322
Derek Jones811f4752010-03-02 18:13:59 -0600323 foreach ($array_js as $js)
324 {
325 $this->jquery_code_for_compile[] = "\t$js\n";
326 }
327 }
328
329 // --------------------------------------------------------------------
330
331 /**
332 * Resize
333 *
334 * Outputs a jQuery resize event
335 *
Derek Jones811f4752010-03-02 18:13:59 -0600336 * @param string The element to attach the event to
337 * @param string The code to execute
338 * @return string
339 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200340 protected function _resize($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600341 {
342 return $this->_add_event($element, $js, 'resize');
343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * Scroll
349 *
350 * Outputs a jQuery scroll event
351 *
Derek Jones811f4752010-03-02 18:13:59 -0600352 * @param string The element to attach the event to
353 * @param string The code to execute
354 * @return string
355 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200356 protected function _scroll($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600357 {
358 return $this->_add_event($element, $js, 'scroll');
359 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200360
Derek Jones811f4752010-03-02 18:13:59 -0600361 // --------------------------------------------------------------------
362
363 /**
364 * Unload
365 *
366 * Outputs a jQuery unload event
367 *
Derek Jones811f4752010-03-02 18:13:59 -0600368 * @param string The element to attach the event to
369 * @param string The code to execute
370 * @return string
371 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200372 protected function _unload($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600373 {
374 return $this->_add_event($element, $js, 'unload');
375 }
376
Andrey Andreevad47f942011-12-25 19:13:48 +0200377 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600378 // Effects
Andrey Andreevad47f942011-12-25 19:13:48 +0200379 // --------------------------------------------------------------------
380
Derek Jones811f4752010-03-02 18:13:59 -0600381 /**
382 * Add Class
383 *
384 * Outputs a jQuery addClass event
385 *
Derek Jones811f4752010-03-02 18:13:59 -0600386 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200387 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600388 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200389 protected function _addClass($element = 'this', $class='')
Derek Jones811f4752010-03-02 18:13:59 -0600390 {
391 $element = $this->_prep_element($element);
Andrey Andreevad47f942011-12-25 19:13:48 +0200392 return "$({$element}).addClass(\"$class\");";
Derek Jones811f4752010-03-02 18:13:59 -0600393 }
394
395 // --------------------------------------------------------------------
396
397 /**
398 * Animate
399 *
400 * Outputs a jQuery animate event
401 *
Derek Jones811f4752010-03-02 18:13:59 -0600402 * @param string - element
403 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
404 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200405 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600406 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200407 protected function _animate($element = 'this', $params = array(), $speed = '', $extra = '')
Derek Jones811f4752010-03-02 18:13:59 -0600408 {
409 $element = $this->_prep_element($element);
410 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200411
Derek Jones811f4752010-03-02 18:13:59 -0600412 $animations = "\t\t\t";
Andrey Andreevad47f942011-12-25 19:13:48 +0200413
Derek Jones811f4752010-03-02 18:13:59 -0600414 foreach ($params as $param=>$value)
415 {
416 $animations .= $param.': \''.$value.'\', ';
417 }
418
419 $animations = substr($animations, 0, -2); // remove the last ", "
420
421 if ($speed != '')
422 {
423 $speed = ', '.$speed;
424 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200425
Derek Jones811f4752010-03-02 18:13:59 -0600426 if ($extra != '')
427 {
428 $extra = ', '.$extra;
429 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200430
431 return "$({$element}).animate({\n$animations\n\t\t}".$speed.$extra.");";
Derek Jones811f4752010-03-02 18:13:59 -0600432 }
433
434 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200435
Derek Jones811f4752010-03-02 18:13:59 -0600436 /**
437 * Fade In
438 *
439 * Outputs a jQuery hide event
440 *
Derek Jones811f4752010-03-02 18:13:59 -0600441 * @param string - element
442 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
443 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200444 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600445 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200446 protected function _fadeIn($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600447 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200448 $element = $this->_prep_element($element);
Derek Jones811f4752010-03-02 18:13:59 -0600449 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200450
Derek Jones811f4752010-03-02 18:13:59 -0600451 if ($callback != '')
452 {
453 $callback = ", function(){\n{$callback}\n}";
454 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200455
456 return "$({$element}).fadeIn({$speed}{$callback});";
Derek Jones811f4752010-03-02 18:13:59 -0600457 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200458
Derek Jones811f4752010-03-02 18:13:59 -0600459 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200460
Derek Jones811f4752010-03-02 18:13:59 -0600461 /**
462 * Fade Out
463 *
464 * Outputs a jQuery hide event
465 *
Derek Jones811f4752010-03-02 18:13:59 -0600466 * @param string - element
467 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
468 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200469 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600470 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200471 protected function _fadeOut($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600472 {
473 $element = $this->_prep_element($element);
474 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200475
Derek Jones811f4752010-03-02 18:13:59 -0600476 if ($callback != '')
477 {
478 $callback = ", function(){\n{$callback}\n}";
479 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200480
481 return "$({$element}).fadeOut({$speed}{$callback});";
Derek Jones811f4752010-03-02 18:13:59 -0600482 }
483
484 // --------------------------------------------------------------------
485
486 /**
487 * Hide
488 *
489 * Outputs a jQuery hide action
490 *
Derek Jones811f4752010-03-02 18:13:59 -0600491 * @param string - element
492 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
493 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200494 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600495 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200496 protected function _hide($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600497 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200498 $element = $this->_prep_element($element);
Derek Jones811f4752010-03-02 18:13:59 -0600499 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200500
Derek Jones811f4752010-03-02 18:13:59 -0600501 if ($callback != '')
502 {
503 $callback = ", function(){\n{$callback}\n}";
504 }
Derek Jones811f4752010-03-02 18:13:59 -0600505
Andrey Andreevad47f942011-12-25 19:13:48 +0200506 return "$({$element}).hide({$speed}{$callback});";
Derek Jones811f4752010-03-02 18:13:59 -0600507 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200508
Derek Jones811f4752010-03-02 18:13:59 -0600509 // --------------------------------------------------------------------
510
511 /**
512 * Remove Class
513 *
514 * Outputs a jQuery remove class event
515 *
Derek Jones811f4752010-03-02 18:13:59 -0600516 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200517 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600518 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200519 protected function _removeClass($element = 'this', $class='')
Derek Jones811f4752010-03-02 18:13:59 -0600520 {
521 $element = $this->_prep_element($element);
Andrey Andreevad47f942011-12-25 19:13:48 +0200522 return "$({$element}).removeClass(\"$class\");";
Derek Jones811f4752010-03-02 18:13:59 -0600523 }
524
525 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200526
Derek Jones811f4752010-03-02 18:13:59 -0600527 /**
528 * Slide Up
529 *
530 * Outputs a jQuery slideUp event
531 *
Derek Jones811f4752010-03-02 18:13:59 -0600532 * @param string - element
533 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
534 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200535 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600536 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200537 protected function _slideUp($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600538 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200539 $element = $this->_prep_element($element);
Derek Jones811f4752010-03-02 18:13:59 -0600540 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200541
Derek Jones811f4752010-03-02 18:13:59 -0600542 if ($callback != '')
543 {
544 $callback = ", function(){\n{$callback}\n}";
545 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200546
547 return "$({$element}).slideUp({$speed}{$callback});";
Derek Jones811f4752010-03-02 18:13:59 -0600548 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200549
Derek Jones811f4752010-03-02 18:13:59 -0600550 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200551
Derek Jones811f4752010-03-02 18:13:59 -0600552 /**
553 * Slide Down
554 *
555 * Outputs a jQuery slideDown event
556 *
Derek Jones811f4752010-03-02 18:13:59 -0600557 * @param string - element
558 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
559 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200560 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600561 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200562 protected function _slideDown($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600563 {
564 $element = $this->_prep_element($element);
565 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200566
Derek Jones811f4752010-03-02 18:13:59 -0600567 if ($callback != '')
568 {
569 $callback = ", function(){\n{$callback}\n}";
570 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200571
572 return "$({$element}).slideDown({$speed}{$callback});";
Derek Jones811f4752010-03-02 18:13:59 -0600573 }
574
575 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200576
Derek Jones811f4752010-03-02 18:13:59 -0600577 /**
578 * Slide Toggle
579 *
580 * Outputs a jQuery slideToggle event
581 *
Derek Jones811f4752010-03-02 18:13:59 -0600582 * @param string - element
583 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
584 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200585 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600586 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200587 protected function _slideToggle($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600588 {
589 $element = $this->_prep_element($element);
590 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200591
Derek Jones811f4752010-03-02 18:13:59 -0600592 if ($callback != '')
593 {
594 $callback = ", function(){\n{$callback}\n}";
595 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200596
597 return "$({$element}).slideToggle({$speed}{$callback});";
Derek Jones811f4752010-03-02 18:13:59 -0600598 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200599
Derek Jones811f4752010-03-02 18:13:59 -0600600 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200601
Derek Jones811f4752010-03-02 18:13:59 -0600602 /**
603 * Toggle
604 *
605 * Outputs a jQuery toggle event
606 *
Derek Jones811f4752010-03-02 18:13:59 -0600607 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200608 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600609 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200610 protected function _toggle($element = 'this')
Derek Jones811f4752010-03-02 18:13:59 -0600611 {
612 $element = $this->_prep_element($element);
Andrey Andreevad47f942011-12-25 19:13:48 +0200613 return "$({$element}).toggle();";
Derek Jones811f4752010-03-02 18:13:59 -0600614 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200615
Derek Jones811f4752010-03-02 18:13:59 -0600616 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200617
Derek Jones811f4752010-03-02 18:13:59 -0600618 /**
619 * Toggle Class
620 *
621 * Outputs a jQuery toggle class event
622 *
Derek Jones811f4752010-03-02 18:13:59 -0600623 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200624 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600625 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200626 protected function _toggleClass($element = 'this', $class='')
Derek Jones811f4752010-03-02 18:13:59 -0600627 {
628 $element = $this->_prep_element($element);
Andrey Andreevad47f942011-12-25 19:13:48 +0200629 return "$({$element}).toggleClass(\"$class\");";
Derek Jones811f4752010-03-02 18:13:59 -0600630 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200631
Derek Jones811f4752010-03-02 18:13:59 -0600632 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200633
Derek Jones811f4752010-03-02 18:13:59 -0600634 /**
635 * Show
636 *
637 * Outputs a jQuery show event
638 *
Derek Jones811f4752010-03-02 18:13:59 -0600639 * @param string - element
640 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
641 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200642 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600643 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200644 protected function _show($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600645 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200646 $element = $this->_prep_element($element);
Derek Jones811f4752010-03-02 18:13:59 -0600647 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200648
Derek Jones811f4752010-03-02 18:13:59 -0600649 if ($callback != '')
650 {
651 $callback = ", function(){\n{$callback}\n}";
652 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200653
654 return "$({$element}).show({$speed}{$callback});";
Derek Jones811f4752010-03-02 18:13:59 -0600655 }
656
657 // --------------------------------------------------------------------
658
659 /**
660 * Updater
661 *
Andrey Andreevad47f942011-12-25 19:13:48 +0200662 * An Ajax call that populates the designated DOM node with
Derek Jones811f4752010-03-02 18:13:59 -0600663 * returned content
664 *
Derek Jones811f4752010-03-02 18:13:59 -0600665 * @param string The element to attach the event to
666 * @param string the controller to run the call against
667 * @param string optional parameters
668 * @return string
669 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200670
671 protected function _updater($container = 'this', $controller, $options = '')
672 {
Derek Jones811f4752010-03-02 18:13:59 -0600673 $container = $this->_prep_element($container);
Derek Jones811f4752010-03-02 18:13:59 -0600674 $controller = (strpos('://', $controller) === FALSE) ? $controller : $this->CI->config->site_url($controller);
Andrey Andreevad47f942011-12-25 19:13:48 +0200675
Derek Jones811f4752010-03-02 18:13:59 -0600676 // ajaxStart and ajaxStop are better choices here... but this is a stop gap
677 if ($this->CI->config->item('javascript_ajax_img') == '')
678 {
679 $loading_notifier = "Loading...";
680 }
681 else
682 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200683 $loading_notifier = '<img src=\''.$this->CI->config->slash_item('base_url').$this->CI->config->item('javascript_ajax_img').'\' alt=\'Loading\' />';
Derek Jones811f4752010-03-02 18:13:59 -0600684 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200685
686 $updater = "$($container).empty();\n" // anything that was in... get it out
687 . "\t\t$($container).prepend(\"$loading_notifier\");\n"; // to replace with an image
Derek Jones811f4752010-03-02 18:13:59 -0600688
689 $request_options = '';
690 if ($options != '')
691 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200692 $request_options .= ', {'
693 . (is_array($options) ? "'".implode("', '", $options)."'" : "'".str_replace(":", "':'", $options)."'")
694 . '}';
Derek Jones811f4752010-03-02 18:13:59 -0600695 }
696
Andrey Andreevad47f942011-12-25 19:13:48 +0200697 return $updater."\t\t$($container).load('$controller'$request_options);";
Derek Jones811f4752010-03-02 18:13:59 -0600698 }
699
700
701 // --------------------------------------------------------------------
702 // Pre-written handy stuff
703 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200704
Derek Jones811f4752010-03-02 18:13:59 -0600705 /**
706 * Zebra tables
707 *
Derek Jones811f4752010-03-02 18:13:59 -0600708 * @param string table name
709 * @param string plugin location
710 * @return string
711 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200712 protected function _zebraTables($class = '', $odd = 'odd', $hover = '')
Derek Jones811f4752010-03-02 18:13:59 -0600713 {
714 $class = ($class != '') ? '.'.$class : '';
Derek Jones37f4b9c2011-07-01 17:56:50 -0500715 $zebra = "\t\$(\"table{$class} tbody tr:nth-child(even)\").addClass(\"{$odd}\");";
Derek Jones811f4752010-03-02 18:13:59 -0600716
717 $this->jquery_code_for_compile[] = $zebra;
718
719 if ($hover != '')
720 {
721 $hover = $this->hover("table{$class} tbody tr", "$(this).addClass('hover');", "$(this).removeClass('hover');");
722 }
723
724 return $zebra;
725 }
726
Derek Jones811f4752010-03-02 18:13:59 -0600727 // --------------------------------------------------------------------
728 // Plugins
729 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200730
Derek Jones811f4752010-03-02 18:13:59 -0600731 /**
732 * Corner Plugin
733 *
734 * http://www.malsup.com/jquery/corner/
735 *
Derek Jones811f4752010-03-02 18:13:59 -0600736 * @param string target
737 * @return string
738 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200739 public function corner($element = '', $corner_style = '')
Derek Jones811f4752010-03-02 18:13:59 -0600740 {
741 // may want to make this configurable down the road
742 $corner_location = '/plugins/jquery.corner.js';
743
744 if ($corner_style != '')
745 {
746 $corner_style = '"'.$corner_style.'"';
747 }
748
749 return "$(" . $this->_prep_element($element) . ").corner(".$corner_style.");";
750 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200751
Derek Jones811f4752010-03-02 18:13:59 -0600752 // --------------------------------------------------------------------
753
754 /**
755 * modal window
756 *
757 * Load a thickbox modal window
758 *
Derek Jones811f4752010-03-02 18:13:59 -0600759 * @return void
760 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200761 public function modal($src, $relative = FALSE)
762 {
Derek Jones811f4752010-03-02 18:13:59 -0600763 $this->jquery_code_for_load[] = $this->external($src, $relative);
764 }
765
766 // --------------------------------------------------------------------
767
768 /**
769 * Effect
770 *
771 * Load an Effect library
772 *
Derek Jones811f4752010-03-02 18:13:59 -0600773 * @return void
774 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200775 public function effect($src, $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600776 {
777 $this->jquery_code_for_load[] = $this->external($src, $relative);
778 }
779
780 // --------------------------------------------------------------------
781
782 /**
783 * Plugin
784 *
785 * Load a plugin library
786 *
Derek Jones811f4752010-03-02 18:13:59 -0600787 * @return void
788 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200789 public function plugin($src, $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600790 {
791 $this->jquery_code_for_load[] = $this->external($src, $relative);
792 }
793
794 // --------------------------------------------------------------------
795
796 /**
797 * UI
798 *
799 * Load a user interface library
800 *
Derek Jones811f4752010-03-02 18:13:59 -0600801 * @return void
802 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200803 public function ui($src, $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600804 {
805 $this->jquery_code_for_load[] = $this->external($src, $relative);
806 }
807 // --------------------------------------------------------------------
808
809 /**
810 * Sortable
811 *
812 * Creates a jQuery sortable
813 *
Derek Jones811f4752010-03-02 18:13:59 -0600814 * @return void
815 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200816 public function sortable($element, $options = array())
Derek Jones811f4752010-03-02 18:13:59 -0600817 {
818
819 if (count($options) > 0)
820 {
821 $sort_options = array();
822 foreach ($options as $k=>$v)
823 {
824 $sort_options[] = "\n\t\t".$k.': '.$v."";
825 }
826 $sort_options = implode(",", $sort_options);
827 }
828 else
829 {
830 $sort_options = '';
831 }
832
833 return "$(" . $this->_prep_element($element) . ").sortable({".$sort_options."\n\t});";
834 }
835
836 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Jones811f4752010-03-02 18:13:59 -0600838 /**
839 * Table Sorter Plugin
840 *
Derek Jones811f4752010-03-02 18:13:59 -0600841 * @param string table name
842 * @param string plugin location
843 * @return string
844 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200845 public function tablesorter($table = '', $options = '')
Derek Jones811f4752010-03-02 18:13:59 -0600846 {
847 $this->jquery_code_for_compile[] = "\t$(" . $this->_prep_element($table) . ").tablesorter($options);\n";
848 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200849
Derek Jones811f4752010-03-02 18:13:59 -0600850 // --------------------------------------------------------------------
851 // Class functions
852 // --------------------------------------------------------------------
853
854 /**
855 * Add Event
856 *
857 * Constructs the syntax for an event, and adds to into the array for compilation
858 *
Derek Jones811f4752010-03-02 18:13:59 -0600859 * @param string The element to attach the event to
860 * @param string The code to execute
861 * @param string The event to pass
862 * @return string
Andrey Andreevad47f942011-12-25 19:13:48 +0200863 */
864 protected function _add_event($element, $js, $event)
Derek Jones811f4752010-03-02 18:13:59 -0600865 {
866 if (is_array($js))
867 {
868 $js = implode("\n\t\t", $js);
869
870 }
871
872 $event = "\n\t$(" . $this->_prep_element($element) . ").{$event}(function(){\n\t\t{$js}\n\t});\n";
873 $this->jquery_code_for_compile[] = $event;
874 return $event;
875 }
876
877 // --------------------------------------------------------------------
878
879 /**
880 * Compile
881 *
882 * As events are specified, they are stored in an array
883 * This funciton compiles them all for output on a page
884 *
Derek Jones811f4752010-03-02 18:13:59 -0600885 * @return string
886 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200887 protected function _compile($view_var = 'script_foot', $script_tags = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600888 {
889 // External references
890 $external_scripts = implode('', $this->jquery_code_for_load);
891 $this->CI->load->vars(array('library_src' => $external_scripts));
892
Andrey Andreevad47f942011-12-25 19:13:48 +0200893 if (count($this->jquery_code_for_compile) === 0)
Derek Jones811f4752010-03-02 18:13:59 -0600894 {
895 // no inline references, let's just return
896 return;
897 }
898
899 // Inline references
Andrey Andreevad47f942011-12-25 19:13:48 +0200900 $script = '$(document).ready(function() {'."\n"
901 . implode('', $this->jquery_code_for_compile)
902 . '});';
903
Derek Jones811f4752010-03-02 18:13:59 -0600904 $output = ($script_tags === FALSE) ? $script : $this->inline($script);
905
906 $this->CI->load->vars(array($view_var => $output));
907
908 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200909
Derek Jones811f4752010-03-02 18:13:59 -0600910 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200911
Derek Jones811f4752010-03-02 18:13:59 -0600912 /**
913 * Clear Compile
914 *
915 * Clears the array of script events collected for output
916 *
Derek Jones811f4752010-03-02 18:13:59 -0600917 * @return void
918 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200919 protected function _clear_compile()
Derek Jones811f4752010-03-02 18:13:59 -0600920 {
921 $this->jquery_code_for_compile = array();
922 }
923
924 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200925
Derek Jones811f4752010-03-02 18:13:59 -0600926 /**
927 * Document Ready
928 *
929 * A wrapper for writing document.ready()
930 *
Derek Jones811f4752010-03-02 18:13:59 -0600931 * @return string
932 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200933 protected function _document_ready($js)
Derek Jones811f4752010-03-02 18:13:59 -0600934 {
935 if ( ! is_array($js))
936 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200937 $js = array($js);
Derek Jones811f4752010-03-02 18:13:59 -0600938 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200939
Derek Jones811f4752010-03-02 18:13:59 -0600940 foreach ($js as $script)
941 {
942 $this->jquery_code_for_compile[] = $script;
943 }
944 }
945
946 // --------------------------------------------------------------------
947
948 /**
949 * Script Tag
950 *
951 * Outputs the script tag that loads the jquery.js file into an HTML document
952 *
Derek Jones811f4752010-03-02 18:13:59 -0600953 * @param string
954 * @return string
955 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200956 public function script($library_src = '', $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600957 {
958 $library_src = $this->external($library_src, $relative);
959 $this->jquery_code_for_load[] = $library_src;
960 return $library_src;
961 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200962
Derek Jones811f4752010-03-02 18:13:59 -0600963 // --------------------------------------------------------------------
964
965 /**
966 * Prep Element
967 *
968 * Puts HTML element in quotes for use in jQuery code
969 * unless the supplied element is the Javascript 'this'
970 * object, in which case no quotes are added
971 *
Derek Jones811f4752010-03-02 18:13:59 -0600972 * @param string
973 * @return string
974 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200975 protected function _prep_element($element)
Derek Jones811f4752010-03-02 18:13:59 -0600976 {
977 if ($element != 'this')
978 {
979 $element = '"'.$element.'"';
980 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200981
Derek Jones811f4752010-03-02 18:13:59 -0600982 return $element;
983 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200984
Derek Jones811f4752010-03-02 18:13:59 -0600985 // --------------------------------------------------------------------
986
987 /**
988 * Validate Speed
989 *
990 * Ensures the speed parameter is valid for jQuery
991 *
Derek Jones811f4752010-03-02 18:13:59 -0600992 * @param string
993 * @return string
Andrey Andreevad47f942011-12-25 19:13:48 +0200994 */
995 protected function _validate_speed($speed)
Derek Jones811f4752010-03-02 18:13:59 -0600996 {
997 if (in_array($speed, array('slow', 'normal', 'fast')))
998 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200999 return '"'.$speed.'"';
Derek Jones811f4752010-03-02 18:13:59 -06001000 }
1001 elseif (preg_match("/[^0-9]/", $speed))
1002 {
Andrey Andreevad47f942011-12-25 19:13:48 +02001003 return '';
Derek Jones811f4752010-03-02 18:13:59 -06001004 }
Andrey Andreevad47f942011-12-25 19:13:48 +02001005
Derek Jones811f4752010-03-02 18:13:59 -06001006 return $speed;
1007 }
1008
1009}
1010
1011/* End of file Jquery.php */
Andrey Andreevad47f942011-12-25 19:13:48 +02001012/* Location: ./system/libraries/Jquery.php */