blob: 074af948b91d882624536261c2e18179fb1fbe82 [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 *
76 * @access public
77 * @param string The element to attach the event to
78 * @param string The code to execute
79 * @return string
80 */
Andrey Andreevad47f942011-12-25 19:13:48 +020081 public function blur($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -060082 {
83 return $this->js->_blur($element, $js);
84 }
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Jones811f4752010-03-02 18:13:59 -060086 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Jones811f4752010-03-02 18:13:59 -060088 /**
89 * Change
90 *
91 * Outputs a javascript library change event
92 *
93 * @access public
94 * @param string The element to attach the event to
95 * @param string The code to execute
96 * @return string
97 */
Andrey Andreevad47f942011-12-25 19:13:48 +020098 public function change($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -060099 {
100 return $this->js->_change($element, $js);
101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Jones811f4752010-03-02 18:13:59 -0600103 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Jones811f4752010-03-02 18:13:59 -0600105 /**
106 * Click
107 *
108 * Outputs a javascript library click event
109 *
110 * @access public
111 * @param string The element to attach the event to
112 * @param string The code to execute
113 * @param boolean whether or not to return false
114 * @return string
115 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200116 public function click($element = 'this', $js = '', $ret_false = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600117 {
118 return $this->js->_click($element, $js, $ret_false);
119 }
120
121 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Jones811f4752010-03-02 18:13:59 -0600123 /**
124 * Double Click
125 *
126 * Outputs a javascript library dblclick event
127 *
128 * @access public
129 * @param string The element to attach the event to
130 * @param string The code to execute
131 * @return string
132 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200133 public function dblclick($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600134 {
135 return $this->js->_dblclick($element, $js);
136 }
137
138 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Jones811f4752010-03-02 18:13:59 -0600140 /**
141 * Error
142 *
143 * Outputs a javascript library error event
144 *
145 * @access public
146 * @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 public function error($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600151 {
152 return $this->js->_error($element, $js);
153 }
154
155 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Jones811f4752010-03-02 18:13:59 -0600157 /**
158 * Focus
159 *
160 * Outputs a javascript library focus event
161 *
162 * @access public
163 * @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 public function focus($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600168 {
169 return $this->js->__add_event($focus, $js);
170 }
171
172 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Jones811f4752010-03-02 18:13:59 -0600174 /**
175 * Hover
176 *
177 * Outputs a javascript library hover event
178 *
179 * @access public
180 * @param string - element
181 * @param string - Javascript code for mouse over
182 * @param string - Javascript code for mouse out
Barry Mienydd671972010-10-04 16:33:58 +0200183 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600184 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200185 public function hover($element = 'this', $over, $out)
Derek Jones811f4752010-03-02 18:13:59 -0600186 {
187 return $this->js->__hover($element, $over, $out);
188 }
189
190 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Jones811f4752010-03-02 18:13:59 -0600192 /**
193 * Keydown
194 *
195 * Outputs a javascript library keydown event
196 *
197 * @access public
198 * @param string The element to attach the event to
199 * @param string The code to execute
200 * @return string
201 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200202 public function keydown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600203 {
204 return $this->js->_keydown($element, $js);
205 }
206
207 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Jones811f4752010-03-02 18:13:59 -0600209 /**
210 * Keyup
211 *
212 * Outputs a javascript library keydown event
213 *
214 * @access public
215 * @param string The element to attach the event to
216 * @param string The code to execute
217 * @return string
218 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200219 public function keyup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600220 {
221 return $this->js->_keyup($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200222 }
Derek Jones811f4752010-03-02 18:13:59 -0600223
224 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Jones811f4752010-03-02 18:13:59 -0600226 /**
227 * Load
228 *
229 * Outputs a javascript library load event
230 *
231 * @access public
232 * @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 public function load($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600237 {
238 return $this->js->_load($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200239 }
240
Derek Jones811f4752010-03-02 18:13:59 -0600241 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Jones811f4752010-03-02 18:13:59 -0600243 /**
244 * Mousedown
245 *
246 * Outputs a javascript library mousedown event
247 *
248 * @access public
249 * @param string The element to attach the event to
250 * @param string The code to execute
251 * @return string
252 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200253 public function mousedown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600254 {
255 return $this->js->_mousedown($element, $js);
256 }
257
258 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Jones811f4752010-03-02 18:13:59 -0600260 /**
261 * Mouse Out
262 *
263 * Outputs a javascript library mouseout event
264 *
265 * @access public
266 * @param string The element to attach the event to
267 * @param string The code to execute
268 * @return string
269 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200270 public function mouseout($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600271 {
272 return $this->js->_mouseout($element, $js);
273 }
274
275 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Jones811f4752010-03-02 18:13:59 -0600277 /**
278 * Mouse Over
279 *
280 * Outputs a javascript library mouseover event
281 *
282 * @access public
283 * @param string The element to attach the event to
284 * @param string The code to execute
285 * @return string
286 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200287 public function mouseover($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600288 {
289 return $this->js->_mouseover($element, $js);
290 }
291
292 // --------------------------------------------------------------------
293
294 /**
295 * Mouseup
296 *
297 * Outputs a javascript library mouseup event
298 *
299 * @access public
300 * @param string The element to attach the event to
301 * @param string The code to execute
302 * @return string
303 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200304 public function mouseup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600305 {
306 return $this->js->_mouseup($element, $js);
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Output
313 *
314 * Outputs the called javascript to the screen
315 *
316 * @access public
317 * @param string The code to output
318 * @return string
319 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200320 public function output($js)
Derek Jones811f4752010-03-02 18:13:59 -0600321 {
322 return $this->js->_output($js);
323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * Ready
329 *
330 * Outputs a javascript library mouseup event
331 *
332 * @access public
333 * @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 ready($js)
Derek Jones811f4752010-03-02 18:13:59 -0600338 {
339 return $this->js->_document_ready($js);
340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * Resize
346 *
347 * Outputs a javascript library resize event
348 *
349 * @access public
350 * @param string The element to attach the event to
351 * @param string The code to execute
352 * @return string
353 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200354 public function resize($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600355 {
356 return $this->js->_resize($element, $js);
357 }
358
359 // --------------------------------------------------------------------
360
361 /**
362 * Scroll
363 *
364 * Outputs a javascript library scroll event
365 *
366 * @access public
367 * @param string The element to attach the event to
368 * @param string The code to execute
369 * @return string
370 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200371 public function scroll($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600372 {
373 return $this->js->_scroll($element, $js);
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Jones811f4752010-03-02 18:13:59 -0600376 // --------------------------------------------------------------------
377
378 /**
379 * Unload
380 *
381 * Outputs a javascript library unload event
382 *
383 * @access public
384 * @param string The element to attach the event to
385 * @param string The code to execute
386 * @return string
387 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200388 public function unload($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600389 {
390 return $this->js->_unload($element, $js);
391 }
392
Derek Jones37f4b9c2011-07-01 17:56:50 -0500393 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200394 // Effects
395 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600396
397
398 /**
399 * Add Class
400 *
401 * Outputs a javascript library addClass event
402 *
403 * @access public
404 * @param string - element
405 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200406 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600407 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200408 public function addClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600409 {
410 return $this->js->_addClass($element, $class);
411 }
412
Barry Mienydd671972010-10-04 16:33:58 +0200413 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600414
415 /**
416 * Animate
417 *
418 * Outputs a javascript library animate event
419 *
420 * @access public
421 * @param string - element
422 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
423 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200424 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600425 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200426 public function animate($element = 'this', $params = array(), $speed = '', $extra = '')
Derek Jones811f4752010-03-02 18:13:59 -0600427 {
428 return $this->js->_animate($element, $params, $speed, $extra);
429 }
430
431 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Jones811f4752010-03-02 18:13:59 -0600433 /**
434 * Fade In
435 *
436 * Outputs a javascript library hide event
437 *
438 * @access public
439 * @param string - element
440 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
441 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200442 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600443 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200444 public function fadeIn($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600445 {
446 return $this->js->_fadeIn($element, $speed, $callback);
447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Jones811f4752010-03-02 18:13:59 -0600449 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Jones811f4752010-03-02 18:13:59 -0600451 /**
452 * Fade Out
453 *
454 * Outputs a javascript library hide event
455 *
456 * @access public
457 * @param string - element
458 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
459 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200460 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600461 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200462 public function fadeOut($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600463 {
464 return $this->js->_fadeOut($element, $speed, $callback);
465 }
466 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Jones811f4752010-03-02 18:13:59 -0600468 /**
469 * Slide Up
470 *
471 * Outputs a javascript library slideUp event
472 *
473 * @access public
474 * @param string - element
475 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
476 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200477 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600478 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200479 public function slideUp($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600480 {
481 return $this->js->_slideUp($element, $speed, $callback);
482
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Jones811f4752010-03-02 18:13:59 -0600485 // --------------------------------------------------------------------
486
487 /**
488 * Remove Class
489 *
490 * Outputs a javascript library removeClass event
491 *
492 * @access public
493 * @param string - element
494 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200495 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600496 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200497 public function removeClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600498 {
499 return $this->js->_removeClass($element, $class);
500 }
501
Barry Mienydd671972010-10-04 16:33:58 +0200502 // --------------------------------------------------------------------
503
Derek Jones811f4752010-03-02 18:13:59 -0600504 /**
505 * Slide Down
506 *
507 * Outputs a javascript library slideDown event
508 *
509 * @access public
510 * @param string - element
511 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
512 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200513 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600514 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200515 public function slideDown($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600516 {
517 return $this->js->_slideDown($element, $speed, $callback);
518 }
519
520 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Jones811f4752010-03-02 18:13:59 -0600522 /**
523 * Slide Toggle
524 *
525 * Outputs a javascript library slideToggle event
526 *
527 * @access public
528 * @param string - element
529 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
530 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200531 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600532 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200533 public function slideToggle($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600534 {
535 return $this->js->_slideToggle($element, $speed, $callback);
536
537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Jones811f4752010-03-02 18:13:59 -0600539 // --------------------------------------------------------------------
540
541 /**
542 * Hide
543 *
544 * Outputs a javascript library hide action
545 *
546 * @access public
547 * @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 public function hide($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600553 {
554 return $this->js->_hide($element, $speed, $callback);
555 }
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Jones811f4752010-03-02 18:13:59 -0600557 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Jones811f4752010-03-02 18:13:59 -0600559 /**
560 * Toggle
561 *
562 * Outputs a javascript library toggle event
563 *
564 * @access public
565 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200566 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600567 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200568 public function toggle($element = 'this')
Derek Jones811f4752010-03-02 18:13:59 -0600569 {
570 return $this->js->_toggle($element);
571
572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Jones811f4752010-03-02 18:13:59 -0600574 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Jones811f4752010-03-02 18:13:59 -0600576 /**
577 * Toggle Class
578 *
579 * Outputs a javascript library toggle class event
580 *
581 * @access public
582 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200583 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600584 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200585 public function toggleClass($element = 'this', $class='')
Derek Jones811f4752010-03-02 18:13:59 -0600586 {
587 return $this->js->_toggleClass($element, $class);
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Jones811f4752010-03-02 18:13:59 -0600590 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Jones811f4752010-03-02 18:13:59 -0600592 /**
593 * Show
594 *
595 * Outputs a javascript library show event
596 *
597 * @access public
598 * @param string - element
599 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
600 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200601 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600602 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200603 public function show($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600604 {
605 return $this->js->_show($element, $speed, $callback);
606 }
607
608
609 // --------------------------------------------------------------------
610
611 /**
612 * Compile
613 *
614 * gather together all script needing to be output
615 *
616 * @access public
617 * @param string The element to attach the event to
618 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200619 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200620 public function compile($view_var = 'script_foot', $script_tags = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600621 {
622 $this->js->_compile($view_var, $script_tags);
623 }
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Jones811f4752010-03-02 18:13:59 -0600625 /**
626 * Clear Compile
627 *
628 * Clears any previous javascript collected for output
629 *
630 * @access public
631 * @return void
632 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200633 public function clear_compile()
Derek Jones811f4752010-03-02 18:13:59 -0600634 {
635 $this->js->_clear_compile();
636 }
637
638 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Jones811f4752010-03-02 18:13:59 -0600640 /**
641 * External
642 *
643 * Outputs a <script> tag with the source as an external js file
644 *
645 * @access public
646 * @param string The element to attach the event to
647 * @return string
648 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200649 public function external($external_file = '', $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600650 {
651 if ($external_file !== '')
652 {
653 $this->_javascript_location = $external_file;
654 }
655 else
656 {
657 if ($this->CI->config->item('javascript_location') != '')
658 {
659 $this->_javascript_location = $this->CI->config->item('javascript_location');
Barry Mienydd671972010-10-04 16:33:58 +0200660 }
Derek Jones811f4752010-03-02 18:13:59 -0600661 }
Barry Mienydd671972010-10-04 16:33:58 +0200662
Andrey Andreevad47f942011-12-25 19:13:48 +0200663 if ($relative === TRUE OR strncmp($external_file, 'http://', 7) === 0 OR strncmp($external_file, 'https://', 8) === 0)
Derek Jones811f4752010-03-02 18:13:59 -0600664 {
Barry Mienydd671972010-10-04 16:33:58 +0200665 $str = $this->_open_script($external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600666 }
667 elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
668 {
Barry Mienydd671972010-10-04 16:33:58 +0200669 $str = $this->_open_script($this->_javascript_location.$external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600670 }
671 else
672 {
673 $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location.$external_file);
674 }
675
Andrey Andreevad47f942011-12-25 19:13:48 +0200676 return $str.$this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600677 }
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Jones811f4752010-03-02 18:13:59 -0600679 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Jones811f4752010-03-02 18:13:59 -0600681 /**
682 * Inline
683 *
Barry Mienydd671972010-10-04 16:33:58 +0200684 * Outputs a <script> tag
Derek Jones811f4752010-03-02 18:13:59 -0600685 *
686 * @access public
687 * @param string The element to attach the event to
688 * @param boolean If a CDATA section should be added
689 * @return string
690 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200691 public function inline($script, $cdata = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600692 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200693 return $this->_open_script()
694 . ($cdata ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n")
695 . $this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600696 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200697
Barry Mienydd671972010-10-04 16:33:58 +0200698 // --------------------------------------------------------------------
699
Derek Jones811f4752010-03-02 18:13:59 -0600700 /**
701 * Open Script
702 *
Barry Mienydd671972010-10-04 16:33:58 +0200703 * Outputs an opening <script>
Derek Jones811f4752010-03-02 18:13:59 -0600704 *
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200705 * @access protected
Barry Mienydd671972010-10-04 16:33:58 +0200706 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600707 * @return string
708 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200709 protected function _open_script($src = '')
Derek Jones811f4752010-03-02 18:13:59 -0600710 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200711 return '<script type="text/javascript" charset="'.strtolower($this->CI->config->item('charset')).'"'
712 . ($src == '' ? '>' : ' src="'.$src.'">');
Derek Jones811f4752010-03-02 18:13:59 -0600713 }
714
715 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200716
Derek Jones811f4752010-03-02 18:13:59 -0600717 /**
718 * Close Script
719 *
Barry Mienydd671972010-10-04 16:33:58 +0200720 * Outputs an closing </script>
Derek Jones811f4752010-03-02 18:13:59 -0600721 *
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200722 * @access protected
Barry Mienydd671972010-10-04 16:33:58 +0200723 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600724 * @return string
725 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200726 protected function _close_script($extra = "\n")
Derek Jones811f4752010-03-02 18:13:59 -0600727 {
728 return "</script>$extra";
729 }
Barry Mienydd671972010-10-04 16:33:58 +0200730
731
Derek Jones811f4752010-03-02 18:13:59 -0600732 // --------------------------------------------------------------------
733 // --------------------------------------------------------------------
734 // AJAX-Y STUFF - still a testbed
735 // --------------------------------------------------------------------
736 // --------------------------------------------------------------------
737
738 /**
739 * Update
740 *
741 * Outputs a javascript library slideDown event
742 *
743 * @access public
744 * @param string - element
745 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
746 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200747 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600748 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200749 public function update($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600750 {
751 return $this->js->_updater($element, $speed, $callback);
752 }
Barry Mienydd671972010-10-04 16:33:58 +0200753
Derek Jones811f4752010-03-02 18:13:59 -0600754 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200755
Derek Jones811f4752010-03-02 18:13:59 -0600756 /**
757 * Generate JSON
758 *
759 * Can be passed a database result or associative array and returns a JSON formatted string
760 *
761 * @param mixed result set or array
762 * @param bool match array types (defaults to objects)
763 * @return string a json formatted string
764 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200765 public function generate_json($result = NULL, $match_array_type = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600766 {
767 // JSON data can optionally be passed to this function
768 // either as a database result object or an array, or a user supplied array
769 if ( ! is_null($result))
770 {
771 if (is_object($result))
772 {
773 $json_result = $result->result_array();
774 }
775 elseif (is_array($result))
776 {
777 $json_result = $result;
778 }
779 else
780 {
781 return $this->_prep_args($result);
782 }
783 }
784 else
785 {
786 return 'null';
787 }
788
789 $json = array();
790 $_is_assoc = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Jones811f4752010-03-02 18:13:59 -0600792 if ( ! is_array($json_result) AND empty($json_result))
793 {
794 show_error("Generate JSON Failed - Illegal key, value pair.");
795 }
796 elseif ($match_array_type)
797 {
798 $_is_assoc = $this->_is_associative_array($json_result);
799 }
800
801 foreach ($json_result as $k => $v)
802 {
803 if ($_is_assoc)
804 {
805 $json[] = $this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
806 }
807 else
808 {
809 $json[] = $this->generate_json($v, $match_array_type);
810 }
811 }
812
813 $json = implode(',', $json);
814
815 return $_is_assoc ? "{".$json."}" : "[".$json."]";
Barry Mienydd671972010-10-04 16:33:58 +0200816
Derek Jones811f4752010-03-02 18:13:59 -0600817 }
Barry Mienydd671972010-10-04 16:33:58 +0200818
Derek Jones811f4752010-03-02 18:13:59 -0600819 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Jones811f4752010-03-02 18:13:59 -0600821 /**
822 * Is associative array
823 *
824 * Checks for an associative array
825 *
Andrey Andreevad47f942011-12-25 19:13:48 +0200826 * @access protected
Derek Jones811f4752010-03-02 18:13:59 -0600827 * @param type
828 * @return type
829 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200830 protected function _is_associative_array($arr)
Derek Jones811f4752010-03-02 18:13:59 -0600831 {
832 foreach (array_keys($arr) as $key => $val)
833 {
834 if ($key !== $val)
835 {
836 return TRUE;
837 }
838 }
Barry Mienydd671972010-10-04 16:33:58 +0200839
Derek Jones811f4752010-03-02 18:13:59 -0600840 return FALSE;
841 }
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Jones811f4752010-03-02 18:13:59 -0600843 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Jones811f4752010-03-02 18:13:59 -0600845 /**
846 * Prep Args
847 *
848 * Ensures a standard json value and escapes values
849 *
Andrey Andreevad47f942011-12-25 19:13:48 +0200850 * @access protected
Derek Jones811f4752010-03-02 18:13:59 -0600851 * @param type
852 * @return type
853 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200854 protected function _prep_args($result, $is_key = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600855 {
856 if (is_null($result))
857 {
858 return 'null';
859 }
860 elseif (is_bool($result))
861 {
862 return ($result === TRUE) ? 'true' : 'false';
863 }
864 elseif (is_string($result) OR $is_key)
865 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200866 return '"'.str_replace(array('\\', "\t", "\n", "\r", '"', '/'), array('\\\\', '\\t', '\\n', "\\r", '\"', '\/'), $result).'"';
Derek Jones811f4752010-03-02 18:13:59 -0600867 }
868 elseif (is_scalar($result))
869 {
870 return $result;
871 }
872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Derek Jones9d653ed2010-03-05 09:52:53 -0600874 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600875}
Derek Jones9d653ed2010-03-05 09:52:53 -0600876// END Javascript Class
Derek Jones811f4752010-03-02 18:13:59 -0600877
878/* End of file Javascript.php */
Andrey Andreevad47f942011-12-25 19:13:48 +0200879/* Location: ./system/libraries/Javascript.php */