This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/routes/topic-from-params.js.es6
Robin Ward de6edf9e4e FIX: Back button would go to previous topic instead of list
See related topic:
https://meta.discourse.org/t/back-button-history-not-properly-working/122183

The issue here is the transition was not completing properly which meant
if you backed out of a topic quickly and entered a new one, hitting back
in the second topic would sometimes take you to the previous one instead
of back to the topic list.
2019-07-05 15:53:14 -04:00

99 lines
2.8 KiB
JavaScript

import DiscourseURL from "discourse/lib/url";
import Draft from "discourse/models/draft";
// This route is used for retrieving a topic based on params
export default Discourse.Route.extend({
// Avoid default model hook
model(params) {
return params;
},
deactivate() {
this._super(...arguments);
this.controllerFor("topic").unsubscribe();
},
setupController(controller, params) {
params = params || {};
params.track_visit = true;
const topic = this.modelFor("topic"),
postStream = topic.postStream,
topicController = this.controllerFor("topic"),
composerController = this.controllerFor("composer");
// I sincerely hope no topic gets this many posts
if (params.nearPost === "last") {
params.nearPost = 999999999;
}
params.forceLoad = true;
postStream
.refresh(params)
.then(() => {
// TODO we are seeing errors where closest post is null and this is exploding
// we need better handling and logging for this condition.
// there are no closestPost for hidden topics
if (topic.view_hidden) {
return;
}
// The post we requested might not exist. Let's find the closest post
const closestPost = postStream.closestPostForPostNumber(
params.nearPost || 1
);
const closest = closestPost.post_number;
topicController.setProperties({
"model.currentPost": closest,
enteredIndex: topic.postStream.progressIndexOfPost(closestPost),
enteredAt: new Date().getTime().toString()
});
topicController.subscribe();
// Highlight our post after the next render
Ember.run.scheduleOnce("afterRender", () =>
this.appEvents.trigger("post:highlight", closest)
);
const opts = {};
if (document.location.hash && document.location.hash.length) {
opts.anchor = document.location.hash;
}
DiscourseURL.jumpToPost(closest, opts);
if (!Ember.isEmpty(topic.draft)) {
composerController.open({
draft: Draft.getLocal(topic.draft_key, topic.draft),
draftKey: topic.draft_key,
draftSequence: topic.draft_sequence,
ignoreIfChanged: true,
topic
});
}
})
.catch(e => {
if (!Ember.testing) {
// eslint-disable-next-line no-console
console.log("Could not view topic", e);
}
});
},
actions: {
willTransition() {
this.controllerFor("topic").set(
"previousURL",
document.location.pathname
);
// NOTE: omitting this return can break the back button when transitioning quickly between
// topics and the latest page.
return true;
}
}
});