1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Javascript
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/libraries/javascript.html
29 var $_javascript_location = 'js';
31 public function __construct($params = array())
33 $defaults = array('js_library_driver' => 'jquery', 'autoload' => TRUE);
35 foreach ($defaults as $key => $val)
37 if (isset($params[$key]) && $params[$key] !== "")
39 $defaults[$key] = $params[$key];
45 $this->CI =& get_instance();
47 // load the requested js library
48 $this->CI->load->library('javascript/'.$js_library_driver, array('autoload' => $autoload));
49 // make js to refer to current library
50 $this->js =& $this->CI->$js_library_driver;
52 log_message('debug', "Javascript Class Initialized and loaded. Driver used: $js_library_driver");
55 // --------------------------------------------------------------------
57 // --------------------------------------------------------------------
62 * Outputs a javascript library blur event
65 * @param string The element to attach the event to
66 * @param string The code to execute
69 function blur($element = 'this', $js = '')
71 return $this->js->_blur($element, $js);
74 // --------------------------------------------------------------------
79 * Outputs a javascript library change event
82 * @param string The element to attach the event to
83 * @param string The code to execute
86 function change($element = 'this', $js = '')
88 return $this->js->_change($element, $js);
91 // --------------------------------------------------------------------
96 * Outputs a javascript library click event
99 * @param string The element to attach the event to
100 * @param string The code to execute
101 * @param boolean whether or not to return false
104 function click($element = 'this', $js = '', $ret_false = TRUE)
106 return $this->js->_click($element, $js, $ret_false);
109 // --------------------------------------------------------------------
114 * Outputs a javascript library dblclick event
117 * @param string The element to attach the event to
118 * @param string The code to execute
121 function dblclick($element = 'this', $js = '')
123 return $this->js->_dblclick($element, $js);
126 // --------------------------------------------------------------------
131 * Outputs a javascript library error event
134 * @param string The element to attach the event to
135 * @param string The code to execute
138 function error($element = 'this', $js = '')
140 return $this->js->_error($element, $js);
143 // --------------------------------------------------------------------
148 * Outputs a javascript library focus event
151 * @param string The element to attach the event to
152 * @param string The code to execute
155 function focus($element = 'this', $js = '')
157 return $this->js->__add_event($focus, $js);
160 // --------------------------------------------------------------------
165 * Outputs a javascript library hover event
168 * @param string - element
169 * @param string - Javascript code for mouse over
170 * @param string - Javascript code for mouse out
173 function hover($element = 'this', $over, $out)
175 return $this->js->__hover($element, $over, $out);
178 // --------------------------------------------------------------------
183 * Outputs a javascript library keydown event
186 * @param string The element to attach the event to
187 * @param string The code to execute
190 function keydown($element = 'this', $js = '')
192 return $this->js->_keydown($element, $js);
195 // --------------------------------------------------------------------
200 * Outputs a javascript library keydown event
203 * @param string The element to attach the event to
204 * @param string The code to execute
207 function keyup($element = 'this', $js = '')
209 return $this->js->_keyup($element, $js);
212 // --------------------------------------------------------------------
217 * Outputs a javascript library load event
220 * @param string The element to attach the event to
221 * @param string The code to execute
224 function load($element = 'this', $js = '')
226 return $this->js->_load($element, $js);
229 // --------------------------------------------------------------------
234 * Outputs a javascript library mousedown event
237 * @param string The element to attach the event to
238 * @param string The code to execute
241 function mousedown($element = 'this', $js = '')
243 return $this->js->_mousedown($element, $js);
246 // --------------------------------------------------------------------
251 * Outputs a javascript library mouseout event
254 * @param string The element to attach the event to
255 * @param string The code to execute
258 function mouseout($element = 'this', $js = '')
260 return $this->js->_mouseout($element, $js);
263 // --------------------------------------------------------------------
268 * Outputs a javascript library mouseover event
271 * @param string The element to attach the event to
272 * @param string The code to execute
275 function mouseover($element = 'this', $js = '')
277 return $this->js->_mouseover($element, $js);
280 // --------------------------------------------------------------------
285 * Outputs a javascript library mouseup event
288 * @param string The element to attach the event to
289 * @param string The code to execute
292 function mouseup($element = 'this', $js = '')
294 return $this->js->_mouseup($element, $js);
297 // --------------------------------------------------------------------
302 * Outputs the called javascript to the screen
305 * @param string The code to output
310 return $this->js->_output($js);
313 // --------------------------------------------------------------------
318 * Outputs a javascript library mouseup event
321 * @param string The element to attach the event to
322 * @param string The code to execute
327 return $this->js->_document_ready($js);
330 // --------------------------------------------------------------------
335 * Outputs a javascript library resize event
338 * @param string The element to attach the event to
339 * @param string The code to execute
342 function resize($element = 'this', $js = '')
344 return $this->js->_resize($element, $js);
347 // --------------------------------------------------------------------
352 * Outputs a javascript library scroll event
355 * @param string The element to attach the event to
356 * @param string The code to execute
359 function scroll($element = 'this', $js = '')
361 return $this->js->_scroll($element, $js);
364 // --------------------------------------------------------------------
369 * Outputs a javascript library unload event
372 * @param string The element to attach the event to
373 * @param string The code to execute
376 function unload($element = 'this', $js = '')
378 return $this->js->_unload($element, $js);
381 // --------------------------------------------------------------------
383 // --------------------------------------------------------------------
389 * Outputs a javascript library addClass event
392 * @param string - element
393 * @param string - Class to add
396 function addClass($element = 'this', $class = '')
398 return $this->js->_addClass($element, $class);
401 // --------------------------------------------------------------------
406 * Outputs a javascript library animate event
409 * @param string - element
410 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
411 * @param string - Javascript callback function
414 function animate($element = 'this', $params = array(), $speed = '', $extra = '')
416 return $this->js->_animate($element, $params, $speed, $extra);
419 // --------------------------------------------------------------------
424 * Outputs a javascript library hide event
427 * @param string - element
428 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
429 * @param string - Javascript callback function
432 function fadeIn($element = 'this', $speed = '', $callback = '')
434 return $this->js->_fadeIn($element, $speed, $callback);
437 // --------------------------------------------------------------------
442 * Outputs a javascript library hide event
445 * @param string - element
446 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
447 * @param string - Javascript callback function
450 function fadeOut($element = 'this', $speed = '', $callback = '')
452 return $this->js->_fadeOut($element, $speed, $callback);
454 // --------------------------------------------------------------------
459 * Outputs a javascript library slideUp event
462 * @param string - element
463 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
464 * @param string - Javascript callback function
467 function slideUp($element = 'this', $speed = '', $callback = '')
469 return $this->js->_slideUp($element, $speed, $callback);
473 // --------------------------------------------------------------------
478 * Outputs a javascript library removeClass event
481 * @param string - element
482 * @param string - Class to add
485 function removeClass($element = 'this', $class = '')
487 return $this->js->_removeClass($element, $class);
490 // --------------------------------------------------------------------
495 * Outputs a javascript library slideDown event
498 * @param string - element
499 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
500 * @param string - Javascript callback function
503 function slideDown($element = 'this', $speed = '', $callback = '')
505 return $this->js->_slideDown($element, $speed, $callback);
508 // --------------------------------------------------------------------
513 * Outputs a javascript library slideToggle event
516 * @param string - element
517 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
518 * @param string - Javascript callback function
521 function slideToggle($element = 'this', $speed = '', $callback = '')
523 return $this->js->_slideToggle($element, $speed, $callback);
527 // --------------------------------------------------------------------
532 * Outputs a javascript library hide action
535 * @param string - element
536 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
537 * @param string - Javascript callback function
540 function hide($element = 'this', $speed = '', $callback = '')
542 return $this->js->_hide($element, $speed, $callback);
545 // --------------------------------------------------------------------
550 * Outputs a javascript library toggle event
553 * @param string - element
556 function toggle($element = 'this')
558 return $this->js->_toggle($element);
562 // --------------------------------------------------------------------
567 * Outputs a javascript library toggle class event
570 * @param string - element
573 function toggleClass($element = 'this', $class='')
575 return $this->js->_toggleClass($element, $class);
578 // --------------------------------------------------------------------
583 * Outputs a javascript library show event
586 * @param string - element
587 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
588 * @param string - Javascript callback function
591 function show($element = 'this', $speed = '', $callback = '')
593 return $this->js->_show($element, $speed, $callback);
597 // --------------------------------------------------------------------
602 * gather together all script needing to be output
605 * @param string The element to attach the event to
608 function compile($view_var = 'script_foot', $script_tags = TRUE)
610 $this->js->_compile($view_var, $script_tags);
616 * Clears any previous javascript collected for output
621 function clear_compile()
623 $this->js->_clear_compile();
626 // --------------------------------------------------------------------
631 * Outputs a <script> tag with the source as an external js file
634 * @param string The element to attach the event to
637 function external($external_file = '', $relative = FALSE)
639 if ($external_file !== '')
641 $this->_javascript_location = $external_file;
645 if ($this->CI->config->item('javascript_location') != '')
647 $this->_javascript_location = $this->CI->config->item('javascript_location');
651 if ($relative === TRUE OR strncmp($external_file, 'http://', 7) == 0 OR strncmp($external_file, 'https://', 8) == 0)
653 $str = $this->_open_script($external_file);
655 elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
657 $str = $this->_open_script($this->_javascript_location.$external_file);
661 $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location.$external_file);
664 $str .= $this->_close_script();
668 // --------------------------------------------------------------------
673 * Outputs a <script> tag
676 * @param string The element to attach the event to
677 * @param boolean If a CDATA section should be added
680 function inline($script, $cdata = TRUE)
682 $str = $this->_open_script();
683 $str .= ($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n";
684 $str .= $this->_close_script();
689 // --------------------------------------------------------------------
694 * Outputs an opening <script>
700 function _open_script($src = '')
702 $str = '<script type="text/javascript" charset="'.strtolower($this->CI->config->item('charset')).'"';
703 $str .= ($src == '') ? '>' : ' src="'.$src.'">';
707 // --------------------------------------------------------------------
712 * Outputs an closing </script>
718 function _close_script($extra = "\n")
720 return "</script>$extra";
724 // --------------------------------------------------------------------
725 // --------------------------------------------------------------------
726 // AJAX-Y STUFF - still a testbed
727 // --------------------------------------------------------------------
728 // --------------------------------------------------------------------
733 * Outputs a javascript library slideDown event
736 * @param string - element
737 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
738 * @param string - Javascript callback function
741 function update($element = 'this', $speed = '', $callback = '')
743 return $this->js->_updater($element, $speed, $callback);
746 // --------------------------------------------------------------------
751 * Can be passed a database result or associative array and returns a JSON formatted string
753 * @param mixed result set or array
754 * @param bool match array types (defaults to objects)
755 * @return string a json formatted string
757 function generate_json($result = NULL, $match_array_type = FALSE)
759 // JSON data can optionally be passed to this function
760 // either as a database result object or an array, or a user supplied array
761 if ( ! is_null($result))
763 if (is_object($result))
765 $json_result = $result->result_array();
767 elseif (is_array($result))
769 $json_result = $result;
773 return $this->_prep_args($result);
784 if ( ! is_array($json_result) AND empty($json_result))
786 show_error("Generate JSON Failed - Illegal key, value pair.");
788 elseif ($match_array_type)
790 $_is_assoc = $this->_is_associative_array($json_result);
793 foreach ($json_result as $k => $v)
797 $json[] = $this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
801 $json[] = $this->generate_json($v, $match_array_type);
805 $json = implode(',', $json);
807 return $_is_assoc ? "{".$json."}" : "[".$json."]";
811 // --------------------------------------------------------------------
814 * Is associative array
816 * Checks for an associative array
822 function _is_associative_array($arr)
824 foreach (array_keys($arr) as $key => $val)
835 // --------------------------------------------------------------------
840 * Ensures a standard json value and escapes values
846 function _prep_args($result, $is_key = FALSE)
848 if (is_null($result))
852 elseif (is_bool($result))
854 return ($result === TRUE) ? 'true' : 'false';
856 elseif (is_string($result) OR $is_key)
858 return '"'.str_replace(array('\\', "\t", "\n", "\r", '"', '/'), array('\\\\', '\\t', '\\n', "\\r", '\"', '\/'), $result).'"';
860 elseif (is_scalar($result))
866 // --------------------------------------------------------------------
868 // END Javascript Class
870 /* End of file Javascript.php */
871 /* Location: ./system/libraries/Javascript.php */