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