From f676e6a96660f3d2b8175e7649687021b5085e7f Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Wed, 6 Jun 2018 23:09:17 -0500 Subject: [PATCH] Fixed #2966 --- CHANGELOG-v3.md | 3 +++ src/web/View.php | 52 ++++++++++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index 62ec324a1f8..6f376f15e20 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -5,6 +5,9 @@ ### Changed - Improved the output of `craft\helpers\DateTimeHelper::humanDurationFromInterval()`. +### Fixed +- Fixed JavaScript errors that could occur in the Control Panel on pages with Ajax requests. ([#2966](https://github.com/craftcms/cms/issues/2966)) + ## 3.0.10.1 - 2018-06-06 ### Fixed diff --git a/src/web/View.php b/src/web/View.php index 71bfb9af91c..de2b8c83f0a 100644 --- a/src/web/View.php +++ b/src/web/View.php @@ -1255,6 +1255,19 @@ public function setRegisteredAssetBundles(array $names) $this->_registeredAssetBundles = array_flip($names); } + /** + * @inheritdoc + */ + public function endPage($ajaxMode = false) + { + if (!$ajaxMode && Craft::$app->getRequest()->getIsCpRequest()) { + $this->_registeredJs('registeredJsFiles', $this->_registeredJsFiles); + $this->_registeredJs('registeredAssetBundles', $this->_registeredAssetBundles); + } + + parent::endPage($ajaxMode); + } + // Events // ------------------------------------------------------------------------- @@ -1384,28 +1397,6 @@ protected function renderBodyEndHtml($ajaxMode) $lines[] = implode("\n", $this->_scripts[self::POS_END]); } - if (Craft::$app->getRequest()->getIsCpRequest()) { - if (!empty($this->_registeredJsFiles)) { - $json = Json::encode($this->_registeredJsFiles); - $js = <<registerJs($js, self::POS_END); - } - - if (!empty($this->_registeredAssetBundles)) { - $json = Json::encode($this->_registeredAssetBundles); - $js = <<registerJs($js, self::POS_END); - } - } - $html = parent::renderBodyEndHtml($ajaxMode); return empty($lines) ? $html : implode("\n", $lines).$html; @@ -1600,6 +1591,23 @@ private function _createTextareaMarker(array $matches): string return $matches[1].$marker.$matches[3]; } + private function _registeredJs($property, $names) + { + if (empty($names)) { + return; + } + + $js = "if (typeof Craft !== 'undefined') {\n"; + foreach (array_keys($names) as $name) { + if ($name) { + $jsName = Json::encode($name); + $js .= " Craft.{$property}[{$jsName}] = true;\n"; + } + } + $js .= '}'; + $this->registerJs($js, self::POS_HEAD); + } + /** * Returns the HTML for an element in the CP. *