1
0
mirror of https://github.com/flarum/core.git synced 2025-08-13 20:04:24 +02:00

Compare commits

...

3 Commits

Author SHA1 Message Date
Alexander Skvortsov
d17cc1beee Only automatically load page for UserPage if username param is present
This avoid issues with SettingsPage
2020-10-04 22:02:16 -04:00
Alexander Skvortsov
759eb80ff9 Remove redundant code from IndexPage oninit 2020-10-04 22:00:11 -04:00
Alexander Skvortsov
46cb32046d Abstract away onNewRoute and routeHasChanged
This provides a cleaner interface for dealing with pages that handle multiple routes. We also move `modal` and `drawer` close calls to ensure those happen on route change with the same component.
2020-10-04 21:49:51 -04:00
4 changed files with 53 additions and 68 deletions

View File

@@ -12,15 +12,18 @@ export default class Page extends Component {
this.onNewRoute();
app.drawer.hide();
app.modal.close();
/**
* A class name to apply to the body while the route is active.
*
* @type {String}
*/
this.bodyClass = '';
this.currentPath = m.route.get();
}
routeHasChanged() {
return this.currentPath !== m.route.get();
}
/**
@@ -30,8 +33,21 @@ export default class Page extends Component {
* adjust the current route name.
*/
onNewRoute() {
this.currentPath = m.route.get();
app.previous = app.current;
app.current = new PageState(this.constructor, { routeName: this.attrs.routeName });
app.drawer.hide();
app.modal.close();
}
onbeforeupdate(vnode, old) {
super.onbeforeupdate(vnode, old);
if (this.routeHasChanged()) {
this.onNewRoute();
}
}
oncreate(vnode) {

View File

@@ -32,23 +32,9 @@ export default class DiscussionPage extends Page {
*/
this.near = m.route.param('near') || 0;
this.load();
// If the discussion list has been loaded, then we'll enable the pane (and
// hide it by default). Also, if we've just come from another discussion
// page, then we don't want Mithril to redraw the whole page if it did,
// then the pane would redraw which would be slow and would cause problems with
// event handlers.
if (app.discussions.hasDiscussions()) {
app.pane.enable();
app.pane.hide();
}
app.history.push('discussion');
this.bodyClass = 'App--discussion';
this.prevRoute = m.route.get();
}
onremove() {
@@ -95,30 +81,32 @@ export default class DiscussionPage extends Page {
);
}
onbeforeupdate(vnode) {
super.onbeforeupdate(vnode);
onNewRoute() {
// If we have routed to the same discussion as we were viewing previously,
// prompt the post stream to jump to the new 'near' param.
// Otherwise, load in a new discussion. The `else` branch will
// be followed when `onNewRoute` is called from `oninit`.
const idParam = m.route.param('id');
if (this.discussion && idParam && idParam.split('-')[0] === this.discussion.id()) {
const near = m.route.param('near') || '1';
if (m.route.get() !== this.prevRoute) {
this.onNewRoute();
this.prevRoute = m.route.get();
if (near !== String(this.near)) {
this.stream.goToNumber(near);
}
// If we have routed to the same discussion as we were viewing previously,
// cancel the unloading of this controller and instead prompt the post
// stream to jump to the new 'near' param.
if (this.discussion) {
const idParam = m.route.param('id');
this.near = near;
} else {
super.onNewRoute();
if (idParam && idParam.split('-')[0] === this.discussion.id()) {
const near = m.route.param('near') || '1';
this.discussion = null;
if (near !== String(this.near)) {
this.stream.goToNumber(near);
}
this.load();
this.near = near;
} else {
this.oninit(vnode);
}
// If the discussion list has been loaded, then we'll enable the pane (and
// hide it by default).
if (app.discussions.hasDiscussions()) {
app.pane.enable();
app.pane.hide();
}
}
}

View File

@@ -29,6 +29,14 @@ export default class IndexPage extends Page {
this.lastDiscussion = app.previous.get('discussion');
}
app.history.push('index', app.translator.trans('core.forum.header.back_to_index_tooltip'));
this.bodyClass = 'App--index';
}
onNewRoute() {
super.onNewRoute();
// If the user is coming from the discussion list, then they have either
// just switched one of the parameters (filter, sort, search) or they
// probably want to refresh the results. We will clear the discussion list
@@ -39,29 +47,7 @@ export default class IndexPage extends Page {
app.discussions.refreshParams(app.search.params());
app.history.push('index', app.translator.trans('core.forum.header.back_to_index_tooltip'));
this.bodyClass = 'App--index';
this.currentPath = m.route.get();
}
onbeforeupdate(vnode) {
super.onbeforeupdate(vnode);
const curPath = m.route.get();
if (this.currentPath !== curPath) {
this.onNewRoute();
app.discussions.clear();
app.discussions.refreshParams(app.search.params());
this.currentPath = curPath;
this.setTitle();
}
this.setTitle();
}
view() {

View File

@@ -27,18 +27,13 @@ export default class UserPage extends Page {
this.user = null;
this.bodyClass = 'App--user';
this.prevUsername = m.route.param('username');
}
onbeforeupdate() {
const currUsername = m.route.param('username');
if (currUsername !== this.prevUsername) {
this.onNewRoute();
onNewRoute() {
super.onNewRoute();
this.prevUsername = currUsername;
this.loadUser(currUsername);
if (m.route.param('username')) {
this.loadUser(m.route.param('username'));
}
}