You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

856 lines
20 KiB

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