blob: c2f458de8a1cebd9c754b41d43d5783786ac3fc8 [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
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 */
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
Derek Jones9d653ed2010-03-05 09:52:53 -060037 */
Derek Jones811f4752010-03-02 18:13:59 -060038class CI_Javascript {
39
Andrey Andreevad47f942011-12-25 19:13:48 +020040 protected $_javascript_location = 'js';
Derek Jones811f4752010-03-02 18:13:59 -060041
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030042 /**
43 * Constructor
44 *
45 * @param array $params = array()
46 * @return void
47 */
Greg Akera9263282010-11-10 15:26:43 -060048 public function __construct($params = array())
Barry Mienydd671972010-10-04 16:33:58 +020049 {
Derek Jones811f4752010-03-02 18:13:59 -060050 $defaults = array('js_library_driver' => 'jquery', 'autoload' => TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020051
Derek Jones811f4752010-03-02 18:13:59 -060052 foreach ($defaults as $key => $val)
53 {
Andrey Andreev443bbd92012-04-03 15:49:11 +030054 if (isset($params[$key]) && $params[$key] !== '')
Derek Jones811f4752010-03-02 18:13:59 -060055 {
56 $defaults[$key] = $params[$key];
57 }
58 }
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Jones811f4752010-03-02 18:13:59 -060060 extract($defaults);
61
62 $this->CI =& get_instance();
63
64 // load the requested js library
Derek Jones9d653ed2010-03-05 09:52:53 -060065 $this->CI->load->library('javascript/'.$js_library_driver, array('autoload' => $autoload));
Derek Jones811f4752010-03-02 18:13:59 -060066 // make js to refer to current library
67 $this->js =& $this->CI->$js_library_driver;
Barry Mienydd671972010-10-04 16:33:58 +020068
Andrey Andreev443bbd92012-04-03 15:49:11 +030069 log_message('debug', 'Javascript Class Initialized and loaded. Driver used: '.$js_library_driver);
Derek Jones811f4752010-03-02 18:13:59 -060070 }
71
Andrey Andreevad47f942011-12-25 19:13:48 +020072 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020073 // Event Code
74 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -060075
76 /**
77 * Blur
78 *
79 * Outputs a javascript library blur event
80 *
Derek Jones811f4752010-03-02 18:13:59 -060081 * @param string The element to attach the event to
82 * @param string The code to execute
83 * @return string
84 */
Andrey Andreevad47f942011-12-25 19:13:48 +020085 public function blur($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -060086 {
87 return $this->js->_blur($element, $js);
88 }
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Jones811f4752010-03-02 18:13:59 -060090 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Jones811f4752010-03-02 18:13:59 -060092 /**
93 * Change
94 *
95 * Outputs a javascript library change event
96 *
Derek Jones811f4752010-03-02 18:13:59 -060097 * @param string The element to attach the event to
98 * @param string The code to execute
99 * @return string
100 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200101 public function change($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600102 {
103 return $this->js->_change($element, $js);
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Jones811f4752010-03-02 18:13:59 -0600106 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Jones811f4752010-03-02 18:13:59 -0600108 /**
109 * Click
110 *
111 * Outputs a javascript library click event
112 *
Derek Jones811f4752010-03-02 18:13:59 -0600113 * @param string The element to attach the event to
114 * @param string The code to execute
Andrey Andreev1b815532012-04-03 16:06:03 +0300115 * @param bool whether or not to return false
Derek Jones811f4752010-03-02 18:13:59 -0600116 * @return string
117 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200118 public function click($element = 'this', $js = '', $ret_false = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600119 {
120 return $this->js->_click($element, $js, $ret_false);
121 }
122
123 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Jones811f4752010-03-02 18:13:59 -0600125 /**
126 * Double Click
127 *
128 * Outputs a javascript library dblclick event
129 *
Derek Jones811f4752010-03-02 18:13:59 -0600130 * @param string The element to attach the event to
131 * @param string The code to execute
132 * @return string
133 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200134 public function dblclick($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600135 {
136 return $this->js->_dblclick($element, $js);
137 }
138
139 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Jones811f4752010-03-02 18:13:59 -0600141 /**
142 * Error
143 *
144 * Outputs a javascript library error event
145 *
Derek Jones811f4752010-03-02 18:13:59 -0600146 * @param string The element to attach the event to
147 * @param string The code to execute
148 * @return string
149 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200150 public function error($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600151 {
152 return $this->js->_error($element, $js);
153 }
154
155 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Jones811f4752010-03-02 18:13:59 -0600157 /**
158 * Focus
159 *
160 * Outputs a javascript library focus event
161 *
Derek Jones811f4752010-03-02 18:13:59 -0600162 * @param string The element to attach the event to
163 * @param string The code to execute
164 * @return string
165 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200166 public function focus($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600167 {
168 return $this->js->__add_event($focus, $js);
169 }
170
171 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Jones811f4752010-03-02 18:13:59 -0600173 /**
174 * Hover
175 *
176 * Outputs a javascript library hover event
177 *
Derek Jones811f4752010-03-02 18:13:59 -0600178 * @param string - element
179 * @param string - Javascript code for mouse over
180 * @param string - Javascript code for mouse out
Barry Mienydd671972010-10-04 16:33:58 +0200181 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600182 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200183 public function hover($element = 'this', $over, $out)
Derek Jones811f4752010-03-02 18:13:59 -0600184 {
185 return $this->js->__hover($element, $over, $out);
186 }
187
188 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Jones811f4752010-03-02 18:13:59 -0600190 /**
191 * Keydown
192 *
193 * Outputs a javascript library keydown event
194 *
Derek Jones811f4752010-03-02 18:13:59 -0600195 * @param string The element to attach the event to
196 * @param string The code to execute
197 * @return string
198 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200199 public function keydown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600200 {
201 return $this->js->_keydown($element, $js);
202 }
203
204 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Jones811f4752010-03-02 18:13:59 -0600206 /**
207 * Keyup
208 *
209 * Outputs a javascript library keydown event
210 *
Derek Jones811f4752010-03-02 18:13:59 -0600211 * @param string The element to attach the event to
212 * @param string The code to execute
213 * @return string
214 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200215 public function keyup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600216 {
217 return $this->js->_keyup($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200218 }
Derek Jones811f4752010-03-02 18:13:59 -0600219
220 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Jones811f4752010-03-02 18:13:59 -0600222 /**
223 * Load
224 *
225 * Outputs a javascript library load event
226 *
Derek Jones811f4752010-03-02 18:13:59 -0600227 * @param string The element to attach the event to
228 * @param string The code to execute
229 * @return string
230 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200231 public function load($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600232 {
233 return $this->js->_load($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200234 }
235
Derek Jones811f4752010-03-02 18:13:59 -0600236 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Jones811f4752010-03-02 18:13:59 -0600238 /**
239 * Mousedown
240 *
241 * Outputs a javascript library mousedown event
242 *
Derek Jones811f4752010-03-02 18:13:59 -0600243 * @param string The element to attach the event to
244 * @param string The code to execute
245 * @return string
246 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200247 public function mousedown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600248 {
249 return $this->js->_mousedown($element, $js);
250 }
251
252 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Jones811f4752010-03-02 18:13:59 -0600254 /**
255 * Mouse Out
256 *
257 * Outputs a javascript library mouseout event
258 *
Derek Jones811f4752010-03-02 18:13:59 -0600259 * @param string The element to attach the event to
260 * @param string The code to execute
261 * @return string
262 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200263 public function mouseout($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600264 {
265 return $this->js->_mouseout($element, $js);
266 }
267
268 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Jones811f4752010-03-02 18:13:59 -0600270 /**
271 * Mouse Over
272 *
273 * Outputs a javascript library mouseover event
274 *
Derek Jones811f4752010-03-02 18:13:59 -0600275 * @param string The element to attach the event to
276 * @param string The code to execute
277 * @return string
278 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200279 public function mouseover($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600280 {
281 return $this->js->_mouseover($element, $js);
282 }
283
284 // --------------------------------------------------------------------
285
286 /**
287 * Mouseup
288 *
289 * Outputs a javascript library mouseup event
290 *
Derek Jones811f4752010-03-02 18:13:59 -0600291 * @param string The element to attach the event to
292 * @param string The code to execute
293 * @return string
294 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200295 public function mouseup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600296 {
297 return $this->js->_mouseup($element, $js);
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Output
304 *
305 * Outputs the called javascript to the screen
306 *
Derek Jones811f4752010-03-02 18:13:59 -0600307 * @param string The code to output
308 * @return string
309 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200310 public function output($js)
Derek Jones811f4752010-03-02 18:13:59 -0600311 {
312 return $this->js->_output($js);
313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * Ready
319 *
320 * Outputs a javascript library mouseup event
321 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300322 * @param string $js Code to execute
Derek Jones811f4752010-03-02 18:13:59 -0600323 * @return string
324 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200325 public function ready($js)
Derek Jones811f4752010-03-02 18:13:59 -0600326 {
327 return $this->js->_document_ready($js);
328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * Resize
334 *
335 * Outputs a javascript library resize event
336 *
Derek Jones811f4752010-03-02 18:13:59 -0600337 * @param string The element to attach the event to
338 * @param string The code to execute
339 * @return string
340 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200341 public function resize($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600342 {
343 return $this->js->_resize($element, $js);
344 }
345
346 // --------------------------------------------------------------------
347
348 /**
349 * Scroll
350 *
351 * Outputs a javascript library scroll event
352 *
Derek Jones811f4752010-03-02 18:13:59 -0600353 * @param string The element to attach the event to
354 * @param string The code to execute
355 * @return string
356 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200357 public function scroll($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600358 {
359 return $this->js->_scroll($element, $js);
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Jones811f4752010-03-02 18:13:59 -0600362 // --------------------------------------------------------------------
363
364 /**
365 * Unload
366 *
367 * Outputs a javascript library unload event
368 *
Derek Jones811f4752010-03-02 18:13:59 -0600369 * @param string The element to attach the event to
370 * @param string The code to execute
371 * @return string
372 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200373 public function unload($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600374 {
375 return $this->js->_unload($element, $js);
376 }
377
Andrey Andreev6aac0ba2011-12-27 02:34:00 +0200378 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200379 // Effects
380 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600381
Derek Jones811f4752010-03-02 18:13:59 -0600382 /**
383 * Add Class
384 *
385 * Outputs a javascript library addClass event
386 *
Derek Jones811f4752010-03-02 18:13:59 -0600387 * @param string - element
388 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200389 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600390 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200391 public function addClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600392 {
393 return $this->js->_addClass($element, $class);
394 }
395
Barry Mienydd671972010-10-04 16:33:58 +0200396 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600397
398 /**
399 * Animate
400 *
401 * Outputs a javascript library animate event
402 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300403 * @param string $element = 'this'
404 * @param array $params = array()
405 * @param mixed $speed 'slow', 'normal', 'fast', or time in milliseconds
406 * @param string $extra
Barry Mienydd671972010-10-04 16:33:58 +0200407 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600408 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200409 public function animate($element = 'this', $params = array(), $speed = '', $extra = '')
Derek Jones811f4752010-03-02 18:13:59 -0600410 {
411 return $this->js->_animate($element, $params, $speed, $extra);
412 }
413
414 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Jones811f4752010-03-02 18:13:59 -0600416 /**
417 * Fade In
418 *
419 * Outputs a javascript library hide event
420 *
Derek Jones811f4752010-03-02 18:13:59 -0600421 * @param string - element
422 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
423 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200424 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600425 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200426 public function fadeIn($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600427 {
428 return $this->js->_fadeIn($element, $speed, $callback);
429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Jones811f4752010-03-02 18:13:59 -0600431 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Jones811f4752010-03-02 18:13:59 -0600433 /**
434 * Fade Out
435 *
436 * Outputs a javascript library hide event
437 *
Derek Jones811f4752010-03-02 18:13:59 -0600438 * @param string - element
439 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
440 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200441 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600442 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200443 public function fadeOut($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600444 {
445 return $this->js->_fadeOut($element, $speed, $callback);
446 }
447 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Jones811f4752010-03-02 18:13:59 -0600449 /**
450 * Slide Up
451 *
452 * Outputs a javascript library slideUp event
453 *
Derek Jones811f4752010-03-02 18:13:59 -0600454 * @param string - element
455 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
456 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200457 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600458 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200459 public function slideUp($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600460 {
461 return $this->js->_slideUp($element, $speed, $callback);
462
463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Jones811f4752010-03-02 18:13:59 -0600465 // --------------------------------------------------------------------
466
467 /**
468 * Remove Class
469 *
470 * Outputs a javascript library removeClass event
471 *
Derek Jones811f4752010-03-02 18:13:59 -0600472 * @param string - element
473 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200474 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600475 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200476 public function removeClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600477 {
478 return $this->js->_removeClass($element, $class);
479 }
480
Barry Mienydd671972010-10-04 16:33:58 +0200481 // --------------------------------------------------------------------
482
Derek Jones811f4752010-03-02 18:13:59 -0600483 /**
484 * Slide Down
485 *
486 * Outputs a javascript library slideDown event
487 *
Derek Jones811f4752010-03-02 18:13:59 -0600488 * @param string - element
489 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
490 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200491 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600492 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200493 public function slideDown($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600494 {
495 return $this->js->_slideDown($element, $speed, $callback);
496 }
497
498 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Jones811f4752010-03-02 18:13:59 -0600500 /**
501 * Slide Toggle
502 *
503 * Outputs a javascript library slideToggle event
504 *
Derek Jones811f4752010-03-02 18:13:59 -0600505 * @param string - element
506 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
507 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200508 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600509 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200510 public function slideToggle($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600511 {
512 return $this->js->_slideToggle($element, $speed, $callback);
513
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Jones811f4752010-03-02 18:13:59 -0600516 // --------------------------------------------------------------------
517
518 /**
519 * Hide
520 *
521 * Outputs a javascript library hide action
522 *
Derek Jones811f4752010-03-02 18:13:59 -0600523 * @param string - element
524 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
525 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200526 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600527 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200528 public function hide($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600529 {
530 return $this->js->_hide($element, $speed, $callback);
531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Jones811f4752010-03-02 18:13:59 -0600533 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Jones811f4752010-03-02 18:13:59 -0600535 /**
536 * Toggle
537 *
538 * Outputs a javascript library toggle event
539 *
Derek Jones811f4752010-03-02 18:13:59 -0600540 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200541 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600542 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200543 public function toggle($element = 'this')
Derek Jones811f4752010-03-02 18:13:59 -0600544 {
545 return $this->js->_toggle($element);
546
547 }
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Jones811f4752010-03-02 18:13:59 -0600549 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200550
Derek Jones811f4752010-03-02 18:13:59 -0600551 /**
552 * Toggle Class
553 *
554 * Outputs a javascript library toggle class event
555 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300556 * @param string $element = 'this'
557 * @param string $class = ''
Barry Mienydd671972010-10-04 16:33:58 +0200558 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600559 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300560 public function toggleClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600561 {
562 return $this->js->_toggleClass($element, $class);
563 }
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Jones811f4752010-03-02 18:13:59 -0600565 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Jones811f4752010-03-02 18:13:59 -0600567 /**
568 * Show
569 *
570 * Outputs a javascript library show event
571 *
Derek Jones811f4752010-03-02 18:13:59 -0600572 * @param string - element
573 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
574 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200575 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600576 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200577 public function show($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600578 {
579 return $this->js->_show($element, $speed, $callback);
580 }
581
582
583 // --------------------------------------------------------------------
584
585 /**
586 * Compile
587 *
588 * gather together all script needing to be output
589 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300590 * @param string $view_var = 'script_foot'
591 * @param bool $script_tags = TRUE
Derek Jones811f4752010-03-02 18:13:59 -0600592 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200593 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200594 public function compile($view_var = 'script_foot', $script_tags = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600595 {
596 $this->js->_compile($view_var, $script_tags);
597 }
Barry Mienydd671972010-10-04 16:33:58 +0200598
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300599 // --------------------------------------------------------------------
600
Derek Jones811f4752010-03-02 18:13:59 -0600601 /**
602 * Clear Compile
603 *
604 * Clears any previous javascript collected for output
605 *
Derek Jones811f4752010-03-02 18:13:59 -0600606 * @return void
607 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200608 public function clear_compile()
Derek Jones811f4752010-03-02 18:13:59 -0600609 {
610 $this->js->_clear_compile();
611 }
612
613 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Jones811f4752010-03-02 18:13:59 -0600615 /**
616 * External
617 *
618 * Outputs a <script> tag with the source as an external js file
619 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300620 * @param string $external_file = ''
621 * @param bool $relative = FALSE
Derek Jones811f4752010-03-02 18:13:59 -0600622 * @return string
623 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200624 public function external($external_file = '', $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600625 {
626 if ($external_file !== '')
627 {
628 $this->_javascript_location = $external_file;
629 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100630 elseif ($this->CI->config->item('javascript_location') !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600631 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300632 $this->_javascript_location = $this->CI->config->item('javascript_location');
Derek Jones811f4752010-03-02 18:13:59 -0600633 }
Barry Mienydd671972010-10-04 16:33:58 +0200634
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300635 if ($relative === TRUE OR strpos($external_file, 'http://') === 0 OR strpos($external_file, 'https://') === 0)
Derek Jones811f4752010-03-02 18:13:59 -0600636 {
Barry Mienydd671972010-10-04 16:33:58 +0200637 $str = $this->_open_script($external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600638 }
639 elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
640 {
Barry Mienydd671972010-10-04 16:33:58 +0200641 $str = $this->_open_script($this->_javascript_location.$external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600642 }
643 else
644 {
645 $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location.$external_file);
646 }
647
Andrey Andreevad47f942011-12-25 19:13:48 +0200648 return $str.$this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600649 }
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Jones811f4752010-03-02 18:13:59 -0600651 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Jones811f4752010-03-02 18:13:59 -0600653 /**
654 * Inline
655 *
Barry Mienydd671972010-10-04 16:33:58 +0200656 * Outputs a <script> tag
Derek Jones811f4752010-03-02 18:13:59 -0600657 *
Derek Jones811f4752010-03-02 18:13:59 -0600658 * @param string The element to attach the event to
Andrey Andreev1b815532012-04-03 16:06:03 +0300659 * @param bool If a CDATA section should be added
Derek Jones811f4752010-03-02 18:13:59 -0600660 * @return string
661 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200662 public function inline($script, $cdata = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600663 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200664 return $this->_open_script()
Andrey Andreev443bbd92012-04-03 15:49:11 +0300665 . ($cdata ? "\n// <![CDATA[\n".$script."\n// ]]>\n" : "\n".$script."\n")
Andrey Andreevad47f942011-12-25 19:13:48 +0200666 . $this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600667 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200668
Barry Mienydd671972010-10-04 16:33:58 +0200669 // --------------------------------------------------------------------
670
Derek Jones811f4752010-03-02 18:13:59 -0600671 /**
672 * Open Script
673 *
Barry Mienydd671972010-10-04 16:33:58 +0200674 * Outputs an opening <script>
Derek Jones811f4752010-03-02 18:13:59 -0600675 *
Barry Mienydd671972010-10-04 16:33:58 +0200676 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600677 * @return string
678 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200679 protected function _open_script($src = '')
Derek Jones811f4752010-03-02 18:13:59 -0600680 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200681 return '<script type="text/javascript" charset="'.strtolower($this->CI->config->item('charset')).'"'
Alex Bilbied261b1e2012-06-02 11:12:16 +0100682 .($src === '' ? '>' : ' src="'.$src.'">');
Derek Jones811f4752010-03-02 18:13:59 -0600683 }
684
685 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Jones811f4752010-03-02 18:13:59 -0600687 /**
688 * Close Script
689 *
Barry Mienydd671972010-10-04 16:33:58 +0200690 * Outputs an closing </script>
Derek Jones811f4752010-03-02 18:13:59 -0600691 *
Barry Mienydd671972010-10-04 16:33:58 +0200692 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600693 * @return string
694 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200695 protected function _close_script($extra = "\n")
Derek Jones811f4752010-03-02 18:13:59 -0600696 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300697 return '</script>'.$extra;
Derek Jones811f4752010-03-02 18:13:59 -0600698 }
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Jones811f4752010-03-02 18:13:59 -0600700 // --------------------------------------------------------------------
701 // AJAX-Y STUFF - still a testbed
702 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600703
704 /**
705 * Update
706 *
707 * Outputs a javascript library slideDown event
708 *
Derek Jones811f4752010-03-02 18:13:59 -0600709 * @param string - element
710 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
711 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200712 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600713 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200714 public function update($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600715 {
716 return $this->js->_updater($element, $speed, $callback);
717 }
Barry Mienydd671972010-10-04 16:33:58 +0200718
Derek Jones811f4752010-03-02 18:13:59 -0600719 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200720
Derek Jones811f4752010-03-02 18:13:59 -0600721 /**
722 * Generate JSON
723 *
724 * Can be passed a database result or associative array and returns a JSON formatted string
725 *
726 * @param mixed result set or array
727 * @param bool match array types (defaults to objects)
728 * @return string a json formatted string
729 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200730 public function generate_json($result = NULL, $match_array_type = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600731 {
732 // JSON data can optionally be passed to this function
733 // either as a database result object or an array, or a user supplied array
734 if ( ! is_null($result))
735 {
736 if (is_object($result))
737 {
Cosmin Atanasiu3259e982012-05-01 20:41:46 -0700738 $json_result = is_callable(array($result, 'result_array')) ? $result->result_array() : (array) $result;
Derek Jones811f4752010-03-02 18:13:59 -0600739 }
740 elseif (is_array($result))
741 {
742 $json_result = $result;
743 }
744 else
745 {
746 return $this->_prep_args($result);
747 }
748 }
749 else
750 {
751 return 'null';
752 }
753
754 $json = array();
755 $_is_assoc = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200756
Andrey Andreev443bbd92012-04-03 15:49:11 +0300757 if ( ! is_array($json_result) && empty($json_result))
Derek Jones811f4752010-03-02 18:13:59 -0600758 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300759 show_error('Generate JSON Failed - Illegal key, value pair.');
Derek Jones811f4752010-03-02 18:13:59 -0600760 }
761 elseif ($match_array_type)
762 {
763 $_is_assoc = $this->_is_associative_array($json_result);
764 }
765
766 foreach ($json_result as $k => $v)
767 {
768 if ($_is_assoc)
769 {
770 $json[] = $this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
771 }
772 else
773 {
774 $json[] = $this->generate_json($v, $match_array_type);
775 }
776 }
777
778 $json = implode(',', $json);
779
Andrey Andreev443bbd92012-04-03 15:49:11 +0300780 return $_is_assoc ? '{'.$json.'}' : '['.$json.']';
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Jones811f4752010-03-02 18:13:59 -0600782 }
Barry Mienydd671972010-10-04 16:33:58 +0200783
Derek Jones811f4752010-03-02 18:13:59 -0600784 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Jones811f4752010-03-02 18:13:59 -0600786 /**
787 * Is associative array
788 *
789 * Checks for an associative array
790 *
Andrey Andreev443bbd92012-04-03 15:49:11 +0300791 * @param array
792 * @return bool
Derek Jones811f4752010-03-02 18:13:59 -0600793 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200794 protected function _is_associative_array($arr)
Derek Jones811f4752010-03-02 18:13:59 -0600795 {
796 foreach (array_keys($arr) as $key => $val)
797 {
798 if ($key !== $val)
799 {
800 return TRUE;
801 }
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Jones811f4752010-03-02 18:13:59 -0600804 return FALSE;
805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Jones811f4752010-03-02 18:13:59 -0600807 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200808
Derek Jones811f4752010-03-02 18:13:59 -0600809 /**
810 * Prep Args
811 *
812 * Ensures a standard json value and escapes values
813 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300814 * @param mixed $result
815 * @param bool $is_key = FALSE
Andrey Andreev443bbd92012-04-03 15:49:11 +0300816 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600817 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200818 protected function _prep_args($result, $is_key = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600819 {
820 if (is_null($result))
821 {
822 return 'null';
823 }
824 elseif (is_bool($result))
825 {
826 return ($result === TRUE) ? 'true' : 'false';
827 }
828 elseif (is_string($result) OR $is_key)
829 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200830 return '"'.str_replace(array('\\', "\t", "\n", "\r", '"', '/'), array('\\\\', '\\t', '\\n', "\\r", '\"', '\/'), $result).'"';
Derek Jones811f4752010-03-02 18:13:59 -0600831 }
832 elseif (is_scalar($result))
833 {
834 return $result;
835 }
836 }
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Jones811f4752010-03-02 18:13:59 -0600838}
Derek Jones811f4752010-03-02 18:13:59 -0600839
840/* End of file Javascript.php */
Andrey Andreev443bbd92012-04-03 15:49:11 +0300841/* Location: ./system/libraries/Javascript.php */