Created
November 13, 2014 22:04
-
-
Save Insolita/0cb7f5b5400cdadd3bb9 to your computer and use it in GitHub Desktop.
add Css and Js Exludes for Ajax Queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: Insolita | |
* Date: 14.11.14 | |
* Time: 4:42 | |
*/ | |
namespace common\components; | |
use yii\helpers\Html; | |
class MyView extends \yii\web\View{ | |
public $ajaxCssExclude=[]; | |
public $ajaxJsExclude=[]; | |
/** | |
* Renders a view in response to an AJAX request. | |
* | |
* This method is similar to [[render()]] except that it will surround the view being rendered | |
* with the calls of [[beginPage()]], [[head()]], [[beginBody()]], [[endBody()]] and [[endPage()]]. | |
* By doing so, the method is able to inject into the rendering result with JS/CSS scripts and files | |
* that are registered with the view. | |
* | |
* @param string $view the view name. Please refer to [[render()]] on how to specify this parameter. | |
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. | |
* @param object $context the context that the view should use for rendering the view. If null, | |
* existing [[context]] will be used. | |
* @return string the rendering result | |
* @see render() | |
*/ | |
public function renderAjax($view, $params = [], $context = null) | |
{ | |
$viewFile = $this->findViewFile($view, $context); | |
ob_start(); | |
ob_implicit_flush(false); | |
$this->beginPage(); | |
$this->head(); | |
$this->beginBody(); | |
echo $this->renderFile($viewFile, $params, $context); | |
$this->endBody(); | |
$this->endPage(true); | |
return ob_get_clean(); | |
} | |
/** | |
* Marks the ending of an HTML page. | |
* @param boolean $ajaxMode whether the view is rendering in AJAX mode. | |
* If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions | |
* will be rendered at the end of the view like normal scripts. | |
*/ | |
public function endPage($ajaxMode = false) | |
{ | |
$this->trigger(self::EVENT_END_PAGE); | |
$content = ob_get_clean(); | |
echo strtr($content, [ | |
self::PH_HEAD => $this->myrenderHeadHtml($ajaxMode), | |
self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(), | |
self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode), | |
]); | |
$this->clear(); | |
} | |
/** | |
* Renders the content to be inserted in the head section. | |
* The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files. | |
* @return string the rendered content | |
*/ | |
protected function myrenderHeadHtml($ajaxMode) | |
{ | |
$lines = []; | |
if (!empty($this->metaTags)) { | |
$lines[] = implode("\n", $this->metaTags); | |
} | |
if (!empty($this->linkTags)) { | |
$lines[] = implode("\n", $this->linkTags); | |
} | |
if (!empty($this->cssFiles)) { | |
if($ajaxMode && !empty($this->ajaxCssExclude)){ | |
foreach($this->cssFiles as $i=>$file){ | |
foreach($this->ajaxCssExclude as $filt){ | |
if(strpos($i,$filt)!==false){ | |
unset($this->cssFiles[$i]); | |
} | |
} | |
} | |
} | |
$lines[] = implode("\n", $this->cssFiles); | |
} | |
if (!empty($this->css)) { | |
$lines[] = implode("\n", $this->css); | |
} | |
if (!empty($this->jsFiles[self::POS_HEAD])) { | |
if($ajaxMode && !empty($this->ajaxJsExclude)){ | |
foreach($this->jsFiles[self::POS_HEAD] as $i=>$file){ | |
foreach($this->ajaxJsExclude as $filt){ | |
if(strpos($i,$filt)!==false){ | |
unset($this->jsFiles[self::POS_HEAD][$i]); | |
} | |
} | |
} | |
} | |
$lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]); | |
} | |
if (!empty($this->js[self::POS_HEAD])) { | |
$lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD]), ['type' => 'text/javascript']); | |
} | |
return empty($lines) ? '' : implode("\n", $lines); | |
} | |
/** | |
* Renders the content to be inserted at the end of the body section. | |
* The content is rendered using the registered JS code blocks and files. | |
* @param boolean $ajaxMode whether the view is rendering in AJAX mode. | |
* If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions | |
* will be rendered at the end of the view like normal scripts. | |
* @return string the rendered content | |
*/ | |
protected function renderBodyEndHtml($ajaxMode) | |
{ | |
$lines = []; | |
if (!empty($this->jsFiles[self::POS_END])) { | |
if($ajaxMode && !empty($this->ajaxJsExclude)){ | |
foreach($this->jsFiles[self::POS_END] as $i=>$file){ | |
foreach($this->ajaxJsExclude as $filt){ | |
if(strpos($i,$filt)!==false){ | |
unset($this->jsFiles[self::POS_END][$i]); | |
} | |
} | |
} | |
} | |
$lines[] = implode("\n", $this->jsFiles[self::POS_END]); | |
} | |
if ($ajaxMode) { | |
$scripts = []; | |
if (!empty($this->js[self::POS_END])) { | |
$scripts[] = implode("\n", $this->js[self::POS_END]); | |
} | |
if (!empty($this->js[self::POS_READY])) { | |
$scripts[] = implode("\n", $this->js[self::POS_READY]); | |
} | |
if (!empty($this->js[self::POS_LOAD])) { | |
$scripts[] = implode("\n", $this->js[self::POS_LOAD]); | |
} | |
if (!empty($scripts)) { | |
$lines[] = Html::script(implode("\n", $scripts), ['type' => 'text/javascript']); | |
} | |
} else { | |
if (!empty($this->js[self::POS_END])) { | |
$lines[] = Html::script(implode("\n", $this->js[self::POS_END]), ['type' => 'text/javascript']); | |
} | |
if (!empty($this->js[self::POS_READY])) { | |
$js = "jQuery(document).ready(function () {\n" . implode("\n", $this->js[self::POS_READY]) . "\n});"; | |
$lines[] = Html::script($js, ['type' => 'text/javascript']); | |
} | |
if (!empty($this->js[self::POS_LOAD])) { | |
$js = "jQuery(window).load(function () {\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});"; | |
$lines[] = Html::script($js, ['type' => 'text/javascript']); | |
} | |
} | |
return empty($lines) ? '' : implode("\n", $lines); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment