blob: 15887cb95680df68a47cb6f3e979732c371e57a2 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
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
41 var $_javascript_location = 'js';
42
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
Derek Jones37f4b9c2011-07-01 17:56:50 -050067 // --------------------------------------------------------------------
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 */
81 function blur($element = 'this', $js = '')
82 {
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 */
98 function change($element = 'this', $js = '')
99 {
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 */
116 function click($element = 'this', $js = '', $ret_false = TRUE)
117 {
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 */
133 function dblclick($element = 'this', $js = '')
134 {
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 */
150 function error($element = 'this', $js = '')
151 {
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 */
167 function focus($element = 'this', $js = '')
168 {
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 */
185 function hover($element = 'this', $over, $out)
186 {
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 */
202 function keydown($element = 'this', $js = '')
203 {
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 */
219 function keyup($element = 'this', $js = '')
220 {
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 */
236 function load($element = 'this', $js = '')
237 {
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 */
253 function mousedown($element = 'this', $js = '')
254 {
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 */
270 function mouseout($element = 'this', $js = '')
271 {
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 */
287 function mouseover($element = 'this', $js = '')
288 {
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 */
304 function mouseup($element = 'this', $js = '')
305 {
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 */
320 function output($js)
321 {
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 */
337 function ready($js)
338 {
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 */
354 function resize($element = 'this', $js = '')
355 {
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 */
371 function scroll($element = 'this', $js = '')
372 {
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 */
388 function unload($element = 'this', $js = '')
389 {
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 */
408 function addClass($element = 'this', $class = '')
409 {
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 */
426 function animate($element = 'this', $params = array(), $speed = '', $extra = '')
427 {
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 */
444 function fadeIn($element = 'this', $speed = '', $callback = '')
445 {
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 */
462 function fadeOut($element = 'this', $speed = '', $callback = '')
463 {
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 */
479 function slideUp($element = 'this', $speed = '', $callback = '')
480 {
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 */
497 function removeClass($element = 'this', $class = '')
498 {
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 */
515 function slideDown($element = 'this', $speed = '', $callback = '')
516 {
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 */
533 function slideToggle($element = 'this', $speed = '', $callback = '')
534 {
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 */
552 function hide($element = 'this', $speed = '', $callback = '')
553 {
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 */
568 function toggle($element = 'this')
569 {
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 */
585 function toggleClass($element = 'this', $class='')
586 {
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 */
603 function show($element = 'this', $speed = '', $callback = '')
604 {
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 */
Derek Jones811f4752010-03-02 18:13:59 -0600620 function compile($view_var = 'script_foot', $script_tags = TRUE)
621 {
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 */
633 function clear_compile()
634 {
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 */
649 function external($external_file = '', $relative = FALSE)
650 {
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
Derek Jones811f4752010-03-02 18:13:59 -0600663 if ($relative === TRUE OR strncmp($external_file, 'http://', 7) == 0 OR strncmp($external_file, 'https://', 8) == 0)
664 {
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
676 $str .= $this->_close_script();
677 return $str;
678 }
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Jones811f4752010-03-02 18:13:59 -0600680 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200681
Derek Jones811f4752010-03-02 18:13:59 -0600682 /**
683 * Inline
684 *
Barry Mienydd671972010-10-04 16:33:58 +0200685 * Outputs a <script> tag
Derek Jones811f4752010-03-02 18:13:59 -0600686 *
687 * @access public
688 * @param string The element to attach the event to
689 * @param boolean If a CDATA section should be added
690 * @return string
691 */
692 function inline($script, $cdata = TRUE)
693 {
694 $str = $this->_open_script();
695 $str .= ($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n";
696 $str .= $this->_close_script();
Barry Mienydd671972010-10-04 16:33:58 +0200697
Derek Jones811f4752010-03-02 18:13:59 -0600698 return $str;
699 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500700
Barry Mienydd671972010-10-04 16:33:58 +0200701 // --------------------------------------------------------------------
702
Derek Jones811f4752010-03-02 18:13:59 -0600703 /**
704 * Open Script
705 *
Barry Mienydd671972010-10-04 16:33:58 +0200706 * Outputs an opening <script>
Derek Jones811f4752010-03-02 18:13:59 -0600707 *
708 * @access private
Barry Mienydd671972010-10-04 16:33:58 +0200709 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600710 * @return string
711 */
712 function _open_script($src = '')
713 {
714 $str = '<script type="text/javascript" charset="'.strtolower($this->CI->config->item('charset')).'"';
715 $str .= ($src == '') ? '>' : ' src="'.$src.'">';
716 return $str;
717 }
718
719 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200720
Derek Jones811f4752010-03-02 18:13:59 -0600721 /**
722 * Close Script
723 *
Barry Mienydd671972010-10-04 16:33:58 +0200724 * Outputs an closing </script>
Derek Jones811f4752010-03-02 18:13:59 -0600725 *
726 * @access private
Barry Mienydd671972010-10-04 16:33:58 +0200727 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600728 * @return string
729 */
730 function _close_script($extra = "\n")
731 {
732 return "</script>$extra";
733 }
Barry Mienydd671972010-10-04 16:33:58 +0200734
735
Derek Jones811f4752010-03-02 18:13:59 -0600736 // --------------------------------------------------------------------
737 // --------------------------------------------------------------------
738 // AJAX-Y STUFF - still a testbed
739 // --------------------------------------------------------------------
740 // --------------------------------------------------------------------
741
742 /**
743 * Update
744 *
745 * Outputs a javascript library slideDown event
746 *
747 * @access public
748 * @param string - element
749 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
750 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200751 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600752 */
753 function update($element = 'this', $speed = '', $callback = '')
754 {
755 return $this->js->_updater($element, $speed, $callback);
756 }
Barry Mienydd671972010-10-04 16:33:58 +0200757
Derek Jones811f4752010-03-02 18:13:59 -0600758 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200759
Derek Jones811f4752010-03-02 18:13:59 -0600760 /**
761 * Generate JSON
762 *
763 * Can be passed a database result or associative array and returns a JSON formatted string
764 *
765 * @param mixed result set or array
766 * @param bool match array types (defaults to objects)
767 * @return string a json formatted string
768 */
769 function generate_json($result = NULL, $match_array_type = FALSE)
770 {
771 // JSON data can optionally be passed to this function
772 // either as a database result object or an array, or a user supplied array
773 if ( ! is_null($result))
774 {
775 if (is_object($result))
776 {
777 $json_result = $result->result_array();
778 }
779 elseif (is_array($result))
780 {
781 $json_result = $result;
782 }
783 else
784 {
785 return $this->_prep_args($result);
786 }
787 }
788 else
789 {
790 return 'null';
791 }
792
793 $json = array();
794 $_is_assoc = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Jones811f4752010-03-02 18:13:59 -0600796 if ( ! is_array($json_result) AND empty($json_result))
797 {
798 show_error("Generate JSON Failed - Illegal key, value pair.");
799 }
800 elseif ($match_array_type)
801 {
802 $_is_assoc = $this->_is_associative_array($json_result);
803 }
804
805 foreach ($json_result as $k => $v)
806 {
807 if ($_is_assoc)
808 {
809 $json[] = $this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
810 }
811 else
812 {
813 $json[] = $this->generate_json($v, $match_array_type);
814 }
815 }
816
817 $json = implode(',', $json);
818
819 return $_is_assoc ? "{".$json."}" : "[".$json."]";
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Jones811f4752010-03-02 18:13:59 -0600821 }
Barry Mienydd671972010-10-04 16:33:58 +0200822
Derek Jones811f4752010-03-02 18:13:59 -0600823 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200824
Derek Jones811f4752010-03-02 18:13:59 -0600825 /**
826 * Is associative array
827 *
828 * Checks for an associative array
829 *
830 * @access public
831 * @param type
832 * @return type
833 */
834 function _is_associative_array($arr)
835 {
836 foreach (array_keys($arr) as $key => $val)
837 {
838 if ($key !== $val)
839 {
840 return TRUE;
841 }
842 }
Barry Mienydd671972010-10-04 16:33:58 +0200843
Derek Jones811f4752010-03-02 18:13:59 -0600844 return FALSE;
845 }
Barry Mienydd671972010-10-04 16:33:58 +0200846
Derek Jones811f4752010-03-02 18:13:59 -0600847 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200848
Derek Jones811f4752010-03-02 18:13:59 -0600849 /**
850 * Prep Args
851 *
852 * Ensures a standard json value and escapes values
853 *
854 * @access public
855 * @param type
856 * @return type
857 */
858 function _prep_args($result, $is_key = FALSE)
859 {
860 if (is_null($result))
861 {
862 return 'null';
863 }
864 elseif (is_bool($result))
865 {
866 return ($result === TRUE) ? 'true' : 'false';
867 }
868 elseif (is_string($result) OR $is_key)
869 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500870 return '"'.str_replace(array('\\', "\t", "\n", "\r", '"', '/'), array('\\\\', '\\t', '\\n', "\\r", '\"', '\/'), $result).'"';
Derek Jones811f4752010-03-02 18:13:59 -0600871 }
872 elseif (is_scalar($result))
873 {
874 return $result;
875 }
876 }
Barry Mienydd671972010-10-04 16:33:58 +0200877
Derek Jones9d653ed2010-03-05 09:52:53 -0600878 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600879}
Derek Jones9d653ed2010-03-05 09:52:53 -0600880// END Javascript Class
Derek Jones811f4752010-03-02 18:13:59 -0600881
882/* End of file Javascript.php */
883/* Location: ./system/libraries/Javascript.php */