/
main.js
105 lines (84 loc) · 2.54 KB
/
main.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const $ = require('jquery')
const EventEmitter = require('eventemitter3')
const SencData = require('./data')
const SencUI = require('./ui')
const anchor = require('./anchor')
// const audit = require('./auditlog')
// app will have all the variables.
// gets populated as we go.
var app = window.app = {}
// set main entry point
window.onload = main
class SencApp extends EventEmitter {
constructor(opts) {
super()
this.opts = opts || {}
this.ui = new SencUI(opts.$el)
this.$el = this.ui.$el
this.onLoadFile = this.onLoadFile.bind(this)
this.ui.on('load', (kp) => this.emit('loadfile', kp)) // fwd event
this.on('loadfile', this.onLoadFile)
}
dataOpts() { // only send some opts to data
return {
gateway: this.opts.gateway,
forceGateway: this.opts.forceGateway,
forceWindowIpfs: this.opts.forceWindowIpfs,
}
}
getFileParams() {
return this.ui.getFileParams()
}
setFileParams(key, path) {
var curr = this.getFileParams()
if (curr.key == key && curr.path == path)
return // nothing to change
this.ui.setFileParams(key, path)
if (SencData.isIpfsPath(path)) {
this.emit('loadfile', {key: key, path: path})
}
}
onLoadFile(kp) {
var key = kp.key
var path = kp.path
if (!SencData.isIpfsPath(path))
return
this.ui.setLoading(true)
path = SencData.normalizePath(path)
var s = SencData.loadAndDecrypt(path, key, this.dataOpts())
this.ui.renderTree(s)
this.emit('params', this.getFileParams())
}
}
// SencApp = audit(SencApp, 'app')
// SencUI = audit(SencUI, 'ui')
function main() {
var opts = {'$el': $('#senc-container')}
opts.gateway = queryParam('gateway') || null
opts.forceGateway = queryParam('forceGateway')
opts.forceWindowIpfs = queryParam('forceWindowIpfs')
$senc = $('#senc-container')
var app = new SencApp(opts)
app.on('params', (kp) => {
anchor.urlHashSet(kp.key, kp.path)
})
$('body').append(app.$el)
var kp = anchor.urlHashLoad()
app.setFileParams(kp.key, kp.path)
}
function queryParam(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
var d = decodeURIComponent(results[2].replace(/\+/g, " "));
if (d) {
var dl = d.toLowerCase()
if (dl === 'true') return true
if (dl === 'false') return false
if (dl === 'undefined') return undefined
}
return d
}