PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Javascript.php

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