blob: 70f9aaccd6d18d165c7279f265584ef66d7f62ae [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Jones811f4752010-03-02 18:13:59 -06002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Jonesf4a4bd82011-10-20 12:18:42 -05006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevad47f942011-12-25 19:13:48 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreevad47f942011-12-25 19:13:48 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jones811f4752010-03-02 18:13:59 -060017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Jones811f4752010-03-02 18:13:59 -060036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Jones811f4752010-03-02 18:13:59 -060039
40/**
41 * Jquery Class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
Derek Jones811f4752010-03-02 18:13:59 -060045 * @category Loader
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/libraries/javascript.html
Derek Jones811f4752010-03-02 18:13:59 -060048 */
Greg Akeraacfe482010-03-03 12:26:31 -060049class CI_Jquery extends CI_Javascript {
Derek Jones811f4752010-03-02 18:13:59 -060050
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020051 /**
52 * JavaScript directory location
53 *
54 * @var string
55 */
Andrey Andreevad47f942011-12-25 19:13:48 +020056 protected $_javascript_folder = 'js';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020057
58 /**
59 * JQuery code for load
60 *
61 * @var array
62 */
Andrey Andreevad47f942011-12-25 19:13:48 +020063 public $jquery_code_for_load = array();
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020064
65 /**
66 * JQuery code for compile
67 *
68 * @var array
69 */
Andrey Andreevad47f942011-12-25 19:13:48 +020070 public $jquery_code_for_compile = array();
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020071
72 /**
73 * JQuery corner active flag
74 *
75 * @var bool
76 */
Andrey Andreevad47f942011-12-25 19:13:48 +020077 public $jquery_corner_active = FALSE;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020078
79 /**
80 * JQuery table sorter active flag
81 *
82 * @var bool
83 */
Andrey Andreevad47f942011-12-25 19:13:48 +020084 public $jquery_table_sorter_active = FALSE;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020085
86 /**
Calvin Tam55bc5052015-07-24 02:27:24 -070087 * JQuery table sorter pager active
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020088 *
89 * @var bool
90 */
Andrey Andreevad47f942011-12-25 19:13:48 +020091 public $jquery_table_sorter_pager_active = FALSE;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020092
93 /**
94 * JQuery AJAX image
95 *
96 * @var string
97 */
Andrey Andreevad47f942011-12-25 19:13:48 +020098 public $jquery_ajax_img = '';
Derek Jones811f4752010-03-02 18:13:59 -060099
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200100 // --------------------------------------------------------------------
101
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300102 /**
103 * Constructor
104 *
105 * @param array $params
106 * @return void
107 */
Greg Akera9263282010-11-10 15:26:43 -0600108 public function __construct($params)
Derek Jones811f4752010-03-02 18:13:59 -0600109 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200110 $this->CI =& get_instance();
Derek Jones811f4752010-03-02 18:13:59 -0600111 extract($params);
112
113 if ($autoload === TRUE)
114 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200115 $this->script();
Derek Jones811f4752010-03-02 18:13:59 -0600116 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200117
Andrey Andreev90726b82015-01-20 12:39:22 +0200118 log_message('info', 'Jquery Class Initialized');
Derek Jones811f4752010-03-02 18:13:59 -0600119 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200120
121 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600122 // Event Code
Andrey Andreevad47f942011-12-25 19:13:48 +0200123 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600124
125 /**
126 * Blur
127 *
128 * Outputs a jQuery blur event
129 *
Derek Jones811f4752010-03-02 18:13:59 -0600130 * @param string The element to attach the event to
131 * @param string The code to execute
132 * @return string
133 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200134 protected function _blur($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600135 {
136 return $this->_add_event($element, $js, 'blur');
137 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200138
Derek Jones811f4752010-03-02 18:13:59 -0600139 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200140
Derek Jones811f4752010-03-02 18:13:59 -0600141 /**
142 * Change
143 *
144 * Outputs a jQuery change event
145 *
Derek Jones811f4752010-03-02 18:13:59 -0600146 * @param string The element to attach the event to
147 * @param string The code to execute
148 * @return string
149 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200150 protected function _change($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600151 {
152 return $this->_add_event($element, $js, 'change');
153 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200154
Derek Jones811f4752010-03-02 18:13:59 -0600155 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200156
Derek Jones811f4752010-03-02 18:13:59 -0600157 /**
158 * Click
159 *
160 * Outputs a jQuery click event
161 *
Derek Jones811f4752010-03-02 18:13:59 -0600162 * @param string The element to attach the event to
163 * @param string The code to execute
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300164 * @param bool whether or not to return false
Derek Jones811f4752010-03-02 18:13:59 -0600165 * @return string
166 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200167 protected function _click($element = 'this', $js = '', $ret_false = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600168 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300169 is_array($js) OR $js = array($js);
Derek Jones811f4752010-03-02 18:13:59 -0600170
171 if ($ret_false)
172 {
Andrey Andreev56454792012-05-17 14:32:19 +0300173 $js[] = 'return false;';
Derek Jones811f4752010-03-02 18:13:59 -0600174 }
175
176 return $this->_add_event($element, $js, 'click');
177 }
178
179 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200180
Derek Jones811f4752010-03-02 18:13:59 -0600181 /**
182 * Double Click
183 *
184 * Outputs a jQuery dblclick event
185 *
Derek Jones811f4752010-03-02 18:13:59 -0600186 * @param string The element to attach the event to
187 * @param string The code to execute
188 * @return string
189 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200190 protected function _dblclick($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600191 {
192 return $this->_add_event($element, $js, 'dblclick');
193 }
194
195 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200196
Derek Jones811f4752010-03-02 18:13:59 -0600197 /**
198 * Error
199 *
200 * Outputs a jQuery error event
201 *
Derek Jones811f4752010-03-02 18:13:59 -0600202 * @param string The element to attach the event to
203 * @param string The code to execute
204 * @return string
205 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200206 protected function _error($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600207 {
208 return $this->_add_event($element, $js, 'error');
209 }
210
211 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200212
Derek Jones811f4752010-03-02 18:13:59 -0600213 /**
214 * Focus
215 *
216 * Outputs a jQuery focus event
217 *
Derek Jones811f4752010-03-02 18:13:59 -0600218 * @param string The element to attach the event to
219 * @param string The code to execute
220 * @return string
221 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200222 protected function _focus($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600223 {
224 return $this->_add_event($element, $js, 'focus');
225 }
226
227 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200228
Derek Jones811f4752010-03-02 18:13:59 -0600229 /**
230 * Hover
231 *
232 * Outputs a jQuery hover event
233 *
Derek Jones811f4752010-03-02 18:13:59 -0600234 * @param string - element
235 * @param string - Javascript code for mouse over
236 * @param string - Javascript code for mouse out
Barry Mienydd671972010-10-04 16:33:58 +0200237 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600238 */
Rasmus Lerdorf0a6a88a2013-05-18 10:26:21 -0400239 protected function _hover($element = 'this', $over = '', $out = '')
Derek Jones811f4752010-03-02 18:13:59 -0600240 {
Andrey Andreev56454792012-05-17 14:32:19 +0300241 $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";
Derek Jones811f4752010-03-02 18:13:59 -0600242
243 $this->jquery_code_for_compile[] = $event;
244
245 return $event;
246 }
247
248 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200249
Derek Jones811f4752010-03-02 18:13:59 -0600250 /**
251 * Keydown
252 *
253 * Outputs a jQuery keydown event
254 *
Derek Jones811f4752010-03-02 18:13:59 -0600255 * @param string The element to attach the event to
256 * @param string The code to execute
257 * @return string
258 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200259 protected function _keydown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600260 {
261 return $this->_add_event($element, $js, 'keydown');
262 }
263
264 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200265
Derek Jones811f4752010-03-02 18:13:59 -0600266 /**
267 * Keyup
268 *
269 * Outputs a jQuery keydown event
270 *
Derek Jones811f4752010-03-02 18:13:59 -0600271 * @param string The element to attach the event to
272 * @param string The code to execute
273 * @return string
274 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200275 protected function _keyup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600276 {
277 return $this->_add_event($element, $js, 'keyup');
Andrey Andreevad47f942011-12-25 19:13:48 +0200278 }
Derek Jones811f4752010-03-02 18:13:59 -0600279
280 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200281
Derek Jones811f4752010-03-02 18:13:59 -0600282 /**
283 * Load
284 *
285 * Outputs a jQuery load event
286 *
Derek Jones811f4752010-03-02 18:13:59 -0600287 * @param string The element to attach the event to
288 * @param string The code to execute
289 * @return string
290 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200291 protected function _load($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600292 {
293 return $this->_add_event($element, $js, 'load');
Andrey Andreevad47f942011-12-25 19:13:48 +0200294 }
295
Derek Jones811f4752010-03-02 18:13:59 -0600296 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200297
Derek Jones811f4752010-03-02 18:13:59 -0600298 /**
299 * Mousedown
300 *
301 * Outputs a jQuery mousedown event
302 *
Derek Jones811f4752010-03-02 18:13:59 -0600303 * @param string The element to attach the event to
304 * @param string The code to execute
305 * @return string
306 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200307 protected function _mousedown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600308 {
309 return $this->_add_event($element, $js, 'mousedown');
310 }
311
312 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200313
Derek Jones811f4752010-03-02 18:13:59 -0600314 /**
315 * Mouse Out
316 *
317 * Outputs a jQuery mouseout event
318 *
Derek Jones811f4752010-03-02 18:13:59 -0600319 * @param string The element to attach the event to
320 * @param string The code to execute
321 * @return string
322 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200323 protected function _mouseout($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600324 {
325 return $this->_add_event($element, $js, 'mouseout');
326 }
327
328 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200329
Derek Jones811f4752010-03-02 18:13:59 -0600330 /**
331 * Mouse Over
332 *
333 * Outputs a jQuery mouseover event
334 *
Derek Jones811f4752010-03-02 18:13:59 -0600335 * @param string The element to attach the event to
336 * @param string The code to execute
337 * @return string
338 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200339 protected function _mouseover($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600340 {
341 return $this->_add_event($element, $js, 'mouseover');
342 }
343
344 // --------------------------------------------------------------------
345
346 /**
347 * Mouseup
348 *
349 * Outputs a jQuery mouseup event
350 *
Derek Jones811f4752010-03-02 18:13:59 -0600351 * @param string The element to attach the event to
352 * @param string The code to execute
353 * @return string
354 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200355 protected function _mouseup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600356 {
357 return $this->_add_event($element, $js, 'mouseup');
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * Output
364 *
365 * Outputs script directly
366 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300367 * @param array $array_js = array()
368 * @return void
Derek Jones811f4752010-03-02 18:13:59 -0600369 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300370 protected function _output($array_js = array())
Derek Jones811f4752010-03-02 18:13:59 -0600371 {
372 if ( ! is_array($array_js))
373 {
374 $array_js = array($array_js);
375 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200376
Derek Jones811f4752010-03-02 18:13:59 -0600377 foreach ($array_js as $js)
378 {
Andrey Andreev56454792012-05-17 14:32:19 +0300379 $this->jquery_code_for_compile[] = "\t".$js."\n";
Derek Jones811f4752010-03-02 18:13:59 -0600380 }
381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * Resize
387 *
388 * Outputs a jQuery resize event
389 *
Derek Jones811f4752010-03-02 18:13:59 -0600390 * @param string The element to attach the event to
391 * @param string The code to execute
392 * @return string
393 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200394 protected function _resize($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600395 {
396 return $this->_add_event($element, $js, 'resize');
397 }
398
399 // --------------------------------------------------------------------
400
401 /**
402 * Scroll
403 *
404 * Outputs a jQuery scroll event
405 *
Derek Jones811f4752010-03-02 18:13:59 -0600406 * @param string The element to attach the event to
407 * @param string The code to execute
408 * @return string
409 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200410 protected function _scroll($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600411 {
412 return $this->_add_event($element, $js, 'scroll');
413 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200414
Derek Jones811f4752010-03-02 18:13:59 -0600415 // --------------------------------------------------------------------
416
417 /**
418 * Unload
419 *
420 * Outputs a jQuery unload event
421 *
Derek Jones811f4752010-03-02 18:13:59 -0600422 * @param string The element to attach the event to
423 * @param string The code to execute
424 * @return string
425 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200426 protected function _unload($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600427 {
428 return $this->_add_event($element, $js, 'unload');
429 }
430
Andrey Andreevad47f942011-12-25 19:13:48 +0200431 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600432 // Effects
Andrey Andreevad47f942011-12-25 19:13:48 +0200433 // --------------------------------------------------------------------
434
Derek Jones811f4752010-03-02 18:13:59 -0600435 /**
436 * Add Class
437 *
438 * Outputs a jQuery addClass event
439 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200440 * @param string $element
441 * @param string $class
Barry Mienydd671972010-10-04 16:33:58 +0200442 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600443 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300444 protected function _addClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600445 {
446 $element = $this->_prep_element($element);
Andrey Andreev56454792012-05-17 14:32:19 +0300447 return '$('.$element.').addClass("'.$class.'");';
Derek Jones811f4752010-03-02 18:13:59 -0600448 }
449
450 // --------------------------------------------------------------------
451
452 /**
453 * Animate
454 *
455 * Outputs a jQuery animate event
456 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200457 * @param string $element
458 * @param array $params
459 * @param string $speed 'slow', 'normal', 'fast', or time in milliseconds
460 * @param string $extra
Barry Mienydd671972010-10-04 16:33:58 +0200461 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600462 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200463 protected function _animate($element = 'this', $params = array(), $speed = '', $extra = '')
Derek Jones811f4752010-03-02 18:13:59 -0600464 {
465 $element = $this->_prep_element($element);
466 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200467
Derek Jones811f4752010-03-02 18:13:59 -0600468 $animations = "\t\t\t";
Andrey Andreevad47f942011-12-25 19:13:48 +0200469
Andrey Andreev56454792012-05-17 14:32:19 +0300470 foreach ($params as $param => $value)
Derek Jones811f4752010-03-02 18:13:59 -0600471 {
Andrey Andreev56454792012-05-17 14:32:19 +0300472 $animations .= $param.": '".$value."', ";
Derek Jones811f4752010-03-02 18:13:59 -0600473 }
474
475 $animations = substr($animations, 0, -2); // remove the last ", "
476
Alex Bilbied261b1e2012-06-02 11:12:16 +0100477 if ($speed !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600478 {
479 $speed = ', '.$speed;
480 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200481
Alex Bilbied261b1e2012-06-02 11:12:16 +0100482 if ($extra !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600483 {
484 $extra = ', '.$extra;
485 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200486
Andrey Andreev56454792012-05-17 14:32:19 +0300487 return "$({$element}).animate({\n$animations\n\t\t}".$speed.$extra.');';
Derek Jones811f4752010-03-02 18:13:59 -0600488 }
489
490 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200491
Derek Jones811f4752010-03-02 18:13:59 -0600492 /**
493 * Fade In
494 *
495 * Outputs a jQuery hide event
496 *
Derek Jones811f4752010-03-02 18:13:59 -0600497 * @param string - element
498 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
499 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200500 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600501 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200502 protected function _fadeIn($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600503 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200504 $element = $this->_prep_element($element);
Derek Jones811f4752010-03-02 18:13:59 -0600505 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200506
Alex Bilbied261b1e2012-06-02 11:12:16 +0100507 if ($callback !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600508 {
509 $callback = ", function(){\n{$callback}\n}";
510 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200511
512 return "$({$element}).fadeIn({$speed}{$callback});";
Derek Jones811f4752010-03-02 18:13:59 -0600513 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200514
Derek Jones811f4752010-03-02 18:13:59 -0600515 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200516
Derek Jones811f4752010-03-02 18:13:59 -0600517 /**
518 * Fade Out
519 *
520 * Outputs a jQuery hide event
521 *
Derek Jones811f4752010-03-02 18:13:59 -0600522 * @param string - element
523 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
524 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200525 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600526 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200527 protected function _fadeOut($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600528 {
529 $element = $this->_prep_element($element);
530 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200531
Alex Bilbied261b1e2012-06-02 11:12:16 +0100532 if ($callback !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600533 {
534 $callback = ", function(){\n{$callback}\n}";
535 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200536
Andrey Andreev56454792012-05-17 14:32:19 +0300537 return '$('.$element.').fadeOut('.$speed.$callback.');';
Derek Jones811f4752010-03-02 18:13:59 -0600538 }
539
540 // --------------------------------------------------------------------
541
542 /**
543 * Hide
544 *
545 * Outputs a jQuery hide action
546 *
Derek Jones811f4752010-03-02 18:13:59 -0600547 * @param string - element
548 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
549 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200550 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600551 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200552 protected function _hide($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600553 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200554 $element = $this->_prep_element($element);
Derek Jones811f4752010-03-02 18:13:59 -0600555 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200556
Alex Bilbied261b1e2012-06-02 11:12:16 +0100557 if ($callback !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600558 {
559 $callback = ", function(){\n{$callback}\n}";
560 }
Derek Jones811f4752010-03-02 18:13:59 -0600561
Andrey Andreevad47f942011-12-25 19:13:48 +0200562 return "$({$element}).hide({$speed}{$callback});";
Derek Jones811f4752010-03-02 18:13:59 -0600563 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200564
Derek Jones811f4752010-03-02 18:13:59 -0600565 // --------------------------------------------------------------------
566
567 /**
568 * Remove Class
569 *
570 * Outputs a jQuery remove class event
571 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200572 * @param string $element
573 * @param string $class
Barry Mienydd671972010-10-04 16:33:58 +0200574 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600575 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300576 protected function _removeClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600577 {
578 $element = $this->_prep_element($element);
Andrey Andreev56454792012-05-17 14:32:19 +0300579 return '$('.$element.').removeClass("'.$class.'");';
Derek Jones811f4752010-03-02 18:13:59 -0600580 }
581
582 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200583
Derek Jones811f4752010-03-02 18:13:59 -0600584 /**
585 * Slide Up
586 *
587 * Outputs a jQuery slideUp event
588 *
Derek Jones811f4752010-03-02 18:13:59 -0600589 * @param string - element
590 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
591 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200592 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600593 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200594 protected function _slideUp($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600595 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200596 $element = $this->_prep_element($element);
Derek Jones811f4752010-03-02 18:13:59 -0600597 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200598
Alex Bilbied261b1e2012-06-02 11:12:16 +0100599 if ($callback !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600600 {
601 $callback = ", function(){\n{$callback}\n}";
602 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200603
Andrey Andreev56454792012-05-17 14:32:19 +0300604 return '$('.$element.').slideUp('.$speed.$callback.');';
Derek Jones811f4752010-03-02 18:13:59 -0600605 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200606
Derek Jones811f4752010-03-02 18:13:59 -0600607 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200608
Derek Jones811f4752010-03-02 18:13:59 -0600609 /**
610 * Slide Down
611 *
612 * Outputs a jQuery slideDown event
613 *
Derek Jones811f4752010-03-02 18:13:59 -0600614 * @param string - element
615 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
616 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200617 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600618 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200619 protected function _slideDown($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600620 {
621 $element = $this->_prep_element($element);
622 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200623
Alex Bilbied261b1e2012-06-02 11:12:16 +0100624 if ($callback !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600625 {
626 $callback = ", function(){\n{$callback}\n}";
627 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200628
Andrey Andreev56454792012-05-17 14:32:19 +0300629 return '$('.$element.').slideDown('.$speed.$callback.');';
Derek Jones811f4752010-03-02 18:13:59 -0600630 }
631
632 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200633
Derek Jones811f4752010-03-02 18:13:59 -0600634 /**
635 * Slide Toggle
636 *
637 * Outputs a jQuery slideToggle 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 _slideToggle($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600645 {
646 $element = $this->_prep_element($element);
647 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200648
Alex Bilbied261b1e2012-06-02 11:12:16 +0100649 if ($callback !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600650 {
651 $callback = ", function(){\n{$callback}\n}";
652 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200653
Andrey Andreev56454792012-05-17 14:32:19 +0300654 return '$('.$element.').slideToggle('.$speed.$callback.');';
Derek Jones811f4752010-03-02 18:13:59 -0600655 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200656
Derek Jones811f4752010-03-02 18:13:59 -0600657 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200658
Derek Jones811f4752010-03-02 18:13:59 -0600659 /**
660 * Toggle
661 *
662 * Outputs a jQuery toggle event
663 *
Derek Jones811f4752010-03-02 18:13:59 -0600664 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200665 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600666 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200667 protected function _toggle($element = 'this')
Derek Jones811f4752010-03-02 18:13:59 -0600668 {
669 $element = $this->_prep_element($element);
Andrey Andreev56454792012-05-17 14:32:19 +0300670 return '$('.$element.').toggle();';
Derek Jones811f4752010-03-02 18:13:59 -0600671 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200672
Derek Jones811f4752010-03-02 18:13:59 -0600673 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200674
Derek Jones811f4752010-03-02 18:13:59 -0600675 /**
676 * Toggle Class
677 *
678 * Outputs a jQuery toggle class event
679 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200680 * @param string $element
681 * @param string $class
Barry Mienydd671972010-10-04 16:33:58 +0200682 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600683 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300684 protected function _toggleClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600685 {
686 $element = $this->_prep_element($element);
Andrey Andreev56454792012-05-17 14:32:19 +0300687 return '$('.$element.').toggleClass("'.$class.'");';
Derek Jones811f4752010-03-02 18:13:59 -0600688 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200689
Derek Jones811f4752010-03-02 18:13:59 -0600690 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200691
Derek Jones811f4752010-03-02 18:13:59 -0600692 /**
693 * Show
694 *
695 * Outputs a jQuery show event
696 *
Derek Jones811f4752010-03-02 18:13:59 -0600697 * @param string - element
698 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
699 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200700 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600701 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200702 protected function _show($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600703 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200704 $element = $this->_prep_element($element);
Derek Jones811f4752010-03-02 18:13:59 -0600705 $speed = $this->_validate_speed($speed);
Andrey Andreevad47f942011-12-25 19:13:48 +0200706
Alex Bilbied261b1e2012-06-02 11:12:16 +0100707 if ($callback !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600708 {
709 $callback = ", function(){\n{$callback}\n}";
710 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200711
Andrey Andreev56454792012-05-17 14:32:19 +0300712 return '$('.$element.').show('.$speed.$callback.');';
Derek Jones811f4752010-03-02 18:13:59 -0600713 }
714
715 // --------------------------------------------------------------------
716
717 /**
718 * Updater
719 *
Andrey Andreevad47f942011-12-25 19:13:48 +0200720 * An Ajax call that populates the designated DOM node with
Derek Jones811f4752010-03-02 18:13:59 -0600721 * returned content
722 *
Derek Jones811f4752010-03-02 18:13:59 -0600723 * @param string The element to attach the event to
724 * @param string the controller to run the call against
725 * @param string optional parameters
726 * @return string
727 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200728
Rasmus Lerdorf0a6a88a2013-05-18 10:26:21 -0400729 protected function _updater($container = 'this', $controller = '', $options = '')
Andrey Andreevad47f942011-12-25 19:13:48 +0200730 {
Derek Jones811f4752010-03-02 18:13:59 -0600731 $container = $this->_prep_element($container);
Derek Jones811f4752010-03-02 18:13:59 -0600732 $controller = (strpos('://', $controller) === FALSE) ? $controller : $this->CI->config->site_url($controller);
Andrey Andreevad47f942011-12-25 19:13:48 +0200733
Derek Jones811f4752010-03-02 18:13:59 -0600734 // ajaxStart and ajaxStop are better choices here... but this is a stop gap
Alex Bilbied261b1e2012-06-02 11:12:16 +0100735 if ($this->CI->config->item('javascript_ajax_img') === '')
Derek Jones811f4752010-03-02 18:13:59 -0600736 {
Andrey Andreev56454792012-05-17 14:32:19 +0300737 $loading_notifier = 'Loading...';
Derek Jones811f4752010-03-02 18:13:59 -0600738 }
739 else
740 {
Andrey Andreev56454792012-05-17 14:32:19 +0300741 $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 -0600742 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200743
Andrey Andreev56454792012-05-17 14:32:19 +0300744 $updater = '$('.$container.").empty();\n" // anything that was in... get it out
745 ."\t\t$(".$container.').prepend("'.$loading_notifier."\");\n"; // to replace with an image
Derek Jones811f4752010-03-02 18:13:59 -0600746
747 $request_options = '';
Alex Bilbied261b1e2012-06-02 11:12:16 +0100748 if ($options !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600749 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200750 $request_options .= ', {'
Andrey Andreev56454792012-05-17 14:32:19 +0300751 .(is_array($options) ? "'".implode("', '", $options)."'" : "'".str_replace(':', "':'", $options)."'")
752 .'}';
Derek Jones811f4752010-03-02 18:13:59 -0600753 }
754
Andrey Andreevad47f942011-12-25 19:13:48 +0200755 return $updater."\t\t$($container).load('$controller'$request_options);";
Derek Jones811f4752010-03-02 18:13:59 -0600756 }
757
Derek Jones811f4752010-03-02 18:13:59 -0600758 // --------------------------------------------------------------------
759 // Pre-written handy stuff
760 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200761
Derek Jones811f4752010-03-02 18:13:59 -0600762 /**
763 * Zebra tables
764 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200765 * @param string $class
766 * @param string $odd
767 * @param string $hover
Derek Jones811f4752010-03-02 18:13:59 -0600768 * @return string
769 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200770 protected function _zebraTables($class = '', $odd = 'odd', $hover = '')
Derek Jones811f4752010-03-02 18:13:59 -0600771 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100772 $class = ($class !== '') ? '.'.$class : '';
vkeranovbfbe8b22012-10-27 18:25:08 +0300773 $zebra = "\t\$(\"table{$class} tbody tr:nth-child(even)\").addClass(\"{$odd}\");";
Derek Jones811f4752010-03-02 18:13:59 -0600774
775 $this->jquery_code_for_compile[] = $zebra;
776
Alex Bilbied261b1e2012-06-02 11:12:16 +0100777 if ($hover !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600778 {
779 $hover = $this->hover("table{$class} tbody tr", "$(this).addClass('hover');", "$(this).removeClass('hover');");
780 }
781
782 return $zebra;
783 }
784
Derek Jones811f4752010-03-02 18:13:59 -0600785 // --------------------------------------------------------------------
786 // Plugins
787 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200788
Derek Jones811f4752010-03-02 18:13:59 -0600789 /**
790 * Corner Plugin
791 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200792 * @link http://www.malsup.com/jquery/corner/
793 * @param string $element
794 * @param string $corner_style
Derek Jones811f4752010-03-02 18:13:59 -0600795 * @return string
796 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200797 public function corner($element = '', $corner_style = '')
Derek Jones811f4752010-03-02 18:13:59 -0600798 {
799 // may want to make this configurable down the road
800 $corner_location = '/plugins/jquery.corner.js';
801
Alex Bilbied261b1e2012-06-02 11:12:16 +0100802 if ($corner_style !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600803 {
804 $corner_style = '"'.$corner_style.'"';
805 }
806
Andrey Andreev56454792012-05-17 14:32:19 +0300807 return '$('.$this->_prep_element($element).').corner('.$corner_style.');';
Derek Jones811f4752010-03-02 18:13:59 -0600808 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200809
Derek Jones811f4752010-03-02 18:13:59 -0600810 // --------------------------------------------------------------------
811
812 /**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300813 * Modal window
Derek Jones811f4752010-03-02 18:13:59 -0600814 *
815 * Load a thickbox modal window
816 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300817 * @param string $src
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200818 * @param bool $relative
Derek Jones811f4752010-03-02 18:13:59 -0600819 * @return void
820 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200821 public function modal($src, $relative = FALSE)
822 {
Derek Jones811f4752010-03-02 18:13:59 -0600823 $this->jquery_code_for_load[] = $this->external($src, $relative);
824 }
825
826 // --------------------------------------------------------------------
827
828 /**
829 * Effect
830 *
831 * Load an Effect library
832 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300833 * @param string $src
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200834 * @param bool $relative
Derek Jones811f4752010-03-02 18:13:59 -0600835 * @return void
836 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200837 public function effect($src, $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600838 {
839 $this->jquery_code_for_load[] = $this->external($src, $relative);
840 }
841
842 // --------------------------------------------------------------------
843
844 /**
845 * Plugin
846 *
847 * Load a plugin library
848 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300849 * @param string $src
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200850 * @param bool $relative
Derek Jones811f4752010-03-02 18:13:59 -0600851 * @return void
852 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200853 public function plugin($src, $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600854 {
855 $this->jquery_code_for_load[] = $this->external($src, $relative);
856 }
857
858 // --------------------------------------------------------------------
859
860 /**
861 * UI
862 *
863 * Load a user interface library
864 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300865 * @param string $src
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200866 * @param bool $relative
Derek Jones811f4752010-03-02 18:13:59 -0600867 * @return void
868 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200869 public function ui($src, $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600870 {
871 $this->jquery_code_for_load[] = $this->external($src, $relative);
872 }
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300873
Derek Jones811f4752010-03-02 18:13:59 -0600874 // --------------------------------------------------------------------
875
876 /**
877 * Sortable
878 *
879 * Creates a jQuery sortable
880 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300881 * @param string $element
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200882 * @param array $options
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300883 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600884 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200885 public function sortable($element, $options = array())
Derek Jones811f4752010-03-02 18:13:59 -0600886 {
Derek Jones811f4752010-03-02 18:13:59 -0600887 if (count($options) > 0)
888 {
889 $sort_options = array();
890 foreach ($options as $k=>$v)
891 {
Andrey Andreev56454792012-05-17 14:32:19 +0300892 $sort_options[] = "\n\t\t".$k.': '.$v;
Derek Jones811f4752010-03-02 18:13:59 -0600893 }
Andrey Andreev56454792012-05-17 14:32:19 +0300894 $sort_options = implode(',', $sort_options);
Derek Jones811f4752010-03-02 18:13:59 -0600895 }
896 else
897 {
898 $sort_options = '';
899 }
900
Andrey Andreev56454792012-05-17 14:32:19 +0300901 return '$('.$this->_prep_element($element).').sortable({'.$sort_options."\n\t});";
Derek Jones811f4752010-03-02 18:13:59 -0600902 }
903
904 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Jones811f4752010-03-02 18:13:59 -0600906 /**
907 * Table Sorter Plugin
908 *
Derek Jones811f4752010-03-02 18:13:59 -0600909 * @param string table name
910 * @param string plugin location
911 * @return string
912 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200913 public function tablesorter($table = '', $options = '')
Derek Jones811f4752010-03-02 18:13:59 -0600914 {
Andrey Andreev56454792012-05-17 14:32:19 +0300915 $this->jquery_code_for_compile[] = "\t$(".$this->_prep_element($table).').tablesorter('.$options.");\n";
Derek Jones811f4752010-03-02 18:13:59 -0600916 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200917
Derek Jones811f4752010-03-02 18:13:59 -0600918 // --------------------------------------------------------------------
919 // Class functions
920 // --------------------------------------------------------------------
921
922 /**
923 * Add Event
924 *
925 * Constructs the syntax for an event, and adds to into the array for compilation
926 *
Derek Jones811f4752010-03-02 18:13:59 -0600927 * @param string The element to attach the event to
928 * @param string The code to execute
929 * @param string The event to pass
930 * @return string
Andrey Andreevad47f942011-12-25 19:13:48 +0200931 */
932 protected function _add_event($element, $js, $event)
Derek Jones811f4752010-03-02 18:13:59 -0600933 {
934 if (is_array($js))
935 {
936 $js = implode("\n\t\t", $js);
Derek Jones811f4752010-03-02 18:13:59 -0600937 }
938
Andrey Andreev56454792012-05-17 14:32:19 +0300939 $event = "\n\t$(".$this->_prep_element($element).').'.$event."(function(){\n\t\t{$js}\n\t});\n";
Derek Jones811f4752010-03-02 18:13:59 -0600940 $this->jquery_code_for_compile[] = $event;
941 return $event;
942 }
943
944 // --------------------------------------------------------------------
945
946 /**
947 * Compile
948 *
949 * As events are specified, they are stored in an array
vlakoff3b8b8922013-07-24 22:41:00 +0200950 * This function compiles them all for output on a page
Derek Jones811f4752010-03-02 18:13:59 -0600951 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200952 * @param string $view_var
953 * @param bool $script_tags
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300954 * @return void
Derek Jones811f4752010-03-02 18:13:59 -0600955 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200956 protected function _compile($view_var = 'script_foot', $script_tags = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600957 {
958 // External references
959 $external_scripts = implode('', $this->jquery_code_for_load);
960 $this->CI->load->vars(array('library_src' => $external_scripts));
961
Andrey Andreevad47f942011-12-25 19:13:48 +0200962 if (count($this->jquery_code_for_compile) === 0)
Derek Jones811f4752010-03-02 18:13:59 -0600963 {
964 // no inline references, let's just return
965 return;
966 }
967
968 // Inline references
Andrey Andreevad47f942011-12-25 19:13:48 +0200969 $script = '$(document).ready(function() {'."\n"
Andrey Andreev56454792012-05-17 14:32:19 +0300970 .implode('', $this->jquery_code_for_compile)
971 .'});';
Andrey Andreevad47f942011-12-25 19:13:48 +0200972
Derek Jones811f4752010-03-02 18:13:59 -0600973 $output = ($script_tags === FALSE) ? $script : $this->inline($script);
974
975 $this->CI->load->vars(array($view_var => $output));
Derek Jones811f4752010-03-02 18:13:59 -0600976 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200977
Derek Jones811f4752010-03-02 18:13:59 -0600978 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200979
Derek Jones811f4752010-03-02 18:13:59 -0600980 /**
981 * Clear Compile
982 *
983 * Clears the array of script events collected for output
984 *
Derek Jones811f4752010-03-02 18:13:59 -0600985 * @return void
986 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200987 protected function _clear_compile()
Derek Jones811f4752010-03-02 18:13:59 -0600988 {
989 $this->jquery_code_for_compile = array();
990 }
991
992 // --------------------------------------------------------------------
Andrey Andreevad47f942011-12-25 19:13:48 +0200993
Derek Jones811f4752010-03-02 18:13:59 -0600994 /**
995 * Document Ready
996 *
997 * A wrapper for writing document.ready()
998 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300999 * @param array $js
1000 * @return void
Derek Jones811f4752010-03-02 18:13:59 -06001001 */
Andrey Andreevad47f942011-12-25 19:13:48 +02001002 protected function _document_ready($js)
Derek Jones811f4752010-03-02 18:13:59 -06001003 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001004 is_array($js) OR $js = array($js);
Andrey Andreevad47f942011-12-25 19:13:48 +02001005
Derek Jones811f4752010-03-02 18:13:59 -06001006 foreach ($js as $script)
1007 {
1008 $this->jquery_code_for_compile[] = $script;
1009 }
1010 }
1011
1012 // --------------------------------------------------------------------
1013
1014 /**
1015 * Script Tag
1016 *
1017 * Outputs the script tag that loads the jquery.js file into an HTML document
1018 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +02001019 * @param string $library_src
1020 * @param bool $relative
Derek Jones811f4752010-03-02 18:13:59 -06001021 * @return string
1022 */
Andrey Andreevad47f942011-12-25 19:13:48 +02001023 public function script($library_src = '', $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -06001024 {
1025 $library_src = $this->external($library_src, $relative);
1026 $this->jquery_code_for_load[] = $library_src;
1027 return $library_src;
1028 }
Andrey Andreevad47f942011-12-25 19:13:48 +02001029
Derek Jones811f4752010-03-02 18:13:59 -06001030 // --------------------------------------------------------------------
1031
1032 /**
1033 * Prep Element
1034 *
1035 * Puts HTML element in quotes for use in jQuery code
1036 * unless the supplied element is the Javascript 'this'
1037 * object, in which case no quotes are added
1038 *
Derek Jones811f4752010-03-02 18:13:59 -06001039 * @param string
1040 * @return string
1041 */
Andrey Andreevad47f942011-12-25 19:13:48 +02001042 protected function _prep_element($element)
Derek Jones811f4752010-03-02 18:13:59 -06001043 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001044 if ($element !== 'this')
Derek Jones811f4752010-03-02 18:13:59 -06001045 {
1046 $element = '"'.$element.'"';
1047 }
Andrey Andreevad47f942011-12-25 19:13:48 +02001048
Derek Jones811f4752010-03-02 18:13:59 -06001049 return $element;
1050 }
Andrey Andreevad47f942011-12-25 19:13:48 +02001051
Derek Jones811f4752010-03-02 18:13:59 -06001052 // --------------------------------------------------------------------
1053
1054 /**
1055 * Validate Speed
1056 *
1057 * Ensures the speed parameter is valid for jQuery
1058 *
Derek Jones811f4752010-03-02 18:13:59 -06001059 * @param string
1060 * @return string
Andrey Andreevad47f942011-12-25 19:13:48 +02001061 */
1062 protected function _validate_speed($speed)
Derek Jones811f4752010-03-02 18:13:59 -06001063 {
1064 if (in_array($speed, array('slow', 'normal', 'fast')))
1065 {
Andrey Andreevad47f942011-12-25 19:13:48 +02001066 return '"'.$speed.'"';
Derek Jones811f4752010-03-02 18:13:59 -06001067 }
Andrey Andreev56454792012-05-17 14:32:19 +03001068 elseif (preg_match('/[^0-9]/', $speed))
Derek Jones811f4752010-03-02 18:13:59 -06001069 {
Andrey Andreevad47f942011-12-25 19:13:48 +02001070 return '';
Derek Jones811f4752010-03-02 18:13:59 -06001071 }
Andrey Andreevad47f942011-12-25 19:13:48 +02001072
Derek Jones811f4752010-03-02 18:13:59 -06001073 return $speed;
1074 }
1075
1076}