Skip to content

Commit

Permalink
API Remove routing from silverstripe-component
Browse files Browse the repository at this point in the history
BUG Fix page routing in subdirectory
BUG Fix top level admin sections not causing ajax load
  • Loading branch information
Damian Mooyman authored and chillu committed Apr 20, 2016
1 parent 869f438 commit dbd17bd
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 207 deletions.
11 changes: 3 additions & 8 deletions admin/client/dist/js/LeftAndMain.js
Expand Up @@ -180,17 +180,12 @@
var self = this,
basePath = getUrlPath($('base')[0].href);

if (basePath[basePath.length - 1] === '/') {
basePath += 'admin';
} else {
basePath = '/admin';
}

basePath = basePath.replace(/\/$/, '');
_router2.default.base(basePath);

_config2.default.getTopLevelRoutes().forEach(function (route) {
(0, _router2.default)('/' + route + '/*', function (ctx, next) {
if (document.readyState !== 'complete' || typeof ctx.state.__forceReferer === 'undefined') {
(0, _router2.default)('/' + route + '*', function (ctx, next) {
if (document.readyState !== 'complete') {
return next();
}

Expand Down
4 changes: 2 additions & 2 deletions admin/client/dist/js/bundle-framework.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions admin/client/dist/js/bundle-legacy.js

Large diffs are not rendered by default.

58 changes: 29 additions & 29 deletions admin/client/dist/js/bundle-lib.js

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions admin/client/dist/js/config.js
Expand Up @@ -58,10 +58,17 @@

Object.keys(window.ss.config.sections).forEach(function (key) {
var route = window.ss.config.sections[key].route;
var isTopLevelRoute = route.indexOf('/') === -1;
var isUnique = topLevelRoutes.indexOf(route) === -1;

if (isTopLevelRoute && isUnique) {
var topLevelMatch = route.match(/^admin\/[^\/]+(\/?)$/);
if (!topLevelMatch) {
return;
}
if (!topLevelMatch[1]) {
route += '/';
}

var isUnique = topLevelRoutes.indexOf(route) === -1;
if (isUnique) {
topLevelRoutes.push(route);
}
});
Expand Down
52 changes: 3 additions & 49 deletions admin/client/dist/js/silverstripe-component.js
Expand Up @@ -78,32 +78,10 @@
var SilverStripeComponent = function (_Component) {
_inherits(SilverStripeComponent, _Component);

function SilverStripeComponent(props) {
function SilverStripeComponent() {
_classCallCheck(this, SilverStripeComponent);

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(SilverStripeComponent).call(this, props));

if (typeof _this.props.route !== 'undefined') {
_this._render = _this.render;

_this.render = function () {
var component = null;

if (_this.isComponentRoute()) {
component = _this._render();
}

return component;
};

window.ss.router(_this.props.route, function (ctx, next) {
_this.handleEnterRoute(ctx, next);
});
window.ss.router.exit(_this.props.route, function (ctx, next) {
_this.handleExitRoute(ctx, next);
});
}
return _this;
return _possibleConstructorReturn(this, Object.getPrototypeOf(SilverStripeComponent).apply(this, arguments));
}

_createClass(SilverStripeComponent, [{
Expand All @@ -130,29 +108,6 @@
}
}
}
}, {
key: 'handleEnterRoute',
value: function handleEnterRoute(ctx, next) {
next();
}
}, {
key: 'handleExitRoute',
value: function handleExitRoute(ctx, next) {
next();
}
}, {
key: 'isComponentRoute',
value: function isComponentRoute() {
var params = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

if (typeof this.props.route === 'undefined') {
return true;
}

var route = new window.ss.router.Route(this.props.route);

return route.match(window.ss.router.current, params);
}
}, {
key: 'emitCmsEvent',
value: function emitCmsEvent(componentEvent, data) {
Expand All @@ -164,8 +119,7 @@
}(_react.Component);

SilverStripeComponent.propTypes = {
cmsEvents: _react2.default.PropTypes.object,
route: _react2.default.PropTypes.string
cmsEvents: _react2.default.PropTypes.object
};

exports.default = SilverStripeComponent;
Expand Down
17 changes: 13 additions & 4 deletions admin/client/src/config.js
Expand Up @@ -26,11 +26,20 @@ class Config {
const topLevelRoutes = [];

Object.keys(window.ss.config.sections).forEach((key) => {
const route = window.ss.config.sections[key].route;
const isTopLevelRoute = route.indexOf('/') === -1;
const isUnique = topLevelRoutes.indexOf(route) === -1;
let route = window.ss.config.sections[key].route;

if (isTopLevelRoute && isUnique) {
// Check if this is a top level route, which ideally hasa trailing slash
const topLevelMatch = route.match(/^admin\/[^\/]+(\/?)$/);
if (!topLevelMatch) {
return;
}
if (!topLevelMatch[1]) {
route += '/';
}

// Check uniqueness and save
const isUnique = topLevelRoutes.indexOf(route) === -1;
if (isUnique) {
topLevelRoutes.push(route);
}
});
Expand Down
17 changes: 6 additions & 11 deletions admin/client/src/legacy/LeftAndMain.js
Expand Up @@ -227,21 +227,16 @@ $.entwine('ss', function($) {
var self = this,
basePath = getUrlPath($('base')[0].href);

// Avoid adding a double slash if the base path is '/'
if (basePath[basePath.length - 1] === '/') {
basePath += 'admin';
} else {
basePath = '/admin';
}

// Remove trailing / from base
basePath = basePath.replace(/\/$/, '');
router.base(basePath);

// Register all top level routes.
Config.getTopLevelRoutes().forEach((route) => {
router(`/${route}/*`, (ctx, next) => {
// If the page isn't ready or the request hasn't come from 'loadPanel'
// then don't PJAX load the panel. Note: __forceReferer is set by 'loadPanel' only.
if (document.readyState !== 'complete' || typeof ctx.state.__forceReferer === 'undefined') {
router(`/${route}*`, (ctx, next) => {

// If the page isn't ready.
if (document.readyState !== 'complete') {
return next();
}

Expand Down
40 changes: 32 additions & 8 deletions admin/client/src/sections/campaign-admin/controller.js
Expand Up @@ -28,8 +28,32 @@ class CampaignAdminContainer extends SilverStripeComponent {
}

componentDidMount() {
window.ss.router(`/${this.props.sectionConfig.campaignViewRoute}`, (ctx) => {
this.props.actions.showCampaignView(ctx.params.id, ctx.params.view);
super.componentDidMount();
// While a component is mounted it will intercept all routes and handle internally
let captureRoute = true;
const route = window.ss.router.resolveURLToBase(this.props.sectionConfig.campaignViewRoute);

// Capture routing within this section
window.ss.router(route, (ctx, next) => {
if (captureRoute) {
// If this component is mounted, then handle all page changes via
// state / redux
this.props.actions.showCampaignView(ctx.params.id, ctx.params.view);
} else {
// If component is not mounted, we need to allow root routes to load
// this section in via ajax
next();
}
});

// When leaving this section to go to another top level section then
// disable route capturing.
window.ss.router.exit(route, (ctx, next) => {
const applies = window.ss.router.routeAppliesToCurrentLocation(route);
if (!applies) {
captureRoute = false;
}
next();
});
}

Expand Down Expand Up @@ -67,17 +91,17 @@ class CampaignAdminContainer extends SilverStripeComponent {
<div className="cms-middle__scrollable">
<div className="content-toolbar">
<div className="btn-toolbar">
<FormAction
label={i18n._t('Campaigns.ADDCAMPAIGN')}
<FormAction
label={i18n._t('Campaigns.ADDCAMPAIGN')}
icon={'plus'}
handleClick={this.addCampaign}
/>
handleClick={this.addCampaign}
/>
</div>
</div>
<FormBuilder schemaUrl={schemaUrl} createFn={this.createFn} />
</div>
<FormBuilder schemaUrl={schemaUrl} createFn={this.createFn} />
</div>
</div>
</div>
);
}

Expand Down
55 changes: 0 additions & 55 deletions admin/client/src/silverstripe-component.js
Expand Up @@ -7,36 +7,6 @@ import $ from '../../../client/src/jQuery';

class SilverStripeComponent extends Component {

constructor(props) {
super(props);

// Setup component routing.
if (typeof this.props.route !== 'undefined') {
// The component's render method gets switched based on the current path.
// If the current path matches the component's route, the component is displayed.
// Otherwise the component's render method returns null, resulting in
// the component not rendering.
this._render = this.render;

this.render = () => {
let component = null;

if (this.isComponentRoute()) {
component = this._render();
}

return component;
};

window.ss.router(this.props.route, (ctx, next) => {
this.handleEnterRoute(ctx, next);
});
window.ss.router.exit(this.props.route, (ctx, next) => {
this.handleExitRoute(ctx, next);
});
}
}

componentDidMount() {
if (typeof this.props.cmsEvents === 'undefined') {
return;
Expand Down Expand Up @@ -64,30 +34,6 @@ class SilverStripeComponent extends Component {
}
}

handleEnterRoute(ctx, next) {
next();
}

handleExitRoute(ctx, next) {
next();
}

/**
* Checks if the component should be rended on the current path.
*
* @param object [params] - If a params object is passed in it's
* mutated by page.js to contains route parans like ':id'.
*/
isComponentRoute(params = {}) {
if (typeof this.props.route === 'undefined') {
return true;
}

const route = new window.ss.router.Route(this.props.route);

return route.match(window.ss.router.current, params);
}

/**
* Notifies legacy-land something has changed within our component.
*
Expand All @@ -102,7 +48,6 @@ class SilverStripeComponent extends Component {

SilverStripeComponent.propTypes = {
cmsEvents: React.PropTypes.object,
route: React.PropTypes.string,
};

export default SilverStripeComponent;
2 changes: 1 addition & 1 deletion admin/code/AdminRootController.php
Expand Up @@ -71,7 +71,7 @@ public function handleRequest(SS_HTTPRequest $request, DataModel $model) {
$base = $this->config()->url_base;
$segment = Config::inst()->get($this->config()->default_panel, 'url_segment');

$this->redirect(Controller::join_links($base, $segment));
$this->redirect(Controller::join_links($base, $segment, '/'));
return $this->getResponse();
}

Expand Down
8 changes: 3 additions & 5 deletions admin/code/CampaignAdmin.php
Expand Up @@ -53,19 +53,17 @@ class CampaignAdmin extends LeftAndMain implements PermissionProvider {
private static $thumbnail_height = 64;

public function getClientConfig() {
$urlSegment = Config::inst()->get($this->class, 'url_segment');

return array_merge(parent::getClientConfig(), [
'forms' => [
// TODO Use schemaUrl instead
'editForm' => [
'schemaUrl' => $this->Link('schema/EditForm')
]
],
'campaignViewRoute' => $urlSegment . '/:type?/:id?/:view?',
'itemListViewEndpoint' => $this->Link('set/:id/show'),
'campaignViewRoute' => $this->Link() . ':type?/:id?/:view?',
'itemListViewEndpoint' => $this->Link() . 'set/:id/show',
'publishEndpoint' => [
'url' => $this->Link('set/:id/publish'),
'url' => $this->Link() . 'set/:id/publish',
'method' => 'post'
]
]);
Expand Down
2 changes: 1 addition & 1 deletion admin/code/LeftAndMain.php
Expand Up @@ -210,7 +210,7 @@ public function getCombinedClientConfig() {
*/
public function getClientConfig() {
return [
'route' => Config::inst()->get($this->class, 'url_segment')
'route' => $this->Link()
];
}

Expand Down

0 comments on commit dbd17bd

Please sign in to comment.