blob: 60309cd834a7c5b8b6fe0889ba0bf3f226a4ec19 [file] [log] [blame]
Andrey Andreevad47f942011-12-25 19:13:48 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Jones9d653ed2010-03-05 09:52:53 -06002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Jones9d653ed2010-03-05 09:52:53 -06006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevad47f942011-12-25 19:13:48 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevad47f942011-12-25 19:13:48 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Jones9d653ed2010-03-05 09:52:53 -060019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Jones9d653ed2010-03-05 09:52:53 -060023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Derek Jones811f4752010-03-02 18:13:59 -060027
Derek Jones9d653ed2010-03-05 09:52:53 -060028/**
29 * Javascript Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Javascript
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
fesplugas3934a4a2010-10-04 09:07:49 +020035 * @link http://codeigniter.com/user_guide/libraries/javascript.html
Derek Jones9d653ed2010-03-05 09:52:53 -060036 */
Derek Jones811f4752010-03-02 18:13:59 -060037class CI_Javascript {
38
Andrey Andreevad47f942011-12-25 19:13:48 +020039 protected $_javascript_location = 'js';
Derek Jones811f4752010-03-02 18:13:59 -060040
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030041 /**
42 * Constructor
43 *
44 * @param array $params = array()
45 * @return void
46 */
Greg Akera9263282010-11-10 15:26:43 -060047 public function __construct($params = array())
Barry Mienydd671972010-10-04 16:33:58 +020048 {
Derek Jones811f4752010-03-02 18:13:59 -060049 $defaults = array('js_library_driver' => 'jquery', 'autoload' => TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020050
Derek Jones811f4752010-03-02 18:13:59 -060051 foreach ($defaults as $key => $val)
52 {
Andrey Andreev443bbd92012-04-03 15:49:11 +030053 if (isset($params[$key]) && $params[$key] !== '')
Derek Jones811f4752010-03-02 18:13:59 -060054 {
55 $defaults[$key] = $params[$key];
56 }
57 }
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Jones811f4752010-03-02 18:13:59 -060059 extract($defaults);
60
61 $this->CI =& get_instance();
62
63 // load the requested js library
Derek Jones9d653ed2010-03-05 09:52:53 -060064 $this->CI->load->library('javascript/'.$js_library_driver, array('autoload' => $autoload));
Derek Jones811f4752010-03-02 18:13:59 -060065 // make js to refer to current library
66 $this->js =& $this->CI->$js_library_driver;
Barry Mienydd671972010-10-04 16:33:58 +020067
Andrey Andreev443bbd92012-04-03 15:49:11 +030068 log_message('debug', 'Javascript Class Initialized and loaded. Driver used: '.$js_library_driver);
Derek Jones811f4752010-03-02 18:13:59 -060069 }
70
Andrey Andreevad47f942011-12-25 19:13:48 +020071 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020072 // Event Code
73 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -060074
75 /**
76 * Blur
77 *
78 * Outputs a javascript library blur event
79 *
Derek Jones811f4752010-03-02 18:13:59 -060080 * @param string The element to attach the event to
81 * @param string The code to execute
82 * @return string
83 */
Andrey Andreevad47f942011-12-25 19:13:48 +020084 public function blur($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -060085 {
86 return $this->js->_blur($element, $js);
87 }
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Jones811f4752010-03-02 18:13:59 -060089 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Jones811f4752010-03-02 18:13:59 -060091 /**
92 * Change
93 *
94 * Outputs a javascript library change event
95 *
Derek Jones811f4752010-03-02 18:13:59 -060096 * @param string The element to attach the event to
97 * @param string The code to execute
98 * @return string
99 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200100 public function change($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600101 {
102 return $this->js->_change($element, $js);
103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Jones811f4752010-03-02 18:13:59 -0600105 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200106
Derek Jones811f4752010-03-02 18:13:59 -0600107 /**
108 * Click
109 *
110 * Outputs a javascript library click event
111 *
Derek Jones811f4752010-03-02 18:13:59 -0600112 * @param string The element to attach the event to
113 * @param string The code to execute
Andrey Andreev1b815532012-04-03 16:06:03 +0300114 * @param bool whether or not to return false
Derek Jones811f4752010-03-02 18:13:59 -0600115 * @return string
116 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200117 public function click($element = 'this', $js = '', $ret_false = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600118 {
119 return $this->js->_click($element, $js, $ret_false);
120 }
121
122 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Jones811f4752010-03-02 18:13:59 -0600124 /**
125 * Double Click
126 *
127 * Outputs a javascript library dblclick event
128 *
Derek Jones811f4752010-03-02 18:13:59 -0600129 * @param string The element to attach the event to
130 * @param string The code to execute
131 * @return string
132 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200133 public function dblclick($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600134 {
135 return $this->js->_dblclick($element, $js);
136 }
137
138 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Jones811f4752010-03-02 18:13:59 -0600140 /**
141 * Error
142 *
143 * Outputs a javascript library error event
144 *
Derek Jones811f4752010-03-02 18:13:59 -0600145 * @param string The element to attach the event to
146 * @param string The code to execute
147 * @return string
148 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200149 public function error($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600150 {
151 return $this->js->_error($element, $js);
152 }
153
154 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Jones811f4752010-03-02 18:13:59 -0600156 /**
157 * Focus
158 *
159 * Outputs a javascript library focus event
160 *
Derek Jones811f4752010-03-02 18:13:59 -0600161 * @param string The element to attach the event to
162 * @param string The code to execute
163 * @return string
164 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200165 public function focus($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600166 {
167 return $this->js->__add_event($focus, $js);
168 }
169
170 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Jones811f4752010-03-02 18:13:59 -0600172 /**
173 * Hover
174 *
175 * Outputs a javascript library hover event
176 *
Derek Jones811f4752010-03-02 18:13:59 -0600177 * @param string - element
178 * @param string - Javascript code for mouse over
179 * @param string - Javascript code for mouse out
Barry Mienydd671972010-10-04 16:33:58 +0200180 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600181 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200182 public function hover($element = 'this', $over, $out)
Derek Jones811f4752010-03-02 18:13:59 -0600183 {
184 return $this->js->__hover($element, $over, $out);
185 }
186
187 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Jones811f4752010-03-02 18:13:59 -0600189 /**
190 * Keydown
191 *
192 * Outputs a javascript library keydown event
193 *
Derek Jones811f4752010-03-02 18:13:59 -0600194 * @param string The element to attach the event to
195 * @param string The code to execute
196 * @return string
197 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200198 public function keydown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600199 {
200 return $this->js->_keydown($element, $js);
201 }
202
203 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Jones811f4752010-03-02 18:13:59 -0600205 /**
206 * Keyup
207 *
208 * Outputs a javascript library keydown event
209 *
Derek Jones811f4752010-03-02 18:13:59 -0600210 * @param string The element to attach the event to
211 * @param string The code to execute
212 * @return string
213 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200214 public function keyup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600215 {
216 return $this->js->_keyup($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200217 }
Derek Jones811f4752010-03-02 18:13:59 -0600218
219 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Jones811f4752010-03-02 18:13:59 -0600221 /**
222 * Load
223 *
224 * Outputs a javascript library load event
225 *
Derek Jones811f4752010-03-02 18:13:59 -0600226 * @param string The element to attach the event to
227 * @param string The code to execute
228 * @return string
229 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200230 public function load($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600231 {
232 return $this->js->_load($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200233 }
234
Derek Jones811f4752010-03-02 18:13:59 -0600235 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Jones811f4752010-03-02 18:13:59 -0600237 /**
238 * Mousedown
239 *
240 * Outputs a javascript library mousedown event
241 *
Derek Jones811f4752010-03-02 18:13:59 -0600242 * @param string The element to attach the event to
243 * @param string The code to execute
244 * @return string
245 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200246 public function mousedown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600247 {
248 return $this->js->_mousedown($element, $js);
249 }
250
251 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Jones811f4752010-03-02 18:13:59 -0600253 /**
254 * Mouse Out
255 *
256 * Outputs a javascript library mouseout event
257 *
Derek Jones811f4752010-03-02 18:13:59 -0600258 * @param string The element to attach the event to
259 * @param string The code to execute
260 * @return string
261 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200262 public function mouseout($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600263 {
264 return $this->js->_mouseout($element, $js);
265 }
266
267 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Jones811f4752010-03-02 18:13:59 -0600269 /**
270 * Mouse Over
271 *
272 * Outputs a javascript library mouseover event
273 *
Derek Jones811f4752010-03-02 18:13:59 -0600274 * @param string The element to attach the event to
275 * @param string The code to execute
276 * @return string
277 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200278 public function mouseover($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600279 {
280 return $this->js->_mouseover($element, $js);
281 }
282
283 // --------------------------------------------------------------------
284
285 /**
286 * Mouseup
287 *
288 * Outputs a javascript library mouseup event
289 *
Derek Jones811f4752010-03-02 18:13:59 -0600290 * @param string The element to attach the event to
291 * @param string The code to execute
292 * @return string
293 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200294 public function mouseup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600295 {
296 return $this->js->_mouseup($element, $js);
297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Output
303 *
304 * Outputs the called javascript to the screen
305 *
Derek Jones811f4752010-03-02 18:13:59 -0600306 * @param string The code to output
307 * @return string
308 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200309 public function output($js)
Derek Jones811f4752010-03-02 18:13:59 -0600310 {
311 return $this->js->_output($js);
312 }
313
314 // --------------------------------------------------------------------
315
316 /**
317 * Ready
318 *
319 * Outputs a javascript library mouseup event
320 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300321 * @param string $js Code to execute
Derek Jones811f4752010-03-02 18:13:59 -0600322 * @return string
323 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200324 public function ready($js)
Derek Jones811f4752010-03-02 18:13:59 -0600325 {
326 return $this->js->_document_ready($js);
327 }
328
329 // --------------------------------------------------------------------
330
331 /**
332 * Resize
333 *
334 * Outputs a javascript library resize event
335 *
Derek Jones811f4752010-03-02 18:13:59 -0600336 * @param string The element to attach the event to
337 * @param string The code to execute
338 * @return string
339 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200340 public function resize($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600341 {
342 return $this->js->_resize($element, $js);
343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * Scroll
349 *
350 * Outputs a javascript library scroll event
351 *
Derek Jones811f4752010-03-02 18:13:59 -0600352 * @param string The element to attach the event to
353 * @param string The code to execute
354 * @return string
355 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200356 public function scroll($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600357 {
358 return $this->js->_scroll($element, $js);
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Jones811f4752010-03-02 18:13:59 -0600361 // --------------------------------------------------------------------
362
363 /**
364 * Unload
365 *
366 * Outputs a javascript library unload event
367 *
Derek Jones811f4752010-03-02 18:13:59 -0600368 * @param string The element to attach the event to
369 * @param string The code to execute
370 * @return string
371 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200372 public function unload($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600373 {
374 return $this->js->_unload($element, $js);
375 }
376
Andrey Andreev6aac0ba2011-12-27 02:34:00 +0200377 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200378 // Effects
379 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600380
Derek Jones811f4752010-03-02 18:13:59 -0600381 /**
382 * Add Class
383 *
384 * Outputs a javascript library addClass event
385 *
Derek Jones811f4752010-03-02 18:13:59 -0600386 * @param string - element
387 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200388 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600389 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200390 public function addClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600391 {
392 return $this->js->_addClass($element, $class);
393 }
394
Barry Mienydd671972010-10-04 16:33:58 +0200395 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600396
397 /**
398 * Animate
399 *
400 * Outputs a javascript library animate event
401 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300402 * @param string $element = 'this'
403 * @param array $params = array()
404 * @param mixed $speed 'slow', 'normal', 'fast', or time in milliseconds
405 * @param string $extra
Barry Mienydd671972010-10-04 16:33:58 +0200406 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600407 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200408 public function animate($element = 'this', $params = array(), $speed = '', $extra = '')
Derek Jones811f4752010-03-02 18:13:59 -0600409 {
410 return $this->js->_animate($element, $params, $speed, $extra);
411 }
412
413 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Jones811f4752010-03-02 18:13:59 -0600415 /**
416 * Fade In
417 *
418 * Outputs a javascript library hide event
419 *
Derek Jones811f4752010-03-02 18:13:59 -0600420 * @param string - element
421 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
422 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200423 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600424 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200425 public function fadeIn($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600426 {
427 return $this->js->_fadeIn($element, $speed, $callback);
428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Jones811f4752010-03-02 18:13:59 -0600430 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Jones811f4752010-03-02 18:13:59 -0600432 /**
433 * Fade Out
434 *
435 * Outputs a javascript library hide event
436 *
Derek Jones811f4752010-03-02 18:13:59 -0600437 * @param string - element
438 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
439 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200440 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600441 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200442 public function fadeOut($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600443 {
444 return $this->js->_fadeOut($element, $speed, $callback);
445 }
446 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Jones811f4752010-03-02 18:13:59 -0600448 /**
449 * Slide Up
450 *
451 * Outputs a javascript library slideUp event
452 *
Derek Jones811f4752010-03-02 18:13:59 -0600453 * @param string - element
454 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
455 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200456 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600457 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200458 public function slideUp($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600459 {
460 return $this->js->_slideUp($element, $speed, $callback);
461
462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Jones811f4752010-03-02 18:13:59 -0600464 // --------------------------------------------------------------------
465
466 /**
467 * Remove Class
468 *
469 * Outputs a javascript library removeClass event
470 *
Derek Jones811f4752010-03-02 18:13:59 -0600471 * @param string - element
472 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200473 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600474 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200475 public function removeClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600476 {
477 return $this->js->_removeClass($element, $class);
478 }
479
Barry Mienydd671972010-10-04 16:33:58 +0200480 // --------------------------------------------------------------------
481
Derek Jones811f4752010-03-02 18:13:59 -0600482 /**
483 * Slide Down
484 *
485 * Outputs a javascript library slideDown event
486 *
Derek Jones811f4752010-03-02 18:13:59 -0600487 * @param string - element
488 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
489 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200490 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600491 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200492 public function slideDown($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600493 {
494 return $this->js->_slideDown($element, $speed, $callback);
495 }
496
497 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Jones811f4752010-03-02 18:13:59 -0600499 /**
500 * Slide Toggle
501 *
502 * Outputs a javascript library slideToggle event
503 *
Derek Jones811f4752010-03-02 18:13:59 -0600504 * @param string - element
505 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
506 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200507 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600508 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200509 public function slideToggle($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600510 {
511 return $this->js->_slideToggle($element, $speed, $callback);
512
513 }
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Jones811f4752010-03-02 18:13:59 -0600515 // --------------------------------------------------------------------
516
517 /**
518 * Hide
519 *
520 * Outputs a javascript library hide action
521 *
Derek Jones811f4752010-03-02 18:13:59 -0600522 * @param string - element
523 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
524 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200525 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600526 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200527 public function hide($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600528 {
529 return $this->js->_hide($element, $speed, $callback);
530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Jones811f4752010-03-02 18:13:59 -0600532 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Jones811f4752010-03-02 18:13:59 -0600534 /**
535 * Toggle
536 *
537 * Outputs a javascript library toggle event
538 *
Derek Jones811f4752010-03-02 18:13:59 -0600539 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200540 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600541 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200542 public function toggle($element = 'this')
Derek Jones811f4752010-03-02 18:13:59 -0600543 {
544 return $this->js->_toggle($element);
545
546 }
Barry Mienydd671972010-10-04 16:33:58 +0200547
Derek Jones811f4752010-03-02 18:13:59 -0600548 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Jones811f4752010-03-02 18:13:59 -0600550 /**
551 * Toggle Class
552 *
553 * Outputs a javascript library toggle class event
554 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300555 * @param string $element = 'this'
556 * @param string $class = ''
Barry Mienydd671972010-10-04 16:33:58 +0200557 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600558 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300559 public function toggleClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600560 {
561 return $this->js->_toggleClass($element, $class);
562 }
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Jones811f4752010-03-02 18:13:59 -0600564 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Jones811f4752010-03-02 18:13:59 -0600566 /**
567 * Show
568 *
569 * Outputs a javascript library show event
570 *
Derek Jones811f4752010-03-02 18:13:59 -0600571 * @param string - element
572 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
573 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200574 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600575 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200576 public function show($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600577 {
578 return $this->js->_show($element, $speed, $callback);
579 }
580
581
582 // --------------------------------------------------------------------
583
584 /**
585 * Compile
586 *
587 * gather together all script needing to be output
588 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300589 * @param string $view_var = 'script_foot'
590 * @param bool $script_tags = TRUE
Derek Jones811f4752010-03-02 18:13:59 -0600591 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200592 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200593 public function compile($view_var = 'script_foot', $script_tags = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600594 {
595 $this->js->_compile($view_var, $script_tags);
596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300598 // --------------------------------------------------------------------
599
Derek Jones811f4752010-03-02 18:13:59 -0600600 /**
601 * Clear Compile
602 *
603 * Clears any previous javascript collected for output
604 *
Derek Jones811f4752010-03-02 18:13:59 -0600605 * @return void
606 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200607 public function clear_compile()
Derek Jones811f4752010-03-02 18:13:59 -0600608 {
609 $this->js->_clear_compile();
610 }
611
612 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Jones811f4752010-03-02 18:13:59 -0600614 /**
615 * External
616 *
617 * Outputs a <script> tag with the source as an external js file
618 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300619 * @param string $external_file = ''
620 * @param bool $relative = FALSE
Derek Jones811f4752010-03-02 18:13:59 -0600621 * @return string
622 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200623 public function external($external_file = '', $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600624 {
625 if ($external_file !== '')
626 {
627 $this->_javascript_location = $external_file;
628 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100629 elseif ($this->CI->config->item('javascript_location') !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600630 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300631 $this->_javascript_location = $this->CI->config->item('javascript_location');
Derek Jones811f4752010-03-02 18:13:59 -0600632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300634 if ($relative === TRUE OR strpos($external_file, 'http://') === 0 OR strpos($external_file, 'https://') === 0)
Derek Jones811f4752010-03-02 18:13:59 -0600635 {
Barry Mienydd671972010-10-04 16:33:58 +0200636 $str = $this->_open_script($external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600637 }
638 elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
639 {
Barry Mienydd671972010-10-04 16:33:58 +0200640 $str = $this->_open_script($this->_javascript_location.$external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600641 }
642 else
643 {
644 $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location.$external_file);
645 }
646
Andrey Andreevad47f942011-12-25 19:13:48 +0200647 return $str.$this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600648 }
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Jones811f4752010-03-02 18:13:59 -0600650 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Jones811f4752010-03-02 18:13:59 -0600652 /**
653 * Inline
654 *
Barry Mienydd671972010-10-04 16:33:58 +0200655 * Outputs a <script> tag
Derek Jones811f4752010-03-02 18:13:59 -0600656 *
Derek Jones811f4752010-03-02 18:13:59 -0600657 * @param string The element to attach the event to
Andrey Andreev1b815532012-04-03 16:06:03 +0300658 * @param bool If a CDATA section should be added
Derek Jones811f4752010-03-02 18:13:59 -0600659 * @return string
660 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200661 public function inline($script, $cdata = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600662 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200663 return $this->_open_script()
Andrey Andreev443bbd92012-04-03 15:49:11 +0300664 . ($cdata ? "\n// <![CDATA[\n".$script."\n// ]]>\n" : "\n".$script."\n")
Andrey Andreevad47f942011-12-25 19:13:48 +0200665 . $this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600666 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200667
Barry Mienydd671972010-10-04 16:33:58 +0200668 // --------------------------------------------------------------------
669
Derek Jones811f4752010-03-02 18:13:59 -0600670 /**
671 * Open Script
672 *
Barry Mienydd671972010-10-04 16:33:58 +0200673 * Outputs an opening <script>
Derek Jones811f4752010-03-02 18:13:59 -0600674 *
Barry Mienydd671972010-10-04 16:33:58 +0200675 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600676 * @return string
677 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200678 protected function _open_script($src = '')
Derek Jones811f4752010-03-02 18:13:59 -0600679 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200680 return '<script type="text/javascript" charset="'.strtolower($this->CI->config->item('charset')).'"'
Alex Bilbied261b1e2012-06-02 11:12:16 +0100681 .($src === '' ? '>' : ' src="'.$src.'">');
Derek Jones811f4752010-03-02 18:13:59 -0600682 }
683
684 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200685
Derek Jones811f4752010-03-02 18:13:59 -0600686 /**
687 * Close Script
688 *
Barry Mienydd671972010-10-04 16:33:58 +0200689 * Outputs an closing </script>
Derek Jones811f4752010-03-02 18:13:59 -0600690 *
Barry Mienydd671972010-10-04 16:33:58 +0200691 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600692 * @return string
693 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200694 protected function _close_script($extra = "\n")
Derek Jones811f4752010-03-02 18:13:59 -0600695 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300696 return '</script>'.$extra;
Derek Jones811f4752010-03-02 18:13:59 -0600697 }
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Jones811f4752010-03-02 18:13:59 -0600699 // --------------------------------------------------------------------
700 // AJAX-Y STUFF - still a testbed
701 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600702
703 /**
704 * Update
705 *
706 * Outputs a javascript library slideDown event
707 *
Derek Jones811f4752010-03-02 18:13:59 -0600708 * @param string - element
709 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
710 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200711 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600712 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200713 public function update($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600714 {
715 return $this->js->_updater($element, $speed, $callback);
716 }
Barry Mienydd671972010-10-04 16:33:58 +0200717
Derek Jones811f4752010-03-02 18:13:59 -0600718 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Jones811f4752010-03-02 18:13:59 -0600720 /**
721 * Generate JSON
722 *
723 * Can be passed a database result or associative array and returns a JSON formatted string
724 *
725 * @param mixed result set or array
726 * @param bool match array types (defaults to objects)
727 * @return string a json formatted string
728 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200729 public function generate_json($result = NULL, $match_array_type = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600730 {
731 // JSON data can optionally be passed to this function
732 // either as a database result object or an array, or a user supplied array
733 if ( ! is_null($result))
734 {
735 if (is_object($result))
736 {
Cosmin Atanasiu3259e982012-05-01 20:41:46 -0700737 $json_result = is_callable(array($result, 'result_array')) ? $result->result_array() : (array) $result;
Derek Jones811f4752010-03-02 18:13:59 -0600738 }
739 elseif (is_array($result))
740 {
741 $json_result = $result;
742 }
743 else
744 {
745 return $this->_prep_args($result);
746 }
747 }
748 else
749 {
750 return 'null';
751 }
752
753 $json = array();
754 $_is_assoc = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200755
Andrey Andreev443bbd92012-04-03 15:49:11 +0300756 if ( ! is_array($json_result) && empty($json_result))
Derek Jones811f4752010-03-02 18:13:59 -0600757 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300758 show_error('Generate JSON Failed - Illegal key, value pair.');
Derek Jones811f4752010-03-02 18:13:59 -0600759 }
760 elseif ($match_array_type)
761 {
762 $_is_assoc = $this->_is_associative_array($json_result);
763 }
764
765 foreach ($json_result as $k => $v)
766 {
767 if ($_is_assoc)
768 {
769 $json[] = $this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
770 }
771 else
772 {
773 $json[] = $this->generate_json($v, $match_array_type);
774 }
775 }
776
777 $json = implode(',', $json);
778
Andrey Andreev443bbd92012-04-03 15:49:11 +0300779 return $_is_assoc ? '{'.$json.'}' : '['.$json.']';
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Jones811f4752010-03-02 18:13:59 -0600781 }
Barry Mienydd671972010-10-04 16:33:58 +0200782
Derek Jones811f4752010-03-02 18:13:59 -0600783 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200784
Derek Jones811f4752010-03-02 18:13:59 -0600785 /**
786 * Is associative array
787 *
788 * Checks for an associative array
789 *
Andrey Andreev443bbd92012-04-03 15:49:11 +0300790 * @param array
791 * @return bool
Derek Jones811f4752010-03-02 18:13:59 -0600792 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200793 protected function _is_associative_array($arr)
Derek Jones811f4752010-03-02 18:13:59 -0600794 {
795 foreach (array_keys($arr) as $key => $val)
796 {
797 if ($key !== $val)
798 {
799 return TRUE;
800 }
801 }
Barry Mienydd671972010-10-04 16:33:58 +0200802
Derek Jones811f4752010-03-02 18:13:59 -0600803 return FALSE;
804 }
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Jones811f4752010-03-02 18:13:59 -0600806 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200807
Derek Jones811f4752010-03-02 18:13:59 -0600808 /**
809 * Prep Args
810 *
811 * Ensures a standard json value and escapes values
812 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300813 * @param mixed $result
814 * @param bool $is_key = FALSE
Andrey Andreev443bbd92012-04-03 15:49:11 +0300815 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600816 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200817 protected function _prep_args($result, $is_key = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600818 {
819 if (is_null($result))
820 {
821 return 'null';
822 }
823 elseif (is_bool($result))
824 {
825 return ($result === TRUE) ? 'true' : 'false';
826 }
827 elseif (is_string($result) OR $is_key)
828 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200829 return '"'.str_replace(array('\\', "\t", "\n", "\r", '"', '/'), array('\\\\', '\\t', '\\n', "\\r", '\"', '\/'), $result).'"';
Derek Jones811f4752010-03-02 18:13:59 -0600830 }
831 elseif (is_scalar($result))
832 {
833 return $result;
834 }
835 }
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Jones811f4752010-03-02 18:13:59 -0600837}
Derek Jones811f4752010-03-02 18:13:59 -0600838
839/* End of file Javascript.php */
Andrey Andreev443bbd92012-04-03 15:49:11 +0300840/* Location: ./system/libraries/Javascript.php */