blob: f872d672f2e3f3d96b4f90160657e0bb8046c0d7 [file] [log] [blame]
Andrey Andreevad47f942011-12-25 19:13:48 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Jones9d653ed2010-03-05 09:52:53 -06002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Jones9d653ed2010-03-05 09:52:53 -06006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * 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.
18 *
Derek Jones9d653ed2010-03-05 09:52:53 -060019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Jones9d653ed2010-03-05 09:52:53 -060023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Derek Jones811f4752010-03-02 18:13:59 -060027
Derek Jones9d653ed2010-03-05 09:52:53 -060028// ------------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -060029
Derek Jones9d653ed2010-03-05 09:52:53 -060030/**
31 * Javascript Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Javascript
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
fesplugas3934a4a2010-10-04 09:07:49 +020037 * @link http://codeigniter.com/user_guide/libraries/javascript.html
Derek Jones9d653ed2010-03-05 09:52:53 -060038 */
Derek Jones811f4752010-03-02 18:13:59 -060039class CI_Javascript {
40
Andrey Andreevad47f942011-12-25 19:13:48 +020041 protected $_javascript_location = 'js';
Derek Jones811f4752010-03-02 18:13:59 -060042
Greg Akera9263282010-11-10 15:26:43 -060043 public function __construct($params = array())
Barry Mienydd671972010-10-04 16:33:58 +020044 {
Derek Jones811f4752010-03-02 18:13:59 -060045 $defaults = array('js_library_driver' => 'jquery', 'autoload' => TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Jones811f4752010-03-02 18:13:59 -060047 foreach ($defaults as $key => $val)
48 {
49 if (isset($params[$key]) && $params[$key] !== "")
50 {
51 $defaults[$key] = $params[$key];
52 }
53 }
Barry Mienydd671972010-10-04 16:33:58 +020054
Derek Jones811f4752010-03-02 18:13:59 -060055 extract($defaults);
56
57 $this->CI =& get_instance();
58
59 // load the requested js library
Derek Jones9d653ed2010-03-05 09:52:53 -060060 $this->CI->load->library('javascript/'.$js_library_driver, array('autoload' => $autoload));
Derek Jones811f4752010-03-02 18:13:59 -060061 // make js to refer to current library
62 $this->js =& $this->CI->$js_library_driver;
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Jones37f4b9c2011-07-01 17:56:50 -050064 log_message('debug', "Javascript Class Initialized and loaded. Driver used: $js_library_driver");
Derek Jones811f4752010-03-02 18:13:59 -060065 }
66
Andrey Andreevad47f942011-12-25 19:13:48 +020067 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020068 // Event Code
69 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -060070
71 /**
72 * Blur
73 *
74 * Outputs a javascript library blur event
75 *
Derek Jones811f4752010-03-02 18:13:59 -060076 * @param string The element to attach the event to
77 * @param string The code to execute
78 * @return string
79 */
Andrey Andreevad47f942011-12-25 19:13:48 +020080 public function blur($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -060081 {
82 return $this->js->_blur($element, $js);
83 }
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Jones811f4752010-03-02 18:13:59 -060085 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Jones811f4752010-03-02 18:13:59 -060087 /**
88 * Change
89 *
90 * Outputs a javascript library change event
91 *
Derek Jones811f4752010-03-02 18:13:59 -060092 * @param string The element to attach the event to
93 * @param string The code to execute
94 * @return string
95 */
Andrey Andreevad47f942011-12-25 19:13:48 +020096 public function change($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -060097 {
98 return $this->js->_change($element, $js);
99 }
Barry Mienydd671972010-10-04 16:33:58 +0200100
Derek Jones811f4752010-03-02 18:13:59 -0600101 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Jones811f4752010-03-02 18:13:59 -0600103 /**
104 * Click
105 *
106 * Outputs a javascript library click event
107 *
Derek Jones811f4752010-03-02 18:13:59 -0600108 * @param string The element to attach the event to
109 * @param string The code to execute
110 * @param boolean whether or not to return false
111 * @return string
112 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200113 public function click($element = 'this', $js = '', $ret_false = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600114 {
115 return $this->js->_click($element, $js, $ret_false);
116 }
117
118 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Jones811f4752010-03-02 18:13:59 -0600120 /**
121 * Double Click
122 *
123 * Outputs a javascript library dblclick event
124 *
Derek Jones811f4752010-03-02 18:13:59 -0600125 * @param string The element to attach the event to
126 * @param string The code to execute
127 * @return string
128 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200129 public function dblclick($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600130 {
131 return $this->js->_dblclick($element, $js);
132 }
133
134 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Jones811f4752010-03-02 18:13:59 -0600136 /**
137 * Error
138 *
139 * Outputs a javascript library error event
140 *
Derek Jones811f4752010-03-02 18:13:59 -0600141 * @param string The element to attach the event to
142 * @param string The code to execute
143 * @return string
144 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200145 public function error($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600146 {
147 return $this->js->_error($element, $js);
148 }
149
150 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Jones811f4752010-03-02 18:13:59 -0600152 /**
153 * Focus
154 *
155 * Outputs a javascript library focus event
156 *
Derek Jones811f4752010-03-02 18:13:59 -0600157 * @param string The element to attach the event to
158 * @param string The code to execute
159 * @return string
160 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200161 public function focus($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600162 {
163 return $this->js->__add_event($focus, $js);
164 }
165
166 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Jones811f4752010-03-02 18:13:59 -0600168 /**
169 * Hover
170 *
171 * Outputs a javascript library hover event
172 *
Derek Jones811f4752010-03-02 18:13:59 -0600173 * @param string - element
174 * @param string - Javascript code for mouse over
175 * @param string - Javascript code for mouse out
Barry Mienydd671972010-10-04 16:33:58 +0200176 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600177 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200178 public function hover($element = 'this', $over, $out)
Derek Jones811f4752010-03-02 18:13:59 -0600179 {
180 return $this->js->__hover($element, $over, $out);
181 }
182
183 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Jones811f4752010-03-02 18:13:59 -0600185 /**
186 * Keydown
187 *
188 * Outputs a javascript library keydown event
189 *
Derek Jones811f4752010-03-02 18:13:59 -0600190 * @param string The element to attach the event to
191 * @param string The code to execute
192 * @return string
193 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200194 public function keydown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600195 {
196 return $this->js->_keydown($element, $js);
197 }
198
199 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Jones811f4752010-03-02 18:13:59 -0600201 /**
202 * Keyup
203 *
204 * Outputs a javascript library keydown event
205 *
Derek Jones811f4752010-03-02 18:13:59 -0600206 * @param string The element to attach the event to
207 * @param string The code to execute
208 * @return string
209 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200210 public function keyup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600211 {
212 return $this->js->_keyup($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200213 }
Derek Jones811f4752010-03-02 18:13:59 -0600214
215 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Jones811f4752010-03-02 18:13:59 -0600217 /**
218 * Load
219 *
220 * Outputs a javascript library load event
221 *
Derek Jones811f4752010-03-02 18:13:59 -0600222 * @param string The element to attach the event to
223 * @param string The code to execute
224 * @return string
225 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200226 public function load($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600227 {
228 return $this->js->_load($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200229 }
230
Derek Jones811f4752010-03-02 18:13:59 -0600231 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Jones811f4752010-03-02 18:13:59 -0600233 /**
234 * Mousedown
235 *
236 * Outputs a javascript library mousedown event
237 *
Derek Jones811f4752010-03-02 18:13:59 -0600238 * @param string The element to attach the event to
239 * @param string The code to execute
240 * @return string
241 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200242 public function mousedown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600243 {
244 return $this->js->_mousedown($element, $js);
245 }
246
247 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Jones811f4752010-03-02 18:13:59 -0600249 /**
250 * Mouse Out
251 *
252 * Outputs a javascript library mouseout event
253 *
Derek Jones811f4752010-03-02 18:13:59 -0600254 * @param string The element to attach the event to
255 * @param string The code to execute
256 * @return string
257 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200258 public function mouseout($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600259 {
260 return $this->js->_mouseout($element, $js);
261 }
262
263 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Jones811f4752010-03-02 18:13:59 -0600265 /**
266 * Mouse Over
267 *
268 * Outputs a javascript library mouseover event
269 *
Derek Jones811f4752010-03-02 18:13:59 -0600270 * @param string The element to attach the event to
271 * @param string The code to execute
272 * @return string
273 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200274 public function mouseover($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600275 {
276 return $this->js->_mouseover($element, $js);
277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Mouseup
283 *
284 * Outputs a javascript library mouseup event
285 *
Derek Jones811f4752010-03-02 18:13:59 -0600286 * @param string The element to attach the event to
287 * @param string The code to execute
288 * @return string
289 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200290 public function mouseup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600291 {
292 return $this->js->_mouseup($element, $js);
293 }
294
295 // --------------------------------------------------------------------
296
297 /**
298 * Output
299 *
300 * Outputs the called javascript to the screen
301 *
Derek Jones811f4752010-03-02 18:13:59 -0600302 * @param string The code to output
303 * @return string
304 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200305 public function output($js)
Derek Jones811f4752010-03-02 18:13:59 -0600306 {
307 return $this->js->_output($js);
308 }
309
310 // --------------------------------------------------------------------
311
312 /**
313 * Ready
314 *
315 * Outputs a javascript library mouseup event
316 *
Derek Jones811f4752010-03-02 18:13:59 -0600317 * @param string The element to attach the event to
318 * @param string The code to execute
319 * @return string
320 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200321 public function ready($js)
Derek Jones811f4752010-03-02 18:13:59 -0600322 {
323 return $this->js->_document_ready($js);
324 }
325
326 // --------------------------------------------------------------------
327
328 /**
329 * Resize
330 *
331 * Outputs a javascript library resize event
332 *
Derek Jones811f4752010-03-02 18:13:59 -0600333 * @param string The element to attach the event to
334 * @param string The code to execute
335 * @return string
336 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200337 public function resize($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600338 {
339 return $this->js->_resize($element, $js);
340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * Scroll
346 *
347 * Outputs a javascript library scroll event
348 *
Derek Jones811f4752010-03-02 18:13:59 -0600349 * @param string The element to attach the event to
350 * @param string The code to execute
351 * @return string
352 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200353 public function scroll($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600354 {
355 return $this->js->_scroll($element, $js);
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Jones811f4752010-03-02 18:13:59 -0600358 // --------------------------------------------------------------------
359
360 /**
361 * Unload
362 *
363 * Outputs a javascript library unload event
364 *
Derek Jones811f4752010-03-02 18:13:59 -0600365 * @param string The element to attach the event to
366 * @param string The code to execute
367 * @return string
368 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200369 public function unload($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600370 {
371 return $this->js->_unload($element, $js);
372 }
373
Andrey Andreev6aac0ba2011-12-27 02:34:00 +0200374 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200375 // Effects
376 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600377
378
379 /**
380 * Add Class
381 *
382 * Outputs a javascript library addClass event
383 *
Derek Jones811f4752010-03-02 18:13:59 -0600384 * @param string - element
385 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200386 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600387 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200388 public function addClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600389 {
390 return $this->js->_addClass($element, $class);
391 }
392
Barry Mienydd671972010-10-04 16:33:58 +0200393 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600394
395 /**
396 * Animate
397 *
398 * Outputs a javascript library animate event
399 *
Derek Jones811f4752010-03-02 18:13:59 -0600400 * @param string - element
401 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
402 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200403 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600404 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200405 public function animate($element = 'this', $params = array(), $speed = '', $extra = '')
Derek Jones811f4752010-03-02 18:13:59 -0600406 {
407 return $this->js->_animate($element, $params, $speed, $extra);
408 }
409
410 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Jones811f4752010-03-02 18:13:59 -0600412 /**
413 * Fade In
414 *
415 * Outputs a javascript library hide event
416 *
Derek Jones811f4752010-03-02 18:13:59 -0600417 * @param string - element
418 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
419 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200420 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600421 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200422 public function fadeIn($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600423 {
424 return $this->js->_fadeIn($element, $speed, $callback);
425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Jones811f4752010-03-02 18:13:59 -0600427 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Jones811f4752010-03-02 18:13:59 -0600429 /**
430 * Fade Out
431 *
432 * Outputs a javascript library hide event
433 *
Derek Jones811f4752010-03-02 18:13:59 -0600434 * @param string - element
435 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
436 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200437 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600438 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200439 public function fadeOut($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600440 {
441 return $this->js->_fadeOut($element, $speed, $callback);
442 }
443 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Jones811f4752010-03-02 18:13:59 -0600445 /**
446 * Slide Up
447 *
448 * Outputs a javascript library slideUp event
449 *
Derek Jones811f4752010-03-02 18:13:59 -0600450 * @param string - element
451 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
452 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200453 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600454 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200455 public function slideUp($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600456 {
457 return $this->js->_slideUp($element, $speed, $callback);
458
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Jones811f4752010-03-02 18:13:59 -0600461 // --------------------------------------------------------------------
462
463 /**
464 * Remove Class
465 *
466 * Outputs a javascript library removeClass event
467 *
Derek Jones811f4752010-03-02 18:13:59 -0600468 * @param string - element
469 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200470 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600471 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200472 public function removeClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600473 {
474 return $this->js->_removeClass($element, $class);
475 }
476
Barry Mienydd671972010-10-04 16:33:58 +0200477 // --------------------------------------------------------------------
478
Derek Jones811f4752010-03-02 18:13:59 -0600479 /**
480 * Slide Down
481 *
482 * Outputs a javascript library slideDown event
483 *
Derek Jones811f4752010-03-02 18:13:59 -0600484 * @param string - element
485 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
486 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200487 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600488 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200489 public function slideDown($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600490 {
491 return $this->js->_slideDown($element, $speed, $callback);
492 }
493
494 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Jones811f4752010-03-02 18:13:59 -0600496 /**
497 * Slide Toggle
498 *
499 * Outputs a javascript library slideToggle event
500 *
Derek Jones811f4752010-03-02 18:13:59 -0600501 * @param string - element
502 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
503 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200504 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600505 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200506 public function slideToggle($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600507 {
508 return $this->js->_slideToggle($element, $speed, $callback);
509
510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Jones811f4752010-03-02 18:13:59 -0600512 // --------------------------------------------------------------------
513
514 /**
515 * Hide
516 *
517 * Outputs a javascript library hide action
518 *
Derek Jones811f4752010-03-02 18:13:59 -0600519 * @param string - element
520 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
521 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200522 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600523 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200524 public function hide($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600525 {
526 return $this->js->_hide($element, $speed, $callback);
527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Jones811f4752010-03-02 18:13:59 -0600529 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Jones811f4752010-03-02 18:13:59 -0600531 /**
532 * Toggle
533 *
534 * Outputs a javascript library toggle event
535 *
Derek Jones811f4752010-03-02 18:13:59 -0600536 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200537 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600538 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200539 public function toggle($element = 'this')
Derek Jones811f4752010-03-02 18:13:59 -0600540 {
541 return $this->js->_toggle($element);
542
543 }
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Jones811f4752010-03-02 18:13:59 -0600545 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Jones811f4752010-03-02 18:13:59 -0600547 /**
548 * Toggle Class
549 *
550 * Outputs a javascript library toggle class event
551 *
Derek Jones811f4752010-03-02 18:13:59 -0600552 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200553 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600554 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200555 public function toggleClass($element = 'this', $class='')
Derek Jones811f4752010-03-02 18:13:59 -0600556 {
557 return $this->js->_toggleClass($element, $class);
558 }
Barry Mienydd671972010-10-04 16:33:58 +0200559
Derek Jones811f4752010-03-02 18:13:59 -0600560 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Jones811f4752010-03-02 18:13:59 -0600562 /**
563 * Show
564 *
565 * Outputs a javascript library show event
566 *
Derek Jones811f4752010-03-02 18:13:59 -0600567 * @param string - element
568 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
569 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200570 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600571 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200572 public function show($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600573 {
574 return $this->js->_show($element, $speed, $callback);
575 }
576
577
578 // --------------------------------------------------------------------
579
580 /**
581 * Compile
582 *
583 * gather together all script needing to be output
584 *
Derek Jones811f4752010-03-02 18:13:59 -0600585 * @param string The element to attach the event to
586 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200587 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200588 public function compile($view_var = 'script_foot', $script_tags = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600589 {
590 $this->js->_compile($view_var, $script_tags);
591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Jones811f4752010-03-02 18:13:59 -0600593 /**
594 * Clear Compile
595 *
596 * Clears any previous javascript collected for output
597 *
Derek Jones811f4752010-03-02 18:13:59 -0600598 * @return void
599 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200600 public function clear_compile()
Derek Jones811f4752010-03-02 18:13:59 -0600601 {
602 $this->js->_clear_compile();
603 }
604
605 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200606
Derek Jones811f4752010-03-02 18:13:59 -0600607 /**
608 * External
609 *
610 * Outputs a <script> tag with the source as an external js file
611 *
Derek Jones811f4752010-03-02 18:13:59 -0600612 * @param string The element to attach the event to
613 * @return string
614 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200615 public function external($external_file = '', $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600616 {
617 if ($external_file !== '')
618 {
619 $this->_javascript_location = $external_file;
620 }
621 else
622 {
623 if ($this->CI->config->item('javascript_location') != '')
624 {
625 $this->_javascript_location = $this->CI->config->item('javascript_location');
Barry Mienydd671972010-10-04 16:33:58 +0200626 }
Derek Jones811f4752010-03-02 18:13:59 -0600627 }
Barry Mienydd671972010-10-04 16:33:58 +0200628
Andrey Andreevad47f942011-12-25 19:13:48 +0200629 if ($relative === TRUE OR strncmp($external_file, 'http://', 7) === 0 OR strncmp($external_file, 'https://', 8) === 0)
Derek Jones811f4752010-03-02 18:13:59 -0600630 {
Barry Mienydd671972010-10-04 16:33:58 +0200631 $str = $this->_open_script($external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600632 }
633 elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
634 {
Barry Mienydd671972010-10-04 16:33:58 +0200635 $str = $this->_open_script($this->_javascript_location.$external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600636 }
637 else
638 {
639 $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location.$external_file);
640 }
641
Andrey Andreevad47f942011-12-25 19:13:48 +0200642 return $str.$this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600643 }
Barry Mienydd671972010-10-04 16:33:58 +0200644
Derek Jones811f4752010-03-02 18:13:59 -0600645 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Jones811f4752010-03-02 18:13:59 -0600647 /**
648 * Inline
649 *
Barry Mienydd671972010-10-04 16:33:58 +0200650 * Outputs a <script> tag
Derek Jones811f4752010-03-02 18:13:59 -0600651 *
Derek Jones811f4752010-03-02 18:13:59 -0600652 * @param string The element to attach the event to
653 * @param boolean If a CDATA section should be added
654 * @return string
655 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200656 public function inline($script, $cdata = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600657 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200658 return $this->_open_script()
659 . ($cdata ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n")
660 . $this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600661 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200662
Barry Mienydd671972010-10-04 16:33:58 +0200663 // --------------------------------------------------------------------
664
Derek Jones811f4752010-03-02 18:13:59 -0600665 /**
666 * Open Script
667 *
Barry Mienydd671972010-10-04 16:33:58 +0200668 * Outputs an opening <script>
Derek Jones811f4752010-03-02 18:13:59 -0600669 *
Barry Mienydd671972010-10-04 16:33:58 +0200670 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600671 * @return string
672 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200673 protected function _open_script($src = '')
Derek Jones811f4752010-03-02 18:13:59 -0600674 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200675 return '<script type="text/javascript" charset="'.strtolower($this->CI->config->item('charset')).'"'
676 . ($src == '' ? '>' : ' src="'.$src.'">');
Derek Jones811f4752010-03-02 18:13:59 -0600677 }
678
679 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Jones811f4752010-03-02 18:13:59 -0600681 /**
682 * Close Script
683 *
Barry Mienydd671972010-10-04 16:33:58 +0200684 * Outputs an closing </script>
Derek Jones811f4752010-03-02 18:13:59 -0600685 *
Barry Mienydd671972010-10-04 16:33:58 +0200686 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600687 * @return string
688 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200689 protected function _close_script($extra = "\n")
Derek Jones811f4752010-03-02 18:13:59 -0600690 {
691 return "</script>$extra";
692 }
Barry Mienydd671972010-10-04 16:33:58 +0200693
694
Derek Jones811f4752010-03-02 18:13:59 -0600695 // --------------------------------------------------------------------
696 // --------------------------------------------------------------------
697 // AJAX-Y STUFF - still a testbed
698 // --------------------------------------------------------------------
699 // --------------------------------------------------------------------
700
701 /**
702 * Update
703 *
704 * Outputs a javascript library slideDown event
705 *
Derek Jones811f4752010-03-02 18:13:59 -0600706 * @param string - element
707 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
708 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200709 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600710 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200711 public function update($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600712 {
713 return $this->js->_updater($element, $speed, $callback);
714 }
Barry Mienydd671972010-10-04 16:33:58 +0200715
Derek Jones811f4752010-03-02 18:13:59 -0600716 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200717
Derek Jones811f4752010-03-02 18:13:59 -0600718 /**
719 * Generate JSON
720 *
721 * Can be passed a database result or associative array and returns a JSON formatted string
722 *
723 * @param mixed result set or array
724 * @param bool match array types (defaults to objects)
725 * @return string a json formatted string
726 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200727 public function generate_json($result = NULL, $match_array_type = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600728 {
729 // JSON data can optionally be passed to this function
730 // either as a database result object or an array, or a user supplied array
731 if ( ! is_null($result))
732 {
733 if (is_object($result))
734 {
735 $json_result = $result->result_array();
736 }
737 elseif (is_array($result))
738 {
739 $json_result = $result;
740 }
741 else
742 {
743 return $this->_prep_args($result);
744 }
745 }
746 else
747 {
748 return 'null';
749 }
750
751 $json = array();
752 $_is_assoc = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200753
Derek Jones811f4752010-03-02 18:13:59 -0600754 if ( ! is_array($json_result) AND empty($json_result))
755 {
756 show_error("Generate JSON Failed - Illegal key, value pair.");
757 }
758 elseif ($match_array_type)
759 {
760 $_is_assoc = $this->_is_associative_array($json_result);
761 }
762
763 foreach ($json_result as $k => $v)
764 {
765 if ($_is_assoc)
766 {
767 $json[] = $this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
768 }
769 else
770 {
771 $json[] = $this->generate_json($v, $match_array_type);
772 }
773 }
774
775 $json = implode(',', $json);
776
777 return $_is_assoc ? "{".$json."}" : "[".$json."]";
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Jones811f4752010-03-02 18:13:59 -0600779 }
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Jones811f4752010-03-02 18:13:59 -0600781 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200782
Derek Jones811f4752010-03-02 18:13:59 -0600783 /**
784 * Is associative array
785 *
786 * Checks for an associative array
787 *
Derek Jones811f4752010-03-02 18:13:59 -0600788 * @param type
789 * @return type
790 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200791 protected function _is_associative_array($arr)
Derek Jones811f4752010-03-02 18:13:59 -0600792 {
793 foreach (array_keys($arr) as $key => $val)
794 {
795 if ($key !== $val)
796 {
797 return TRUE;
798 }
799 }
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Jones811f4752010-03-02 18:13:59 -0600801 return FALSE;
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Jones811f4752010-03-02 18:13:59 -0600804 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Jones811f4752010-03-02 18:13:59 -0600806 /**
807 * Prep Args
808 *
809 * Ensures a standard json value and escapes values
810 *
Derek Jones811f4752010-03-02 18:13:59 -0600811 * @param type
812 * @return type
813 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200814 protected function _prep_args($result, $is_key = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600815 {
816 if (is_null($result))
817 {
818 return 'null';
819 }
820 elseif (is_bool($result))
821 {
822 return ($result === TRUE) ? 'true' : 'false';
823 }
824 elseif (is_string($result) OR $is_key)
825 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200826 return '"'.str_replace(array('\\', "\t", "\n", "\r", '"', '/'), array('\\\\', '\\t', '\\n', "\\r", '\"', '\/'), $result).'"';
Derek Jones811f4752010-03-02 18:13:59 -0600827 }
828 elseif (is_scalar($result))
829 {
830 return $result;
831 }
832 }
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Jones9d653ed2010-03-05 09:52:53 -0600834 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600835}
Derek Jones9d653ed2010-03-05 09:52:53 -0600836// END Javascript Class
Derek Jones811f4752010-03-02 18:13:59 -0600837
838/* End of file Javascript.php */
Andrey Andreevad47f942011-12-25 19:13:48 +0200839/* Location: ./system/libraries/Javascript.php */