blob: baa044117d8f77b44a661797eaff0005eb6cfbe9 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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/**
29 * Javascript Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Javascript
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
fesplugas3934a4a2010-10-04 09:07:49 +020035 * @link http://codeigniter.com/user_guide/libraries/javascript.html
Derek Jones9d653ed2010-03-05 09:52:53 -060036 */
Derek Jones811f4752010-03-02 18:13:59 -060037class CI_Javascript {
38
Andrey Andreevad47f942011-12-25 19:13:48 +020039 protected $_javascript_location = 'js';
Derek Jones811f4752010-03-02 18:13:59 -060040
Greg Akera9263282010-11-10 15:26:43 -060041 public function __construct($params = array())
Barry Mienydd671972010-10-04 16:33:58 +020042 {
Derek Jones811f4752010-03-02 18:13:59 -060043 $defaults = array('js_library_driver' => 'jquery', 'autoload' => TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Jones811f4752010-03-02 18:13:59 -060045 foreach ($defaults as $key => $val)
46 {
Andrey Andreev443bbd92012-04-03 15:49:11 +030047 if (isset($params[$key]) && $params[$key] !== '')
Derek Jones811f4752010-03-02 18:13:59 -060048 {
49 $defaults[$key] = $params[$key];
50 }
51 }
Barry Mienydd671972010-10-04 16:33:58 +020052
Derek Jones811f4752010-03-02 18:13:59 -060053 extract($defaults);
54
55 $this->CI =& get_instance();
56
57 // load the requested js library
Derek Jones9d653ed2010-03-05 09:52:53 -060058 $this->CI->load->library('javascript/'.$js_library_driver, array('autoload' => $autoload));
Derek Jones811f4752010-03-02 18:13:59 -060059 // make js to refer to current library
60 $this->js =& $this->CI->$js_library_driver;
Barry Mienydd671972010-10-04 16:33:58 +020061
Andrey Andreev443bbd92012-04-03 15:49:11 +030062 log_message('debug', 'Javascript Class Initialized and loaded. Driver used: '.$js_library_driver);
Derek Jones811f4752010-03-02 18:13:59 -060063 }
64
Andrey Andreevad47f942011-12-25 19:13:48 +020065 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020066 // Event Code
67 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -060068
69 /**
70 * Blur
71 *
72 * Outputs a javascript library blur event
73 *
Derek Jones811f4752010-03-02 18:13:59 -060074 * @param string The element to attach the event to
75 * @param string The code to execute
76 * @return string
77 */
Andrey Andreevad47f942011-12-25 19:13:48 +020078 public function blur($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -060079 {
80 return $this->js->_blur($element, $js);
81 }
Barry Mienydd671972010-10-04 16:33:58 +020082
Derek Jones811f4752010-03-02 18:13:59 -060083 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Jones811f4752010-03-02 18:13:59 -060085 /**
86 * Change
87 *
88 * Outputs a javascript library change event
89 *
Derek Jones811f4752010-03-02 18:13:59 -060090 * @param string The element to attach the event to
91 * @param string The code to execute
92 * @return string
93 */
Andrey Andreevad47f942011-12-25 19:13:48 +020094 public function change($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -060095 {
96 return $this->js->_change($element, $js);
97 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Jones811f4752010-03-02 18:13:59 -060099 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200100
Derek Jones811f4752010-03-02 18:13:59 -0600101 /**
102 * Click
103 *
104 * Outputs a javascript library click event
105 *
Derek Jones811f4752010-03-02 18:13:59 -0600106 * @param string The element to attach the event to
107 * @param string The code to execute
108 * @param boolean whether or not to return false
109 * @return string
110 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200111 public function click($element = 'this', $js = '', $ret_false = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600112 {
113 return $this->js->_click($element, $js, $ret_false);
114 }
115
116 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Jones811f4752010-03-02 18:13:59 -0600118 /**
119 * Double Click
120 *
121 * Outputs a javascript library dblclick event
122 *
Derek Jones811f4752010-03-02 18:13:59 -0600123 * @param string The element to attach the event to
124 * @param string The code to execute
125 * @return string
126 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200127 public function dblclick($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600128 {
129 return $this->js->_dblclick($element, $js);
130 }
131
132 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Jones811f4752010-03-02 18:13:59 -0600134 /**
135 * Error
136 *
137 * Outputs a javascript library error event
138 *
Derek Jones811f4752010-03-02 18:13:59 -0600139 * @param string The element to attach the event to
140 * @param string The code to execute
141 * @return string
142 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200143 public function error($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600144 {
145 return $this->js->_error($element, $js);
146 }
147
148 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Jones811f4752010-03-02 18:13:59 -0600150 /**
151 * Focus
152 *
153 * Outputs a javascript library focus event
154 *
Derek Jones811f4752010-03-02 18:13:59 -0600155 * @param string The element to attach the event to
156 * @param string The code to execute
157 * @return string
158 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200159 public function focus($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600160 {
161 return $this->js->__add_event($focus, $js);
162 }
163
164 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Jones811f4752010-03-02 18:13:59 -0600166 /**
167 * Hover
168 *
169 * Outputs a javascript library hover event
170 *
Derek Jones811f4752010-03-02 18:13:59 -0600171 * @param string - element
172 * @param string - Javascript code for mouse over
173 * @param string - Javascript code for mouse out
Barry Mienydd671972010-10-04 16:33:58 +0200174 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600175 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200176 public function hover($element = 'this', $over, $out)
Derek Jones811f4752010-03-02 18:13:59 -0600177 {
178 return $this->js->__hover($element, $over, $out);
179 }
180
181 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Jones811f4752010-03-02 18:13:59 -0600183 /**
184 * Keydown
185 *
186 * Outputs a javascript library keydown event
187 *
Derek Jones811f4752010-03-02 18:13:59 -0600188 * @param string The element to attach the event to
189 * @param string The code to execute
190 * @return string
191 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200192 public function keydown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600193 {
194 return $this->js->_keydown($element, $js);
195 }
196
197 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Jones811f4752010-03-02 18:13:59 -0600199 /**
200 * Keyup
201 *
202 * Outputs a javascript library keydown event
203 *
Derek Jones811f4752010-03-02 18:13:59 -0600204 * @param string The element to attach the event to
205 * @param string The code to execute
206 * @return string
207 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200208 public function keyup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600209 {
210 return $this->js->_keyup($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200211 }
Derek Jones811f4752010-03-02 18:13:59 -0600212
213 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Jones811f4752010-03-02 18:13:59 -0600215 /**
216 * Load
217 *
218 * Outputs a javascript library load event
219 *
Derek Jones811f4752010-03-02 18:13:59 -0600220 * @param string The element to attach the event to
221 * @param string The code to execute
222 * @return string
223 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200224 public function load($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600225 {
226 return $this->js->_load($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200227 }
228
Derek Jones811f4752010-03-02 18:13:59 -0600229 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Jones811f4752010-03-02 18:13:59 -0600231 /**
232 * Mousedown
233 *
234 * Outputs a javascript library mousedown event
235 *
Derek Jones811f4752010-03-02 18:13:59 -0600236 * @param string The element to attach the event to
237 * @param string The code to execute
238 * @return string
239 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200240 public function mousedown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600241 {
242 return $this->js->_mousedown($element, $js);
243 }
244
245 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Jones811f4752010-03-02 18:13:59 -0600247 /**
248 * Mouse Out
249 *
250 * Outputs a javascript library mouseout event
251 *
Derek Jones811f4752010-03-02 18:13:59 -0600252 * @param string The element to attach the event to
253 * @param string The code to execute
254 * @return string
255 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200256 public function mouseout($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600257 {
258 return $this->js->_mouseout($element, $js);
259 }
260
261 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Jones811f4752010-03-02 18:13:59 -0600263 /**
264 * Mouse Over
265 *
266 * Outputs a javascript library mouseover event
267 *
Derek Jones811f4752010-03-02 18:13:59 -0600268 * @param string The element to attach the event to
269 * @param string The code to execute
270 * @return string
271 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200272 public function mouseover($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600273 {
274 return $this->js->_mouseover($element, $js);
275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * Mouseup
281 *
282 * Outputs a javascript library mouseup event
283 *
Derek Jones811f4752010-03-02 18:13:59 -0600284 * @param string The element to attach the event to
285 * @param string The code to execute
286 * @return string
287 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200288 public function mouseup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600289 {
290 return $this->js->_mouseup($element, $js);
291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Output
297 *
298 * Outputs the called javascript to the screen
299 *
Derek Jones811f4752010-03-02 18:13:59 -0600300 * @param string The code to output
301 * @return string
302 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200303 public function output($js)
Derek Jones811f4752010-03-02 18:13:59 -0600304 {
305 return $this->js->_output($js);
306 }
307
308 // --------------------------------------------------------------------
309
310 /**
311 * Ready
312 *
313 * Outputs a javascript library mouseup event
314 *
Derek Jones811f4752010-03-02 18:13:59 -0600315 * @param string The element to attach the event to
316 * @param string The code to execute
317 * @return string
318 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200319 public function ready($js)
Derek Jones811f4752010-03-02 18:13:59 -0600320 {
321 return $this->js->_document_ready($js);
322 }
323
324 // --------------------------------------------------------------------
325
326 /**
327 * Resize
328 *
329 * Outputs a javascript library resize event
330 *
Derek Jones811f4752010-03-02 18:13:59 -0600331 * @param string The element to attach the event to
332 * @param string The code to execute
333 * @return string
334 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200335 public function resize($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600336 {
337 return $this->js->_resize($element, $js);
338 }
339
340 // --------------------------------------------------------------------
341
342 /**
343 * Scroll
344 *
345 * Outputs a javascript library scroll event
346 *
Derek Jones811f4752010-03-02 18:13:59 -0600347 * @param string The element to attach the event to
348 * @param string The code to execute
349 * @return string
350 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200351 public function scroll($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600352 {
353 return $this->js->_scroll($element, $js);
354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Jones811f4752010-03-02 18:13:59 -0600356 // --------------------------------------------------------------------
357
358 /**
359 * Unload
360 *
361 * Outputs a javascript library unload event
362 *
Derek Jones811f4752010-03-02 18:13:59 -0600363 * @param string The element to attach the event to
364 * @param string The code to execute
365 * @return string
366 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200367 public function unload($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600368 {
369 return $this->js->_unload($element, $js);
370 }
371
Andrey Andreev6aac0ba2011-12-27 02:34:00 +0200372 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200373 // Effects
374 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600375
Derek Jones811f4752010-03-02 18:13:59 -0600376 /**
377 * Add Class
378 *
379 * Outputs a javascript library addClass event
380 *
Derek Jones811f4752010-03-02 18:13:59 -0600381 * @param string - element
382 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200383 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600384 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200385 public function addClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600386 {
387 return $this->js->_addClass($element, $class);
388 }
389
Barry Mienydd671972010-10-04 16:33:58 +0200390 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600391
392 /**
393 * Animate
394 *
395 * Outputs a javascript library animate event
396 *
Derek Jones811f4752010-03-02 18:13:59 -0600397 * @param string - element
398 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
399 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200400 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600401 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200402 public function animate($element = 'this', $params = array(), $speed = '', $extra = '')
Derek Jones811f4752010-03-02 18:13:59 -0600403 {
404 return $this->js->_animate($element, $params, $speed, $extra);
405 }
406
407 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Jones811f4752010-03-02 18:13:59 -0600409 /**
410 * Fade In
411 *
412 * Outputs a javascript library hide event
413 *
Derek Jones811f4752010-03-02 18:13:59 -0600414 * @param string - element
415 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
416 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200417 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600418 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200419 public function fadeIn($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600420 {
421 return $this->js->_fadeIn($element, $speed, $callback);
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Jones811f4752010-03-02 18:13:59 -0600424 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Jones811f4752010-03-02 18:13:59 -0600426 /**
427 * Fade Out
428 *
429 * Outputs a javascript library hide event
430 *
Derek Jones811f4752010-03-02 18:13:59 -0600431 * @param string - element
432 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
433 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200434 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600435 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200436 public function fadeOut($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600437 {
438 return $this->js->_fadeOut($element, $speed, $callback);
439 }
440 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Jones811f4752010-03-02 18:13:59 -0600442 /**
443 * Slide Up
444 *
445 * Outputs a javascript library slideUp event
446 *
Derek Jones811f4752010-03-02 18:13:59 -0600447 * @param string - element
448 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
449 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200450 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600451 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200452 public function slideUp($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600453 {
454 return $this->js->_slideUp($element, $speed, $callback);
455
456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Jones811f4752010-03-02 18:13:59 -0600458 // --------------------------------------------------------------------
459
460 /**
461 * Remove Class
462 *
463 * Outputs a javascript library removeClass event
464 *
Derek Jones811f4752010-03-02 18:13:59 -0600465 * @param string - element
466 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200467 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600468 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200469 public function removeClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600470 {
471 return $this->js->_removeClass($element, $class);
472 }
473
Barry Mienydd671972010-10-04 16:33:58 +0200474 // --------------------------------------------------------------------
475
Derek Jones811f4752010-03-02 18:13:59 -0600476 /**
477 * Slide Down
478 *
479 * Outputs a javascript library slideDown event
480 *
Derek Jones811f4752010-03-02 18:13:59 -0600481 * @param string - element
482 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
483 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200484 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600485 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200486 public function slideDown($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600487 {
488 return $this->js->_slideDown($element, $speed, $callback);
489 }
490
491 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Jones811f4752010-03-02 18:13:59 -0600493 /**
494 * Slide Toggle
495 *
496 * Outputs a javascript library slideToggle event
497 *
Derek Jones811f4752010-03-02 18:13:59 -0600498 * @param string - element
499 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
500 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200501 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600502 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200503 public function slideToggle($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600504 {
505 return $this->js->_slideToggle($element, $speed, $callback);
506
507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Jones811f4752010-03-02 18:13:59 -0600509 // --------------------------------------------------------------------
510
511 /**
512 * Hide
513 *
514 * Outputs a javascript library hide action
515 *
Derek Jones811f4752010-03-02 18:13:59 -0600516 * @param string - element
517 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
518 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200519 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600520 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200521 public function hide($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600522 {
523 return $this->js->_hide($element, $speed, $callback);
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Jones811f4752010-03-02 18:13:59 -0600526 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Jones811f4752010-03-02 18:13:59 -0600528 /**
529 * Toggle
530 *
531 * Outputs a javascript library toggle event
532 *
Derek Jones811f4752010-03-02 18:13:59 -0600533 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200534 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600535 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200536 public function toggle($element = 'this')
Derek Jones811f4752010-03-02 18:13:59 -0600537 {
538 return $this->js->_toggle($element);
539
540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Jones811f4752010-03-02 18:13:59 -0600542 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Jones811f4752010-03-02 18:13:59 -0600544 /**
545 * Toggle Class
546 *
547 * Outputs a javascript library toggle class event
548 *
Derek Jones811f4752010-03-02 18:13:59 -0600549 * @param string - element
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 toggleClass($element = 'this', $class='')
Derek Jones811f4752010-03-02 18:13:59 -0600553 {
554 return $this->js->_toggleClass($element, $class);
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 * Show
561 *
562 * Outputs a javascript library show event
563 *
Derek Jones811f4752010-03-02 18:13:59 -0600564 * @param string - element
565 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
566 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200567 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600568 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200569 public function show($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600570 {
571 return $this->js->_show($element, $speed, $callback);
572 }
573
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Compile
579 *
580 * gather together all script needing to be output
581 *
Derek Jones811f4752010-03-02 18:13:59 -0600582 * @param string The element to attach the event to
583 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200584 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200585 public function compile($view_var = 'script_foot', $script_tags = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600586 {
587 $this->js->_compile($view_var, $script_tags);
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Jones811f4752010-03-02 18:13:59 -0600590 /**
591 * Clear Compile
592 *
593 * Clears any previous javascript collected for output
594 *
Derek Jones811f4752010-03-02 18:13:59 -0600595 * @return void
596 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200597 public function clear_compile()
Derek Jones811f4752010-03-02 18:13:59 -0600598 {
599 $this->js->_clear_compile();
600 }
601
602 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Jones811f4752010-03-02 18:13:59 -0600604 /**
605 * External
606 *
607 * Outputs a <script> tag with the source as an external js file
608 *
Derek Jones811f4752010-03-02 18:13:59 -0600609 * @param string The element to attach the event to
610 * @return string
611 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200612 public function external($external_file = '', $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600613 {
614 if ($external_file !== '')
615 {
616 $this->_javascript_location = $external_file;
617 }
Andrey Andreev443bbd92012-04-03 15:49:11 +0300618 elseif ($this->CI->config->item('javascript_location') != '')
Derek Jones811f4752010-03-02 18:13:59 -0600619 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300620 $this->_javascript_location = $this->CI->config->item('javascript_location');
Derek Jones811f4752010-03-02 18:13:59 -0600621 }
Barry Mienydd671972010-10-04 16:33:58 +0200622
Andrey Andreevad47f942011-12-25 19:13:48 +0200623 if ($relative === TRUE OR strncmp($external_file, 'http://', 7) === 0 OR strncmp($external_file, 'https://', 8) === 0)
Derek Jones811f4752010-03-02 18:13:59 -0600624 {
Barry Mienydd671972010-10-04 16:33:58 +0200625 $str = $this->_open_script($external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600626 }
627 elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
628 {
Barry Mienydd671972010-10-04 16:33:58 +0200629 $str = $this->_open_script($this->_javascript_location.$external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600630 }
631 else
632 {
633 $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location.$external_file);
634 }
635
Andrey Andreevad47f942011-12-25 19:13:48 +0200636 return $str.$this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600637 }
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Jones811f4752010-03-02 18:13:59 -0600639 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Jones811f4752010-03-02 18:13:59 -0600641 /**
642 * Inline
643 *
Barry Mienydd671972010-10-04 16:33:58 +0200644 * Outputs a <script> tag
Derek Jones811f4752010-03-02 18:13:59 -0600645 *
Derek Jones811f4752010-03-02 18:13:59 -0600646 * @param string The element to attach the event to
647 * @param boolean If a CDATA section should be added
648 * @return string
649 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200650 public function inline($script, $cdata = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600651 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200652 return $this->_open_script()
Andrey Andreev443bbd92012-04-03 15:49:11 +0300653 . ($cdata ? "\n// <![CDATA[\n".$script."\n// ]]>\n" : "\n".$script."\n")
Andrey Andreevad47f942011-12-25 19:13:48 +0200654 . $this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600655 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200656
Barry Mienydd671972010-10-04 16:33:58 +0200657 // --------------------------------------------------------------------
658
Derek Jones811f4752010-03-02 18:13:59 -0600659 /**
660 * Open Script
661 *
Barry Mienydd671972010-10-04 16:33:58 +0200662 * Outputs an opening <script>
Derek Jones811f4752010-03-02 18:13:59 -0600663 *
Barry Mienydd671972010-10-04 16:33:58 +0200664 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600665 * @return string
666 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200667 protected function _open_script($src = '')
Derek Jones811f4752010-03-02 18:13:59 -0600668 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200669 return '<script type="text/javascript" charset="'.strtolower($this->CI->config->item('charset')).'"'
Andrey Andreev443bbd92012-04-03 15:49:11 +0300670 .($src == '' ? '>' : ' src="'.$src.'">');
Derek Jones811f4752010-03-02 18:13:59 -0600671 }
672
673 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Jones811f4752010-03-02 18:13:59 -0600675 /**
676 * Close Script
677 *
Barry Mienydd671972010-10-04 16:33:58 +0200678 * Outputs an closing </script>
Derek Jones811f4752010-03-02 18:13:59 -0600679 *
Barry Mienydd671972010-10-04 16:33:58 +0200680 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600681 * @return string
682 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200683 protected function _close_script($extra = "\n")
Derek Jones811f4752010-03-02 18:13:59 -0600684 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300685 return '</script>'.$extra;
Derek Jones811f4752010-03-02 18:13:59 -0600686 }
Barry Mienydd671972010-10-04 16:33:58 +0200687
Derek Jones811f4752010-03-02 18:13:59 -0600688 // --------------------------------------------------------------------
689 // AJAX-Y STUFF - still a testbed
690 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600691
692 /**
693 * Update
694 *
695 * Outputs a javascript library slideDown event
696 *
Derek Jones811f4752010-03-02 18:13:59 -0600697 * @param string - element
698 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
699 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200700 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600701 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200702 public function update($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600703 {
704 return $this->js->_updater($element, $speed, $callback);
705 }
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Jones811f4752010-03-02 18:13:59 -0600707 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Jones811f4752010-03-02 18:13:59 -0600709 /**
710 * Generate JSON
711 *
712 * Can be passed a database result or associative array and returns a JSON formatted string
713 *
714 * @param mixed result set or array
715 * @param bool match array types (defaults to objects)
716 * @return string a json formatted string
717 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200718 public function generate_json($result = NULL, $match_array_type = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600719 {
720 // JSON data can optionally be passed to this function
721 // either as a database result object or an array, or a user supplied array
722 if ( ! is_null($result))
723 {
724 if (is_object($result))
725 {
726 $json_result = $result->result_array();
727 }
728 elseif (is_array($result))
729 {
730 $json_result = $result;
731 }
732 else
733 {
734 return $this->_prep_args($result);
735 }
736 }
737 else
738 {
739 return 'null';
740 }
741
742 $json = array();
743 $_is_assoc = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200744
Andrey Andreev443bbd92012-04-03 15:49:11 +0300745 if ( ! is_array($json_result) && empty($json_result))
Derek Jones811f4752010-03-02 18:13:59 -0600746 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300747 show_error('Generate JSON Failed - Illegal key, value pair.');
Derek Jones811f4752010-03-02 18:13:59 -0600748 }
749 elseif ($match_array_type)
750 {
751 $_is_assoc = $this->_is_associative_array($json_result);
752 }
753
754 foreach ($json_result as $k => $v)
755 {
756 if ($_is_assoc)
757 {
758 $json[] = $this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
759 }
760 else
761 {
762 $json[] = $this->generate_json($v, $match_array_type);
763 }
764 }
765
766 $json = implode(',', $json);
767
Andrey Andreev443bbd92012-04-03 15:49:11 +0300768 return $_is_assoc ? '{'.$json.'}' : '['.$json.']';
Barry Mienydd671972010-10-04 16:33:58 +0200769
Derek Jones811f4752010-03-02 18:13:59 -0600770 }
Barry Mienydd671972010-10-04 16:33:58 +0200771
Derek Jones811f4752010-03-02 18:13:59 -0600772 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200773
Derek Jones811f4752010-03-02 18:13:59 -0600774 /**
775 * Is associative array
776 *
777 * Checks for an associative array
778 *
Andrey Andreev443bbd92012-04-03 15:49:11 +0300779 * @param array
780 * @return bool
Derek Jones811f4752010-03-02 18:13:59 -0600781 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200782 protected function _is_associative_array($arr)
Derek Jones811f4752010-03-02 18:13:59 -0600783 {
784 foreach (array_keys($arr) as $key => $val)
785 {
786 if ($key !== $val)
787 {
788 return TRUE;
789 }
790 }
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Jones811f4752010-03-02 18:13:59 -0600792 return FALSE;
793 }
Barry Mienydd671972010-10-04 16:33:58 +0200794
Derek Jones811f4752010-03-02 18:13:59 -0600795 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Jones811f4752010-03-02 18:13:59 -0600797 /**
798 * Prep Args
799 *
800 * Ensures a standard json value and escapes values
801 *
Andrey Andreev443bbd92012-04-03 15:49:11 +0300802 * @param mixed
803 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600804 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200805 protected function _prep_args($result, $is_key = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600806 {
807 if (is_null($result))
808 {
809 return 'null';
810 }
811 elseif (is_bool($result))
812 {
813 return ($result === TRUE) ? 'true' : 'false';
814 }
815 elseif (is_string($result) OR $is_key)
816 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200817 return '"'.str_replace(array('\\', "\t", "\n", "\r", '"', '/'), array('\\\\', '\\t', '\\n', "\\r", '\"', '\/'), $result).'"';
Derek Jones811f4752010-03-02 18:13:59 -0600818 }
819 elseif (is_scalar($result))
820 {
821 return $result;
822 }
823 }
Barry Mienydd671972010-10-04 16:33:58 +0200824
Derek Jones811f4752010-03-02 18:13:59 -0600825}
Derek Jones811f4752010-03-02 18:13:59 -0600826
827/* End of file Javascript.php */
Andrey Andreev443bbd92012-04-03 15:49:11 +0300828/* Location: ./system/libraries/Javascript.php */