Jquery.php 24 KB

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