Skip to content

Commit

Permalink
Fix conditional rendering
Browse files Browse the repository at this point in the history
Ensure scripts and css are only loaded with navigator markup is present, and support HTTPResponse as input. Fixes #46, fixes #47
  • Loading branch information
jonom committed Aug 7, 2020
1 parent d037dc8 commit fa68738
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
40 changes: 18 additions & 22 deletions src/Extension/BetterNavigatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\Security\LogoutForm;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
Expand All @@ -26,35 +27,23 @@ class BetterNavigatorExtension extends DataExtension
*/
private $shouldDisplay = null;

/**
* Load requirements in before final render. When the next extension point is called, it's too late.
* @return void
*/
public function beforeCallActionHandler()
{
if ($this->shouldDisplay()) {
Requirements::javascript('jonom/silverstripe-betternavigator: javascript/betternavigator.js');
Requirements::css('jonom/silverstripe-betternavigator: css/betternavigator.css');
}
}

/**
* @param $request
* @param $action
* @param DBHTMLText $result
* @return DBHTMLText
* @param DBHTMLText|HTTPResponse $result
* @return DBHTMLText|HTTPResponse
*/
public function afterCallActionHandler($request, $action, $result)
{
if (!$this->shouldDisplay()) {
return $result;
}
// Check that we're dealing with HTML
$isHtmlResponse = $result instanceof DBHTMLText ||
$result instanceof HTTPResponse && strpos($result->getHeader('content-type'), 'text/html') !== false;

if (!($result instanceof DBHTMLText)) {
if (!$isHtmlResponse || !$this->shouldDisplay()) {
return $result;
}

$html = $result->getValue();
$html = $result instanceof DBHTMLText ? $result->getValue() : $result->getBody();
$navigatorHTML = $this->generateNavigator()->getValue();

// Inject the NavigatorHTML before the closing </body> tag
Expand All @@ -63,7 +52,11 @@ public function afterCallActionHandler($request, $action, $result)
$navigatorHTML . '\\1',
$html
);
$result->setValue($html);
if ($result instanceof DBHTMLText) {
$result->setValue($html);
} else {
$result->setBody($html);
}

return $result;
}
Expand Down Expand Up @@ -120,6 +113,7 @@ private function generateNavigator()
$backURL = '?BackURL=' . urlencode($this->owner->Link());
$logoutForm = LogoutForm::create($this->owner)->setName('BetterNavigatorLogoutForm');
$logoutForm->Fields()->fieldByName('BackURL')->setValue($this->owner->Link());
$bnModule = ModuleLoader::getModule('jonom/silverstripe-betternavigator');
$bNData = array_merge($nav, [
'Member' => $member,
'Stage' => Versioned::get_stage(),
Expand All @@ -129,7 +123,9 @@ private function generateNavigator()
'LogoutForm' => $logoutForm,
'EditLink' => $editLink,
'Mode' => Director::get_environment_type(),
'IsDeveloper' => $isDeveloper
'IsDeveloper' => $isDeveloper,
'ScriptUrl' => $bnModule->getResource('javascript/betternavigator.js')->getURL(),
'CssUrl' => $bnModule->getResource('css/betternavigator.css')->getURL(),
]);

// Merge with page data, send to template and render
Expand Down
3 changes: 3 additions & 0 deletions templates/BetterNavigator/BetterNavigator.ss
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,6 @@
</div>

</div>

<script type="application/javascript" src="$ScriptUrl"></script>
<link rel="stylesheet" href="$CssUrl" />

0 comments on commit fa68738

Please sign in to comment.