-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspa.html
392 lines (386 loc) · 14.3 KB
/
spa.html
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!doctype html>
<html lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="icon" type="image/png" href="https://manifold.picolabs.io:9090/pico-logo-transparent-48x48.png" />
<title>Browser Agent</title>
<link rel="stylesheet" href="https://manifold.picolabs.io:9090/css/index.css" />
<script src="https://manifold.picolabs.io:9090/js/jquery-3.1.0.min.js" type="text/javascript"></script>
<script src="https://manifold.picolabs.io:9090/js/handlebars.min.js" type="text/javascript"></script>
<script id="links-template" type="text/x-handlebars-template">
<div id="connection-list">
<h1>Connections page for {{ui.name}}</h1>
{{#if ui.connections}}
<h3>Connections</h3>
<ul>
{{#each ui.connections}}
{{#if url}}
<li><a href="{{url}}#{{my_did}}" target="_blank">{{label}}</a></li>
{{/if}}
{{/each}}
</ul>
{{else}}
<h3>No connections</h3>
{{/if}}
<form class="js-ajax-form" action="/sky/event/{{eci}}/accept/sovrin/new_invitation">
<p>
When you receive a new connection invitation
<input type="text" name="url" placeholder="paste invitation here" required>
<button
{{#unless ui.routerUI}}
disabled
style="cursor:default"
title="need configured inbound router"
{{/unless}}
>Accept</button>
<input type="hidden" name="need_router_connection" value="true">
</p>
</form>
<p>
<a class="turn-page" href="#agent">agent</a>
</p>
</div>
</script>
<script id="agent-template" type="text/x-handlebars-template">
<div id="connection-list">
{{#if disabled}}
<form class="js-ajax-form" action="/sky/event/{{eci}}/add-ruleset/wrangler/install_rulesets_requested">
Agent label
<input type="text" name="label" value="{{name}}">
<input type="hidden" name="rids" value="org.sovrin.agent">
<button>Create agent</button>
</form>
{{else}}
<a href="https://sovrin.org" target="_blank"><img src="https://manifold.picolabs.io:9090/img/SovrinIcon.png" alt="Sovrin Icon" class="icon"></a>
{{#if ui.wf}}
{{#if ui.wf.url}}<a href="{{ui.wf.url}}" target="_blank">{{/if}}{{#if ui.wf.photo}}<img src="{{ui.wf.photo}}" alt="photo" class="icon">{{else}}{{#if ui.wf.url}}<img src="img/blank-profile-picture.png" alt="blank profile picture" class="icon">{{/if}}{{/if}}{{#if ui.wf.url}}</a>{{/if}}
{{/if}}
<h2>{{ui.name}}</h2>
<form class="js-ajax-form" action="/sky/event/{{eci}}/accept/sovrin/new_invitation">
<table>
{{#if ui.routerUI.invitationViaRouter}}
<tr>
<th align="right">Invitation via {{ui.routerUI.routerName}}</th>
<td><input id="inviteVia" value="{{ui.routerUI.invitationViaRouter}}"></td>
<td>
<button type="button" onclick="$('#inviteVia').select();document.execCommand('copy')">Copy</button>
<button type="button" onclick="window.open($('#inviteVia').val(),'invitation')">View</button>
</td>
</tr>
{{/if}}
<tr>
<th align="right">Receive Invitation</th>
<td><input type="text" name="url" placeholder="paste invitation URL here" required></td>
<td>
<button
{{#unless ui.routerUI}}
disabled
style="cursor:default"
title="need configured inbound router"
{{/unless}}
>Accept</button>
<input type="hidden" name="need_router_connection" value="true">
</td>
</tr>
</table>
</form>
<h3>Inbound router: {{#if ui.routerUI}}<span title="{{ui.routerUI.routerHost}} {{ui.routerUI.routerRequestECI}}">{{ui.routerUI.routerName}}</span> {{#if ui.routerUI.unused}}<a class="js-ajax-link" href="/sky/event/{{eci}}/remove-router/edge/router_removal_requested">del</a>{{/if}}{{else}}<a href="#" onclick="event.preventDefault();$(this).parent().next('form').toggle()" style="font-size:75%">not configured</a>{{/if}}</h3>
{{#unless ui.routerUI.routerName}}
<form class="js-ajax-form" action="/sky/event/{{eci}}/add-ruleset/wrangler/install_rulesets_requested?rids=org.sovrin.edge" style="display:none">
<input name="host" placeholder="router host">
<input name="eci" placeholder="router request eci">
<input name="label" placeholder="router label">
<button type="submit">Configure</button>
</form>
{{/unless}}
{{#if ui.connections}}
<h3>Connections <a class="turn-page" href="#links" style="font-size:75%">links</a></h3>
<ul>
{{#each ui.connections}}
<li id="{{my_did}}">
<input type="checkbox" id="connection-{{@index}}">
<label for="connection-{{@index}}">{{label}}{{#if routerName}} (via {{routerName}}){{/if}}</label>
<div class="connection-detail">
{{#if error}}<p>{{error}}</p>{{/if}}
{{#if messages}}
<div class="agent-basic-messages">
{{#each messages}}
<p class="{{from}}"{{#if color}} style="background:rgb({{color}},0.1)"{{/if}}>{{content}}<span>{{sent_time}}</span></p>
<hr>
{{/each}}
</div>
{{/if}}
<form class="js-ajax-form" action="/sky/event/{{my_did}}/msg/sovrin/send_basicmessage?their_vk={{their_vk}}" style="float:right">
<input name="content">
<button>Send</button>
</form>
<br clear="all">
<form class="js-ajax-form" action="/sky/event/{{my_did}}/ping/sovrin/trust_ping_requested?their_vk={{their_vk}}">
<button>Send Trust Ping</button>
</form>
<a class="js-ajax-link" href="/sky/event/{{my_did}}/ui/sovrin/connection_expired?their_vk={{their_vk}}">delete connection with {{label}}</a>
</div>
</li>
{{/each}}
</ul>
{{else}}
<h3>No connections <a class="turn-page" href="#links" style="font-size:75%">links</a></h3>
{{/if}}
<h3>Technical details</h3>
<input type="checkbox" id="connection-technical-details">
<label for="connection-technical-details">shown</label>
<pre class="connection-detail">{{text}}</pre>
</div>
{{/if}}
</script>
<style type="text/css">
.cursor-wait { cursor: wait !important; }
</style>
</head>
<body>
<div id="refresh" style="padding:10px;float:right">
<button>Refresh</button>
</div>
<div id="section" style="padding:10px">
<p>Loading...</p>
</div>
<script src="https://manifold.picolabs.io/pico-engine.min.js" type="text/javascript"></script>
<script type="text/javascript">
async function main(){
var cursor_wait = function(){
var elements = $(':hover')
if(elements.length){
elements.last().addClass('cursor-wait')
}
$('html')
.off('mouseover.cursorwait')
.on('mouseover.cursorwait',function(e){
$(e.target).addClass('cursor-wait')
})
}
var remove_cursor_wait = function(){
$('html').off('mouseover.cursorwait')
$('.cursor-wait').removeClass('cursor-wait')
}
cursor_wait()
// start it up
var pe = await PicoEngine();
window.pe = pe;// make it global so we can use it in the devtools console
// now you can use pe (instance of pico-engine-core)
var rootECI = await pe.getRootECI()
var ridWrangler = 'io.picolabs.wrangler'
var agent_rbase = 'https://raw.githubusercontent.com/Picolab/G2S/master/krl/';
var agent_rids = [
'html',
'webfinger',
'org.sovrin.agent.ui',
'org.sovrin.agent_message',
'org.sovrin.agent',
'org.sovrin.edge',
];
var regRids = await pe.runQuery({eci:rootECI,rid:ridWrangler,name:'registeredRulesets'})
for ( var i=0; i<agent_rids.length; ++i ){
if (regRids.includes(agent_rids[i])) {
} else {
console.log('register: ',agent_rids[i]);
await pe.registerRulesetURL(agent_rbase+agent_rids[i]+'.krl');
}
}
console.log('started');
var agentECI = null
var children = await pe.runQuery({
eci: rootECI,
rid: ridWrangler,
name: "children"
})
if(children && children.length){
agentECI = children[0].eci
} else {
var agentLabel = prompt('Agent label')
var directives = await pe.signalEvent({
eci: rootECI,
domain: "wrangler",
type: "child_creation",
attrs: {name:agentLabel,rids:"org.sovrin.agent"}
})
var dirs = directives.directives
if(dirs
&& dirs.length
&& dirs[0].name === 'Pico_Created'
&& dirs[0].options
&& dirs[0].options.pico
&& dirs[0].options.pico.eci)
{
agentECI = dirs[0].options.pico.eci
} else {
alert('failure')
}
}
var myself = await pe.runQuery({
eci: agentECI,
rid: 'io.picolabs.wrangler',
name: 'myself'
})
console.log('myself:', JSON.stringify(myself));
$('#refresh').click(function (e) {
e.preventDefault()
cursor_wait()
doDisplay()
})
var agentTemplate = Handlebars.compile($('#agent-template').html());
var linksTemplate = Handlebars.compile($('#links-template').html());
var ridAgent = 'org.sovrin.agent'
var ridEdge = 'org.sovrin.edge'
var eventRE = /^\/sky\/event\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)/
//var queryRE = /^\/sky\/query\/([^\/]*)\/([^\/]*)\/([^\/]*)/
var formToJSON = function (form,qs) {
var json = {}
if(form){
$.each($(form).serializeArray(), function (key, elm) {
json[elm.name] = elm.value
})
}
if(qs){
var args = qs.split('&')
for(var i=0; i<args.length; ++i){
kv = args[i].split('=')
var name = kv.shift()
var value = kv.length ? (kv.length>1 ? kv.join('=') : kv.shift()) : ''
json[name] = value
}
}
return json
}
var convert = function(url,form){
var parts = url.split('?')
var isEvent = parts[0].match(eventRE)
//var isQuery = parts[0].match(queryRE)
parts.shift()
var qs = parts.length ? parts.join('?') : ''
if(isEvent){
return({
eci:isEvent[1],
eid:isEvent[2],
domain:isEvent[3],
type:isEvent[4],
attrs:formToJSON(form,qs)
})
}
// else if(isQuery){
//return({
//eci:isQuery[1],
//rid:isQuery[2],
//name:isQuery[3],
//args:formToJSON(null,qs)
//})
//}
}
var $theSection = $('#section')
var doDisplay = async function(){
var whereSpec = location.hash.substring(1).split('-')
var whichPage = whereSpec[0] || 'agent'
var pageName = whichPage === 'agent' ? 'Agent' : 'Links'
document.title = myself.name + ' ' + pageName
await pe.signalEvent(
{eci:agentECI,eid:'poll_router',domain:'edge',type:'poll_all_needed'})
var linksUI = {
name: myself.name,
}
var hasRids = await pe.runQuery({eci:agentECI,rid:ridWrangler,name:'installedRulesets'})
var isAgent = hasRids.includes(ridAgent)
var isEdge = hasRids.includes(ridEdge)
if(isAgent && whichPage === 'links'){
linksUI.ui = await pe.runQuery({eci:agentECI,rid:ridAgent,name:'ui'})
var connections = linksUI.ui.connections
if(connections){
for(var i=0; i<connections.length; ++i){
var conn = connections[i]
var names = conn.label.split(' ')
var url = null
if(names.length===2){ //kludge
url = 'https://'+names[0]+'.bruceatbyu.com'
}
linksUI.ui.connections[i].url = url
}
}
if(isEdge){
linksUI.ui.routerUI = await pe.runQuery({eci:agentECI,rid:ridEdge,name:'ui'})
}
} else {
linksUI.disabled = true
}
linksUI.eci = agentECI
var agentUI = {
name: myself.name,
eci: myself.eci,
pico_id: myself.id,
}
if(isAgent && whichPage === 'agent'){
agentUI.ui = await pe.runQuery({eci:agentECI,rid:ridAgent,name:'ui'})
if(isEdge){
agentUI.ui.routerUI = await pe.runQuery({eci:agentECI,rid:ridEdge,name:'ui'})
// adjust ui with router information
var routerUsedCount = 0
if (agentUI.ui && agentUI.ui.routerUI) {
if (agentUI.ui.routerUI.routerName && agentUI.ui.connections) {
var toSuffix = " to " + agentUI.ui.name
for (var ic=0; ic < agentUI.ui.connections.length; ++ic) {
var rConnName = agentUI.ui.connections[ic].label + toSuffix
if (agentUI.ui.routerUI.routerConnections && agentUI.ui.routerUI.routerConnections[rConnName]) {
agentUI.ui.connections[ic].routerName = agentUI.ui.routerUI.routerName
++routerUsedCount
}
}
}
agentUI.ui.routerUI.unused = routerUsedCount == 0
}
}
agentUI.text = JSON.stringify(agentUI.ui, undefined, 2)
} else {
agentUI.disabled = true
}
var whichTemplate = whichPage === 'agent' ? agentTemplate : linksTemplate
var whichUI = whichPage === 'agent' ? agentUI : linksUI
$theSection.html(whichTemplate(whichUI))
remove_cursor_wait()
$theSection.find('.js-ajax-link').click(async function (e) {
e.preventDefault()
var href = $(this).attr('href')
cursor_wait()
await pe.signalEvent(convert(href))
doDisplay()
})
$theSection.find('.js-ajax-form').submit(async function (e) {
e.preventDefault()
var action = $(this).attr('action')
cursor_wait()
await pe.signalEvent(convert(action,this))
doDisplay()
})
$theSection.find('.turn-page').click(function(e) {
e.preventDefault()
location.hash = $(this).attr('href').substring(1)
cursor_wait()
doDisplay()
})
if (whereSpec.length) {
var $theLi = $('li#' + whereSpec[1])
$theLi.find('input').first().trigger('click')
$theLi.find('input').last().focus()
}
$('#connection-list li > input').click(function() {
if (this.checked) {
location.hash = whichPage + '-' + $(this).parent().attr('id')
} else {
location.hash = whichPage
}
cursor_wait()
doDisplay()
})
}
doDisplay()
}
main().catch(console.error)
</script>
</body>
</html>