00001 <?php 00026 require_once "ZendX/JQuery.php"; 00027 00037 class ZendX_JQuery_View_Helper_JQuery_Container 00038 { 00044 protected $_jqueryLibraryPath = null; 00045 00051 protected $_javascriptSources = array(); 00052 00058 protected $_enabled = false; 00059 00065 protected $_captureLock = false; 00066 00072 protected $_javascriptStatements = array(); 00073 00079 protected $_stylesheets = array(); 00080 00086 protected $_onLoadActions = array(); 00087 00093 protected $_isXhtml = false; 00094 00100 protected $_version = ZendX_JQuery::DEFAULT_JQUERY_VERSION; 00101 00107 protected $_renderMode = ZendX_JQuery::RENDER_ALL; 00108 00114 protected $_uiEnabled = false; 00115 00122 protected $_uiPath = null; 00123 00129 protected $_uiVersion = ZendX_JQuery::DEFAULT_UI_VERSION; 00130 00136 protected $_loadSslCdnPath = false; 00137 00143 public $view = null; 00144 00151 public function setView(Zend_View_Interface $view) 00152 { 00153 $this->view = $view; 00154 } 00155 00161 public function enable() 00162 { 00163 $this->_enabled = true; 00164 return $this; 00165 } 00166 00172 public function disable() 00173 { 00174 $this->uiDisable(); 00175 $this->_enabled = false; 00176 return $this; 00177 } 00178 00184 public function isEnabled() 00185 { 00186 return $this->_enabled; 00187 } 00188 00195 public function setVersion($version) 00196 { 00197 $this->_version = $version; 00198 return $this; 00199 } 00200 00206 public function getVersion() 00207 { 00208 return $this->_version; 00209 } 00210 00219 public function setCdnVersion($version = null) 00220 { 00221 return $this->setVersion($version); 00222 } 00223 00230 public function getCdnVersion() 00231 { 00232 return $this->getVersion(); 00233 } 00234 00240 public function setCdnSsl($flag) 00241 { 00242 $this->_loadSslCdnPath = $flag; 00243 return $this; 00244 } 00245 00251 public function useCdn() 00252 { 00253 return !$this->useLocalPath(); 00254 } 00255 00262 public function setLocalPath($path) 00263 { 00264 $this->_jqueryLibraryPath = (string) $path; 00265 return $this; 00266 } 00267 00273 public function uiEnable() 00274 { 00275 $this->enable(); 00276 $this->_uiEnabled = true; 00277 return $this; 00278 } 00279 00285 public function uiDisable() 00286 { 00287 $this->_uiEnabled = false; 00288 return $this; 00289 } 00290 00296 public function uiIsEnabled() 00297 { 00298 return $this->_uiEnabled; 00299 } 00300 00307 public function setUiVersion($version) 00308 { 00309 $this->_uiVersion = $version; 00310 return $this; 00311 } 00312 00318 public function getUiVersion() 00319 { 00320 return $this->_uiVersion; 00321 } 00322 00330 public function setUiCdnVersion($version="1.5.2") 00331 { 00332 return $this->setUiVersion($version); 00333 } 00334 00341 public function getUiCdnVersion() 00342 { 00343 return $this->getUiVersion(); 00344 } 00345 00352 public function setUiLocalPath($path) 00353 { 00354 $this->_uiPath = (string) $path; 00355 return $this; 00356 } 00357 00363 public function getUiPath() 00364 { 00365 return $this->_uiPath; 00366 } 00367 00373 public function getUiLocalPath() 00374 { 00375 return $this->getUiPath(); 00376 } 00377 00383 public function useUiLocal() 00384 { 00385 return (null===$this->_uiPath ? false : true); 00386 } 00387 00393 public function useUiCdn() 00394 { 00395 return !$this->useUiLocal(); 00396 } 00397 00403 public function getLocalPath() 00404 { 00405 return $this->_jqueryLibraryPath; 00406 } 00407 00413 public function useLocalPath() 00414 { 00415 return (null === $this->_jqueryLibraryPath) ? false : true; 00416 } 00417 00423 public function onLoadCaptureStart() 00424 { 00425 if ($this->_captureLock) { 00426 require_once 'Zend/Exception.php'; 00427 throw new Zend_Exception('Cannot nest onLoad captures'); 00428 } 00429 00430 $this->_captureLock = true; 00431 return ob_start(); 00432 } 00433 00439 public function onLoadCaptureEnd() 00440 { 00441 $data = ob_get_clean(); 00442 $this->_captureLock = false; 00443 00444 $this->addOnLoad($data); 00445 return true; 00446 } 00447 00453 public function javascriptCaptureStart() 00454 { 00455 if ($this->_captureLock) { 00456 require_once 'Zend/Exception.php'; 00457 throw new Zend_Exception('Cannot nest captures'); 00458 } 00459 00460 $this->_captureLock = true; 00461 return ob_start(); 00462 } 00463 00469 public function javascriptCaptureEnd() 00470 { 00471 $data = ob_get_clean(); 00472 $this->_captureLock = false; 00473 00474 $this->addJavascript($data); 00475 return true; 00476 } 00477 00483 public function addJavascriptFile($path) 00484 { 00485 $path = (string) $path; 00486 if (!in_array($path, $this->_javascriptSources)) { 00487 $this->_javascriptSources[] = (string) $path; 00488 } 00489 return $this; 00490 } 00491 00500 public function getJavascriptFiles() 00501 { 00502 return $this->_javascriptSources; 00503 } 00504 00510 public function clearJavascriptFiles() 00511 { 00512 $this->_javascriptSources = array(); 00513 return $this; 00514 } 00515 00522 public function addJavascript($js) 00523 { 00524 $this->_javascriptStatements[] = $js; 00525 $this->enable(); 00526 return $this; 00527 } 00528 00534 public function getJavascript() 00535 { 00536 return $this->_javascriptStatements; 00537 } 00538 00544 public function clearJavascript() 00545 { 00546 $this->_javascriptStatements = array(); 00547 return $this; 00548 } 00549 00556 public function addStylesheet($path) 00557 { 00558 $path = (string) $path; 00559 if (!in_array($path, $this->_stylesheets)) { 00560 $this->_stylesheets[] = (string) $path; 00561 } 00562 return $this; 00563 } 00564 00570 public function getStylesheets() 00571 { 00572 return $this->_stylesheets; 00573 } 00574 00581 public function addOnLoad($callback) 00582 { 00583 if (!in_array($callback, $this->_onLoadActions, true)) { 00584 $this->_onLoadActions[] = $callback; 00585 } 00586 $this->enable(); 00587 return $this; 00588 } 00589 00595 public function getOnLoadActions() 00596 { 00597 return $this->_onLoadActions; 00598 } 00599 00605 public function clearOnLoadActions() 00606 { 00607 $this->_onLoadActions = array(); 00608 return $this; 00609 } 00610 00622 public function setRenderMode($mask) 00623 { 00624 $this->_renderMode = $mask; 00625 return $this; 00626 } 00627 00632 public function getRenderMode() 00633 { 00634 return $this->_renderMode; 00635 } 00636 00642 public function __toString() 00643 { 00644 if (!$this->isEnabled()) { 00645 return ''; 00646 } 00647 00648 $this->_isXhtml = $this->view->doctype()->isXhtml(); 00649 00650 $html = $this->_renderStylesheets() . PHP_EOL 00651 . $this->_renderScriptTags() . PHP_EOL 00652 . $this->_renderExtras(); 00653 return $html; 00654 } 00655 00661 protected function _renderStylesheets() 00662 { 00663 if( ($this->getRenderMode() & ZendX_JQuery::RENDER_STYLESHEETS) == 0) { 00664 return ''; 00665 } 00666 00667 foreach ($this->getStylesheets() as $stylesheet) { 00668 $stylesheets[] = $stylesheet; 00669 } 00670 00671 if (empty($stylesheets)) { 00672 return ''; 00673 } 00674 00675 array_reverse($stylesheets); 00676 $style = ""; 00677 foreach($stylesheets AS $stylesheet) { 00678 if ($this->view instanceof Zend_View_Abstract) { 00679 $closingBracket = ($this->view->doctype()->isXhtml()) ? ' />' : '>'; 00680 } else { 00681 $closingBracket = ' />'; 00682 } 00683 00684 $style .= '<link rel="stylesheet" href="'.$stylesheet.'" '. 00685 'type="text/css" media="screen"' . $closingBracket . PHP_EOL; 00686 } 00687 00688 return $style; 00689 } 00690 00696 protected function _renderScriptTags() 00697 { 00698 $scriptTags = ''; 00699 if( ($this->getRenderMode() & ZendX_JQuery::RENDER_LIBRARY) > 0) { 00700 $source = $this->_getJQueryLibraryPath(); 00701 00702 $scriptTags .= '<script type="text/javascript" src="' . $source . '"></script>'.PHP_EOL; 00703 00704 if($this->uiIsEnabled()) { 00705 $uiPath = $this->_getJQueryUiLibraryPath(); 00706 $scriptTags .= '<script type="text/javascript" src="'.$uiPath.'"></script>'.PHP_EOL; 00707 } 00708 00709 if(ZendX_JQuery_View_Helper_JQuery::getNoConflictMode() == true) { 00710 $scriptTags .= '<script type="text/javascript">var $j = jQuery.noConflict();</script>'.PHP_EOL; 00711 } 00712 } 00713 00714 if( ($this->getRenderMode() & ZendX_JQuery::RENDER_SOURCES) > 0) { 00715 foreach($this->getJavascriptFiles() AS $javascriptFile) { 00716 $scriptTags .= '<script type="text/javascript" src="' . $javascriptFile . '"></script>'.PHP_EOL; 00717 } 00718 } 00719 00720 return $scriptTags; 00721 } 00722 00728 protected function _renderExtras() 00729 { 00730 $onLoadActions = array(); 00731 if( ($this->getRenderMode() & ZendX_JQuery::RENDER_JQUERY_ON_LOAD) > 0) { 00732 foreach ($this->getOnLoadActions() as $callback) { 00733 $onLoadActions[] = $callback; 00734 } 00735 } 00736 00737 $javascript = ''; 00738 if( ($this->getRenderMode() & ZendX_JQuery::RENDER_JAVASCRIPT) > 0) { 00739 $javascript = implode("\n ", $this->getJavascript()); 00740 } 00741 00742 $content = ''; 00743 00744 if (!empty($onLoadActions)) { 00745 if(ZendX_JQuery_View_Helper_JQuery::getNoConflictMode() == true) { 00746 $content .= '$j(document).ready(function() {'."\n "; 00747 } else { 00748 $content .= '$(document).ready(function() {'."\n "; 00749 } 00750 $content .= implode("\n ", $onLoadActions) . "\n"; 00751 $content .= '});'."\n"; 00752 } 00753 00754 if (!empty($javascript)) { 00755 $content .= $javascript . "\n"; 00756 } 00757 00758 if (preg_match('/^\s*$/s', $content)) { 00759 return ''; 00760 } 00761 00762 $html = '<script type="text/javascript">' . PHP_EOL 00763 . (($this->_isXhtml) ? '//<![CDATA[' : '//<!--') . PHP_EOL 00764 . $content 00765 . (($this->_isXhtml) ? '//]]>' : '//-->') . PHP_EOL 00766 . PHP_EOL . '</script>'; 00767 return $html; 00768 } 00769 00773 protected function _getJQueryLibraryBaseCdnUri() 00774 { 00775 if($this->_loadSslCdnPath == true) { 00776 $baseUri = ZendX_JQuery::CDN_BASE_GOOGLE_SSL; 00777 } else { 00778 $baseUri = ZendX_JQuery::CDN_BASE_GOOGLE; 00779 } 00780 return $baseUri; 00781 } 00782 00786 protected function _getJQueryUiLibraryBaseCdnUri() 00787 { 00788 if($this->_loadSslCdnPath == true) { 00789 $baseUri = ZendX_JQuery::CDN_BASEUI_GOOGLE_SSL; 00790 } else { 00791 $baseUri = ZendX_JQuery::CDN_BASEUI_GOOGLE; 00792 } 00793 return $baseUri; 00794 } 00795 00801 protected function _getJQueryLibraryPath() 00802 { 00803 if($this->_jqueryLibraryPath != null) { 00804 $source = $this->_jqueryLibraryPath; 00805 } else { 00806 $baseUri = $this->_getJQueryLibraryBaseCdnUri(); 00807 $source = $baseUri . 00808 ZendX_JQuery::CDN_SUBFOLDER_JQUERY . 00809 $this->getCdnVersion() . 00810 ZendX_JQuery::CDN_JQUERY_PATH_GOOGLE; 00811 } 00812 00813 return $source; 00814 } 00815 00819 protected function _getJQueryUiLibraryPath() 00820 { 00821 if($this->useUiCdn()) { 00822 $baseUri = $this->_getJQueryLibraryBaseCdnUri(); 00823 $uiPath = $baseUri. 00824 ZendX_JQuery::CDN_SUBFOLDER_JQUERYUI . 00825 $this->getUiCdnVersion() . 00826 "/jquery-ui.min.js"; 00827 } else if($this->useUiLocal()) { 00828 $uiPath = $this->getUiPath(); 00829 } 00830 return $uiPath; 00831 } 00832 }