client/posts: strip junk from post permalinks

Thanks for inspiration, StackExchange
This commit is contained in:
rr- 2016-07-07 22:13:58 +02:00
parent 5ac5eb5503
commit 8901658c17
2 changed files with 18 additions and 4 deletions

View file

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const router = require('../router.js');
const api = require('../api.js'); const api = require('../api.js');
const misc = require('../util/misc.js'); const misc = require('../util/misc.js');
const settings = require('../models/settings.js'); const settings = require('../models/settings.js');
@ -11,9 +12,10 @@ const PostView = require('../views/post_view.js');
const EmptyView = require('../views/empty_view.js'); const EmptyView = require('../views/empty_view.js');
class PostController { class PostController {
constructor(id, editMode, parameters) { constructor(id, editMode, ctx) {
topNavigation.activate('posts'); topNavigation.activate('posts');
let parameters = ctx.parameters;
Promise.all([ Promise.all([
Post.get(id), Post.get(id),
PostList.getAround( PostList.getAround(
@ -21,6 +23,14 @@ class PostController {
parameters ? parameters.query : '')), parameters ? parameters.query : '')),
]).then(responses => { ]).then(responses => {
const [post, aroundResponse] = responses; const [post, aroundResponse] = responses;
// remove junk from query, but save it into history so that it can
// be still accessed after history navigation / page refresh
if (parameters.query) {
ctx.state.parameters = parameters;
router.replace('/post/' + id, ctx.state, false);
}
this._post = post; this._post = post;
this._view = new PostView({ this._view = new PostView({
post: post, post: post,
@ -159,7 +169,10 @@ module.exports = router => {
'/post/:id/:parameters?', '/post/:id/:parameters?',
(ctx, next) => { misc.parseUrlParametersRoute(ctx, next); }, (ctx, next) => { misc.parseUrlParametersRoute(ctx, next); },
(ctx, next) => { (ctx, next) => {
ctx.controller = new PostController( // restore parameters from history state
ctx.parameters.id, false, ctx.parameters); if (ctx.state.parameters) {
Object.assign(ctx.parameters, ctx.state.parameters);
}
ctx.controller = new PostController(ctx.parameters.id, false, ctx);
}); });
}; };

View file

@ -5,6 +5,7 @@
// - refactored to classes // - refactored to classes
// - simplified method chains // - simplified method chains
// - added ability to call .save() in .exit() without side effects // - added ability to call .save() in .exit() without side effects
// - page refresh recovers state from history
const pathToRegexp = require('path-to-regexp'); const pathToRegexp = require('path-to-regexp');
const clickEvent = document.ontouchstart ? 'touchstart' : 'click'; const clickEvent = document.ontouchstart ? 'touchstart' : 'click';
@ -121,7 +122,7 @@ class Router {
window.addEventListener('popstate', this._onPopState, false); window.addEventListener('popstate', this._onPopState, false);
document.addEventListener(clickEvent, this._onClick, false); document.addEventListener(clickEvent, this._onClick, false);
const url = location.pathname + location.search + location.hash; const url = location.pathname + location.search + location.hash;
return this.replace(url, null, true); return this.replace(url, history.state, true);
} }
stop() { stop() {