summaryrefslogtreecommitdiff
path: root/src/web/app/desktop/router.js
blob: f4d2ec347af9ce741390dd63ebab52c1ad9cb6d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
 * Desktop App Router
 */

const riot = require('riot');
const route = require('page');
let page = null;

module.exports = me => {
	route('/',              index);
	route('/i>mentions',    mentions);
	route('/post::post',    post);
	route('/search::query', search);
	route('/:user',         user.bind(null, 'home'));
	route('/:user/graphs',  user.bind(null, 'graphs'));
	route('/:user/:post',   post);
	route('*',              notFound);

	function index() {
		me ? home() : entrance();
	}

	function home() {
		mount(document.createElement('mk-home-page'));
	}

	function entrance() {
		mount(document.createElement('mk-entrance'));
		document.documentElement.setAttribute('data-page', 'entrance');
	}

	function mentions() {
		const el = document.createElement('mk-home-page');
		el.setAttribute('mode', 'mentions');
		mount(el);
	}

	function search(ctx) {
		const el = document.createElement('mk-search-page');
		el.setAttribute('query', ctx.params.query);
		mount(el);
	}

	function user(page, ctx) {
		const el = document.createElement('mk-user-page');
		el.setAttribute('user', ctx.params.user);
		el.setAttribute('page', page);
		mount(el);
	}

	function post(ctx) {
		const el = document.createElement('mk-post-page');
		el.setAttribute('post', ctx.params.post);
		mount(el);
	}

	function notFound() {
		mount(document.createElement('mk-not-found'));
	}

	riot.mixin('page', {
		page: route
	});

	// EXEC
	route();
};

function mount(content) {
	document.documentElement.removeAttribute('data-page');
	if (page) page.unmount();
	const body = document.getElementById('app');
	page = riot.mount(body.appendChild(content))[0];
}