Skip to content

Commit

Permalink
Fixed #2966
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jun 7, 2018
1 parent b0e6963 commit f676e6a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 30 additions & 22 deletions src/web/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
// -------------------------------------------------------------------------

Expand Down Expand Up @@ -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 = <<<JS
if (typeof Craft !== 'undefined') {
jQuery.extend(Craft.registeredJsFiles, {$json});
}
JS;
$this->registerJs($js, self::POS_END);
}

if (!empty($this->_registeredAssetBundles)) {
$json = Json::encode($this->_registeredAssetBundles);
$js = <<<JS
if (typeof Craft !== 'undefined') {
jQuery.extend(Craft.registeredAssetBundles, {$json});
}
JS;
$this->registerJs($js, self::POS_END);
}
}

$html = parent::renderBodyEndHtml($ajaxMode);

return empty($lines) ? $html : implode("\n", $lines).$html;
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit f676e6a

Please sign in to comment.