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.

1076 lines
25 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. * Jquery Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Loader
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/libraries/javascript.html
  47. */
  48. class CI_Jquery extends CI_Javascript {
  49. /**
  50. * JavaScript directory location
  51. *
  52. * @var string
  53. */
  54. protected $_javascript_folder = 'js';
  55. /**
  56. * JQuery code for load
  57. *
  58. * @var array
  59. */
  60. public $jquery_code_for_load = array();
  61. /**
  62. * JQuery code for compile
  63. *
  64. * @var array
  65. */
  66. public $jquery_code_for_compile = array();
  67. /**
  68. * JQuery corner active flag
  69. *
  70. * @var bool
  71. */
  72. public $jquery_corner_active = FALSE;
  73. /**
  74. * JQuery table sorter active flag
  75. *
  76. * @var bool
  77. */
  78. public $jquery_table_sorter_active = FALSE;
  79. /**
  80. * JQuery table sorter pager active
  81. *
  82. * @var bool
  83. */
  84. public $jquery_table_sorter_pager_active = FALSE;
  85. /**
  86. * JQuery AJAX image
  87. *
  88. * @var string
  89. */
  90. public $jquery_ajax_img = '';
  91. // --------------------------------------------------------------------
  92. /**
  93. * Constructor
  94. *
  95. * @param array $params
  96. * @return void
  97. */
  98. public function __construct($params)
  99. {
  100. $this->CI =& get_instance();
  101. extract($params);
  102. if ($autoload === TRUE)
  103. {
  104. $this->script();
  105. }
  106. log_message('info', 'Jquery Class Initialized');
  107. }
  108. // --------------------------------------------------------------------
  109. // Event Code
  110. // --------------------------------------------------------------------
  111. /**
  112. * Blur
  113. *
  114. * Outputs a jQuery blur event
  115. *
  116. * @param string The element to attach the event to
  117. * @param string The code to execute
  118. * @return string
  119. */
  120. protected function _blur($element = 'this', $js = '')
  121. {
  122. return $this->_add_event($element, $js, 'blur');
  123. }
  124. // --------------------------------------------------------------------
  125. /**
  126. * Change
  127. *
  128. * Outputs a jQuery change event
  129. *
  130. * @param string The element to attach the event to
  131. * @param string The code to execute
  132. * @return string
  133. */
  134. protected function _change($element = 'this', $js = '')
  135. {
  136. return $this->_add_event($element, $js, 'change');
  137. }
  138. // --------------------------------------------------------------------
  139. /**
  140. * Click
  141. *
  142. * Outputs a jQuery click event
  143. *
  144. * @param string The element to attach the event to
  145. * @param string The code to execute
  146. * @param bool whether or not to return false
  147. * @return string
  148. */
  149. protected function _click($element = 'this', $js = '', $ret_false = TRUE)
  150. {
  151. is_array($js) OR $js = array($js);
  152. if ($ret_false)
  153. {
  154. $js[] = 'return false;';
  155. }
  156. return $this->_add_event($element, $js, 'click');
  157. }
  158. // --------------------------------------------------------------------
  159. /**
  160. * Double Click
  161. *
  162. * Outputs a jQuery dblclick event
  163. *
  164. * @param string The element to attach the event to
  165. * @param string The code to execute
  166. * @return string
  167. */
  168. protected function _dblclick($element = 'this', $js = '')
  169. {
  170. return $this->_add_event($element, $js, 'dblclick');
  171. }
  172. // --------------------------------------------------------------------
  173. /**
  174. * Error
  175. *
  176. * Outputs a jQuery error event
  177. *
  178. * @param string The element to attach the event to
  179. * @param string The code to execute
  180. * @return string
  181. */
  182. protected function _error($element = 'this', $js = '')
  183. {
  184. return $this->_add_event($element, $js, 'error');
  185. }
  186. // --------------------------------------------------------------------
  187. /**
  188. * Focus
  189. *
  190. * Outputs a jQuery focus event
  191. *
  192. * @param string The element to attach the event to
  193. * @param string The code to execute
  194. * @return string
  195. */
  196. protected function _focus($element = 'this', $js = '')
  197. {
  198. return $this->_add_event($element, $js, 'focus');
  199. }
  200. // --------------------------------------------------------------------
  201. /**
  202. * Hover
  203. *
  204. * Outputs a jQuery hover event
  205. *
  206. * @param string - element
  207. * @param string - Javascript code for mouse over
  208. * @param string - Javascript code for mouse out
  209. * @return string
  210. */
  211. protected function _hover($element = 'this', $over = '', $out = '')
  212. {
  213. $event = "\n\t$(".$this->_prep_element($element).").hover(\n\t\tfunction()\n\t\t{\n\t\t\t{$over}\n\t\t}, \n\t\tfunction()\n\t\t{\n\t\t\t{$out}\n\t\t});\n";
  214. $this->jquery_code_for_compile[] = $event;
  215. return $event;
  216. }
  217. // --------------------------------------------------------------------
  218. /**
  219. * Keydown
  220. *
  221. * Outputs a jQuery keydown event
  222. *
  223. * @param string The element to attach the event to
  224. * @param string The code to execute
  225. * @return string
  226. */
  227. protected function _keydown($element = 'this', $js = '')
  228. {
  229. return $this->_add_event($element, $js, 'keydown');
  230. }
  231. // --------------------------------------------------------------------
  232. /**
  233. * Keyup
  234. *
  235. * Outputs a jQuery keydown event
  236. *
  237. * @param string The element to attach the event to
  238. * @param string The code to execute
  239. * @return string
  240. */
  241. protected function _keyup($element = 'this', $js = '')
  242. {
  243. return $this->_add_event($element, $js, 'keyup');
  244. }
  245. // --------------------------------------------------------------------
  246. /**
  247. * Load
  248. *
  249. * Outputs a jQuery load event
  250. *
  251. * @param string The element to attach the event to
  252. * @param string The code to execute
  253. * @return string
  254. */
  255. protected function _load($element = 'this', $js = '')
  256. {
  257. return $this->_add_event($element, $js, 'load');
  258. }
  259. // --------------------------------------------------------------------
  260. /**
  261. * Mousedown
  262. *
  263. * Outputs a jQuery mousedown event
  264. *
  265. * @param string The element to attach the event to
  266. * @param string The code to execute
  267. * @return string
  268. */
  269. protected function _mousedown($element = 'this', $js = '')
  270. {
  271. return $this->_add_event($element, $js, 'mousedown');
  272. }
  273. // --------------------------------------------------------------------
  274. /**
  275. * Mouse Out
  276. *
  277. * Outputs a jQuery mouseout event
  278. *
  279. * @param string The element to attach the event to
  280. * @param string The code to execute
  281. * @return string
  282. */
  283. protected function _mouseout($element = 'this', $js = '')
  284. {
  285. return $this->_add_event($element, $js, 'mouseout');
  286. }
  287. // --------------------------------------------------------------------
  288. /**
  289. * Mouse Over
  290. *
  291. * Outputs a jQuery mouseover event
  292. *
  293. * @param string The element to attach the event to
  294. * @param string The code to execute
  295. * @return string
  296. */
  297. protected function _mouseover($element = 'this', $js = '')
  298. {
  299. return $this->_add_event($element, $js, 'mouseover');
  300. }
  301. // --------------------------------------------------------------------
  302. /**
  303. * Mouseup
  304. *
  305. * Outputs a jQuery mouseup event
  306. *
  307. * @param string The element to attach the event to
  308. * @param string The code to execute
  309. * @return string
  310. */
  311. protected function _mouseup($element = 'this', $js = '')
  312. {
  313. return $this->_add_event($element, $js, 'mouseup');
  314. }
  315. // --------------------------------------------------------------------
  316. /**
  317. * Output
  318. *
  319. * Outputs script directly
  320. *
  321. * @param array $array_js = array()
  322. * @return void
  323. */
  324. protected function _output($array_js = array())
  325. {
  326. if ( ! is_array($array_js))
  327. {
  328. $array_js = array($array_js);
  329. }
  330. foreach ($array_js as $js)
  331. {
  332. $this->jquery_code_for_compile[] = "\t".$js."\n";
  333. }
  334. }
  335. // --------------------------------------------------------------------
  336. /**
  337. * Resize
  338. *
  339. * Outputs a jQuery resize event
  340. *
  341. * @param string The element to attach the event to
  342. * @param string The code to execute
  343. * @return string
  344. */
  345. protected function _resize($element = 'this', $js = '')
  346. {
  347. return $this->_add_event($element, $js, 'resize');
  348. }
  349. // --------------------------------------------------------------------
  350. /**
  351. * Scroll
  352. *
  353. * Outputs a jQuery scroll event
  354. *
  355. * @param string The element to attach the event to
  356. * @param string The code to execute
  357. * @return string
  358. */
  359. protected function _scroll($element = 'this', $js = '')
  360. {
  361. return $this->_add_event($element, $js, 'scroll');
  362. }
  363. // --------------------------------------------------------------------
  364. /**
  365. * Unload
  366. *
  367. * Outputs a jQuery unload event
  368. *
  369. * @param string The element to attach the event to
  370. * @param string The code to execute
  371. * @return string
  372. */
  373. protected function _unload($element = 'this', $js = '')
  374. {
  375. return $this->_add_event($element, $js, 'unload');
  376. }
  377. // --------------------------------------------------------------------
  378. // Effects
  379. // --------------------------------------------------------------------
  380. /**
  381. * Add Class
  382. *
  383. * Outputs a jQuery addClass event
  384. *
  385. * @param string $element
  386. * @param string $class
  387. * @return string
  388. */
  389. protected function _addClass($element = 'this', $class = '')
  390. {
  391. $element = $this->_prep_element($element);
  392. return '$('.$element.').addClass("'.$class.'");';
  393. }
  394. // --------------------------------------------------------------------
  395. /**
  396. * Animate
  397. *
  398. * Outputs a jQuery animate event
  399. *
  400. * @param string $element
  401. * @param array $params
  402. * @param string $speed 'slow', 'normal', 'fast', or time in milliseconds
  403. * @param string $extra
  404. * @return string
  405. */
  406. protected function _animate($element = 'this', $params = array(), $speed = '', $extra = '')
  407. {
  408. $element = $this->_prep_element($element);
  409. $speed = $this->_validate_speed($speed);
  410. $animations = "\t\t\t";
  411. foreach ($params as $param => $value)
  412. {
  413. $animations .= $param.": '".$value."', ";
  414. }
  415. $animations = substr($animations, 0, -2); // remove the last ", "
  416. if ($speed !== '')
  417. {
  418. $speed = ', '.$speed;
  419. }
  420. if ($extra !== '')
  421. {
  422. $extra = ', '.$extra;
  423. }
  424. return "$({$element}).animate({\n$animations\n\t\t}".$speed.$extra.');';
  425. }
  426. // --------------------------------------------------------------------
  427. /**
  428. * Fade In
  429. *
  430. * Outputs a jQuery hide event
  431. *
  432. * @param string - element
  433. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  434. * @param string - Javascript callback function
  435. * @return string
  436. */
  437. protected function _fadeIn($element = 'this', $speed = '', $callback = '')
  438. {
  439. $element = $this->_prep_element($element);
  440. $speed = $this->_validate_speed($speed);
  441. if ($callback !== '')
  442. {
  443. $callback = ", function(){\n{$callback}\n}";
  444. }
  445. return "$({$element}).fadeIn({$speed}{$callback});";
  446. }
  447. // --------------------------------------------------------------------
  448. /**
  449. * Fade Out
  450. *
  451. * Outputs a jQuery hide event
  452. *
  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. protected function _fadeOut($element = 'this', $speed = '', $callback = '')
  459. {
  460. $element = $this->_prep_element($element);
  461. $speed = $this->_validate_speed($speed);
  462. if ($callback !== '')
  463. {
  464. $callback = ", function(){\n{$callback}\n}";
  465. }
  466. return '$('.$element.').fadeOut('.$speed.$callback.');';
  467. }
  468. // --------------------------------------------------------------------
  469. /**
  470. * Hide
  471. *
  472. * Outputs a jQuery hide action
  473. *
  474. * @param string - element
  475. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  476. * @param string - Javascript callback function
  477. * @return string
  478. */
  479. protected function _hide($element = 'this', $speed = '', $callback = '')
  480. {
  481. $element = $this->_prep_element($element);
  482. $speed = $this->_validate_speed($speed);
  483. if ($callback !== '')
  484. {
  485. $callback = ", function(){\n{$callback}\n}";
  486. }
  487. return "$({$element}).hide({$speed}{$callback});";
  488. }
  489. // --------------------------------------------------------------------
  490. /**
  491. * Remove Class
  492. *
  493. * Outputs a jQuery remove class event
  494. *
  495. * @param string $element
  496. * @param string $class
  497. * @return string
  498. */
  499. protected function _removeClass($element = 'this', $class = '')
  500. {
  501. $element = $this->_prep_element($element);
  502. return '$('.$element.').removeClass("'.$class.'");';
  503. }
  504. // --------------------------------------------------------------------
  505. /**
  506. * Slide Up
  507. *
  508. * Outputs a jQuery slideUp event
  509. *
  510. * @param string - element
  511. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  512. * @param string - Javascript callback function
  513. * @return string
  514. */
  515. protected function _slideUp($element = 'this', $speed = '', $callback = '')
  516. {
  517. $element = $this->_prep_element($element);
  518. $speed = $this->_validate_speed($speed);
  519. if ($callback !== '')
  520. {
  521. $callback = ", function(){\n{$callback}\n}";
  522. }
  523. return '$('.$element.').slideUp('.$speed.$callback.');';
  524. }
  525. // --------------------------------------------------------------------
  526. /**
  527. * Slide Down
  528. *
  529. * Outputs a jQuery slideDown event
  530. *
  531. * @param string - element
  532. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  533. * @param string - Javascript callback function
  534. * @return string
  535. */
  536. protected function _slideDown($element = 'this', $speed = '', $callback = '')
  537. {
  538. $element = $this->_prep_element($element);
  539. $speed = $this->_validate_speed($speed);
  540. if ($callback !== '')
  541. {
  542. $callback = ", function(){\n{$callback}\n}";
  543. }
  544. return '$('.$element.').slideDown('.$speed.$callback.');';
  545. }
  546. // --------------------------------------------------------------------
  547. /**
  548. * Slide Toggle
  549. *
  550. * Outputs a jQuery slideToggle event
  551. *
  552. * @param string - element
  553. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  554. * @param string - Javascript callback function
  555. * @return string
  556. */
  557. protected function _slideToggle($element = 'this', $speed = '', $callback = '')
  558. {
  559. $element = $this->_prep_element($element);
  560. $speed = $this->_validate_speed($speed);
  561. if ($callback !== '')
  562. {
  563. $callback = ", function(){\n{$callback}\n}";
  564. }
  565. return '$('.$element.').slideToggle('.$speed.$callback.');';
  566. }
  567. // --------------------------------------------------------------------
  568. /**
  569. * Toggle
  570. *
  571. * Outputs a jQuery toggle event
  572. *
  573. * @param string - element
  574. * @return string
  575. */
  576. protected function _toggle($element = 'this')
  577. {
  578. $element = $this->_prep_element($element);
  579. return '$('.$element.').toggle();';
  580. }
  581. // --------------------------------------------------------------------
  582. /**
  583. * Toggle Class
  584. *
  585. * Outputs a jQuery toggle class event
  586. *
  587. * @param string $element
  588. * @param string $class
  589. * @return string
  590. */
  591. protected function _toggleClass($element = 'this', $class = '')
  592. {
  593. $element = $this->_prep_element($element);
  594. return '$('.$element.').toggleClass("'.$class.'");';
  595. }
  596. // --------------------------------------------------------------------
  597. /**
  598. * Show
  599. *
  600. * Outputs a jQuery show event
  601. *
  602. * @param string - element
  603. * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
  604. * @param string - Javascript callback function
  605. * @return string
  606. */
  607. protected function _show($element = 'this', $speed = '', $callback = '')
  608. {
  609. $element = $this->_prep_element($element);
  610. $speed = $this->_validate_speed($speed);
  611. if ($callback !== '')
  612. {
  613. $callback = ", function(){\n{$callback}\n}";
  614. }
  615. return '$('.$element.').show('.$speed.$callback.');';
  616. }
  617. // --------------------------------------------------------------------
  618. /**
  619. * Updater
  620. *
  621. * An Ajax call that populates the designated DOM node with
  622. * returned content
  623. *
  624. * @param string The element to attach the event to
  625. * @param string the controller to run the call against
  626. * @param string optional parameters
  627. * @return string
  628. */
  629. protected function _updater($container = 'this', $controller = '', $options = '')
  630. {
  631. $container = $this->_prep_element($container);
  632. $controller = (strpos('://', $controller) === FALSE) ? $controller : $this->CI->config->site_url($controller);
  633. // ajaxStart and ajaxStop are better choices here... but this is a stop gap
  634. if ($this->CI->config->item('javascript_ajax_img') === '')
  635. {
  636. $loading_notifier = 'Loading...';
  637. }
  638. else
  639. {
  640. $loading_notifier = '<img src="'.$this->CI->config->slash_item('base_url').$this->CI->config->item('javascript_ajax_img').'" alt="Loading" />';
  641. }
  642. $updater = '$('.$container.").empty();\n" // anything that was in... get it out
  643. ."\t\t$(".$container.').prepend("'.$loading_notifier."\");\n"; // to replace with an image
  644. $request_options = '';
  645. if ($options !== '')
  646. {
  647. $request_options .= ', {'
  648. .(is_array($options) ? "'".implode("', '", $options)."'" : "'".str_replace(':', "':'", $options)."'")
  649. .'}';
  650. }
  651. return $updater."\t\t$($container).load('$controller'$request_options);";
  652. }
  653. // --------------------------------------------------------------------
  654. // Pre-written handy stuff
  655. // --------------------------------------------------------------------
  656. /**
  657. * Zebra tables
  658. *
  659. * @param string $class
  660. * @param string $odd
  661. * @param string $hover
  662. * @return string
  663. */
  664. protected function _zebraTables($class = '', $odd = 'odd', $hover = '')
  665. {
  666. $class = ($class !== '') ? '.'.$class : '';
  667. $zebra = "\t\$(\"table{$class} tbody tr:nth-child(even)\").addClass(\"{$odd}\");";
  668. $this->jquery_code_for_compile[] = $zebra;
  669. if ($hover !== '')
  670. {
  671. $hover = $this->hover("table{$class} tbody tr", "$(this).addClass('hover');", "$(this).removeClass('hover');");
  672. }
  673. return $zebra;
  674. }
  675. // --------------------------------------------------------------------
  676. // Plugins
  677. // --------------------------------------------------------------------
  678. /**
  679. * Corner Plugin
  680. *
  681. * @link http://www.malsup.com/jquery/corner/
  682. * @param string $element
  683. * @param string $corner_style
  684. * @return string
  685. */
  686. public function corner($element = '', $corner_style = '')
  687. {
  688. // may want to make this configurable down the road
  689. $corner_location = '/plugins/jquery.corner.js';
  690. if ($corner_style !== '')
  691. {
  692. $corner_style = '"'.$corner_style.'"';
  693. }
  694. return '$('.$this->_prep_element($element).').corner('.$corner_style.');';
  695. }
  696. // --------------------------------------------------------------------
  697. /**
  698. * Modal window
  699. *
  700. * Load a thickbox modal window
  701. *
  702. * @param string $src
  703. * @param bool $relative
  704. * @return void
  705. */
  706. public function modal($src, $relative = FALSE)
  707. {
  708. $this->jquery_code_for_load[] = $this->external($src, $relative);
  709. }
  710. // --------------------------------------------------------------------
  711. /**
  712. * Effect
  713. *
  714. * Load an Effect library
  715. *
  716. * @param string $src
  717. * @param bool $relative
  718. * @return void
  719. */
  720. public function effect($src, $relative = FALSE)
  721. {
  722. $this->jquery_code_for_load[] = $this->external($src, $relative);
  723. }
  724. // --------------------------------------------------------------------
  725. /**
  726. * Plugin
  727. *
  728. * Load a plugin library
  729. *
  730. * @param string $src
  731. * @param bool $relative
  732. * @return void
  733. */
  734. public function plugin($src, $relative = FALSE)
  735. {
  736. $this->jquery_code_for_load[] = $this->external($src, $relative);
  737. }
  738. // --------------------------------------------------------------------
  739. /**
  740. * UI
  741. *
  742. * Load a user interface library
  743. *
  744. * @param string $src
  745. * @param bool $relative
  746. * @return void
  747. */
  748. public function ui($src, $relative = FALSE)
  749. {
  750. $this->jquery_code_for_load[] = $this->external($src, $relative);
  751. }
  752. // --------------------------------------------------------------------
  753. /**
  754. * Sortable
  755. *
  756. * Creates a jQuery sortable
  757. *
  758. * @param string $element
  759. * @param array $options
  760. * @return string
  761. */
  762. public function sortable($element, $options = array())
  763. {
  764. if (count($options) > 0)
  765. {
  766. $sort_options = array();
  767. foreach ($options as $k=>$v)
  768. {
  769. $sort_options[] = "\n\t\t".$k.': '.$v;
  770. }
  771. $sort_options = implode(',', $sort_options);
  772. }
  773. else
  774. {
  775. $sort_options = '';
  776. }
  777. return '$('.$this->_prep_element($element).').sortable({'.$sort_options."\n\t});";
  778. }
  779. // --------------------------------------------------------------------
  780. /**
  781. * Table Sorter Plugin
  782. *
  783. * @param string table name
  784. * @param string plugin location
  785. * @return string
  786. */
  787. public function tablesorter($table = '', $options = '')
  788. {
  789. $this->jquery_code_for_compile[] = "\t$(".$this->_prep_element($table).').tablesorter('.$options.");\n";
  790. }
  791. // --------------------------------------------------------------------
  792. // Class functions
  793. // --------------------------------------------------------------------
  794. /**
  795. * Add Event
  796. *
  797. * Constructs the syntax for an event, and adds to into the array for compilation
  798. *
  799. * @param string The element to attach the event to
  800. * @param string The code to execute
  801. * @param string The event to pass
  802. * @return string
  803. */
  804. protected function _add_event($element, $js, $event)
  805. {
  806. if (is_array($js))
  807. {
  808. $js = implode("\n\t\t", $js);
  809. }
  810. $event = "\n\t$(".$this->_prep_element($element).').'.$event."(function(){\n\t\t{$js}\n\t});\n";
  811. $this->jquery_code_for_compile[] = $event;
  812. return $event;
  813. }
  814. // --------------------------------------------------------------------
  815. /**
  816. * Compile
  817. *
  818. * As events are specified, they are stored in an array
  819. * This function compiles them all for output on a page
  820. *
  821. * @param string $view_var
  822. * @param bool $script_tags
  823. * @return void
  824. */
  825. protected function _compile($view_var = 'script_foot', $script_tags = TRUE)
  826. {
  827. // External references
  828. $external_scripts = implode('', $this->jquery_code_for_load);
  829. $this->CI->load->vars(array('library_src' => $external_scripts));
  830. if (count($this->jquery_code_for_compile) === 0)
  831. {
  832. // no inline references, let's just return
  833. return;
  834. }
  835. // Inline references
  836. $script = '$(document).ready(function() {'."\n"
  837. .implode('', $this->jquery_code_for_compile)
  838. .'});';
  839. $output = ($script_tags === FALSE) ? $script : $this->inline($script);
  840. $this->CI->load->vars(array($view_var => $output));
  841. }
  842. // --------------------------------------------------------------------
  843. /**
  844. * Clear Compile
  845. *
  846. * Clears the array of script events collected for output
  847. *
  848. * @return void
  849. */
  850. protected function _clear_compile()
  851. {
  852. $this->jquery_code_for_compile = array();
  853. }
  854. // --------------------------------------------------------------------
  855. /**
  856. * Document Ready
  857. *
  858. * A wrapper for writing document.ready()
  859. *
  860. * @param array $js
  861. * @return void
  862. */
  863. protected function _document_ready($js)
  864. {
  865. is_array($js) OR $js = array($js);
  866. foreach ($js as $script)
  867. {
  868. $this->jquery_code_for_compile[] = $script;
  869. }
  870. }
  871. // --------------------------------------------------------------------
  872. /**
  873. * Script Tag
  874. *
  875. * Outputs the script tag that loads the jquery.js file into an HTML document
  876. *
  877. * @param string $library_src
  878. * @param bool $relative
  879. * @return string
  880. */
  881. public function script($library_src = '', $relative = FALSE)
  882. {
  883. $library_src = $this->external($library_src, $relative);
  884. $this->jquery_code_for_load[] = $library_src;
  885. return $library_src;
  886. }
  887. // --------------------------------------------------------------------
  888. /**
  889. * Prep Element
  890. *
  891. * Puts HTML element in quotes for use in jQuery code
  892. * unless the supplied element is the Javascript 'this'
  893. * object, in which case no quotes are added
  894. *
  895. * @param string
  896. * @return string
  897. */
  898. protected function _prep_element($element)
  899. {
  900. if ($element !== 'this')
  901. {
  902. $element = '"'.$element.'"';
  903. }
  904. return $element;
  905. }
  906. // --------------------------------------------------------------------
  907. /**
  908. * Validate Speed
  909. *
  910. * Ensures the speed parameter is valid for jQuery
  911. *
  912. * @param string
  913. * @return string
  914. */
  915. protected function _validate_speed($speed)
  916. {
  917. if (in_array($speed, array('slow', 'normal', 'fast')))
  918. {
  919. return '"'.$speed.'"';
  920. }
  921. elseif (preg_match('/[^0-9]/', $speed))
  922. {
  923. return '';
  924. }
  925. return $speed;
  926. }
  927. }