1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
6 * An open source application development framework for PHP 4.3.2 or newer
9 * @author ExpressionEngine Dev Team
10 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
11 * @license http://www.codeigniter.com/user_guide/license.html
12 * @link http://www.codeigniter.com
20 * @package CodeIgniter
21 * @subpackage Libraries
22 * @author ExpressionEngine Dev Team
24 * @link http://www.codeigniter.com/user_guide/libraries/javascript.html
27 class CI_Jquery extends CI_Javascript {
29 var $_javascript_folder = 'js';
30 var $jquery_code_for_load = array();
31 var $jquery_code_for_compile = array();
32 var $jquery_corner_active = FALSE;
33 var $jquery_table_sorter_active = FALSE;
34 var $jquery_table_sorter_pager_active = FALSE;
35 var $jquery_ajax_img = '';
37 public function __construct($params)
39 $this->CI =& get_instance();
42 if ($autoload === TRUE)
47 log_message('debug', "Jquery Class Initialized");
50 // --------------------------------------------------------------------
52 // --------------------------------------------------------------------
57 * Outputs a jQuery blur event
60 * @param string The element to attach the event to
61 * @param string The code to execute
64 function _blur($element = 'this', $js = '')
66 return $this->_add_event($element, $js, 'blur');
69 // --------------------------------------------------------------------
74 * Outputs a jQuery change event
77 * @param string The element to attach the event to
78 * @param string The code to execute
81 function _change($element = 'this', $js = '')
83 return $this->_add_event($element, $js, 'change');
86 // --------------------------------------------------------------------
91 * Outputs a jQuery click event
94 * @param string The element to attach the event to
95 * @param string The code to execute
96 * @param boolean whether or not to return false
99 function _click($element = 'this', $js = '', $ret_false = TRUE)
101 if ( ! is_array($js))
108 $js[] = "return false;";
111 return $this->_add_event($element, $js, 'click');
114 // --------------------------------------------------------------------
119 * Outputs a jQuery dblclick event
122 * @param string The element to attach the event to
123 * @param string The code to execute
126 function _dblclick($element = 'this', $js = '')
128 return $this->_add_event($element, $js, 'dblclick');
131 // --------------------------------------------------------------------
136 * Outputs a jQuery error event
139 * @param string The element to attach the event to
140 * @param string The code to execute
143 function _error($element = 'this', $js = '')
145 return $this->_add_event($element, $js, 'error');
148 // --------------------------------------------------------------------
153 * Outputs a jQuery focus event
156 * @param string The element to attach the event to
157 * @param string The code to execute
160 function _focus($element = 'this', $js = '')
162 return $this->_add_event($element, $js, 'focus');
165 // --------------------------------------------------------------------
170 * Outputs a jQuery hover event
173 * @param string - element
174 * @param string - Javascript code for mouse over
175 * @param string - Javascript code for mouse out
178 function _hover($element = 'this', $over, $out)
180 $event = "\n\t$(" . $this->_prep_element($element) . ").hover(\n\t\tfunction()\n\t\t{\n\t\t\t{$over}\n\t\t}, \n\t\tfunction()\n\t\t{\n\t\t\t{$out}\n\t\t});\n";
182 $this->jquery_code_for_compile[] = $event;
187 // --------------------------------------------------------------------
192 * Outputs a jQuery keydown event
195 * @param string The element to attach the event to
196 * @param string The code to execute
199 function _keydown($element = 'this', $js = '')
201 return $this->_add_event($element, $js, 'keydown');
204 // --------------------------------------------------------------------
209 * Outputs a jQuery keydown event
212 * @param string The element to attach the event to
213 * @param string The code to execute
216 function _keyup($element = 'this', $js = '')
218 return $this->_add_event($element, $js, 'keyup');
221 // --------------------------------------------------------------------
226 * Outputs a jQuery load event
229 * @param string The element to attach the event to
230 * @param string The code to execute
233 function _load($element = 'this', $js = '')
235 return $this->_add_event($element, $js, 'load');
238 // --------------------------------------------------------------------
243 * Outputs a jQuery mousedown event
246 * @param string The element to attach the event to
247 * @param string The code to execute
250 function _mousedown($element = 'this', $js = '')
252 return $this->_add_event($element, $js, 'mousedown');
255 // --------------------------------------------------------------------
260 * Outputs a jQuery mouseout event
263 * @param string The element to attach the event to
264 * @param string The code to execute
267 function _mouseout($element = 'this', $js = '')
269 return $this->_add_event($element, $js, 'mouseout');
272 // --------------------------------------------------------------------
277 * Outputs a jQuery mouseover event
280 * @param string The element to attach the event to
281 * @param string The code to execute
284 function _mouseover($element = 'this', $js = '')
286 return $this->_add_event($element, $js, 'mouseover');
289 // --------------------------------------------------------------------
294 * Outputs a jQuery mouseup event
297 * @param string The element to attach the event to
298 * @param string The code to execute
301 function _mouseup($element = 'this', $js = '')
303 return $this->_add_event($element, $js, 'mouseup');
306 // --------------------------------------------------------------------
311 * Outputs script directly
314 * @param string The element to attach the event to
315 * @param string The code to execute
318 function _output($array_js = '')
320 if ( ! is_array($array_js))
322 $array_js = array($array_js);
325 foreach ($array_js as $js)
327 $this->jquery_code_for_compile[] = "\t$js\n";
331 // --------------------------------------------------------------------
336 * Outputs a jQuery resize event
339 * @param string The element to attach the event to
340 * @param string The code to execute
343 function _resize($element = 'this', $js = '')
345 return $this->_add_event($element, $js, 'resize');
348 // --------------------------------------------------------------------
353 * Outputs a jQuery scroll event
356 * @param string The element to attach the event to
357 * @param string The code to execute
360 function _scroll($element = 'this', $js = '')
362 return $this->_add_event($element, $js, 'scroll');
365 // --------------------------------------------------------------------
370 * Outputs a jQuery unload event
373 * @param string The element to attach the event to
374 * @param string The code to execute
377 function _unload($element = 'this', $js = '')
379 return $this->_add_event($element, $js, 'unload');
382 // --------------------------------------------------------------------
384 // --------------------------------------------------------------------
389 * Outputs a jQuery addClass event
392 * @param string - element
395 function _addClass($element = 'this', $class='')
397 $element = $this->_prep_element($element);
398 $str = "$({$element}).addClass(\"$class\");";
402 // --------------------------------------------------------------------
407 * Outputs a jQuery animate event
410 * @param string - element
411 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
412 * @param string - Javascript callback function
415 function _animate($element = 'this', $params = array(), $speed = '', $extra = '')
417 $element = $this->_prep_element($element);
418 $speed = $this->_validate_speed($speed);
420 $animations = "\t\t\t";
422 foreach ($params as $param=>$value)
424 $animations .= $param.': \''.$value.'\', ';
427 $animations = substr($animations, 0, -2); // remove the last ", "
431 $speed = ', '.$speed;
436 $extra = ', '.$extra;
439 $str = "$({$element}).animate({\n$animations\n\t\t}".$speed.$extra.");";
444 // --------------------------------------------------------------------
449 * Outputs a jQuery hide event
452 * @param string - element
453 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
454 * @param string - Javascript callback function
457 function _fadeIn($element = 'this', $speed = '', $callback = '')
459 $element = $this->_prep_element($element);
460 $speed = $this->_validate_speed($speed);
464 $callback = ", function(){\n{$callback}\n}";
467 $str = "$({$element}).fadeIn({$speed}{$callback});";
472 // --------------------------------------------------------------------
477 * Outputs a jQuery hide event
480 * @param string - element
481 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
482 * @param string - Javascript callback function
485 function _fadeOut($element = 'this', $speed = '', $callback = '')
487 $element = $this->_prep_element($element);
488 $speed = $this->_validate_speed($speed);
492 $callback = ", function(){\n{$callback}\n}";
495 $str = "$({$element}).fadeOut({$speed}{$callback});";
500 // --------------------------------------------------------------------
505 * Outputs a jQuery hide action
508 * @param string - element
509 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
510 * @param string - Javascript callback function
513 function _hide($element = 'this', $speed = '', $callback = '')
515 $element = $this->_prep_element($element);
516 $speed = $this->_validate_speed($speed);
520 $callback = ", function(){\n{$callback}\n}";
523 $str = "$({$element}).hide({$speed}{$callback});";
528 // --------------------------------------------------------------------
533 * Outputs a jQuery remove class event
536 * @param string - element
539 function _removeClass($element = 'this', $class='')
541 $element = $this->_prep_element($element);
542 $str = "$({$element}).removeClass(\"$class\");";
546 // --------------------------------------------------------------------
551 * Outputs a jQuery slideUp event
554 * @param string - element
555 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
556 * @param string - Javascript callback function
559 function _slideUp($element = 'this', $speed = '', $callback = '')
561 $element = $this->_prep_element($element);
562 $speed = $this->_validate_speed($speed);
566 $callback = ", function(){\n{$callback}\n}";
569 $str = "$({$element}).slideUp({$speed}{$callback});";
574 // --------------------------------------------------------------------
579 * Outputs a jQuery slideDown event
582 * @param string - element
583 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
584 * @param string - Javascript callback function
587 function _slideDown($element = 'this', $speed = '', $callback = '')
589 $element = $this->_prep_element($element);
590 $speed = $this->_validate_speed($speed);
594 $callback = ", function(){\n{$callback}\n}";
597 $str = "$({$element}).slideDown({$speed}{$callback});";
602 // --------------------------------------------------------------------
607 * Outputs a jQuery slideToggle event
610 * @param string - element
611 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
612 * @param string - Javascript callback function
615 function _slideToggle($element = 'this', $speed = '', $callback = '')
617 $element = $this->_prep_element($element);
618 $speed = $this->_validate_speed($speed);
622 $callback = ", function(){\n{$callback}\n}";
625 $str = "$({$element}).slideToggle({$speed}{$callback});";
630 // --------------------------------------------------------------------
635 * Outputs a jQuery toggle event
638 * @param string - element
641 function _toggle($element = 'this')
643 $element = $this->_prep_element($element);
644 $str = "$({$element}).toggle();";
648 // --------------------------------------------------------------------
653 * Outputs a jQuery toggle class event
656 * @param string - element
659 function _toggleClass($element = 'this', $class='')
661 $element = $this->_prep_element($element);
662 $str = "$({$element}).toggleClass(\"$class\");";
666 // --------------------------------------------------------------------
671 * Outputs a jQuery show event
674 * @param string - element
675 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
676 * @param string - Javascript callback function
679 function _show($element = 'this', $speed = '', $callback = '')
681 $element = $this->_prep_element($element);
682 $speed = $this->_validate_speed($speed);
686 $callback = ", function(){\n{$callback}\n}";
689 $str = "$({$element}).show({$speed}{$callback});";
694 // --------------------------------------------------------------------
699 * An Ajax call that populates the designated DOM node with
703 * @param string The element to attach the event to
704 * @param string the controller to run the call against
705 * @param string optional parameters
709 function _updater($container = 'this', $controller, $options = '')
711 $container = $this->_prep_element($container);
713 $controller = (strpos('://', $controller) === FALSE) ? $controller : $this->CI->config->site_url($controller);
715 // ajaxStart and ajaxStop are better choices here... but this is a stop gap
716 if ($this->CI->config->item('javascript_ajax_img') == '')
718 $loading_notifier = "Loading...";
722 $loading_notifier = '<img src=\'' . $this->CI->config->slash_item('base_url') . $this->CI->config->item('javascript_ajax_img') . '\' alt=\'Loading\' />';
725 $updater = "$($container).empty();\n"; // anything that was in... get it out
726 $updater .= "\t\t$($container).prepend(\"$loading_notifier\");\n"; // to replace with an image
728 $request_options = '';
731 $request_options .= ", {";
732 $request_options .= (is_array($options)) ? "'".implode("', '", $options)."'" : "'".str_replace(":", "':'", $options)."'";
733 $request_options .= "}";
736 $updater .= "\t\t$($container).load('$controller'$request_options);";
741 // --------------------------------------------------------------------
742 // Pre-written handy stuff
743 // --------------------------------------------------------------------
749 * @param string table name
750 * @param string plugin location
753 function _zebraTables($class = '', $odd = 'odd', $hover = '')
755 $class = ($class != '') ? '.'.$class : '';
757 $zebra = "\t\$(\"table{$class} tbody tr:nth-child(even)\").addClass(\"{$odd}\");";
759 $this->jquery_code_for_compile[] = $zebra;
763 $hover = $this->hover("table{$class} tbody tr", "$(this).addClass('hover');", "$(this).removeClass('hover');");
771 // --------------------------------------------------------------------
773 // --------------------------------------------------------------------
778 * http://www.malsup.com/jquery/corner/
781 * @param string target
784 function corner($element = '', $corner_style = '')
786 // may want to make this configurable down the road
787 $corner_location = '/plugins/jquery.corner.js';
789 if ($corner_style != '')
791 $corner_style = '"'.$corner_style.'"';
794 return "$(" . $this->_prep_element($element) . ").corner(".$corner_style.");";
797 // --------------------------------------------------------------------
802 * Load a thickbox modal window
807 function modal($src, $relative = FALSE)
809 $this->jquery_code_for_load[] = $this->external($src, $relative);
812 // --------------------------------------------------------------------
817 * Load an Effect library
822 function effect($src, $relative = FALSE)
824 $this->jquery_code_for_load[] = $this->external($src, $relative);
827 // --------------------------------------------------------------------
832 * Load a plugin library
837 function plugin($src, $relative = FALSE)
839 $this->jquery_code_for_load[] = $this->external($src, $relative);
842 // --------------------------------------------------------------------
847 * Load a user interface library
852 function ui($src, $relative = FALSE)
854 $this->jquery_code_for_load[] = $this->external($src, $relative);
856 // --------------------------------------------------------------------
861 * Creates a jQuery sortable
866 function sortable($element, $options = array())
869 if (count($options) > 0)
871 $sort_options = array();
872 foreach ($options as $k=>$v)
874 $sort_options[] = "\n\t\t".$k.': '.$v."";
876 $sort_options = implode(",", $sort_options);
883 return "$(" . $this->_prep_element($element) . ").sortable({".$sort_options."\n\t});";
886 // --------------------------------------------------------------------
889 * Table Sorter Plugin
892 * @param string table name
893 * @param string plugin location
896 function tablesorter($table = '', $options = '')
898 $this->jquery_code_for_compile[] = "\t$(" . $this->_prep_element($table) . ").tablesorter($options);\n";
901 // --------------------------------------------------------------------
903 // --------------------------------------------------------------------
908 * Constructs the syntax for an event, and adds to into the array for compilation
911 * @param string The element to attach the event to
912 * @param string The code to execute
913 * @param string The event to pass
916 function _add_event($element, $js, $event)
920 $js = implode("\n\t\t", $js);
924 $event = "\n\t$(" . $this->_prep_element($element) . ").{$event}(function(){\n\t\t{$js}\n\t});\n";
925 $this->jquery_code_for_compile[] = $event;
929 // --------------------------------------------------------------------
934 * As events are specified, they are stored in an array
935 * This funciton compiles them all for output on a page
940 function _compile($view_var = 'script_foot', $script_tags = TRUE)
942 // External references
943 $external_scripts = implode('', $this->jquery_code_for_load);
944 $this->CI->load->vars(array('library_src' => $external_scripts));
946 if (count($this->jquery_code_for_compile) == 0 )
948 // no inline references, let's just return
953 $script = '$(document).ready(function() {' . "\n";
954 $script .= implode('', $this->jquery_code_for_compile);
957 $output = ($script_tags === FALSE) ? $script : $this->inline($script);
959 $this->CI->load->vars(array($view_var => $output));
963 // --------------------------------------------------------------------
968 * Clears the array of script events collected for output
973 function _clear_compile()
975 $this->jquery_code_for_compile = array();
978 // --------------------------------------------------------------------
983 * A wrapper for writing document.ready()
988 function _document_ready($js)
990 if ( ! is_array($js))
996 foreach ($js as $script)
998 $this->jquery_code_for_compile[] = $script;
1002 // --------------------------------------------------------------------
1007 * Outputs the script tag that loads the jquery.js file into an HTML document
1013 function script($library_src = '', $relative = FALSE)
1015 $library_src = $this->external($library_src, $relative);
1016 $this->jquery_code_for_load[] = $library_src;
1017 return $library_src;
1020 // --------------------------------------------------------------------
1025 * Puts HTML element in quotes for use in jQuery code
1026 * unless the supplied element is the Javascript 'this'
1027 * object, in which case no quotes are added
1033 function _prep_element($element)
1035 if ($element != 'this')
1037 $element = '"'.$element.'"';
1043 // --------------------------------------------------------------------
1048 * Ensures the speed parameter is valid for jQuery
1054 function _validate_speed($speed)
1056 if (in_array($speed, array('slow', 'normal', 'fast')))
1058 $speed = '"'.$speed.'"';
1060 elseif (preg_match("/[^0-9]/", $speed))
1070 /* End of file Jquery.php */
1071 /* Location: ./system/libraries/Jquery.php */