Update composer constraint, remove pre-rendering, breaking changes for major release

This commit is contained in:
Aaron Carlino 2019-07-11 15:08:05 +12:00
parent f892c0450e
commit cf55be3f7b
3 changed files with 93 additions and 91 deletions

View File

@ -16,7 +16,7 @@
}
],
"require": {
"silverstripe/framework": ">= 4.2"
"silverstripe/framework": "^4.2"
},
"extra": {
"installer-name": "betternavigator",

View File

@ -16,45 +16,77 @@ use SilverStripe\Security\Security;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ArrayData;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\View\Requirements;
class BetterNavigatorExtension extends DataExtension
{
/**
* @var string
* @var bool|null
*/
private $navigatorHTML;
private $shouldDisplay = null;
/**
* Noop, pseudo backward compatability only
* Load requirements in before final render. When the next extension point is called, it's too late.
* @return void
*/
public function beforeCallActionHandler(): void
{
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
*/
public function BetterNavigator()
public function afterCallActionHandler($request, $action, $result): DBHTMLText
{
return DBField::create_field('HTMLText', '');
if (!$this->shouldDisplay()) {
return $result;
}
$html = $result->getValue();
$navigatorHTML = $this->generateNavigator()->getValue();
// Inject the NavigatorHTML before the closing </body> tag
$html = preg_replace(
'/(<\/body[^>]*>)/i',
$navigatorHTML . '\\1',
$html
);
$result->setValue($html);
return $result;
}
/**
* Override on a per-controller basis to add custom logic
* @return bool
*/
public function showBetterNavigator(): bool
{
return true;
}
/**
* Provides a front-end utility menu with administrative functions and developer tools
* Relies on SilverStripeNavigator
*
* @return DBHTMLText|false
* @return DBHTMLText
*/
protected function generateNavigator()
private function generateNavigator(): DBHTMLText
{
// Make sure this is a page
if (!$this->isAPage() || !$this->owner->showBetterNavigator()) {
return false;
}
// Only show navigator to appropriate users
$isDev = Director::isDev();
$canViewDraft = (Permission::check('VIEW_DRAFT_CONTENT') || Permission::check('CMS_ACCESS_CMSMain'));
if ($isDev || $canViewDraft) {
// Get SilverStripeNavigator links & stage info (CMS/Stage/Live/Archive)
$nav = [];
$viewing = '';
$navigator = SilverStripeNavigator::create($this->owner->dataRecord);
$isDev = Director::isDev();
$items = $navigator->getItems();
foreach ($items as $item) {
$name = $item->getName();
@ -97,62 +129,35 @@ class BetterNavigatorExtension extends DataExtension
// Merge with page data, send to template and render
$navigator = new ArrayData($bNData);
return $navigator->renderWith('BetterNavigator\\BetterNavigator');
}
return false;
return $this->owner->customise($navigator)->renderWith('BetterNavigator\\BetterNavigator');
}
/**
* Prerender HTML to ensure that <% require %> gets loaded
* @return void
*/
public function beforeCallActionHandler(): void
{
$navigator = $this->generateNavigator();
if ($navigator) {
$this->navigatorHTML = $navigator->getValue();
}
}
/**
* @param $request
* @param $action
* @param DBHTMLText $result
* @return DBHTMLText
*/
public function afterCallActionHandler($request, $action, $result): DBHTMLText
{
if (!$this->navigatorHTML) {
return $result;
}
$html = $result->getValue();
// Inject the NavigatorHTML before the closing </body> tag
$html = preg_replace(
'/(<\/body[^>]*>)/i',
$this->navigatorHTML . '\\1',
$html
);
$result->setValue($html);
return $result;
}
/**
* Override on a per-controller basis to add custom logic
* Internally compute and cache weather the navigator should display
* @return bool
*/
public function showBetterNavigator(): bool
private function shouldDisplay(): bool
{
return true;
if ($this->shouldDisplay !== null) {
return $this->shouldDisplay;
}
// Make sure this is a page
if (!$this->isAPage() || !$this->owner->showBetterNavigator()) {
return $this->shouldDisplay = false;
}
// Only show navigator to appropriate users
$isDev = Director::isDev();
$canViewDraft = (Permission::check('VIEW_DRAFT_CONTENT') || Permission::check('CMS_ACCESS_CMSMain'));
return $this->shouldDisplay = ($isDev || $canViewDraft);
}
/**
* @return boolean
*/
protected function isAPage()
private function isAPage(): bool
{
return $this->owner
&& $this->owner->dataRecord

View File

@ -1,6 +1,3 @@
<% require javascript("jonom/silverstripe-betternavigator: javascript/betternavigator.js") %>
<% require css("jonom/silverstripe-betternavigator: css/betternavigator.css") %>
<div id="BetterNavigator" class="collapsed">
<div id="BetterNavigatorStatus" class="$Viewing">