Files: 1e749ac35548ddbe1f4662e9cfc2afadcfe9a2cf / modules / app / views.js
2697 bytesRaw
1 | var { h, Value, Dict, dictToCollection, map, computed } = require('mutant') |
2 | var nest = require('depnest') |
3 | |
4 | exports.gives = nest('app.views') |
5 | |
6 | exports.create = function (api) { |
7 | return nest('app.views', function (renderPage, defaultViews) { |
8 | var views = Dict({}) |
9 | |
10 | var forwardHistory = [] |
11 | var backHistory = [] |
12 | |
13 | if (defaultViews) { |
14 | defaultViews.forEach((view) => { |
15 | views.put(view, renderPage(view)) |
16 | }) |
17 | } |
18 | |
19 | var lastViewed = {} |
20 | |
21 | // delete cached view after 5 mins of last seeing |
22 | setInterval(() => { |
23 | views.keys().forEach((view) => { |
24 | if (!defaultViews.includes(view)) { |
25 | if (lastViewed[view] !== true && Date.now() - lastViewed[view] > (5 * 60e3) && view !== currentView()) { |
26 | views.delete(view) |
27 | } |
28 | } |
29 | }) |
30 | }, 60e3) |
31 | |
32 | var canGoForward = Value(false) |
33 | var canGoBack = Value(false) |
34 | var currentView = Value(defaultViews && defaultViews[0] || null) |
35 | |
36 | var viewCollection = dictToCollection(views) |
37 | var html = h('div.main', map(viewCollection, (item) => { |
38 | return h('div.view', { |
39 | attributes: { |
40 | 'data-href': item.key |
41 | }, |
42 | hidden: computed([item.key, currentView], (a, b) => a !== b) |
43 | }, [ item.value ]) |
44 | })) |
45 | |
46 | return { |
47 | get: views.get, |
48 | defaultViews, |
49 | canGoForward, |
50 | canGoBack, |
51 | currentView, |
52 | setView, |
53 | goBack, |
54 | goForward, |
55 | html |
56 | } |
57 | |
58 | // scoped |
59 | |
60 | function goBack () { |
61 | if (backHistory.length) { |
62 | canGoForward.set(true) |
63 | forwardHistory.push(currentView()) |
64 | |
65 | var view = backHistory.pop() |
66 | if (!views.has(view)) { |
67 | views.put(view, renderPage(view)) |
68 | } |
69 | |
70 | currentView.set(view) |
71 | canGoBack.set(backHistory.length > 0) |
72 | } |
73 | } |
74 | |
75 | function goForward () { |
76 | if (forwardHistory.length) { |
77 | backHistory.push(currentView()) |
78 | |
79 | var view = forwardHistory.pop() |
80 | if (!views.has(view)) { |
81 | views.put(view, renderPage(view)) |
82 | } |
83 | |
84 | currentView.set(view) |
85 | canGoForward.set(forwardHistory.length > 0) |
86 | canGoBack.set(true) |
87 | } |
88 | } |
89 | |
90 | function setView (view) { |
91 | if (!views.has(view)) { |
92 | views.put(view, renderPage(view)) |
93 | } |
94 | |
95 | if (lastViewed[view] !== true) { |
96 | lastViewed[view] = Date.now() |
97 | } |
98 | |
99 | if (currentView() && lastViewed[currentView()] !== true) { |
100 | lastViewed[currentView()] = Date.now() |
101 | } |
102 | |
103 | if (view !== currentView()) { |
104 | canGoForward.set(false) |
105 | canGoBack.set(true) |
106 | forwardHistory.length = 0 |
107 | backHistory.push(currentView()) |
108 | currentView.set(view) |
109 | } |
110 | } |
111 | }) |
112 | } |
113 |
Built with git-ssb-web