blob: 34a216c22bdfee3656d2163768b5922873b37e29 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Jones811f4752010-03-02 18:13:59 -060028
Derek Jones9d653ed2010-03-05 09:52:53 -060029/**
30 * Javascript Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Javascript
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
fesplugas3934a4a2010-10-04 09:07:49 +020036 * @link http://codeigniter.com/user_guide/libraries/javascript.html
Andrey Andreevd8a561a2014-02-08 20:14:52 +020037 * @deprecated 3.0.0 This was never a good idea in the first place.
Derek Jones9d653ed2010-03-05 09:52:53 -060038 */
Derek Jones811f4752010-03-02 18:13:59 -060039class CI_Javascript {
40
Andrey Andreev597ea272012-11-01 22:56:26 +020041 /**
42 * JavaScript location
43 *
44 * @var string
45 */
Andrey Andreevad47f942011-12-25 19:13:48 +020046 protected $_javascript_location = 'js';
Derek Jones811f4752010-03-02 18:13:59 -060047
Andrey Andreev597ea272012-11-01 22:56:26 +020048 // --------------------------------------------------------------------
49
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030050 /**
51 * Constructor
52 *
Andrey Andreev597ea272012-11-01 22:56:26 +020053 * @param array $params
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030054 * @return void
55 */
Greg Akera9263282010-11-10 15:26:43 -060056 public function __construct($params = array())
Barry Mienydd671972010-10-04 16:33:58 +020057 {
Derek Jones811f4752010-03-02 18:13:59 -060058 $defaults = array('js_library_driver' => 'jquery', 'autoload' => TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Jones811f4752010-03-02 18:13:59 -060060 foreach ($defaults as $key => $val)
61 {
Andrey Andreev443bbd92012-04-03 15:49:11 +030062 if (isset($params[$key]) && $params[$key] !== '')
Derek Jones811f4752010-03-02 18:13:59 -060063 {
64 $defaults[$key] = $params[$key];
65 }
66 }
Barry Mienydd671972010-10-04 16:33:58 +020067
Derek Jones811f4752010-03-02 18:13:59 -060068 extract($defaults);
69
70 $this->CI =& get_instance();
71
72 // load the requested js library
Andrey Andreev3608e1a2013-01-28 16:27:30 +020073 $this->CI->load->library('Javascript/'.$js_library_driver, array('autoload' => $autoload));
Derek Jones811f4752010-03-02 18:13:59 -060074 // make js to refer to current library
75 $this->js =& $this->CI->$js_library_driver;
Barry Mienydd671972010-10-04 16:33:58 +020076
Andrey Andreev443bbd92012-04-03 15:49:11 +030077 log_message('debug', 'Javascript Class Initialized and loaded. Driver used: '.$js_library_driver);
Derek Jones811f4752010-03-02 18:13:59 -060078 }
79
Andrey Andreevad47f942011-12-25 19:13:48 +020080 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020081 // Event Code
82 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -060083
84 /**
85 * Blur
86 *
87 * Outputs a javascript library blur event
88 *
Derek Jones811f4752010-03-02 18:13:59 -060089 * @param string The element to attach the event to
90 * @param string The code to execute
91 * @return string
92 */
Andrey Andreevad47f942011-12-25 19:13:48 +020093 public function blur($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -060094 {
95 return $this->js->_blur($element, $js);
96 }
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Jones811f4752010-03-02 18:13:59 -060098 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020099
Derek Jones811f4752010-03-02 18:13:59 -0600100 /**
101 * Change
102 *
103 * Outputs a javascript library change event
104 *
Derek Jones811f4752010-03-02 18:13:59 -0600105 * @param string The element to attach the event to
106 * @param string The code to execute
107 * @return string
108 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200109 public function change($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600110 {
111 return $this->js->_change($element, $js);
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Jones811f4752010-03-02 18:13:59 -0600114 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Jones811f4752010-03-02 18:13:59 -0600116 /**
117 * Click
118 *
119 * Outputs a javascript library click event
120 *
Derek Jones811f4752010-03-02 18:13:59 -0600121 * @param string The element to attach the event to
122 * @param string The code to execute
Andrey Andreev1b815532012-04-03 16:06:03 +0300123 * @param bool whether or not to return false
Derek Jones811f4752010-03-02 18:13:59 -0600124 * @return string
125 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200126 public function click($element = 'this', $js = '', $ret_false = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600127 {
128 return $this->js->_click($element, $js, $ret_false);
129 }
130
131 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Jones811f4752010-03-02 18:13:59 -0600133 /**
134 * Double Click
135 *
136 * Outputs a javascript library dblclick event
137 *
Derek Jones811f4752010-03-02 18:13:59 -0600138 * @param string The element to attach the event to
139 * @param string The code to execute
140 * @return string
141 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200142 public function dblclick($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600143 {
144 return $this->js->_dblclick($element, $js);
145 }
146
147 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Jones811f4752010-03-02 18:13:59 -0600149 /**
150 * Error
151 *
152 * Outputs a javascript library error event
153 *
Derek Jones811f4752010-03-02 18:13:59 -0600154 * @param string The element to attach the event to
155 * @param string The code to execute
156 * @return string
157 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200158 public function error($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600159 {
160 return $this->js->_error($element, $js);
161 }
162
163 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Jones811f4752010-03-02 18:13:59 -0600165 /**
166 * Focus
167 *
168 * Outputs a javascript library focus event
169 *
Derek Jones811f4752010-03-02 18:13:59 -0600170 * @param string The element to attach the event to
171 * @param string The code to execute
172 * @return string
173 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200174 public function focus($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600175 {
vlakoff3b8b8922013-07-24 22:41:00 +0200176 return $this->js->_focus($element, $js);
Derek Jones811f4752010-03-02 18:13:59 -0600177 }
178
179 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Jones811f4752010-03-02 18:13:59 -0600181 /**
182 * Hover
183 *
184 * Outputs a javascript library hover event
185 *
Derek Jones811f4752010-03-02 18:13:59 -0600186 * @param string - element
187 * @param string - Javascript code for mouse over
188 * @param string - Javascript code for mouse out
Barry Mienydd671972010-10-04 16:33:58 +0200189 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600190 */
Rasmus Lerdorf86417d02013-05-18 10:22:00 -0400191 public function hover($element = 'this', $over = '', $out = '')
Derek Jones811f4752010-03-02 18:13:59 -0600192 {
vlakoff3b8b8922013-07-24 22:41:00 +0200193 return $this->js->_hover($element, $over, $out);
Derek Jones811f4752010-03-02 18:13:59 -0600194 }
195
196 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Jones811f4752010-03-02 18:13:59 -0600198 /**
199 * Keydown
200 *
201 * Outputs a javascript library keydown event
202 *
Derek Jones811f4752010-03-02 18:13:59 -0600203 * @param string The element to attach the event to
204 * @param string The code to execute
205 * @return string
206 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200207 public function keydown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600208 {
209 return $this->js->_keydown($element, $js);
210 }
211
212 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Jones811f4752010-03-02 18:13:59 -0600214 /**
215 * Keyup
216 *
217 * Outputs a javascript library keydown event
218 *
Derek Jones811f4752010-03-02 18:13:59 -0600219 * @param string The element to attach the event to
220 * @param string The code to execute
221 * @return string
222 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200223 public function keyup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600224 {
225 return $this->js->_keyup($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200226 }
Derek Jones811f4752010-03-02 18:13:59 -0600227
228 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Jones811f4752010-03-02 18:13:59 -0600230 /**
231 * Load
232 *
233 * Outputs a javascript library load event
234 *
Derek Jones811f4752010-03-02 18:13:59 -0600235 * @param string The element to attach the event to
236 * @param string The code to execute
237 * @return string
238 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200239 public function load($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600240 {
241 return $this->js->_load($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200242 }
243
Derek Jones811f4752010-03-02 18:13:59 -0600244 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Jones811f4752010-03-02 18:13:59 -0600246 /**
247 * Mousedown
248 *
249 * Outputs a javascript library mousedown event
250 *
Derek Jones811f4752010-03-02 18:13:59 -0600251 * @param string The element to attach the event to
252 * @param string The code to execute
253 * @return string
254 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200255 public function mousedown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600256 {
257 return $this->js->_mousedown($element, $js);
258 }
259
260 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Jones811f4752010-03-02 18:13:59 -0600262 /**
263 * Mouse Out
264 *
265 * Outputs a javascript library mouseout event
266 *
Derek Jones811f4752010-03-02 18:13:59 -0600267 * @param string The element to attach the event to
268 * @param string The code to execute
269 * @return string
270 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200271 public function mouseout($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600272 {
273 return $this->js->_mouseout($element, $js);
274 }
275
276 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Jones811f4752010-03-02 18:13:59 -0600278 /**
279 * Mouse Over
280 *
281 * Outputs a javascript library mouseover event
282 *
Derek Jones811f4752010-03-02 18:13:59 -0600283 * @param string The element to attach the event to
284 * @param string The code to execute
285 * @return string
286 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200287 public function mouseover($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600288 {
289 return $this->js->_mouseover($element, $js);
290 }
291
292 // --------------------------------------------------------------------
293
294 /**
295 * Mouseup
296 *
297 * Outputs a javascript library mouseup event
298 *
Derek Jones811f4752010-03-02 18:13:59 -0600299 * @param string The element to attach the event to
300 * @param string The code to execute
301 * @return string
302 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200303 public function mouseup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600304 {
305 return $this->js->_mouseup($element, $js);
306 }
307
308 // --------------------------------------------------------------------
309
310 /**
311 * Output
312 *
313 * Outputs the called javascript to the screen
314 *
Derek Jones811f4752010-03-02 18:13:59 -0600315 * @param string The code to output
316 * @return string
317 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200318 public function output($js)
Derek Jones811f4752010-03-02 18:13:59 -0600319 {
320 return $this->js->_output($js);
321 }
322
323 // --------------------------------------------------------------------
324
325 /**
326 * Ready
327 *
328 * Outputs a javascript library mouseup event
329 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300330 * @param string $js Code to execute
Derek Jones811f4752010-03-02 18:13:59 -0600331 * @return string
332 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200333 public function ready($js)
Derek Jones811f4752010-03-02 18:13:59 -0600334 {
335 return $this->js->_document_ready($js);
336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * Resize
342 *
343 * Outputs a javascript library resize event
344 *
Derek Jones811f4752010-03-02 18:13:59 -0600345 * @param string The element to attach the event to
346 * @param string The code to execute
347 * @return string
348 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200349 public function resize($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600350 {
351 return $this->js->_resize($element, $js);
352 }
353
354 // --------------------------------------------------------------------
355
356 /**
357 * Scroll
358 *
359 * Outputs a javascript library scroll event
360 *
Derek Jones811f4752010-03-02 18:13:59 -0600361 * @param string The element to attach the event to
362 * @param string The code to execute
363 * @return string
364 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200365 public function scroll($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600366 {
367 return $this->js->_scroll($element, $js);
368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Jones811f4752010-03-02 18:13:59 -0600370 // --------------------------------------------------------------------
371
372 /**
373 * Unload
374 *
375 * Outputs a javascript library unload event
376 *
Derek Jones811f4752010-03-02 18:13:59 -0600377 * @param string The element to attach the event to
378 * @param string The code to execute
379 * @return string
380 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200381 public function unload($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600382 {
383 return $this->js->_unload($element, $js);
384 }
385
Andrey Andreev6aac0ba2011-12-27 02:34:00 +0200386 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200387 // Effects
388 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600389
Derek Jones811f4752010-03-02 18:13:59 -0600390 /**
391 * Add Class
392 *
393 * Outputs a javascript library addClass event
394 *
Derek Jones811f4752010-03-02 18:13:59 -0600395 * @param string - element
396 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200397 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600398 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200399 public function addClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600400 {
401 return $this->js->_addClass($element, $class);
402 }
403
Barry Mienydd671972010-10-04 16:33:58 +0200404 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600405
406 /**
407 * Animate
408 *
409 * Outputs a javascript library animate event
410 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300411 * @param string $element = 'this'
412 * @param array $params = array()
413 * @param mixed $speed 'slow', 'normal', 'fast', or time in milliseconds
414 * @param string $extra
Barry Mienydd671972010-10-04 16:33:58 +0200415 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600416 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200417 public function animate($element = 'this', $params = array(), $speed = '', $extra = '')
Derek Jones811f4752010-03-02 18:13:59 -0600418 {
419 return $this->js->_animate($element, $params, $speed, $extra);
420 }
421
422 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Jones811f4752010-03-02 18:13:59 -0600424 /**
425 * Fade In
426 *
427 * Outputs a javascript library hide event
428 *
Derek Jones811f4752010-03-02 18:13:59 -0600429 * @param string - element
430 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
431 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200432 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600433 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200434 public function fadeIn($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600435 {
436 return $this->js->_fadeIn($element, $speed, $callback);
437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Jones811f4752010-03-02 18:13:59 -0600439 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Jones811f4752010-03-02 18:13:59 -0600441 /**
442 * Fade Out
443 *
444 * Outputs a javascript library hide event
445 *
Derek Jones811f4752010-03-02 18:13:59 -0600446 * @param string - element
447 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
448 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200449 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600450 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200451 public function fadeOut($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600452 {
453 return $this->js->_fadeOut($element, $speed, $callback);
454 }
455 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Jones811f4752010-03-02 18:13:59 -0600457 /**
458 * Slide Up
459 *
460 * Outputs a javascript library slideUp event
461 *
Derek Jones811f4752010-03-02 18:13:59 -0600462 * @param string - element
463 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
464 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200465 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600466 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200467 public function slideUp($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600468 {
469 return $this->js->_slideUp($element, $speed, $callback);
470
471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Jones811f4752010-03-02 18:13:59 -0600473 // --------------------------------------------------------------------
474
475 /**
476 * Remove Class
477 *
478 * Outputs a javascript library removeClass event
479 *
Derek Jones811f4752010-03-02 18:13:59 -0600480 * @param string - element
481 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200482 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600483 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200484 public function removeClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600485 {
486 return $this->js->_removeClass($element, $class);
487 }
488
Barry Mienydd671972010-10-04 16:33:58 +0200489 // --------------------------------------------------------------------
490
Derek Jones811f4752010-03-02 18:13:59 -0600491 /**
492 * Slide Down
493 *
494 * Outputs a javascript library slideDown event
495 *
Derek Jones811f4752010-03-02 18:13:59 -0600496 * @param string - element
497 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
498 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200499 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600500 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200501 public function slideDown($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600502 {
503 return $this->js->_slideDown($element, $speed, $callback);
504 }
505
506 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Jones811f4752010-03-02 18:13:59 -0600508 /**
509 * Slide Toggle
510 *
511 * Outputs a javascript library slideToggle event
512 *
Derek Jones811f4752010-03-02 18:13:59 -0600513 * @param string - element
514 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
515 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200516 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600517 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200518 public function slideToggle($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600519 {
520 return $this->js->_slideToggle($element, $speed, $callback);
521
522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Jones811f4752010-03-02 18:13:59 -0600524 // --------------------------------------------------------------------
525
526 /**
527 * Hide
528 *
529 * Outputs a javascript library hide action
530 *
Derek Jones811f4752010-03-02 18:13:59 -0600531 * @param string - element
532 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
533 * @param string - Javascript callback function
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 hide($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600537 {
538 return $this->js->_hide($element, $speed, $callback);
539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Jones811f4752010-03-02 18:13:59 -0600541 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Jones811f4752010-03-02 18:13:59 -0600543 /**
544 * Toggle
545 *
546 * Outputs a javascript library toggle event
547 *
Derek Jones811f4752010-03-02 18:13:59 -0600548 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200549 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600550 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200551 public function toggle($element = 'this')
Derek Jones811f4752010-03-02 18:13:59 -0600552 {
553 return $this->js->_toggle($element);
554
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 Class
561 *
562 * Outputs a javascript library toggle class event
563 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300564 * @param string $element = 'this'
565 * @param string $class = ''
Barry Mienydd671972010-10-04 16:33:58 +0200566 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600567 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300568 public function toggleClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600569 {
570 return $this->js->_toggleClass($element, $class);
571 }
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Jones811f4752010-03-02 18:13:59 -0600573 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Jones811f4752010-03-02 18:13:59 -0600575 /**
576 * Show
577 *
578 * Outputs a javascript library show event
579 *
Derek Jones811f4752010-03-02 18:13:59 -0600580 * @param string - element
581 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
582 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200583 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600584 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200585 public function show($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600586 {
587 return $this->js->_show($element, $speed, $callback);
588 }
589
Derek Jones811f4752010-03-02 18:13:59 -0600590 // --------------------------------------------------------------------
591
592 /**
593 * Compile
594 *
595 * gather together all script needing to be output
596 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200597 * @param string $view_var
598 * @param bool $script_tags
Derek Jones811f4752010-03-02 18:13:59 -0600599 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200600 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200601 public function compile($view_var = 'script_foot', $script_tags = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600602 {
603 $this->js->_compile($view_var, $script_tags);
604 }
Barry Mienydd671972010-10-04 16:33:58 +0200605
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300606 // --------------------------------------------------------------------
607
Derek Jones811f4752010-03-02 18:13:59 -0600608 /**
609 * Clear Compile
610 *
611 * Clears any previous javascript collected for output
612 *
Derek Jones811f4752010-03-02 18:13:59 -0600613 * @return void
614 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200615 public function clear_compile()
Derek Jones811f4752010-03-02 18:13:59 -0600616 {
617 $this->js->_clear_compile();
618 }
619
620 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Jones811f4752010-03-02 18:13:59 -0600622 /**
623 * External
624 *
625 * Outputs a <script> tag with the source as an external js file
626 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200627 * @param string $external_file
628 * @param bool $relative
Derek Jones811f4752010-03-02 18:13:59 -0600629 * @return string
630 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200631 public function external($external_file = '', $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600632 {
633 if ($external_file !== '')
634 {
635 $this->_javascript_location = $external_file;
636 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100637 elseif ($this->CI->config->item('javascript_location') !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600638 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300639 $this->_javascript_location = $this->CI->config->item('javascript_location');
Derek Jones811f4752010-03-02 18:13:59 -0600640 }
Barry Mienydd671972010-10-04 16:33:58 +0200641
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300642 if ($relative === TRUE OR strpos($external_file, 'http://') === 0 OR strpos($external_file, 'https://') === 0)
Derek Jones811f4752010-03-02 18:13:59 -0600643 {
Barry Mienydd671972010-10-04 16:33:58 +0200644 $str = $this->_open_script($external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600645 }
646 elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
647 {
Barry Mienydd671972010-10-04 16:33:58 +0200648 $str = $this->_open_script($this->_javascript_location.$external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600649 }
650 else
651 {
652 $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location.$external_file);
653 }
654
Andrey Andreevad47f942011-12-25 19:13:48 +0200655 return $str.$this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600656 }
Barry Mienydd671972010-10-04 16:33:58 +0200657
Derek Jones811f4752010-03-02 18:13:59 -0600658 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200659
Derek Jones811f4752010-03-02 18:13:59 -0600660 /**
661 * Inline
662 *
Barry Mienydd671972010-10-04 16:33:58 +0200663 * Outputs a <script> tag
Derek Jones811f4752010-03-02 18:13:59 -0600664 *
Derek Jones811f4752010-03-02 18:13:59 -0600665 * @param string The element to attach the event to
Andrey Andreev1b815532012-04-03 16:06:03 +0300666 * @param bool If a CDATA section should be added
Derek Jones811f4752010-03-02 18:13:59 -0600667 * @return string
668 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200669 public function inline($script, $cdata = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600670 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200671 return $this->_open_script()
Andrey Andreev443bbd92012-04-03 15:49:11 +0300672 . ($cdata ? "\n// <![CDATA[\n".$script."\n// ]]>\n" : "\n".$script."\n")
Andrey Andreevad47f942011-12-25 19:13:48 +0200673 . $this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600674 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200675
Barry Mienydd671972010-10-04 16:33:58 +0200676 // --------------------------------------------------------------------
677
Derek Jones811f4752010-03-02 18:13:59 -0600678 /**
679 * Open Script
680 *
Barry Mienydd671972010-10-04 16:33:58 +0200681 * Outputs an opening <script>
Derek Jones811f4752010-03-02 18:13:59 -0600682 *
Barry Mienydd671972010-10-04 16:33:58 +0200683 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600684 * @return string
685 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200686 protected function _open_script($src = '')
Derek Jones811f4752010-03-02 18:13:59 -0600687 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200688 return '<script type="text/javascript" charset="'.strtolower($this->CI->config->item('charset')).'"'
Alex Bilbied261b1e2012-06-02 11:12:16 +0100689 .($src === '' ? '>' : ' src="'.$src.'">');
Derek Jones811f4752010-03-02 18:13:59 -0600690 }
691
692 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200693
Derek Jones811f4752010-03-02 18:13:59 -0600694 /**
695 * Close Script
696 *
Barry Mienydd671972010-10-04 16:33:58 +0200697 * Outputs an closing </script>
Derek Jones811f4752010-03-02 18:13:59 -0600698 *
Barry Mienydd671972010-10-04 16:33:58 +0200699 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600700 * @return string
701 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200702 protected function _close_script($extra = "\n")
Derek Jones811f4752010-03-02 18:13:59 -0600703 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300704 return '</script>'.$extra;
Derek Jones811f4752010-03-02 18:13:59 -0600705 }
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Jones811f4752010-03-02 18:13:59 -0600707 // --------------------------------------------------------------------
708 // AJAX-Y STUFF - still a testbed
709 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600710
711 /**
712 * Update
713 *
714 * Outputs a javascript library slideDown event
715 *
Derek Jones811f4752010-03-02 18:13:59 -0600716 * @param string - element
717 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
718 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200719 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600720 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200721 public function update($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600722 {
723 return $this->js->_updater($element, $speed, $callback);
724 }
Barry Mienydd671972010-10-04 16:33:58 +0200725
Derek Jones811f4752010-03-02 18:13:59 -0600726 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200727
Derek Jones811f4752010-03-02 18:13:59 -0600728 /**
729 * Generate JSON
730 *
731 * Can be passed a database result or associative array and returns a JSON formatted string
732 *
733 * @param mixed result set or array
734 * @param bool match array types (defaults to objects)
735 * @return string a json formatted string
736 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200737 public function generate_json($result = NULL, $match_array_type = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600738 {
739 // JSON data can optionally be passed to this function
740 // either as a database result object or an array, or a user supplied array
vlakoff1228fe22013-01-14 01:30:09 +0100741 if ($result !== NULL)
Derek Jones811f4752010-03-02 18:13:59 -0600742 {
743 if (is_object($result))
744 {
Cosmin Atanasiu3259e982012-05-01 20:41:46 -0700745 $json_result = is_callable(array($result, 'result_array')) ? $result->result_array() : (array) $result;
Derek Jones811f4752010-03-02 18:13:59 -0600746 }
747 elseif (is_array($result))
748 {
749 $json_result = $result;
750 }
751 else
752 {
753 return $this->_prep_args($result);
754 }
755 }
756 else
757 {
758 return 'null';
759 }
760
761 $json = array();
762 $_is_assoc = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200763
Andrey Andreev443bbd92012-04-03 15:49:11 +0300764 if ( ! is_array($json_result) && empty($json_result))
Derek Jones811f4752010-03-02 18:13:59 -0600765 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300766 show_error('Generate JSON Failed - Illegal key, value pair.');
Derek Jones811f4752010-03-02 18:13:59 -0600767 }
768 elseif ($match_array_type)
769 {
770 $_is_assoc = $this->_is_associative_array($json_result);
771 }
772
773 foreach ($json_result as $k => $v)
774 {
775 if ($_is_assoc)
776 {
777 $json[] = $this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
778 }
779 else
780 {
781 $json[] = $this->generate_json($v, $match_array_type);
782 }
783 }
784
785 $json = implode(',', $json);
786
Andrey Andreev443bbd92012-04-03 15:49:11 +0300787 return $_is_assoc ? '{'.$json.'}' : '['.$json.']';
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Jones811f4752010-03-02 18:13:59 -0600789 }
Barry Mienydd671972010-10-04 16:33:58 +0200790
Derek Jones811f4752010-03-02 18:13:59 -0600791 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200792
Derek Jones811f4752010-03-02 18:13:59 -0600793 /**
794 * Is associative array
795 *
796 * Checks for an associative array
797 *
Andrey Andreev443bbd92012-04-03 15:49:11 +0300798 * @param array
799 * @return bool
Derek Jones811f4752010-03-02 18:13:59 -0600800 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200801 protected function _is_associative_array($arr)
Derek Jones811f4752010-03-02 18:13:59 -0600802 {
803 foreach (array_keys($arr) as $key => $val)
804 {
805 if ($key !== $val)
806 {
807 return TRUE;
808 }
809 }
Barry Mienydd671972010-10-04 16:33:58 +0200810
Derek Jones811f4752010-03-02 18:13:59 -0600811 return FALSE;
812 }
Barry Mienydd671972010-10-04 16:33:58 +0200813
Derek Jones811f4752010-03-02 18:13:59 -0600814 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Jones811f4752010-03-02 18:13:59 -0600816 /**
817 * Prep Args
818 *
819 * Ensures a standard json value and escapes values
820 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300821 * @param mixed $result
822 * @param bool $is_key = FALSE
Andrey Andreev443bbd92012-04-03 15:49:11 +0300823 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600824 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200825 protected function _prep_args($result, $is_key = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600826 {
vlakoff1228fe22013-01-14 01:30:09 +0100827 if ($result === NULL)
Derek Jones811f4752010-03-02 18:13:59 -0600828 {
829 return 'null';
830 }
831 elseif (is_bool($result))
832 {
833 return ($result === TRUE) ? 'true' : 'false';
834 }
835 elseif (is_string($result) OR $is_key)
836 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200837 return '"'.str_replace(array('\\', "\t", "\n", "\r", '"', '/'), array('\\\\', '\\t', '\\n', "\\r", '\"', '\/'), $result).'"';
Derek Jones811f4752010-03-02 18:13:59 -0600838 }
839 elseif (is_scalar($result))
840 {
841 return $result;
842 }
843 }
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Jones811f4752010-03-02 18:13:59 -0600845}
Derek Jones811f4752010-03-02 18:13:59 -0600846
847/* End of file Javascript.php */
Andrey Andreevc5a7c5f2013-07-17 20:10:09 +0300848/* Location: ./system/libraries/Javascript.php */