JQuery.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 Zend_Json
  24. */
  25. require_once "Zend/Json.php";
  26. /**
  27. * jQuery Global Class holding constants and static convienience methods.
  28. *
  29. * @todo Offer convenience methods to add a tab or accordion container/pane combination.
  30. * @package ZendX_JQuery
  31. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class ZendX_JQuery
  35. {
  36. /**
  37. * Current default supported jQuery library version with ZendX_JQuery
  38. *
  39. * @const string
  40. */
  41. const DEFAULT_JQUERY_VERSION = "1.3.2";
  42. /**
  43. * Currently supported jQuery UI library version with ZendX_JQuery
  44. *
  45. * @const string
  46. */
  47. const DEFAULT_UI_VERSION = "1.7.1";
  48. /**
  49. * @see http://code.google.com/apis/ajaxlibs/documentation/index.html#jquery
  50. * @const string Base path to CDN
  51. */
  52. const CDN_BASE_GOOGLE = 'http://ajax.googleapis.com/ajax/libs/';
  53. /**
  54. * @see http://code.google.com/apis/ajaxlibs/documentation/index.html#jquery
  55. * @const string Base path to CDN
  56. */
  57. const CDN_BASE_GOOGLE_SSL = 'https://ajax.googleapis.com/ajax/libs/';
  58. /**
  59. * @const string
  60. */
  61. const CDN_SUBFOLDER_JQUERY = 'jquery/';
  62. /**
  63. * @const string
  64. */
  65. const CDN_SUBFOLDER_JQUERYUI = 'jqueryui/';
  66. /**
  67. * Always uses compressed version, because this is assumed to be the use case
  68. * in production enviroment. An uncompressed version has to included manually.
  69. *
  70. * @see http://code.google.com/apis/ajaxlibs/documentation/index.html#jquery
  71. * @const string File path after base and version
  72. */
  73. const CDN_JQUERY_PATH_GOOGLE = '/jquery.min.js';
  74. /**
  75. * Which parts of the the jQuery library should be rendered on echo'ing
  76. * the jQuery library to the View. The constants act as bit-mask. This
  77. * way the jQuery autogenerated code can be refactored based on personal needs.
  78. *
  79. * @see ZendX_JQuery_Helper_JQuery::setRenderMode
  80. * @const Integer
  81. */
  82. const RENDER_LIBRARY = 1;
  83. const RENDER_SOURCES = 2;
  84. const RENDER_STYLESHEETS = 4;
  85. const RENDER_JAVASCRIPT = 8;
  86. const RENDER_JQUERY_ON_LOAD = 16;
  87. const RENDER_ALL = 255;
  88. /**
  89. * jQuery-enable a view instance
  90. *
  91. * @param Zend_View_Interface $view
  92. * @return void
  93. */
  94. public static function enableView(Zend_View_Interface $view)
  95. {
  96. if (false === $view->getPluginLoader('helper')->getPaths('ZendX_JQuery_View_Helper')) {
  97. $view->addHelperPath('ZendX/JQuery/View/Helper', 'ZendX_JQuery_View_Helper');
  98. }
  99. }
  100. /**
  101. * jQuery-enable a form instance
  102. *
  103. * @param Zend_Form $form
  104. * @return void
  105. */
  106. public static function enableForm(Zend_Form $form)
  107. {
  108. $form->addPrefixPath('ZendX_JQuery_Form_Decorator', 'ZendX/JQuery/Form/Decorator', 'decorator')
  109. ->addPrefixPath('ZendX_JQuery_Form_Element', 'ZendX/JQuery/Form/Element', 'element')
  110. ->addElementPrefixPath('ZendX_JQuery_Form_Decorator', 'ZendX/JQuery/Form/Decorator', 'decorator')
  111. ->addDisplayGroupPrefixPath('ZendX_JQuery_Form_Decorator', 'ZendX/JQuery/Form/Decorator');
  112. foreach ($form->getSubForms() as $subForm) {
  113. self::enableForm($subForm);
  114. }
  115. if (null !== ($view = $form->getView())) {
  116. self::enableView($view);
  117. }
  118. }
  119. /**
  120. * Encode Json that may include javascript expressions.
  121. *
  122. * Take care of using the Zend_Json_Encoder to alleviate problems with the json_encode
  123. * magic key mechanism as of now.
  124. *
  125. * @see Zend_Json::encode
  126. * @param mixed $value
  127. * @return mixed
  128. */
  129. public static function encodeJson($value)
  130. {
  131. if (is_array($value) && count($value) == 0) {
  132. return '{}';
  133. }
  134. if(!class_exists('Zend_Json')) {
  135. /**
  136. * @see Zend_Json
  137. */
  138. require_once "Zend/Json.php";
  139. }
  140. return Zend_Json::encode($value, false, array('enableJsonExprFinder' => true));
  141. }
  142. }