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(); this.onNewRoute();
app.drawer.hide();
app.modal.close();
/** /**
* A class name to apply to the body while the route is active. * A class name to apply to the body while the route is active.
* *
* @type {String} * @type {String}
*/ */
this.bodyClass = ''; 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. * adjust the current route name.
*/ */
onNewRoute() { onNewRoute() {
this.currentPath = m.route.get();
app.previous = app.current; app.previous = app.current;
app.current = new PageState(this.constructor, { routeName: this.attrs.routeName }); 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) { oncreate(vnode) {

View File

@@ -32,23 +32,9 @@ export default class DiscussionPage extends Page {
*/ */
this.near = m.route.param('near') || 0; 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'); app.history.push('discussion');
this.bodyClass = 'App--discussion'; this.bodyClass = 'App--discussion';
this.prevRoute = m.route.get();
} }
onremove() { onremove() {
@@ -95,30 +81,32 @@ export default class DiscussionPage extends Page {
); );
} }
onbeforeupdate(vnode) { onNewRoute() {
super.onbeforeupdate(vnode); // 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) { if (near !== String(this.near)) {
this.onNewRoute(); this.stream.goToNumber(near);
this.prevRoute = m.route.get(); }
// If we have routed to the same discussion as we were viewing previously, this.near = near;
// cancel the unloading of this controller and instead prompt the post } else {
// stream to jump to the new 'near' param. super.onNewRoute();
if (this.discussion) {
const idParam = m.route.param('id');
if (idParam && idParam.split('-')[0] === this.discussion.id()) { this.discussion = null;
const near = m.route.param('near') || '1';
if (near !== String(this.near)) { this.load();
this.stream.goToNumber(near);
}
this.near = near; // If the discussion list has been loaded, then we'll enable the pane (and
} else { // hide it by default).
this.oninit(vnode); 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'); 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 // If the user is coming from the discussion list, then they have either
// just switched one of the parameters (filter, sort, search) or they // just switched one of the parameters (filter, sort, search) or they
// probably want to refresh the results. We will clear the discussion list // 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.discussions.refreshParams(app.search.params());
app.history.push('index', app.translator.trans('core.forum.header.back_to_index_tooltip')); this.setTitle();
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();
}
} }
view() { view() {

View File

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