summaryrefslogtreecommitdiff
path: root/src/web/app/desktop/router.ls
blob: 02a7e11816cf1ef2c4e12001f53ea68ca737a0fb (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
75
76
77
# Router
#================================

riot = require \riot
route = require \page
page = null

module.exports = (me) ~>

	# Routing
	#--------------------------------

	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 \* not-found

	# Handlers
	#--------------------------------

	function index
		if me? then home! else entrance!

	function home
		mount document.create-element \mk-home-page

	function entrance
		mount document.create-element \mk-entrance
		document.document-element.set-attribute \data-page \entrance

	function mentions
		document.create-element \mk-home-page
			..set-attribute \mode \mentions
			.. |> mount

	function search ctx
		document.create-element \mk-search-page
			..set-attribute \query ctx.params.query
			.. |> mount

	function user page, ctx
		document.create-element \mk-user-page
			..set-attribute \user ctx.params.user
			..set-attribute \page page
			.. |> mount

	function post ctx
		document.create-element \mk-post-page
			..set-attribute \post ctx.params.post
			.. |> mount

	function not-found
		mount document.create-element \mk-not-found

	# Register mixin
	#--------------------------------

	riot.mixin \page do
		page: route

	# Exec
	#--------------------------------

	route!

# Mount
#================================

function mount content
	document.document-element.remove-attribute \data-page
	if page? then page.unmount!
	body = document.get-element-by-id \app
	page := riot.mount body.append-child content .0