blob: ec5fe594860d3e79bb91243c84160d3310462e05 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Jones9d653ed2010-03-05 09:52:53 -06002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Jones9d653ed2010-03-05 09:52:53 -06006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevad47f942011-12-25 19:13:48 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreevad47f942011-12-25 19:13:48 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Jones9d653ed2010-03-05 09:52:53 -060036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Jones811f4752010-03-02 18:13:59 -060039
Derek Jones9d653ed2010-03-05 09:52:53 -060040/**
41 * Javascript Class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Javascript
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/libraries/javascript.html
Andrey Andreevd8a561a2014-02-08 20:14:52 +020048 * @deprecated 3.0.0 This was never a good idea in the first place.
Derek Jones9d653ed2010-03-05 09:52:53 -060049 */
Derek Jones811f4752010-03-02 18:13:59 -060050class CI_Javascript {
51
Andrey Andreev597ea272012-11-01 22:56:26 +020052 /**
53 * JavaScript location
54 *
55 * @var string
56 */
Andrey Andreevad47f942011-12-25 19:13:48 +020057 protected $_javascript_location = 'js';
Derek Jones811f4752010-03-02 18:13:59 -060058
Andrey Andreev597ea272012-11-01 22:56:26 +020059 // --------------------------------------------------------------------
60
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030061 /**
62 * Constructor
63 *
Andrey Andreev597ea272012-11-01 22:56:26 +020064 * @param array $params
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030065 * @return void
66 */
Greg Akera9263282010-11-10 15:26:43 -060067 public function __construct($params = array())
Barry Mienydd671972010-10-04 16:33:58 +020068 {
Derek Jones811f4752010-03-02 18:13:59 -060069 $defaults = array('js_library_driver' => 'jquery', 'autoload' => TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Jones811f4752010-03-02 18:13:59 -060071 foreach ($defaults as $key => $val)
72 {
Andrey Andreev443bbd92012-04-03 15:49:11 +030073 if (isset($params[$key]) && $params[$key] !== '')
Derek Jones811f4752010-03-02 18:13:59 -060074 {
75 $defaults[$key] = $params[$key];
76 }
77 }
Barry Mienydd671972010-10-04 16:33:58 +020078
Derek Jones811f4752010-03-02 18:13:59 -060079 extract($defaults);
80
81 $this->CI =& get_instance();
82
83 // load the requested js library
Andrey Andreev3608e1a2013-01-28 16:27:30 +020084 $this->CI->load->library('Javascript/'.$js_library_driver, array('autoload' => $autoload));
Derek Jones811f4752010-03-02 18:13:59 -060085 // make js to refer to current library
86 $this->js =& $this->CI->$js_library_driver;
Barry Mienydd671972010-10-04 16:33:58 +020087
Andrey Andreev90726b82015-01-20 12:39:22 +020088 log_message('info', 'Javascript Class Initialized and loaded. Driver used: '.$js_library_driver);
Derek Jones811f4752010-03-02 18:13:59 -060089 }
90
Andrey Andreevad47f942011-12-25 19:13:48 +020091 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020092 // Event Code
93 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -060094
95 /**
96 * Blur
97 *
98 * Outputs a javascript library blur event
99 *
Derek Jones811f4752010-03-02 18:13:59 -0600100 * @param string The element to attach the event to
101 * @param string The code to execute
102 * @return string
103 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200104 public function blur($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600105 {
106 return $this->js->_blur($element, $js);
107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Jones811f4752010-03-02 18:13:59 -0600109 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Jones811f4752010-03-02 18:13:59 -0600111 /**
112 * Change
113 *
114 * Outputs a javascript library change event
115 *
Derek Jones811f4752010-03-02 18:13:59 -0600116 * @param string The element to attach the event to
117 * @param string The code to execute
118 * @return string
119 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200120 public function change($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600121 {
122 return $this->js->_change($element, $js);
123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Jones811f4752010-03-02 18:13:59 -0600125 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Jones811f4752010-03-02 18:13:59 -0600127 /**
128 * Click
129 *
130 * Outputs a javascript library click event
131 *
Derek Jones811f4752010-03-02 18:13:59 -0600132 * @param string The element to attach the event to
133 * @param string The code to execute
Andrey Andreev1b815532012-04-03 16:06:03 +0300134 * @param bool whether or not to return false
Derek Jones811f4752010-03-02 18:13:59 -0600135 * @return string
136 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200137 public function click($element = 'this', $js = '', $ret_false = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600138 {
139 return $this->js->_click($element, $js, $ret_false);
140 }
141
142 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Jones811f4752010-03-02 18:13:59 -0600144 /**
145 * Double Click
146 *
147 * Outputs a javascript library dblclick event
148 *
Derek Jones811f4752010-03-02 18:13:59 -0600149 * @param string The element to attach the event to
150 * @param string The code to execute
151 * @return string
152 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200153 public function dblclick($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600154 {
155 return $this->js->_dblclick($element, $js);
156 }
157
158 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Jones811f4752010-03-02 18:13:59 -0600160 /**
161 * Error
162 *
163 * Outputs a javascript library error event
164 *
Derek Jones811f4752010-03-02 18:13:59 -0600165 * @param string The element to attach the event to
166 * @param string The code to execute
167 * @return string
168 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200169 public function error($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600170 {
171 return $this->js->_error($element, $js);
172 }
173
174 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Jones811f4752010-03-02 18:13:59 -0600176 /**
177 * Focus
178 *
179 * Outputs a javascript library focus event
180 *
Derek Jones811f4752010-03-02 18:13:59 -0600181 * @param string The element to attach the event to
182 * @param string The code to execute
183 * @return string
184 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200185 public function focus($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600186 {
vlakoff3b8b8922013-07-24 22:41:00 +0200187 return $this->js->_focus($element, $js);
Derek Jones811f4752010-03-02 18:13:59 -0600188 }
189
190 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Jones811f4752010-03-02 18:13:59 -0600192 /**
193 * Hover
194 *
195 * Outputs a javascript library hover event
196 *
Derek Jones811f4752010-03-02 18:13:59 -0600197 * @param string - element
198 * @param string - Javascript code for mouse over
199 * @param string - Javascript code for mouse out
Barry Mienydd671972010-10-04 16:33:58 +0200200 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600201 */
Rasmus Lerdorf86417d02013-05-18 10:22:00 -0400202 public function hover($element = 'this', $over = '', $out = '')
Derek Jones811f4752010-03-02 18:13:59 -0600203 {
vlakoff3b8b8922013-07-24 22:41:00 +0200204 return $this->js->_hover($element, $over, $out);
Derek Jones811f4752010-03-02 18:13:59 -0600205 }
206
207 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Jones811f4752010-03-02 18:13:59 -0600209 /**
210 * Keydown
211 *
212 * Outputs a javascript library keydown event
213 *
Derek Jones811f4752010-03-02 18:13:59 -0600214 * @param string The element to attach the event to
215 * @param string The code to execute
216 * @return string
217 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200218 public function keydown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600219 {
220 return $this->js->_keydown($element, $js);
221 }
222
223 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Jones811f4752010-03-02 18:13:59 -0600225 /**
226 * Keyup
227 *
228 * Outputs a javascript library keydown event
229 *
Derek Jones811f4752010-03-02 18:13:59 -0600230 * @param string The element to attach the event to
231 * @param string The code to execute
232 * @return string
233 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200234 public function keyup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600235 {
236 return $this->js->_keyup($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200237 }
Derek Jones811f4752010-03-02 18:13:59 -0600238
239 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Jones811f4752010-03-02 18:13:59 -0600241 /**
242 * Load
243 *
244 * Outputs a javascript library load event
245 *
Derek Jones811f4752010-03-02 18:13:59 -0600246 * @param string The element to attach the event to
247 * @param string The code to execute
248 * @return string
249 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200250 public function load($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600251 {
252 return $this->js->_load($element, $js);
Barry Mienydd671972010-10-04 16:33:58 +0200253 }
254
Derek Jones811f4752010-03-02 18:13:59 -0600255 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Jones811f4752010-03-02 18:13:59 -0600257 /**
258 * Mousedown
259 *
260 * Outputs a javascript library mousedown event
261 *
Derek Jones811f4752010-03-02 18:13:59 -0600262 * @param string The element to attach the event to
263 * @param string The code to execute
264 * @return string
265 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200266 public function mousedown($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600267 {
268 return $this->js->_mousedown($element, $js);
269 }
270
271 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Jones811f4752010-03-02 18:13:59 -0600273 /**
274 * Mouse Out
275 *
276 * Outputs a javascript library mouseout event
277 *
Derek Jones811f4752010-03-02 18:13:59 -0600278 * @param string The element to attach the event to
279 * @param string The code to execute
280 * @return string
281 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200282 public function mouseout($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600283 {
284 return $this->js->_mouseout($element, $js);
285 }
286
287 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Jones811f4752010-03-02 18:13:59 -0600289 /**
290 * Mouse Over
291 *
292 * Outputs a javascript library mouseover event
293 *
Derek Jones811f4752010-03-02 18:13:59 -0600294 * @param string The element to attach the event to
295 * @param string The code to execute
296 * @return string
297 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200298 public function mouseover($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600299 {
300 return $this->js->_mouseover($element, $js);
301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Mouseup
307 *
308 * Outputs a javascript library mouseup event
309 *
Derek Jones811f4752010-03-02 18:13:59 -0600310 * @param string The element to attach the event to
311 * @param string The code to execute
312 * @return string
313 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200314 public function mouseup($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600315 {
316 return $this->js->_mouseup($element, $js);
317 }
318
319 // --------------------------------------------------------------------
320
321 /**
322 * Output
323 *
324 * Outputs the called javascript to the screen
325 *
Derek Jones811f4752010-03-02 18:13:59 -0600326 * @param string The code to output
327 * @return string
328 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200329 public function output($js)
Derek Jones811f4752010-03-02 18:13:59 -0600330 {
331 return $this->js->_output($js);
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * Ready
338 *
339 * Outputs a javascript library mouseup event
340 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300341 * @param string $js Code to execute
Derek Jones811f4752010-03-02 18:13:59 -0600342 * @return string
343 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200344 public function ready($js)
Derek Jones811f4752010-03-02 18:13:59 -0600345 {
346 return $this->js->_document_ready($js);
347 }
348
349 // --------------------------------------------------------------------
350
351 /**
352 * Resize
353 *
354 * Outputs a javascript library resize event
355 *
Derek Jones811f4752010-03-02 18:13:59 -0600356 * @param string The element to attach the event to
357 * @param string The code to execute
358 * @return string
359 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200360 public function resize($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600361 {
362 return $this->js->_resize($element, $js);
363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * Scroll
369 *
370 * Outputs a javascript library scroll event
371 *
Derek Jones811f4752010-03-02 18:13:59 -0600372 * @param string The element to attach the event to
373 * @param string The code to execute
374 * @return string
375 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200376 public function scroll($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600377 {
378 return $this->js->_scroll($element, $js);
379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Jones811f4752010-03-02 18:13:59 -0600381 // --------------------------------------------------------------------
382
383 /**
384 * Unload
385 *
386 * Outputs a javascript library unload event
387 *
Derek Jones811f4752010-03-02 18:13:59 -0600388 * @param string The element to attach the event to
389 * @param string The code to execute
390 * @return string
391 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200392 public function unload($element = 'this', $js = '')
Derek Jones811f4752010-03-02 18:13:59 -0600393 {
394 return $this->js->_unload($element, $js);
395 }
396
Andrey Andreev6aac0ba2011-12-27 02:34:00 +0200397 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200398 // Effects
399 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600400
Derek Jones811f4752010-03-02 18:13:59 -0600401 /**
402 * Add Class
403 *
404 * Outputs a javascript library addClass event
405 *
Derek Jones811f4752010-03-02 18:13:59 -0600406 * @param string - element
407 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200408 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600409 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200410 public function addClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600411 {
412 return $this->js->_addClass($element, $class);
413 }
414
Barry Mienydd671972010-10-04 16:33:58 +0200415 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600416
417 /**
418 * Animate
419 *
420 * Outputs a javascript library animate event
421 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300422 * @param string $element = 'this'
423 * @param array $params = array()
424 * @param mixed $speed 'slow', 'normal', 'fast', or time in milliseconds
425 * @param string $extra
Barry Mienydd671972010-10-04 16:33:58 +0200426 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600427 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200428 public function animate($element = 'this', $params = array(), $speed = '', $extra = '')
Derek Jones811f4752010-03-02 18:13:59 -0600429 {
430 return $this->js->_animate($element, $params, $speed, $extra);
431 }
432
433 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Jones811f4752010-03-02 18:13:59 -0600435 /**
436 * Fade In
437 *
438 * Outputs a javascript library hide event
439 *
Derek Jones811f4752010-03-02 18:13:59 -0600440 * @param string - element
441 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
442 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200443 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600444 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200445 public function fadeIn($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600446 {
447 return $this->js->_fadeIn($element, $speed, $callback);
448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Jones811f4752010-03-02 18:13:59 -0600450 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Jones811f4752010-03-02 18:13:59 -0600452 /**
453 * Fade Out
454 *
455 * Outputs a javascript library hide event
456 *
Derek Jones811f4752010-03-02 18:13:59 -0600457 * @param string - element
458 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
459 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200460 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600461 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200462 public function fadeOut($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600463 {
464 return $this->js->_fadeOut($element, $speed, $callback);
465 }
466 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Jones811f4752010-03-02 18:13:59 -0600468 /**
469 * Slide Up
470 *
471 * Outputs a javascript library slideUp event
472 *
Derek Jones811f4752010-03-02 18:13:59 -0600473 * @param string - element
474 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
475 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200476 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600477 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200478 public function slideUp($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600479 {
480 return $this->js->_slideUp($element, $speed, $callback);
481
482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Jones811f4752010-03-02 18:13:59 -0600484 // --------------------------------------------------------------------
485
486 /**
487 * Remove Class
488 *
489 * Outputs a javascript library removeClass event
490 *
Derek Jones811f4752010-03-02 18:13:59 -0600491 * @param string - element
492 * @param string - Class to add
Barry Mienydd671972010-10-04 16:33:58 +0200493 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600494 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200495 public function removeClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600496 {
497 return $this->js->_removeClass($element, $class);
498 }
499
Barry Mienydd671972010-10-04 16:33:58 +0200500 // --------------------------------------------------------------------
501
Derek Jones811f4752010-03-02 18:13:59 -0600502 /**
503 * Slide Down
504 *
505 * Outputs a javascript library slideDown event
506 *
Derek Jones811f4752010-03-02 18:13:59 -0600507 * @param string - element
508 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
509 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200510 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600511 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200512 public function slideDown($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600513 {
514 return $this->js->_slideDown($element, $speed, $callback);
515 }
516
517 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Jones811f4752010-03-02 18:13:59 -0600519 /**
520 * Slide Toggle
521 *
522 * Outputs a javascript library slideToggle event
523 *
Derek Jones811f4752010-03-02 18:13:59 -0600524 * @param string - element
525 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
526 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200527 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600528 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200529 public function slideToggle($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600530 {
531 return $this->js->_slideToggle($element, $speed, $callback);
532
533 }
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Jones811f4752010-03-02 18:13:59 -0600535 // --------------------------------------------------------------------
536
537 /**
538 * Hide
539 *
540 * Outputs a javascript library hide action
541 *
Derek Jones811f4752010-03-02 18:13:59 -0600542 * @param string - element
543 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
544 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200545 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600546 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200547 public function hide($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600548 {
549 return $this->js->_hide($element, $speed, $callback);
550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Jones811f4752010-03-02 18:13:59 -0600552 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Jones811f4752010-03-02 18:13:59 -0600554 /**
555 * Toggle
556 *
557 * Outputs a javascript library toggle event
558 *
Derek Jones811f4752010-03-02 18:13:59 -0600559 * @param string - element
Barry Mienydd671972010-10-04 16:33:58 +0200560 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600561 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200562 public function toggle($element = 'this')
Derek Jones811f4752010-03-02 18:13:59 -0600563 {
564 return $this->js->_toggle($element);
565
566 }
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Jones811f4752010-03-02 18:13:59 -0600568 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200569
Derek Jones811f4752010-03-02 18:13:59 -0600570 /**
571 * Toggle Class
572 *
573 * Outputs a javascript library toggle class event
574 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300575 * @param string $element = 'this'
576 * @param string $class = ''
Barry Mienydd671972010-10-04 16:33:58 +0200577 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600578 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300579 public function toggleClass($element = 'this', $class = '')
Derek Jones811f4752010-03-02 18:13:59 -0600580 {
581 return $this->js->_toggleClass($element, $class);
582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Jones811f4752010-03-02 18:13:59 -0600584 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200585
Derek Jones811f4752010-03-02 18:13:59 -0600586 /**
587 * Show
588 *
589 * Outputs a javascript library show event
590 *
Derek Jones811f4752010-03-02 18:13:59 -0600591 * @param string - element
592 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
593 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200594 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600595 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200596 public function show($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600597 {
598 return $this->js->_show($element, $speed, $callback);
599 }
600
Derek Jones811f4752010-03-02 18:13:59 -0600601 // --------------------------------------------------------------------
602
603 /**
604 * Compile
605 *
606 * gather together all script needing to be output
607 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200608 * @param string $view_var
609 * @param bool $script_tags
Derek Jones811f4752010-03-02 18:13:59 -0600610 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200611 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200612 public function compile($view_var = 'script_foot', $script_tags = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600613 {
614 $this->js->_compile($view_var, $script_tags);
615 }
Barry Mienydd671972010-10-04 16:33:58 +0200616
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300617 // --------------------------------------------------------------------
618
Derek Jones811f4752010-03-02 18:13:59 -0600619 /**
620 * Clear Compile
621 *
622 * Clears any previous javascript collected for output
623 *
Derek Jones811f4752010-03-02 18:13:59 -0600624 * @return void
625 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200626 public function clear_compile()
Derek Jones811f4752010-03-02 18:13:59 -0600627 {
628 $this->js->_clear_compile();
629 }
630
631 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Jones811f4752010-03-02 18:13:59 -0600633 /**
634 * External
635 *
636 * Outputs a <script> tag with the source as an external js file
637 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200638 * @param string $external_file
639 * @param bool $relative
Derek Jones811f4752010-03-02 18:13:59 -0600640 * @return string
641 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200642 public function external($external_file = '', $relative = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600643 {
644 if ($external_file !== '')
645 {
646 $this->_javascript_location = $external_file;
647 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100648 elseif ($this->CI->config->item('javascript_location') !== '')
Derek Jones811f4752010-03-02 18:13:59 -0600649 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300650 $this->_javascript_location = $this->CI->config->item('javascript_location');
Derek Jones811f4752010-03-02 18:13:59 -0600651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300653 if ($relative === TRUE OR strpos($external_file, 'http://') === 0 OR strpos($external_file, 'https://') === 0)
Derek Jones811f4752010-03-02 18:13:59 -0600654 {
Barry Mienydd671972010-10-04 16:33:58 +0200655 $str = $this->_open_script($external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600656 }
657 elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
658 {
Barry Mienydd671972010-10-04 16:33:58 +0200659 $str = $this->_open_script($this->_javascript_location.$external_file);
Derek Jones811f4752010-03-02 18:13:59 -0600660 }
661 else
662 {
663 $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location.$external_file);
664 }
665
Andrey Andreevad47f942011-12-25 19:13:48 +0200666 return $str.$this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600667 }
Barry Mienydd671972010-10-04 16:33:58 +0200668
Derek Jones811f4752010-03-02 18:13:59 -0600669 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200670
Derek Jones811f4752010-03-02 18:13:59 -0600671 /**
672 * Inline
673 *
Barry Mienydd671972010-10-04 16:33:58 +0200674 * Outputs a <script> tag
Derek Jones811f4752010-03-02 18:13:59 -0600675 *
Derek Jones811f4752010-03-02 18:13:59 -0600676 * @param string The element to attach the event to
Andrey Andreev1b815532012-04-03 16:06:03 +0300677 * @param bool If a CDATA section should be added
Derek Jones811f4752010-03-02 18:13:59 -0600678 * @return string
679 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200680 public function inline($script, $cdata = TRUE)
Derek Jones811f4752010-03-02 18:13:59 -0600681 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200682 return $this->_open_script()
Andrey Andreev443bbd92012-04-03 15:49:11 +0300683 . ($cdata ? "\n// <![CDATA[\n".$script."\n// ]]>\n" : "\n".$script."\n")
Andrey Andreevad47f942011-12-25 19:13:48 +0200684 . $this->_close_script();
Derek Jones811f4752010-03-02 18:13:59 -0600685 }
Andrey Andreevad47f942011-12-25 19:13:48 +0200686
Barry Mienydd671972010-10-04 16:33:58 +0200687 // --------------------------------------------------------------------
688
Derek Jones811f4752010-03-02 18:13:59 -0600689 /**
690 * Open Script
691 *
Barry Mienydd671972010-10-04 16:33:58 +0200692 * Outputs an opening <script>
Derek Jones811f4752010-03-02 18:13:59 -0600693 *
Barry Mienydd671972010-10-04 16:33:58 +0200694 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600695 * @return string
696 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200697 protected function _open_script($src = '')
Derek Jones811f4752010-03-02 18:13:59 -0600698 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200699 return '<script type="text/javascript" charset="'.strtolower($this->CI->config->item('charset')).'"'
Alex Bilbied261b1e2012-06-02 11:12:16 +0100700 .($src === '' ? '>' : ' src="'.$src.'">');
Derek Jones811f4752010-03-02 18:13:59 -0600701 }
702
703 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200704
Derek Jones811f4752010-03-02 18:13:59 -0600705 /**
706 * Close Script
707 *
Barry Mienydd671972010-10-04 16:33:58 +0200708 * Outputs an closing </script>
Derek Jones811f4752010-03-02 18:13:59 -0600709 *
Barry Mienydd671972010-10-04 16:33:58 +0200710 * @param string
Derek Jones811f4752010-03-02 18:13:59 -0600711 * @return string
712 */
Andrey Andreev92ba08a2011-12-26 16:56:00 +0200713 protected function _close_script($extra = "\n")
Derek Jones811f4752010-03-02 18:13:59 -0600714 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300715 return '</script>'.$extra;
Derek Jones811f4752010-03-02 18:13:59 -0600716 }
Barry Mienydd671972010-10-04 16:33:58 +0200717
Derek Jones811f4752010-03-02 18:13:59 -0600718 // --------------------------------------------------------------------
719 // AJAX-Y STUFF - still a testbed
720 // --------------------------------------------------------------------
Derek Jones811f4752010-03-02 18:13:59 -0600721
722 /**
723 * Update
724 *
725 * Outputs a javascript library slideDown event
726 *
Derek Jones811f4752010-03-02 18:13:59 -0600727 * @param string - element
728 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
729 * @param string - Javascript callback function
Barry Mienydd671972010-10-04 16:33:58 +0200730 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600731 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200732 public function update($element = 'this', $speed = '', $callback = '')
Derek Jones811f4752010-03-02 18:13:59 -0600733 {
734 return $this->js->_updater($element, $speed, $callback);
735 }
Barry Mienydd671972010-10-04 16:33:58 +0200736
Derek Jones811f4752010-03-02 18:13:59 -0600737 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200738
Derek Jones811f4752010-03-02 18:13:59 -0600739 /**
740 * Generate JSON
741 *
742 * Can be passed a database result or associative array and returns a JSON formatted string
743 *
744 * @param mixed result set or array
745 * @param bool match array types (defaults to objects)
746 * @return string a json formatted string
747 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200748 public function generate_json($result = NULL, $match_array_type = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600749 {
750 // JSON data can optionally be passed to this function
751 // either as a database result object or an array, or a user supplied array
vlakoff1228fe22013-01-14 01:30:09 +0100752 if ($result !== NULL)
Derek Jones811f4752010-03-02 18:13:59 -0600753 {
754 if (is_object($result))
755 {
Cosmin Atanasiu3259e982012-05-01 20:41:46 -0700756 $json_result = is_callable(array($result, 'result_array')) ? $result->result_array() : (array) $result;
Derek Jones811f4752010-03-02 18:13:59 -0600757 }
758 elseif (is_array($result))
759 {
760 $json_result = $result;
761 }
762 else
763 {
764 return $this->_prep_args($result);
765 }
766 }
767 else
768 {
769 return 'null';
770 }
771
772 $json = array();
773 $_is_assoc = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200774
Andrey Andreev443bbd92012-04-03 15:49:11 +0300775 if ( ! is_array($json_result) && empty($json_result))
Derek Jones811f4752010-03-02 18:13:59 -0600776 {
Andrey Andreev443bbd92012-04-03 15:49:11 +0300777 show_error('Generate JSON Failed - Illegal key, value pair.');
Derek Jones811f4752010-03-02 18:13:59 -0600778 }
779 elseif ($match_array_type)
780 {
781 $_is_assoc = $this->_is_associative_array($json_result);
782 }
783
784 foreach ($json_result as $k => $v)
785 {
786 if ($_is_assoc)
787 {
788 $json[] = $this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
789 }
790 else
791 {
792 $json[] = $this->generate_json($v, $match_array_type);
793 }
794 }
795
796 $json = implode(',', $json);
797
Andrey Andreev443bbd92012-04-03 15:49:11 +0300798 return $_is_assoc ? '{'.$json.'}' : '['.$json.']';
Barry Mienydd671972010-10-04 16:33:58 +0200799
Derek Jones811f4752010-03-02 18:13:59 -0600800 }
Barry Mienydd671972010-10-04 16:33:58 +0200801
Derek Jones811f4752010-03-02 18:13:59 -0600802 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Jones811f4752010-03-02 18:13:59 -0600804 /**
805 * Is associative array
806 *
807 * Checks for an associative array
808 *
Andrey Andreev443bbd92012-04-03 15:49:11 +0300809 * @param array
810 * @return bool
Derek Jones811f4752010-03-02 18:13:59 -0600811 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200812 protected function _is_associative_array($arr)
Derek Jones811f4752010-03-02 18:13:59 -0600813 {
814 foreach (array_keys($arr) as $key => $val)
815 {
816 if ($key !== $val)
817 {
818 return TRUE;
819 }
820 }
Barry Mienydd671972010-10-04 16:33:58 +0200821
Derek Jones811f4752010-03-02 18:13:59 -0600822 return FALSE;
823 }
Barry Mienydd671972010-10-04 16:33:58 +0200824
Derek Jones811f4752010-03-02 18:13:59 -0600825 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Jones811f4752010-03-02 18:13:59 -0600827 /**
828 * Prep Args
829 *
830 * Ensures a standard json value and escapes values
831 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300832 * @param mixed $result
833 * @param bool $is_key = FALSE
Andrey Andreev443bbd92012-04-03 15:49:11 +0300834 * @return string
Derek Jones811f4752010-03-02 18:13:59 -0600835 */
Andrey Andreevad47f942011-12-25 19:13:48 +0200836 protected function _prep_args($result, $is_key = FALSE)
Derek Jones811f4752010-03-02 18:13:59 -0600837 {
vlakoff1228fe22013-01-14 01:30:09 +0100838 if ($result === NULL)
Derek Jones811f4752010-03-02 18:13:59 -0600839 {
840 return 'null';
841 }
842 elseif (is_bool($result))
843 {
844 return ($result === TRUE) ? 'true' : 'false';
845 }
846 elseif (is_string($result) OR $is_key)
847 {
Andrey Andreevad47f942011-12-25 19:13:48 +0200848 return '"'.str_replace(array('\\', "\t", "\n", "\r", '"', '/'), array('\\\\', '\\t', '\\n', "\\r", '\"', '\/'), $result).'"';
Derek Jones811f4752010-03-02 18:13:59 -0600849 }
850 elseif (is_scalar($result))
851 {
852 return $result;
853 }
854 }
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Jones811f4752010-03-02 18:13:59 -0600856}