JQuery.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category ZendX
  16. * @package ZendX_JQuery
  17. * @subpackage View
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see ZendX_JQuery
  24. */
  25. require_once "ZendX/JQuery.php";
  26. /**
  27. * @see Zend_Registry
  28. */
  29. require_once 'Zend/Registry.php';
  30. /**
  31. * @see Zend_View_Helper_Abstract
  32. */
  33. require_once 'Zend/View/Helper/Abstract.php';
  34. /**
  35. * @see ZendX_JQuery_View_Helper_JQuery_Container
  36. */
  37. require_once "ZendX/JQuery/View/Helper/JQuery/Container.php";
  38. /**
  39. * jQuery Helper. Functions as a stack for code and loads all jQuery dependencies.
  40. *
  41. * @uses Zend_Json
  42. * @package ZendX_JQuery
  43. * @subpackage View
  44. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. */
  47. class ZendX_JQuery_View_Helper_JQuery extends Zend_View_Helper_Abstract
  48. {
  49. /**
  50. * @var Zend_View_Interface
  51. */
  52. public $view;
  53. /**
  54. * jQuery no Conflict Mode
  55. *
  56. * @see http://docs.jquery.com/Using_jQuery_with_Other_Libraries
  57. * @staticvar Boolean Status of noConflict Mode
  58. */
  59. private static $noConflictMode = false;
  60. /**
  61. * Initialize helper
  62. *
  63. * Retrieve container from registry or create new container and store in
  64. * registry.
  65. *
  66. * @return void
  67. */
  68. public function __construct()
  69. {
  70. $registry = Zend_Registry::getInstance();
  71. if (!isset($registry[__CLASS__])) {
  72. require_once 'ZendX/JQuery/View/Helper/JQuery/Container.php';
  73. $container = new ZendX_JQuery_View_Helper_JQuery_Container();
  74. $registry[__CLASS__] = $container;
  75. }
  76. $this->_container = $registry[__CLASS__];
  77. }
  78. /**
  79. * Return jQuery View Helper class, to execute jQuery library related functions.
  80. *
  81. * @return ZendX_JQuery_View_Helper_JQuery_Container
  82. */
  83. public function jQuery()
  84. {
  85. return $this->_container;
  86. }
  87. /**
  88. * Set view object
  89. *
  90. * @param Zend_View_Interface $view
  91. * @return void
  92. */
  93. public function setView(Zend_View_Interface $view)
  94. {
  95. $this->view = $view;
  96. $this->_container->setView($view);
  97. }
  98. /**
  99. * Proxy to container methods
  100. *
  101. * @param string $method
  102. * @param array $args
  103. * @return mixed
  104. * @throws Zend_View_Exception For invalid method calls
  105. */
  106. public function __call($method, $args)
  107. {
  108. if (!method_exists($this->_container, $method)) {
  109. require_once 'Zend/View/Exception.php';
  110. throw new Zend_View_Exception(sprintf('Invalid method "%s" called on jQuery view helper', $method));
  111. }
  112. return call_user_func_array(array($this->_container, $method), $args);
  113. }
  114. /**
  115. * Enable the jQuery internal noConflict Mode to work with
  116. * other Javascript libraries. Will setup jQuery in the variable
  117. * $j instead of $ to overcome conflicts.
  118. *
  119. * @link http://docs.jquery.com/Using_jQuery_with_Other_Libraries
  120. */
  121. public static function enableNoConflictMode()
  122. {
  123. self::$noConflictMode = true;
  124. }
  125. /**
  126. * Disable noConflict Mode of jQuery if this was previously enabled.
  127. *
  128. * @return void
  129. */
  130. public static function disableNoConflictMode()
  131. {
  132. self::$noConflictMode = false;
  133. }
  134. /**
  135. * Return current status of the jQuery no Conflict Mode
  136. *
  137. * @return Boolean
  138. */
  139. public static function getNoConflictMode()
  140. {
  141. return self::$noConflictMode;
  142. }
  143. /**
  144. * Return current jQuery handler based on noConflict mode settings.
  145. *
  146. * @return String
  147. */
  148. public static function getJQueryHandler()
  149. {
  150. return ((self::getNoConflictMode()==true)?'$j':'$');
  151. }
  152. }