Files: bfa5d0d4750109fb027d52d8738df5830003c664 / volunteers / jobs.js
1992 bytesRaw
1 | const smarkt = require('smarkt') |
2 | const _ = require('lodash') |
3 | const html = require('nanohtml') |
4 | |
5 | |
6 | var archive = new DatArchive(window.location) |
7 | |
8 | function store (state, emitter) { |
9 | state.library = [] |
10 | state.info = {} |
11 | state.covers = {} |
12 | state.identity = '' |
13 | |
14 | emitter.on('DOMContentLoaded', function () { |
15 | setIdentity() |
16 | archive.readdir('zines') |
17 | .then(zines => mapToState(zines)) |
18 | }) |
19 | |
20 | emitter.on('update library', function (zine) { |
21 | addToLibrary(zine) |
22 | addZineInfo(zine.url) |
23 | addZineCover(zine.url) |
24 | }) |
25 | |
26 | function mapToState (zines) { |
27 | zines.map(zine => { |
28 | archive.readFile(`zines/${zine}`) |
29 | .then(rawText => { |
30 | var textJson = smarkt.parse(rawText) |
31 | addToLibrary(textJson) |
32 | addZineInfo(textJson.url) |
33 | addZineCover(textJson.url) |
34 | }) |
35 | }) |
36 | } |
37 | |
38 | function addToLibrary (text) { |
39 | state.library = [...state.library, text] |
40 | emitter.emit('pushState') |
41 | } |
42 | |
43 | function addZineInfo (datUrl) { |
44 | var zine = new DatArchive(datUrl) |
45 | zine.readFile('distro/info.txt') |
46 | .then(text => { |
47 | mapInfoToState(text, zine['url']) |
48 | }) |
49 | } |
50 | |
51 | function addZineCover (datUrl) { |
52 | var zine = new DatArchive(datUrl) |
53 | zine.readdir('distro') |
54 | .then(files => { |
55 | mapCoverToState(files, zine['url']) |
56 | }) |
57 | } |
58 | |
59 | function mapInfoToState (info, datUrl) { |
60 | var infoJson = smarkt.parse(info) |
61 | state.info[datUrl] = infoJson |
62 | emitter.emit('pushState') |
63 | } |
64 | |
65 | function mapCoverToState (files, datUrl) { |
66 | var cover = files.find(file => sansExtension(file) === 'cover') |
67 | state.covers[datUrl] = `${datUrl}/distro/${cover}` |
68 | emitter.emit('pushState') |
69 | } |
70 | |
71 | async function setIdentity () { |
72 | var info = await archive.getInfo() |
73 | if (info.isOwner) { |
74 | state.identity = 'librarian' |
75 | emitter.emit('pushState') |
76 | } else { |
77 | state.identity = 'visitor' |
78 | emitter.emit('pushstate') |
79 | } |
80 | } |
81 | } |
82 | |
83 | function sansExtension (file) { |
84 | return file.split('.')[0] |
85 | } |
86 | |
87 | module.exports = store |
88 |
Built with git-ssb-web