index.js (5035B)
1 import lexicon from './database/lexicon.js' 2 3 localStorage.debug = 'wyve' 4 5 class Wyve { 6 constructor(debug = true) { 7 this._debug = debug 8 this.state = {} 9 this.bus = bus('wyve') 10 this.router = wayfarer('/404') 11 this.debug = create_debug('wyve') 12 this.lexicon = lexicon 13 14 this.node = document.getElementById('wyve') 15 this.content_node = html`<div></div>` 16 this.node.appendChild(this.content_node) 17 } 18 19 async start() { 20 await this.init() 21 this.debug('successfully started') 22 this.router(hash()) 23 } 24 25 async init() { 26 // get relevant Dat archive information 27 await $is_dat(async () => { 28 this.archive = await DatArchive.load(await DatArchive.resolveName(window.location.href)) 29 this.is_owner = (await this.archive.getInfo()).isOwner 30 try { 31 this.state.peers = (await experimental.datPeers.list(await DatArchive.resolveName(window.location.href))).length 32 let p = this.state.peers 33 this.debug(`right now there ${p > 1 ? 'are' : 'is'} ${this.state.peers} ${p > 1 ? 'peers' : 'peer'} connected`) 34 } catch (e) { 35 this.debug('something went wrong with the peers API') 36 } 37 if (this.is_owner) { 38 this.debug('current peer is owner') 39 } 40 }) 41 42 let sorted = _.sortBy(_.map(this.lexicon, (v,k) => { 43 v.key = k 44 return v 45 }), (o) => _.snakeCase(o.name)) 46 this.categories = _.groupBy(sorted, 'under') 47 48 // Setup router 49 this.router.on('/', () => { 50 this.render(this.lexicon.home) 51 }) 52 53 this.router.on('/home', () => { 54 this.render(this.lexicon.home) 55 }) 56 57 this.router.on('/index', () => { 58 morph(this.content_node, html` 59 <div> 60 <nav> 61 <div id="desc"> 62 <h3 id="desc">${raw(new Curlic("{Wyve(code/wyve)}'s index.").toString())}</h3> 63 </div> 64 <hr /> 65 </nav> 66 <main> 67 <bpre>${raw(constructTree(this.categories))}</bpre> 68 </main> 69 </div> 70 `) 71 }) 72 73 this.router.on('/special/screenshot', () => { 74 if (this.is_owner) { 75 experimental.capturePage('dat://1001.hashbase.io/#/code/wyve', { 76 width: 19*64, 77 height: 8*64 78 }).then((png) => { 79 this.archive.writeFile('/screenshot.png', png) 80 }).then(() => { 81 this.debug('screenshot saved!') 82 window.location.hash = "#home" 83 }) 84 } 85 }) 86 87 this.router.on('/:slug', ({ slug }) => { 88 this.debug(`root, ${slug}`) 89 this.render( _.find(this.categories.root, ['key', $slugify(slug)]) ) 90 }) 91 92 this.router.on('/:category/:slug', ({ category, slug }) => { 93 this.debug(`${category}, ${slug}`) 94 this.render( _.find(this.categories[category], ['key', $slugify(slug)]) ) 95 }) 96 97 // Setup bus 98 addEventListener('hashchange', () => this.bus.emit('navigate')) 99 100 this.on('navigate', () => { 101 this.debug(`navigate to ${hash()}`) 102 this.router(hash()) 103 }) 104 } 105 106 on(event, callback) { 107 if (this._debug) { 108 this.bus.on(event, (...parameters) => { 109 this.debug(`event '${event}' called with ${!(!(parameters)) ? 'no parameters' : JSON.stringify(parameters)}`) 110 callback( ...parameters ) 111 }) 112 } else { 113 this.bus.on(event, callback) 114 } 115 } 116 117 render(entry) { 118 if (entry == undefined) { 119 this.render(this.lexicon['404']) 120 } 121 // TODO: Highlight current category and page on tree. 122 // FIXME please 123 // XXX lewd 124 // README important af 125 let long = new Runic(entry.long, Curlic).toString() 126 let desc = new Curlic(entry.desc).toString() 127 128 $retitle(entry.title ? entry.title : $title(entry.key)) 129 130 morph(this.content_node, html` 131 <div> 132 <nav> 133 <div id="desc"> 134 <h3 id="desc">${raw(desc)}</h3> 135 </div> 136 <hr /> 137 </nav> 138 <aside> 139 <bpre>${raw(constructTree(this.categories, this, { highlight: true }))}</bpre> 140 </aside> 141 <main> 142 ${raw(long)} 143 </main> 144 </div> 145 `) 146 } 147 } 148 149 function constructTree(lexicon, self) { 150 let things = lexicon 151 let str = '' 152 153 for (let cat of Object.keys(things)) { 154 if (!self.is_owner && cat == 'special') continue; 155 let pre = '<span class="accent">├╴</span>' 156 let end = '<span class="accent">└╴</span>' 157 if (cat == 'root') { pre=''; end='' } 158 if (cat != 'root') str += '<span class="accent">' + cat + '/</span>' + '\n'; 159 for (let th in things[cat]) { 160 let self = things[cat][th] 161 if (self.key == '404') continue; 162 if (th < things[cat].length-1) { 163 if (self.external) { 164 str += pre + new Curlic('{' + self.key + '('+self.link+')}').toString() + '\n' 165 } else { 166 if (cat != 'root') { 167 str += pre + new Curlic('{' + self.key + '('+cat+'/'+$slugify(self.key)+')}').toString() + '\n' 168 } else { 169 str += pre + new Curlic('{' + self.key + '('+$slugify(self.key)+')}').toString() + '\n' 170 } 171 } 172 } else { 173 if (self.external) { 174 str += end + new Curlic('{' + self.key + '('+self.link+')}').toString() + '\n' 175 } else { 176 if (cat != 'root') { 177 str += end + new Curlic('{' + self.key + '('+cat+'/'+$slugify(self.key)+')}').toString() + '\n' 178 } else { 179 str += end + new Curlic('{' + self.key + '('+$slugify(self.key)+')}').toString() + '\n' 180 } 181 } 182 } 183 } 184 } 185 186 return str 187 } 188 189 export default Wyve