Javascript.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * Javascript Class
  19. *
  20. * @package CodeIgniter
  21. * @subpackage Libraries
  22. * @category Javascript
  23. * @author EllisLab Dev Team
  24. * @link http://codeigniter.com/user_guide/libraries/javascript.html
  25. */
  26. class CI_Javascript {
  27. var $_javascript_location = 'js';
  28. public function __construct($params = array())
  29. {
  30. $defaults = array('js_library_driver' => 'jquery', 'autoload' => TRUE);
  31. foreach ($defaults as $key => $val)
  32. {
  33. if (isset($params[$key]) && $params[$key] !== "")
  34. {
  35. $defaults[$key] = $params[$key];
  36. }
  37. }
  38. extract($defaults);
  39. $this->CI =& get_instance();
  40. // load the requested js library
  41. $this->CI->load->library('javascript/'.$js_library_driver, array('autoload' => $autoload));
  42. // make js to refer to current library
  43. $this->js =& $this->CI->$js_library_driver;
  44. log_message('debug', "Javascript Class Initialized and loaded. Driver used: $js_library_driver");
  45. }
  46. // --------------------------------------------------------------------
  47. // Event Code
  48. // --------------------------------------------------------------------
  49. /**
  50. * Blur
  51. *
  52. * Outputs a javascript library blur event
  53. *
  54. * @access public
  55. * @param string The element to attach the event to
  56. * @param string The code to execute
  57. * @return string
  58. */
  59. function blur($element = 'this', $js = '')
  60. {
  61. return $this->js->_blur($element, $js);
  62. }
  63. // --------------------------------------------------------------------
  64. /**
  65. * Change
  66. *
  67. * Outputs a javascript library change event
  68. *
  69. * @access public
  70. * @param string The element to attach the event to
  71. * @param string The code to execute
  72. * @return string
  73. */
  74. function change($element = 'this', $js = '')
  75. {
  76. return $this->js->_change($element, $js);
  77. }
  78. // --------------------------------------------------------------------
  79. /**
  80. * Click
  81. *
  82. * Outputs a javascript library click event
  83. *
  84. * @access public
  85. * @param string The element to attach the event to
  86. * @param string The code to execute
  87. * @param boolean whether or not to return false
  88. * @return string
  89. */
  90. function click($element = 'this', $js = '', $ret_false = TRUE)
  91. {
  92. return $this->js->_click($element, $js, $ret_false);
  93. }
  94. // --------------------------------------------------------------------
  95. /**
  96. * Double Click
  97. *
  98. * Outputs a javascript library dblclick event
  99. *
  100. * @access public
  101. * @param string The element to attach the event to
  102. * @param string The code to execute
  103. * @return string
  104. */
  105. function dblclick($element = 'this', $js = '')
  106. {
  107. return $this->js->_dblclick($element, $js);
  108. }
  109. // --------------------------------------------------------------------
  110. /**
  111. * Error
  112. *
  113. * Outputs a javascript library error event
  114. *
  115. * @access public
  116. * @param string The element to attach the event to
  117. * @param string The code to execute
  118. * @return string
  119. */
  120. function error($element = 'this', $js = '')
  121. {
  122. return $this->js->_error($element, $js);
  123. }
  124. // --------------------------------------------------------------------
  125. /**
  126. * Focus
  127. *
  128. * Outputs a javascript library focus event
  129. *
  130. * @access public
  131. * @param string The element to attach the event to
  132. * @param string The code to execute
  133. * @return string
  134. */
  135. function focus($element = 'this', $js = '')
  136. {
  137. return $this->js->__add_event($focus, $js);
  138. }
  139. // --------------------------------------------------------------------
  140. /**
  141. * Hover
  142. *
  143. * Outputs a javascript library hover event
  144. *
  145. * @access public
  146. * @param string - element
  147. * @param string - Javascript code for mouse over
  148. * @param string - Javascript code for mouse out
  149. * @return string
  150. */
  151. function hover($element = 'this', $over, $out)
  152. {
  153. return $this->js->__hover($element, $over, $out);
  154. }
  155. // --------------------------------------------------------------------
  156. /**
  157. * Keydown
  158. *
  159. * Outputs a javascript library keydown event
  160. *
  161. * @access public
  162. * @param string The element to attach the event to
  163. * @param string The code to execute
  164. * @return string
  165. */
  166. function keydown($element = 'this', $js = '')
  167. {
  168. return $this->js->_keydown($element, $js);
  169. }
  170. // --------------------------------------------------------------------
  171. /**
  172. * Keyup
  173. *
  174. * Outputs a javascript library keydown event
  175. *
  176. * @access public
  177. * @param string The element to attach the event to
  178. * @param string The code to execute
  179. * @return string
  180. */
  181. function keyup($element = 'this', $js = '')
  182. {
  183. return $this->js->_keyup($element, $js);
  184. }
  185. // --------------------------------------------------------------------
  186. /**
  187. * Load
  188. *
  189. * Outputs a javascript library load event
  190. *
  191. * @access public
  192. * @param string The element to attach the event to
  193. * @param string The code to execute
  194. * @return string
  195. */
  196. function load($element = 'this', $js = '')
  197. {
  198. return $this->js->_load($element, $js);
  199. }
  200. // --------------------------------------------------------------------
  201. /**
  202. * Mousedown
  203. *
  204. * Outputs a javascript library mousedown event
  205. *
  206. * @access public
  207. * @param string The element to attach the event to
  208. * @param string The code to execute
  209. * @return string
  210. */
  211. function mousedown($element = 'this', $js = '')
  212. {
  213. return $this->js->_mousedown($element, $js);
  214. }
  215. // --------------------------------------------------------------------
  216. /**
  217. * Mouse Out
  218. *
  219. * Outputs a javascript library mouseout event
  220. *
  221. * @access public
  222. * @param string The element to attach the event to
  223. * @param string The code to execute
  224. * @return string
  225. */
  226. function mouseout($element = 'this', $js = '')
  227. {
  228. return $this->js->_mouseout($element, $js);
  229. }
  230. // --------------------------------------------------------------------
  231. /**
  232. * Mouse Over
  233. *
  234. * Outputs a javascript library mouseover event
  235. *
  236. * @access public
  237. * @param string The element to attach the event to
  238. * @param string The code to execute
  239. * @return string
  240. */
  241. function mouseover($element = 'this', $js = '')
  242. {
  243. return $this->js->_mouseover($element, $js);
  244. }
  245. // --------------------------------------------------------------------
  246. /**
  247. * Mouseup
  248. *
  249. * Outputs a javascript library mouseup event
  250. *
  251. * @access public
  252. * @param string The element to attach the event to
  253. * @param string The code to execute
  254. * @return string
  255. */
  256. function mouseup($element = 'this', $js = '')
  257. {
  258. return $this->js->_mouseup($element, $js);
  259. }
  260. // --------------------------------------------------------------------
  261. /**
  262. * Output
  263. *
  264. * Outputs the called javascript to the screen
  265. *
  266. * @access public
  267. * @param string The code to output
  268. * @return string
  269. */
  270. function output($js)
  271. {
  272. return $this->js->_output($js);
  273. }
  274. // --------------------------------------------------------------------
  275. /**
  276. * Ready
  277. *
  278. * Outputs a javascript library mouseup event
  279. *
  280. * @access public
  281. * @param string The element to attach the event to
  282. * @param string The code to execute
  283. * @return string
  284. */
  285. function ready($js)
  286. {
  287. return $this->js->_document_ready($js);
  288. }
  289. // --------------------------------------------------------------------
  290. /**
  291. * Resize
  292. *
  293. * Outputs a javascript library resize event
  294. *
  295. * @access public
  296. * @param string The element to attach the event to
  297. * @param string The code to execute
  298. * @return string
  299. */
  300. function resize($element = 'this', $js = '')
  301. {
  302. return $this->js->_resize($element, $js);
  303. }
  304. // --------------------------------------------------------------------
  305. /**
  306. * Scroll
  307. *
  308. * Outputs a javascript library scroll event
  309. *
  310. * @access public
  311. * @param string The element to attach the event to
  312. * @param string The code to execute
  313. * @return string
  314. */
  315. function scroll($element = 'this', $js = '')
  316. {
  317. return $this->js->_scroll($element, $js);
  318. }
  319. // --------------------------------------------------------------------
  320. /**
  321. * Unload
  322. *
  323. * Outputs a javascript library unload event
  324. *
  325. * @access public
  326. * @param string The element to attach the event to
  327. * @param string The code to execute
  328. * @return string
  329. */
  330. function unload($element = 'this', $js = '')
  331. {
  332. return $this->js->_unload($element, $js);
  333. }
  334. // --------------------------------------------------------------------
  335. // Effects
  336. // --------------------------------------------------------------------
  337. /**
  338. * Add Class
  339. *
  340. * Outputs a javascript library addClass event
  341. *
  342. * @access public
  343. * @param string - element
  344. * @param string - Class to add
  345. * @return string
  346. */
  347. function addClass($element = 'this', $class = '')
  348. {
  349. return $this->js->_addClass($element, $class);
  350. }
  351. // --------------------------------------------------------------------
  352. /**
  353. * Animate
  354. *
  355. * Outputs a javascript library animate event
  356. *
  357. * @access public
  358. * @param string - element
  359. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  360. * @param string - Javascript callback function
  361. * @return string
  362. */
  363. function animate($element = 'this', $params = array(), $speed = '', $extra = '')
  364. {
  365. return $this->js->_animate($element, $params, $speed, $extra);
  366. }
  367. // --------------------------------------------------------------------
  368. /**
  369. * Fade In
  370. *
  371. * Outputs a javascript library hide event
  372. *
  373. * @access public
  374. * @param string - element
  375. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  376. * @param string - Javascript callback function
  377. * @return string
  378. */
  379. function fadeIn($element = 'this', $speed = '', $callback = '')
  380. {
  381. return $this->js->_fadeIn($element, $speed, $callback);
  382. }
  383. // --------------------------------------------------------------------
  384. /**
  385. * Fade Out
  386. *
  387. * Outputs a javascript library hide event
  388. *
  389. * @access public
  390. * @param string - element
  391. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  392. * @param string - Javascript callback function
  393. * @return string
  394. */
  395. function fadeOut($element = 'this', $speed = '', $callback = '')
  396. {
  397. return $this->js->_fadeOut($element, $speed, $callback);
  398. }
  399. // --------------------------------------------------------------------
  400. /**
  401. * Slide Up
  402. *
  403. * Outputs a javascript library slideUp event
  404. *
  405. * @access public
  406. * @param string - element
  407. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  408. * @param string - Javascript callback function
  409. * @return string
  410. */
  411. function slideUp($element = 'this', $speed = '', $callback = '')
  412. {
  413. return $this->js->_slideUp($element, $speed, $callback);
  414. }
  415. // --------------------------------------------------------------------
  416. /**
  417. * Remove Class
  418. *
  419. * Outputs a javascript library removeClass event
  420. *
  421. * @access public
  422. * @param string - element
  423. * @param string - Class to add
  424. * @return string
  425. */
  426. function removeClass($element = 'this', $class = '')
  427. {
  428. return $this->js->_removeClass($element, $class);
  429. }
  430. // --------------------------------------------------------------------
  431. /**
  432. * Slide Down
  433. *
  434. * Outputs a javascript library slideDown event
  435. *
  436. * @access public
  437. * @param string - element
  438. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  439. * @param string - Javascript callback function
  440. * @return string
  441. */
  442. function slideDown($element = 'this', $speed = '', $callback = '')
  443. {
  444. return $this->js->_slideDown($element, $speed, $callback);
  445. }
  446. // --------------------------------------------------------------------
  447. /**
  448. * Slide Toggle
  449. *
  450. * Outputs a javascript library slideToggle event
  451. *
  452. * @access public
  453. * @param string - element
  454. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  455. * @param string - Javascript callback function
  456. * @return string
  457. */
  458. function slideToggle($element = 'this', $speed = '', $callback = '')
  459. {
  460. return $this->js->_slideToggle($element, $speed, $callback);
  461. }
  462. // --------------------------------------------------------------------
  463. /**
  464. * Hide
  465. *
  466. * Outputs a javascript library hide action
  467. *
  468. * @access public
  469. * @param string - element
  470. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  471. * @param string - Javascript callback function
  472. * @return string
  473. */
  474. function hide($element = 'this', $speed = '', $callback = '')
  475. {
  476. return $this->js->_hide($element, $speed, $callback);
  477. }
  478. // --------------------------------------------------------------------
  479. /**
  480. * Toggle
  481. *
  482. * Outputs a javascript library toggle event
  483. *
  484. * @access public
  485. * @param string - element
  486. * @return string
  487. */
  488. function toggle($element = 'this')
  489. {
  490. return $this->js->_toggle($element);
  491. }
  492. // --------------------------------------------------------------------
  493. /**
  494. * Toggle Class
  495. *
  496. * Outputs a javascript library toggle class event
  497. *
  498. * @access public
  499. * @param string - element
  500. * @return string
  501. */
  502. function toggleClass($element = 'this', $class='')
  503. {
  504. return $this->js->_toggleClass($element, $class);
  505. }
  506. // --------------------------------------------------------------------
  507. /**
  508. * Show
  509. *
  510. * Outputs a javascript library show event
  511. *
  512. * @access public
  513. * @param string - element
  514. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  515. * @param string - Javascript callback function
  516. * @return string
  517. */
  518. function show($element = 'this', $speed = '', $callback = '')
  519. {
  520. return $this->js->_show($element, $speed, $callback);
  521. }
  522. // --------------------------------------------------------------------
  523. /**
  524. * Compile
  525. *
  526. * gather together all script needing to be output
  527. *
  528. * @access public
  529. * @param string The element to attach the event to
  530. * @return string
  531. */
  532. function compile($view_var = 'script_foot', $script_tags = TRUE)
  533. {
  534. $this->js->_compile($view_var, $script_tags);
  535. }
  536. /**
  537. * Clear Compile
  538. *
  539. * Clears any previous javascript collected for output
  540. *
  541. * @access public
  542. * @return void
  543. */
  544. function clear_compile()
  545. {
  546. $this->js->_clear_compile();
  547. }
  548. // --------------------------------------------------------------------
  549. /**
  550. * External
  551. *
  552. * Outputs a <script> tag with the source as an external js file
  553. *
  554. * @access public
  555. * @param string The element to attach the event to
  556. * @return string
  557. */
  558. function external($external_file = '', $relative = FALSE)
  559. {
  560. if ($external_file !== '')
  561. {
  562. $this->_javascript_location = $external_file;
  563. }
  564. else
  565. {
  566. if ($this->CI->config->item('javascript_location') != '')
  567. {
  568. $this->_javascript_location = $this->CI->config->item('javascript_location');
  569. }
  570. }
  571. if ($relative === TRUE OR strncmp($external_file, 'http://', 7) == 0 OR strncmp($external_file, 'https://', 8) == 0)
  572. {
  573. $str = $this->_open_script($external_file);
  574. }
  575. elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
  576. {
  577. $str = $this->_open_script($this->_javascript_location.$external_file);
  578. }
  579. else
  580. {
  581. $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location.$external_file);
  582. }
  583. $str .= $this->_close_script();
  584. return $str;
  585. }
  586. // --------------------------------------------------------------------
  587. /**
  588. * Inline
  589. *
  590. * Outputs a <script> tag
  591. *
  592. * @access public
  593. * @param string The element to attach the event to
  594. * @param boolean If a CDATA section should be added
  595. * @return string
  596. */
  597. function inline($script, $cdata = TRUE)
  598. {
  599. $str = $this->_open_script();
  600. $str .= ($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n";
  601. $str .= $this->_close_script();
  602. return $str;
  603. }
  604. // --------------------------------------------------------------------
  605. /**
  606. * Open Script
  607. *
  608. * Outputs an opening <script>
  609. *
  610. * @access private
  611. * @param string
  612. * @return string
  613. */
  614. function _open_script($src = '')
  615. {
  616. $str = '<script type="text/javascript" charset="'.strtolower($this->CI->config->item('charset')).'"';
  617. $str .= ($src == '') ? '>' : ' src="'.$src.'">';
  618. return $str;
  619. }
  620. // --------------------------------------------------------------------
  621. /**
  622. * Close Script
  623. *
  624. * Outputs an closing </script>
  625. *
  626. * @access private
  627. * @param string
  628. * @return string
  629. */
  630. function _close_script($extra = "\n")
  631. {
  632. return "</script>$extra";
  633. }
  634. // --------------------------------------------------------------------
  635. // --------------------------------------------------------------------
  636. // AJAX-Y STUFF - still a testbed
  637. // --------------------------------------------------------------------
  638. // --------------------------------------------------------------------
  639. /**
  640. * Update
  641. *
  642. * Outputs a javascript library slideDown event
  643. *
  644. * @access public
  645. * @param string - element
  646. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  647. * @param string - Javascript callback function
  648. * @return string
  649. */
  650. function update($element = 'this', $speed = '', $callback = '')
  651. {
  652. return $this->js->_updater($element, $speed, $callback);
  653. }
  654. // --------------------------------------------------------------------
  655. /**
  656. * Generate JSON
  657. *
  658. * Can be passed a database result or associative array and returns a JSON formatted string
  659. *
  660. * @param mixed result set or array
  661. * @param bool match array types (defaults to objects)
  662. * @return string a json formatted string
  663. */
  664. function generate_json($result = NULL, $match_array_type = FALSE)
  665. {
  666. // JSON data can optionally be passed to this function
  667. // either as a database result object or an array, or a user supplied array
  668. if ( ! is_null($result))
  669. {
  670. if (is_object($result))
  671. {
  672. $json_result = $result->result_array();
  673. }
  674. elseif (is_array($result))
  675. {
  676. $json_result = $result;
  677. }
  678. else
  679. {
  680. return $this->_prep_args($result);
  681. }
  682. }
  683. else
  684. {
  685. return 'null';
  686. }
  687. $json = array();
  688. $_is_assoc = TRUE;
  689. if ( ! is_array($json_result) AND empty($json_result))
  690. {
  691. show_error("Generate JSON Failed - Illegal key, value pair.");
  692. }
  693. elseif ($match_array_type)
  694. {
  695. $_is_assoc = $this->_is_associative_array($json_result);
  696. }
  697. foreach ($json_result as $k => $v)
  698. {
  699. if ($_is_assoc)
  700. {
  701. $json[] = $this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
  702. }
  703. else
  704. {
  705. $json[] = $this->generate_json($v, $match_array_type);
  706. }
  707. }
  708. $json = implode(',', $json);
  709. return $_is_assoc ? "{".$json."}" : "[".$json."]";
  710. }
  711. // --------------------------------------------------------------------
  712. /**
  713. * Is associative array
  714. *
  715. * Checks for an associative array
  716. *
  717. * @access public
  718. * @param type
  719. * @return type
  720. */
  721. function _is_associative_array($arr)
  722. {
  723. foreach (array_keys($arr) as $key => $val)
  724. {
  725. if ($key !== $val)
  726. {
  727. return TRUE;
  728. }
  729. }
  730. return FALSE;
  731. }
  732. // --------------------------------------------------------------------
  733. /**
  734. * Prep Args
  735. *
  736. * Ensures a standard json value and escapes values
  737. *
  738. * @access public
  739. * @param type
  740. * @return type
  741. */
  742. function _prep_args($result, $is_key = FALSE)
  743. {
  744. if (is_null($result))
  745. {
  746. return 'null';
  747. }
  748. elseif (is_bool($result))
  749. {
  750. return ($result === TRUE) ? 'true' : 'false';
  751. }
  752. elseif (is_string($result) OR $is_key)
  753. {
  754. return '"'.str_replace(array('\\', "\t", "\n", "\r", '"', '/'), array('\\\\', '\\t', '\\n', "\\r", '\"', '\/'), $result).'"';
  755. }
  756. elseif (is_scalar($result))
  757. {
  758. return $result;
  759. }
  760. }
  761. // --------------------------------------------------------------------
  762. }
  763. // END Javascript Class
  764. /* End of file Javascript.php */
  765. /* Location: ./system/libraries/Javascript.php */