Javascript.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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 - 2019, 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 - 2019, 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. }