Files: ef1a2eaa163709173d29cc9f3fc446695e466fe7 / ui.js
3415 bytesRaw
1 | var h = require('hscrpt') |
2 | var input_file = require('hyperfile') |
3 | var Progress = require('hyperprogress') |
4 | var u = require('./util') |
5 | var prog = Progress() |
6 | |
7 | //split the hash. |
8 | var parts = window.location.hash.split('#').slice(1) |
9 | var QUOTA = 5*1024*1024 |
10 | |
11 | var human = require('human-time') |
12 | |
13 | module.exports = function (appname, wb) { |
14 | document.body.appendChild(prog) |
15 | |
16 | function nice_error(err, msg) { |
17 | if(err.name && err.message && err.stack) |
18 | return err |
19 | if('object' !== typeof err) |
20 | return new Error(msg+JSON.stringify(err)) |
21 | } |
22 | |
23 | function btn (label, action) { |
24 | return h('button', {onclick: action}, [label]) |
25 | } |
26 | |
27 | function kb (bytes) { |
28 | return (Math.round((bytes / 1024) * 100)/100) + 'k' |
29 | } |
30 | |
31 | // var isElectron = typeof process !== 'undefined' |
32 | // && process.env && process.env[appname+'_INIT'] |
33 | |
34 | function handleQuota(err, data, id) { |
35 | if(err |
36 | && err.name == "QuotaExceededError" |
37 | && confirm ('adding: '+id+' exceedes quota, clear cache?') |
38 | ) { |
39 | wb.prune(data.length, function (err) { |
40 | if(err) throw prog.fail(err) |
41 | wb.add(data, id, function (err) { |
42 | if(err) throw prog.fail(err) |
43 | wb.run(id, function (err) { |
44 | if(err) throw prog.fail(err) |
45 | }) |
46 | }) |
47 | }) |
48 | } |
49 | else if(!err) wb.run(id, function (err) { |
50 | if(err) throw prog.fail(err) |
51 | }) |
52 | else |
53 | throw prog.fail(err) |
54 | } |
55 | |
56 | ;(function redraw () { |
57 | wb.versions(function (err, log) { |
58 | if(err) throw prog.fail(err) |
59 | document.body.innerHTML = '' |
60 | if(log.length && !wb.isInit()) |
61 | return wb.run(log[0].value, function (err) { |
62 | if(err) throw prog.fail(err) |
63 | }) |
64 | |
65 | document.body.appendChild( |
66 | h('div', {classList: 'WebBoot'}, [ |
67 | ( !log.length |
68 | ? h('h2', 'please enter secure-url or select file to run') |
69 | : h('ol', {classList: 'WebBoot__recent'}, log.map(function (v) { |
70 | return h('li', [ |
71 | h('code', [v.value]), |
72 | h('label', |
73 | { title: new Date(v.ts).toString() }, |
74 | [ ' (loaded ', human(new Date(v.ts)), ') '] |
75 | ), |
76 | h('div', [ |
77 | btn('run', function () { |
78 | wb.run(v.value) |
79 | }), |
80 | btn('revert', function () { |
81 | wb.revert(v.ts, function (err) { |
82 | if(err) throw prog.fail(err) |
83 | redraw() |
84 | }) |
85 | }) |
86 | ]) |
87 | ]) |
88 | })) |
89 | ), |
90 | |
91 | //paste a secure url into this text input |
92 | h('input', { |
93 | placeholder: 'enter secure url', |
94 | onchange: function (ev) { |
95 | //else, download it. if that succeeds, |
96 | //add to store, if success, run. |
97 | //if that fails, offer to clean up, or fail. |
98 | //add to store, if success, run. |
99 | |
100 | //this can fail from quota exceeded. |
101 | //check whether we have this already, if so, run it. |
102 | var url = ev.target.value |
103 | |
104 | wb.install(url, handleQuota) |
105 | } |
106 | }), |
107 | |
108 | //or select a local file to run |
109 | input_file(function (data) { |
110 | wb.add(data, function (err, id) { |
111 | handleQuota(err, data, id) |
112 | }) |
113 | }) |
114 | ]) |
115 | ) |
116 | }) |
117 | })() |
118 | |
119 | } |
120 | |
121 | |
122 | |
123 | |
124 | |
125 | |
126 | |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | |
133 |
Built with git-ssb-web