blob: 7f1d8551152edf840df4f558bb5a6e560fcce8bf [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
Derek Jones9d653ed2010-03-05 09:52:53 -060037 */
Derek Jones811f4752010-03-02 18:13:59 -060038class CI_Javascript {
39
Andrey Andreev597ea272012-11-01 22:56:26 +020040 /**
41 * JavaScript location
42 *
43 * @var string
44 */
Andrey Andreevad47f942011-12-25 19:13:48 +020045 protected $_javascript_location = 'js';
Derek Jones811f4752010-03-02 18:13:59 -060046
Andrey Andreev597ea272012-11-01 22:56:26 +020047 // --------------------------------------------------------------------
48
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030049 /**
50 * Constructor
51 *
Andrey Andreev597ea272012-11-01 22:56:26 +020052 * @param array $params
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030053 * @return void
54 */
Greg Akera9263282010-11-10 15:26:43 -060055 public function __construct($params = array())
Barry Mienydd671972010-10-04 16:33:58 +020056 {
Derek Jones811f4752010-03-02 18:13:59 -060057 $defaults = array('js_library_driver' => 'jquery', 'autoload' => TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Jones811f4752010-03-02 18:13:59 -060059 foreach ($defaults as $key => $val)
60 {
Andrey Andreev443bbd92012-04-03 15:49:11 +030061 if (isset($params[$key]) && $params[$key] !== '')
Derek Jones811f4752010-03-02 18:13:59 -060062 {
63 $defaults[$key] = $params[$key];
64 }
65 }
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Jones811f4752010-03-02 18:13:59 -060067 extract($defaults);
68
69 $this->CI =& get_instance();
70
71 // load the requested js library
Derek Jones9d653ed2010-03-05 09:52:53 -060072 $this->CI->load->library('javascript/'.$js_library_driver, array('autoload' => $autoload));
Derek Jones811f4752010-03-02 18:13:59 -060073 // make js to refer to current library
74 $this->js =& $this->CI->$js_library_driver;
Barry Mienydd671972010-10-04 16:33:58 +020075
Andrey Andreev443bbd92012-04-03 15:49:11 +030076 log_message('debug', 'Javascript Class Initialized and loaded. Driver used: '.$js_library_driver);
Derek Jones811f4752010-03-02 18:13:59 -060077 }
78
Andrey Andreevad47f942011-12-25 19:13:48 +020079 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020080 // Event Code
81 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -060082
83 /**
84 * Blur
85 *
86 * Outputs a javascript library blur event
87 *
Derek Jones811f4752010-03-02 18:13:59 -060088 * @param string The element to attach the event to
89 * @param string The code to execute
90 * @return string
91 */
Andrey Andreevad47f942011-12-25 19:13:48 +020092 public function blur($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -060093 {
94 return $this->js->_blur($element, $js);
95 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Jones811f4752010-03-02 18:13:59 -060097 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Jones811f4752010-03-02 18:13:59 -060099 /**
100 * Change
101 *
102 * Outputs a javascript library change event
103 *
Derek Jones811f4752010-03-02 18:13:59 -0600104 * @param string The element to attach the event to
105 * @param string The code to execute
106 * @return string
107 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200108 public function change($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600109 {
110 return $this->js->_change($element, $js);
111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Jones811f4752010-03-02 18:13:59 -0600113 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Jones811f4752010-03-02 18:13:59 -0600115 /**
116 * Click
117 *
118 * Outputs a javascript library click event
119 *
Derek Jones811f4752010-03-02 18:13:59 -0600120 * @param string The element to attach the event to
121 * @param string The code to execute
Andrey Andreev1b815532012-04-03 16:06:03 +0300122 * @param bool whether or not to return false
Derek Jones811f4752010-03-02 18:13:59 -0600123 * @return string
124 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200125 public function click($element = 'this', $js = '', $ret_false = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600126 {
127 return $this->js->_click($element, $js, $ret_false);
128 }
129
130 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Jones811f4752010-03-02 18:13:59 -0600132 /**
133 * Double Click
134 *
135 * Outputs a javascript library dblclick event
136 *
Derek Jones811f4752010-03-02 18:13:59 -0600137 * @param string The element to attach the event to
138 * @param string The code to execute
139 * @return string
140 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200141 public function dblclick($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600142 {
143 return $this->js->_dblclick($element, $js);
144 }
145
146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Jones811f4752010-03-02 18:13:59 -0600148 /**
149 * Error
150 *
151 * Outputs a javascript library error event
152 *
Derek Jones811f4752010-03-02 18:13:59 -0600153 * @param string The element to attach the event to
154 * @param string The code to execute
155 * @return string
156 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200157 public function error($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600158 {
159 return $this->js->_error($element, $js);
160 }
161
162 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Jones811f4752010-03-02 18:13:59 -0600164 /**
165 * Focus
166 *
167 * Outputs a javascript library focus event
168 *
Derek Jones811f4752010-03-02 18:13:59 -0600169 * @param string The element to attach the event to
170 * @param string The code to execute
171 * @return string
172 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200173 public function focus($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600174 {
175 return $this->js->__add_event($focus, $js);
176 }
177
178 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Jones811f4752010-03-02 18:13:59 -0600180 /**
181 * Hover
182 *
183 * Outputs a javascript library hover event
184 *
Derek Jones811f4752010-03-02 18:13:59 -0600185 * @param string - element
186 * @param string - Javascript code for mouse over
187 * @param string - Javascript code for mouse out
Barry Mienydd671972010-10-04 16:33:58 +0200188 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600189 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200190 public function hover($element = 'this', $over, $out)
Derek Jones811f4752010-03-02 18:13:59 -0600191 {
192 return $this->js->__hover($element, $over, $out);
193 }
194
195 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Jones811f4752010-03-02 18:13:59 -0600197 /**
198 * Keydown
199 *
200 * Outputs a javascript library keydown event
201 *
Derek Jones811f4752010-03-02 18:13:59 -0600202 * @param string The element to attach the event to
203 * @param string The code to execute
204 * @return string
205 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200206 public function keydown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600207 {
208 return $this->js->_keydown($element, $js);
209 }
210
211 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Jones811f4752010-03-02 18:13:59 -0600213 /**
214 * Keyup
215 *
216 * Outputs a javascript library keydown event
217 *
Derek Jones811f4752010-03-02 18:13:59 -0600218 * @param string The element to attach the event to
219 * @param string The code to execute
220 * @return string
221 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200222 public function keyup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600223 {
224 return $this->js->_keyup($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200225 }
Derek Jones811f4752010-03-02 18:13:59 -0600226
227 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Jones811f4752010-03-02 18:13:59 -0600229 /**
230 * Load
231 *
232 * Outputs a javascript library load event
233 *
Derek Jones811f4752010-03-02 18:13:59 -0600234 * @param string The element to attach the event to
235 * @param string The code to execute
236 * @return string
237 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200238 public function load($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600239 {
240 return $this->js->_load($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200241 }
242
Derek Jones811f4752010-03-02 18:13:59 -0600243 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Jones811f4752010-03-02 18:13:59 -0600245 /**
246 * Mousedown
247 *
248 * Outputs a javascript library mousedown event
249 *
Derek Jones811f4752010-03-02 18:13:59 -0600250 * @param string The element to attach the event to
251 * @param string The code to execute
252 * @return string
253 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200254 public function mousedown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600255 {
256 return $this->js->_mousedown($element, $js);
257 }
258
259 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Jones811f4752010-03-02 18:13:59 -0600261 /**
262 * Mouse Out
263 *
264 * Outputs a javascript library mouseout event
265 *
Derek Jones811f4752010-03-02 18:13:59 -0600266 * @param string The element to attach the event to
267 * @param string The code to execute
268 * @return string
269 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200270 public function mouseout($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600271 {
272 return $this->js->_mouseout($element, $js);
273 }
274
275 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Jones811f4752010-03-02 18:13:59 -0600277 /**
278 * Mouse Over
279 *
280 * Outputs a javascript library mouseover event
281 *
Derek Jones811f4752010-03-02 18:13:59 -0600282 * @param string The element to attach the event to
283 * @param string The code to execute
284 * @return string
285 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200286 public function mouseover($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600287 {
288 return $this->js->_mouseover($element, $js);
289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * Mouseup
295 *
296 * Outputs a javascript library mouseup event
297 *
Derek Jones811f4752010-03-02 18:13:59 -0600298 * @param string The element to attach the event to
299 * @param string The code to execute
300 * @return string
301 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200302 public function mouseup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600303 {
304 return $this->js->_mouseup($element, $js);
305 }
306
307 // --------------------------------------------------------------------
308
309 /**
310 * Output
311 *
312 * Outputs the called javascript to the screen
313 *
Derek Jones811f4752010-03-02 18:13:59 -0600314 * @param string The code to output
315 * @return string
316 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200317 public function output($js)
Derek Jones811f4752010-03-02 18:13:59 -0600318 {
319 return $this->js->_output($js);
320 }
321
322 // --------------------------------------------------------------------
323
324 /**
325 * Ready
326 *
327 * Outputs a javascript library mouseup event
328 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300329 * @param string $js Code to execute
Derek Jones811f4752010-03-02 18:13:59 -0600330 * @return string
331 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200332 public function ready($js)
Derek Jones811f4752010-03-02 18:13:59 -0600333 {
334 return $this->js->_document_ready($js);
335 }
336
337 // --------------------------------------------------------------------
338
339 /**
340 * Resize
341 *
342 * Outputs a javascript library resize event
343 *
Derek Jones811f4752010-03-02 18:13:59 -0600344 * @param string The element to attach the event to
345 * @param string The code to execute
346 * @return string
347 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200348 public function resize($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600349 {
350 return $this->js->_resize($element, $js);
351 }
352
353 // --------------------------------------------------------------------
354
355 /**
356 * Scroll
357 *
358 * Outputs a javascript library scroll event
359 *
Derek Jones811f4752010-03-02 18:13:59 -0600360 * @param string The element to attach the event to
361 * @param string The code to execute
362 * @return string
363 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200364 public function scroll($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600365 {
366 return $this->js->_scroll($element, $js);
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Jones811f4752010-03-02 18:13:59 -0600369 // --------------------------------------------------------------------
370
371 /**
372 * Unload
373 *
374 * Outputs a javascript library unload event
375 *
Derek Jones811f4752010-03-02 18:13:59 -0600376 * @param string The element to attach the event to
377 * @param string The code to execute
378 * @return string
379 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200380 public function unload($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600381 {
382 return $this->js->_unload($element, $js);
383 }
384
Andrey Andreev6aac0ba2011-12-27 02:34:00 +0200385 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200386 // Effects
387 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600388
Derek Jones811f4752010-03-02 18:13:59 -0600389 /**
390 * Add Class
391 *
392 * Outputs a javascript library addClass event
393 *
Derek Jones811f4752010-03-02 18:13:59 -0600394 * @param string - element
395 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200396 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600397 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200398 public function addClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600399 {
400 return $this->js->_addClass($element, $class);
401 }
402
Barry Mienydd671972010-10-04 16:33:58 +0200403 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600404
405 /**
406 * Animate
407 *
408 * Outputs a javascript library animate event
409 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300410 * @param string $element = 'this'
411 * @param array $params = array()
412 * @param mixed $speed 'slow', 'normal', 'fast', or time in milliseconds
413 * @param string $extra
Barry Mienydd671972010-10-04 16:33:58 +0200414 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600415 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200416 public function animate($element = 'this', $params = array(), $speed = '', $extra = '')
Derek Jones811f4752010-03-02 18:13:59 -0600417 {
418 return $this->js->_animate($element, $params, $speed, $extra);
419 }
420
421 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Jones811f4752010-03-02 18:13:59 -0600423 /**
424 * Fade In
425 *
426 * Outputs a javascript library hide event
427 *
Derek Jones811f4752010-03-02 18:13:59 -0600428 * @param string - element
429 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
430 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200431 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600432 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200433 public function fadeIn($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600434 {
435 return $this->js->_fadeIn($element, $speed, $callback);
436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Jones811f4752010-03-02 18:13:59 -0600438 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Jones811f4752010-03-02 18:13:59 -0600440 /**
441 * Fade Out
442 *
443 * Outputs a javascript library hide event
444 *
Derek Jones811f4752010-03-02 18:13:59 -0600445 * @param string - element
446 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
447 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200448 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600449 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200450 public function fadeOut($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600451 {
452 return $this->js->_fadeOut($element, $speed, $callback);
453 }
454 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Jones811f4752010-03-02 18:13:59 -0600456 /**
457 * Slide Up
458 *
459 * Outputs a javascript library slideUp event
460 *
Derek Jones811f4752010-03-02 18:13:59 -0600461 * @param string - element
462 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
463 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200464 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600465 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200466 public function slideUp($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600467 {
468 return $this->js->_slideUp($element, $speed, $callback);
469
470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Jones811f4752010-03-02 18:13:59 -0600472 // --------------------------------------------------------------------
473
474 /**
475 * Remove Class
476 *
477 * Outputs a javascript library removeClass event
478 *
Derek Jones811f4752010-03-02 18:13:59 -0600479 * @param string - element
480 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200481 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600482 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200483 public function removeClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600484 {
485 return $this->js->_removeClass($element, $class);
486 }
487
Barry Mienydd671972010-10-04 16:33:58 +0200488 // --------------------------------------------------------------------
489
Derek Jones811f4752010-03-02 18:13:59 -0600490 /**
491 * Slide Down
492 *
493 * Outputs a javascript library slideDown event
494 *
Derek Jones811f4752010-03-02 18:13:59 -0600495 * @param string - element
496 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
497 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200498 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600499 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200500 public function slideDown($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600501 {
502 return $this->js->_slideDown($element, $speed, $callback);
503 }
504
505 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Jones811f4752010-03-02 18:13:59 -0600507 /**
508 * Slide Toggle
509 *
510 * Outputs a javascript library slideToggle event
511 *
Derek Jones811f4752010-03-02 18:13:59 -0600512 * @param string - element
513 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
514 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200515 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600516 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200517 public function slideToggle($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600518 {
519 return $this->js->_slideToggle($element, $speed, $callback);
520
521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Jones811f4752010-03-02 18:13:59 -0600523 // --------------------------------------------------------------------
524
525 /**
526 * Hide
527 *
528 * Outputs a javascript library hide action
529 *
Derek Jones811f4752010-03-02 18:13:59 -0600530 * @param string - element
531 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
532 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200533 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600534 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200535 public function hide($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600536 {
537 return $this->js->_hide($element, $speed, $callback);
538 }
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Jones811f4752010-03-02 18:13:59 -0600540 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Jones811f4752010-03-02 18:13:59 -0600542 /**
543 * Toggle
544 *
545 * Outputs a javascript library toggle event
546 *
Derek Jones811f4752010-03-02 18:13:59 -0600547 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200548 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600549 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200550 public function toggle($element = 'this')
Derek Jones811f4752010-03-02 18:13:59 -0600551 {
552 return $this->js->_toggle($element);
553
554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Jones811f4752010-03-02 18:13:59 -0600556 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Jones811f4752010-03-02 18:13:59 -0600558 /**
559 * Toggle Class
560 *
561 * Outputs a javascript library toggle class event
562 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300563 * @param string $element = 'this'
564 * @param string $class = ''
Barry Mienydd671972010-10-04 16:33:58 +0200565 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600566 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300567 public function toggleClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600568 {
569 return $this->js->_toggleClass($element, $class);
570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Jones811f4752010-03-02 18:13:59 -0600572 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Jones811f4752010-03-02 18:13:59 -0600574 /**
575 * Show
576 *
577 * Outputs a javascript library show event
578 *
Derek Jones811f4752010-03-02 18:13:59 -0600579 * @param string - element
580 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
581 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200582 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600583 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200584 public function show($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600585 {
586 return $this->js->_show($element, $speed, $callback);
587 }
588
Derek Jones811f4752010-03-02 18:13:59 -0600589 // --------------------------------------------------------------------
590
591 /**
592 * Compile
593 *
594 * gather together all script needing to be output
595 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200596 * @param string $view_var
597 * @param bool $script_tags
Derek Jones811f4752010-03-02 18:13:59 -0600598 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200599 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200600 public function compile($view_var = 'script_foot', $script_tags = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600601 {
602 $this->js->_compile($view_var, $script_tags);
603 }
Barry Mienydd671972010-10-04 16:33:58 +0200604
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300605 // --------------------------------------------------------------------
606
Derek Jones811f4752010-03-02 18:13:59 -0600607 /**
608 * Clear Compile
609 *
610 * Clears any previous javascript collected for output
611 *
Derek Jones811f4752010-03-02 18:13:59 -0600612 * @return void
613 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200614 public function clear_compile()
Derek Jones811f4752010-03-02 18:13:59 -0600615 {
616 $this->js->_clear_compile();
617 }
618
619 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Jones811f4752010-03-02 18:13:59 -0600621 /**
622 * External
623 *
624 * Outputs a <script> tag with the source as an external js file
625 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200626 * @param string $external_file
627 * @param bool $relative
Derek Jones811f4752010-03-02 18:13:59 -0600628 * @return string
629 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200630 public function external($external_file = '', $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600631 {
632 if ($external_file !== '')
633 {
634 $this->_javascript_location = $external_file;
635 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100636 elseif ($this->CI->config->item('javascript_location') !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600637 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300638 $this->_javascript_location = $this->CI->config->item('javascript_location');
Derek Jones811f4752010-03-02 18:13:59 -0600639 }
Barry Mienydd671972010-10-04 16:33:58 +0200640
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300641 if ($relative === TRUE OR strpos($external_file, 'http://') === 0 OR strpos($external_file, 'https://') === 0)
Derek Jones811f4752010-03-02 18:13:59 -0600642 {
Barry Mienydd671972010-10-04 16:33:58 +0200643 $str = $this->_open_script($external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600644 }
645 elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
646 {
Barry Mienydd671972010-10-04 16:33:58 +0200647 $str = $this->_open_script($this->_javascript_location.$external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600648 }
649 else
650 {
651 $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location.$external_file);
652 }
653
Andrey Andreevad47f942011-12-25 19:13:48 +0200654 return $str.$this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600655 }
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Jones811f4752010-03-02 18:13:59 -0600657 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Jones811f4752010-03-02 18:13:59 -0600659 /**
660 * Inline
661 *
Barry Mienydd671972010-10-04 16:33:58 +0200662 * Outputs a <script> tag
Derek Jones811f4752010-03-02 18:13:59 -0600663 *
Derek Jones811f4752010-03-02 18:13:59 -0600664 * @param string The element to attach the event to
Andrey Andreev1b815532012-04-03 16:06:03 +0300665 * @param bool If a CDATA section should be added
Derek Jones811f4752010-03-02 18:13:59 -0600666 * @return string
667 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200668 public function inline($script, $cdata = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600669 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200670 return $this->_open_script()
Andrey Andreev443bbd92012-04-03 15:49:11 +0300671 . ($cdata ? "\n// <![CDATA[\n".$script."\n// ]]>\n" : "\n".$script."\n")
Andrey Andreevad47f942011-12-25 19:13:48 +0200672 . $this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600673 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200674
Barry Mienydd671972010-10-04 16:33:58 +0200675 // --------------------------------------------------------------------
676
Derek Jones811f4752010-03-02 18:13:59 -0600677 /**
678 * Open Script
679 *
Barry Mienydd671972010-10-04 16:33:58 +0200680 * Outputs an opening <script>
Derek Jones811f4752010-03-02 18:13:59 -0600681 *
Barry Mienydd671972010-10-04 16:33:58 +0200682 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600683 * @return string
684 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200685 protected function _open_script($src = '')
Derek Jones811f4752010-03-02 18:13:59 -0600686 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200687 return '<script type="text/javascript" charset="'.strtolower($this->CI->config->item('charset')).'"'
Alex Bilbied261b1e2012-06-02 11:12:16 +0100688 .($src === '' ? '>' : ' src="'.$src.'">');
Derek Jones811f4752010-03-02 18:13:59 -0600689 }
690
691 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Jones811f4752010-03-02 18:13:59 -0600693 /**
694 * Close Script
695 *
Barry Mienydd671972010-10-04 16:33:58 +0200696 * Outputs an closing </script>
Derek Jones811f4752010-03-02 18:13:59 -0600697 *
Barry Mienydd671972010-10-04 16:33:58 +0200698 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600699 * @return string
700 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200701 protected function _close_script($extra = "\n")
Derek Jones811f4752010-03-02 18:13:59 -0600702 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300703 return '</script>'.$extra;
Derek Jones811f4752010-03-02 18:13:59 -0600704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Jones811f4752010-03-02 18:13:59 -0600706 // --------------------------------------------------------------------
707 // AJAX-Y STUFF - still a testbed
708 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600709
710 /**
711 * Update
712 *
713 * Outputs a javascript library slideDown event
714 *
Derek Jones811f4752010-03-02 18:13:59 -0600715 * @param string - element
716 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
717 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200718 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600719 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200720 public function update($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600721 {
722 return $this->js->_updater($element, $speed, $callback);
723 }
Barry Mienydd671972010-10-04 16:33:58 +0200724
Derek Jones811f4752010-03-02 18:13:59 -0600725 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200726
Derek Jones811f4752010-03-02 18:13:59 -0600727 /**
728 * Generate JSON
729 *
730 * Can be passed a database result or associative array and returns a JSON formatted string
731 *
732 * @param mixed result set or array
733 * @param bool match array types (defaults to objects)
734 * @return string a json formatted string
735 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200736 public function generate_json($result = NULL, $match_array_type = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600737 {
738 // JSON data can optionally be passed to this function
739 // either as a database result object or an array, or a user supplied array
vlakoff1228fe22013-01-14 01:30:09 +0100740 if ($result !== NULL)
Derek Jones811f4752010-03-02 18:13:59 -0600741 {
742 if (is_object($result))
743 {
Cosmin Atanasiu3259e982012-05-01 20:41:46 -0700744 $json_result = is_callable(array($result, 'result_array')) ? $result->result_array() : (array) $result;
Derek Jones811f4752010-03-02 18:13:59 -0600745 }
746 elseif (is_array($result))
747 {
748 $json_result = $result;
749 }
750 else
751 {
752 return $this->_prep_args($result);
753 }
754 }
755 else
756 {
757 return 'null';
758 }
759
760 $json = array();
761 $_is_assoc = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200762
Andrey Andreev443bbd92012-04-03 15:49:11 +0300763 if ( ! is_array($json_result) && empty($json_result))
Derek Jones811f4752010-03-02 18:13:59 -0600764 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300765 show_error('Generate JSON Failed - Illegal key, value pair.');
Derek Jones811f4752010-03-02 18:13:59 -0600766 }
767 elseif ($match_array_type)
768 {
769 $_is_assoc = $this->_is_associative_array($json_result);
770 }
771
772 foreach ($json_result as $k => $v)
773 {
774 if ($_is_assoc)
775 {
776 $json[] = $this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
777 }
778 else
779 {
780 $json[] = $this->generate_json($v, $match_array_type);
781 }
782 }
783
784 $json = implode(',', $json);
785
Andrey Andreev443bbd92012-04-03 15:49:11 +0300786 return $_is_assoc ? '{'.$json.'}' : '['.$json.']';
Barry Mienydd671972010-10-04 16:33:58 +0200787
Derek Jones811f4752010-03-02 18:13:59 -0600788 }
Barry Mienydd671972010-10-04 16:33:58 +0200789
Derek Jones811f4752010-03-02 18:13:59 -0600790 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Jones811f4752010-03-02 18:13:59 -0600792 /**
793 * Is associative array
794 *
795 * Checks for an associative array
796 *
Andrey Andreev443bbd92012-04-03 15:49:11 +0300797 * @param array
798 * @return bool
Derek Jones811f4752010-03-02 18:13:59 -0600799 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200800 protected function _is_associative_array($arr)
Derek Jones811f4752010-03-02 18:13:59 -0600801 {
802 foreach (array_keys($arr) as $key => $val)
803 {
804 if ($key !== $val)
805 {
806 return TRUE;
807 }
808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Jones811f4752010-03-02 18:13:59 -0600810 return FALSE;
811 }
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Jones811f4752010-03-02 18:13:59 -0600813 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Jones811f4752010-03-02 18:13:59 -0600815 /**
816 * Prep Args
817 *
818 * Ensures a standard json value and escapes values
819 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300820 * @param mixed $result
821 * @param bool $is_key = FALSE
Andrey Andreev443bbd92012-04-03 15:49:11 +0300822 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600823 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200824 protected function _prep_args($result, $is_key = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600825 {
vlakoff1228fe22013-01-14 01:30:09 +0100826 if ($result === NULL)
Derek Jones811f4752010-03-02 18:13:59 -0600827 {
828 return 'null';
829 }
830 elseif (is_bool($result))
831 {
832 return ($result === TRUE) ? 'true' : 'false';
833 }
834 elseif (is_string($result) OR $is_key)
835 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200836 return '"'.str_replace(array('\\', "\t", "\n", "\r", '"', '/'), array('\\\\', '\\t', '\\n', "\\r", '\"', '\/'), $result).'"';
Derek Jones811f4752010-03-02 18:13:59 -0600837 }
838 elseif (is_scalar($result))
839 {
840 return $result;
841 }
842 }
Barry Mienydd671972010-10-04 16:33:58 +0200843
Derek Jones811f4752010-03-02 18:13:59 -0600844}
Derek Jones811f4752010-03-02 18:13:59 -0600845
846/* End of file Javascript.php */
Andrey Andreev443bbd92012-04-03 15:49:11 +0300847/* Location: ./system/libraries/Javascript.php */