This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
playground.html
644 lines (565 loc) · 31.3 KB
/
playground.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
<html>
<head>
<title>SAFE Web App Sample</title>
<link href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAABILAAASCwAAAAAAAAAAAABjZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9jZ8b/ZGjG/2Roxv9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9lacf/Y2fG/19jxf9gZMX/ZGjG/2Roxv9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9hZcX/WFzC/2Jmxv9xdMv/bXHK/1xgw/9eYsT/ZGjG/2Nnxv9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9iZsb/aW3I/6yu4f/v8Pn////////////b3PL/e3/P/11hxP9kaMf/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9kaMb/XWHE/3+C0P/+/v//4eL0/6Kk3f+wsuL//Pz+/+zs+P9qbsj/YWXF/2Roxv9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Roxv9dYsT/e37P/2hsyP9aXsP/S0+9/6Om3f//////jZDV/1pew/9lacf/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9jZ8b/ZGjG/19jxf9cYMP/W1/D/2Roxv/Ky+v//////4yP1f9aX8P/ZWnH/2Nnxv9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Rnxv9eYsT/g4bS/7/A5//y8vr//////9fY8P9kaMb/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Vpx/9bX8P/pafe////////////4eL0/6Wn3v9masf/YGTF/2Roxv9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Roxv9hZcX/aGzI//Hy+v/y8/r/gIPR/19jxP9YXcL/YWXF/2Vpx/9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9kaMb/YGTF/2xwyf/8/P7/19jx/05Tvv9WW8H/ZWnH/2hsyP9fY8T/ZGjG/2Nnxv9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Rox/9cYMP/xcfq///////Nzu3/p6nf/+Lj9P/k5fX/aGzI/2Jmxv9kaMb/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9kaMb/YWXF/2Vpx/+3ueX/8/P7///////t7vn/sbPi/2dryP9iZsb/Y2fG/2Nnxv9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Roxv9iZsb/WF3C/2Jmxv9qbsj/YWXF/1dcwv9hZcX/Y2fG/2Nnxv9jZ8b/Y2fG/2Rox/9jZ8b/Y2fG/2Nnxv9jZ8b/Y2fG/2Vpx/9iZsb/YGTF/2Nnxv9lacf/Y2fG/2Nnxv9jZ8b/Y2fG/2Rox/9kaMb/ZWnH/2Rox/9kaMf/ZGjH/2Rox/9kaMf/ZWnH/2Vpx/9lacf/ZGjH/2Rox/9kaMf/ZGjH/2Vpx/9kaMb/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==" rel="icon" type="image/x-icon" />
<!-- Load the API library -->
<script src="./lib/index.js"></script>
<!-- The below is only for this example page. It is NOT required for the API. -->
<script src="./assets/js/jquery-2.2.2.min.js"></script>
<script src="./assets/js/bootstrap.min.js"></script>
<script src="./assets/js/prism.js"></script>
<link rel="stylesheet" href="./assets/css/bootstrap.min.css">
<link rel="stylesheet" href="./assets/css/boostrap-theme.css">
<link rel="stylesheet" href="./assets/css/prism.css">
<!-- The above is only for this example page. It is NOT required for the API. -->
<script>
// Set the logger for the client
SafeApp.log = console.log.bind(console);
var Safe = null;
function addError(element, error) {
console.log(error.toString());
element.innerHTML = error.message;
}
$(document).ready(function() {
// defaults
window.isPathShared = false;
// pathShared toggle
var pathShared = $('#isPathShared');
pathShared.change(function(e) {
window.isPathShared = pathShared.is(":checked");
});
});
// After api results, do some work
function finalize(node) {
// Rerun code highlighter
Prism.highlightAll();
// Remove results areas except current
$('.result-area').each(function(i, el) {
if (el !== node) {
el.innerText = '';
}
});
}
</script>
<style>
.source-link {
text-decoration: underline;
cursor: pointer;
}
input[type=text] {
width: 300px;
}
.result-area {
font-family: "Courier New", Courier, monospace
}
button.submit {
margin-bottom: 10px;
}
.file-link {
text-decoration: underline;
font-size: 16px;
font-family: Helvetica, Arial, sans-serif;
}
</style>
</head>
<body class="language-javascript">
<div class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a href="./example.html" class="navbar-brand">SafeNet JS</a>
</div>
</div>
</div>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Global Options</h3>
</div>
<div class="panel-body">
<p>The following options will be used for the appropriate API calls in the playground below.</p>
<label><input type="checkbox" id="isPathShared"> Use SAFEDrive (isPathShared)</label><br/>
<label><input type="checkbox" id="autoAuth"> Automatically authenticate on page load</label><br/>
<script>
function showExplanation() {
$('#showExplanation').toggle();
}
</script>
<p><a href="#" onclick="showExplanation()">Explanation</a></p>
<div style="display: none" id="showExplanation">
<p><b>Use SAFEDrive:</b> applied to DNS and NFS calls. If TRUE, all directories and files will be
considered as residing in a root public folder called "SAFEDrive". Everything here can be written and modified by any
application. If FALSE, the another default root directory is used. Files and directories created with one option are not visible
when the other option is toggled, so you will see how important it is to keep track of it.</p>
<p><b>Automatically authenticate:</b> For convenience, automatically authenticate using saved data when the page loads. It will remember
what values you used last for the Safe initialization and attempt to use them again.</p>
</div>
</div>
</div>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">API Playground</h3>
</div>
<div class="panel-body">
<ul class="nav nav-tabs" style="margin-bottom: 15px;font-size:18px;">
<li><a aria-expanded="true" href="#auth" data-toggle="tab">Auth</a></li>
<li><a aria-expanded="false" href="#dns" data-toggle="tab">DNS</a></li>
<li><a aria-expanded="false" href="#nfs" data-toggle="tab">NFS</a></li>
</ul>
<div id="playgroundTabs" class="tab-content">
<div class="tab-pane active" id="auth">
<div>
<h3>Initialize <small>new SafeApp(app, permissions, config)</small></h3>
<div class="form-group">
<input type="text" value="com.samp-company.web" class="form-control" class="form-control" class="form-control" id="initializeIdBox" placeholder="App ID (e.g. com.samp-company.web)"/>
</div>
<div class="form-group">
<input type="text" value="Dat App" class="form-control" class="form-control" class="form-control" id="initializeNameBox" placeholder="App Name (e.g. Dat App)"/>
</div>
<div class="form-group">
<input type="text" value="My Company" class="form-control" class="form-control" class="form-control" id="initializeVendorBox" placeholder="Vendor (e.g. My Company)"/>
</div>
<div class="form-group">
<input type="text" value="0.0.1" class="form-control" class="form-control" class="form-control" id="initializeVersionBox" placeholder="Version (e.g. 0.0.1)"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="initialize(this)" id="initializeButton">Initialize Safe Instance</button>
<p id="initializeArea" class="result-area"></p>
<script>
function initialize(node) {
Safe = new SafeApp({
id: document.getElementById('initializeIdBox').value,
name: document.getElementById('initializeNameBox').value,
vendor: document.getElementById('initializeVendorBox').value,
version: document.getElementById('initializeVersionBox').value
}, ['SAFE_DRIVE_ACCESS']); // include SAFEDrive access
node.nextElementSibling.innerHTML = 'Initialized a new Safe instance. You can authenticate now.';
}
</script>
</div>
<div>
<h3>Authorize <small>authorize()</small></h3>
<button type="button" class="btn btn-primary submit" onClick="authorize(this)" id="authorizeButton">Authorize</button>
<p id="authorizeArea" class="result-area"></p>
<script>
function authorize(node) {
Safe.auth.authenticate().then(function() {
node.nextElementSibling.innerHTML = 'Authorized successfully.';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
</div>
<h3>Am I Authorized? <small>isAuth()</small></h3>
<button type="button" class="btn btn-primary submit" onClick="isAuthorized(this)">Am I Authorized?</button>
<p id="isAuthorizedArea" class="result-area"></p>
<script class="source">
function isAuthorized(node) {
Safe.auth.isAuth().then(function(bool) {
node.nextElementSibling.innerHTML = bool ? 'Authorized.' : 'Not authorized.';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Deauthorize <small>deauthorize()</small></h3>
<button type="button" class="btn btn-primary submit" onClick="deauthorize(this)">Deauthorize</button>
<p id="deauthorizeArea" class="result-area"></p>
<script class="source">
function deauthorize(node) {
Safe.auth.deauthorize().then(function() {
node.nextElementSibling.innerHTML = 'Unauthorized.';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
</div>
<div class="tab-pane" id="dns">
<h3>List Names <small>listNames()</small></h3>
<button type="button" class="btn btn-primary submit" onClick="getNames(this)">List Names</button>
<p id="namesArea" class="result-area"></p>
<script class="source">
function getNames(node) {
Safe.dns.listNames().then(function(names) {
node.nextElementSibling.innerHTML = names.length > 0 ? names.join(', ') : 'No names found.';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Create Name <small>createName(name)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" class="form-control" id="createNameBox" placeholder="Name"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="createName(this)">Create Name</button>
<p id="createNamesArea" class="result-area"></p>
<script class="source">
function createName(node) {
var name = document.getElementById('createNameBox').value;
Safe.dns.createName(name).then(function() {
node.nextElementSibling.innerHTML = 'Created new name "'+name+'".';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Delete Name <small>deleteName(name)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" class="form-control" id="deleteNameBox" placeholder="Name"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="deleteName(this)">Delete Name</button>
<p id="deleteNamesArea" class="result-area"></p>
<script class="source">
function deleteName(node) {
var name = document.getElementById('deleteNameBox').value;
Safe.dns.deleteName(name).then(function() {
node.nextElementSibling.innerHTML = 'Deleted name "'+name+'".';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>List Services For Name <small>listServices(name)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="listServicesBox" placeholder="Name"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="listServices(this)">List Services</button>
<p id="listServicesArea" class="result-area"></p>
<script class="source">
function listServices(node) {
var name = document.getElementById('listServicesBox').value;
Safe.dns.listServices(name).then(function(services) {
node.nextElementSibling.innerHTML = services.length > 0 ? services.join(', ') : 'No services found.';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Create Service For Name <small>createServiceForName(obj)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="createServiceForNameBoxName" placeholder="Name"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="createServiceForNameBoxServiceName" placeholder="Service Name"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="createServiceForNameBoxDir" placeholder="Directory Name"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="createServiceForName(this)">Create Service</button>
<p id="createServiceForNameArea" class="result-area"></p>
<script class="source">
function createServiceForName(node) {
var payload = {
longName: document.getElementById('createServiceForNameBoxName').value,
serviceName: document.getElementById('createServiceForNameBoxServiceName').value,
serviceHomeDirPath: document.getElementById('createServiceForNameBoxDir').value,
isPathShared: window.isPathShared
};
Safe.dns.createServiceForName(payload).then(function() {
node.nextElementSibling.innerHTML = 'Created new service for name "' + payload.longName + '".';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Create Name and Service <small>createServiceAndName(obj)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="createServiceAndNameBoxName" placeholder="Name"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="createServiceAndNameBoxServiceName" placeholder="Service Name"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="createServiceAndNameBoxDir" placeholder="Directory Name"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="createServiceAndName(this)">Create Service</button>
<p id="createServiceAndNameArea" class="result-area"></p>
<script class="source">
function createServiceAndName(node) {
var payload = {
longName: document.getElementById('createServiceAndNameBoxName').value,
serviceName: document.getElementById('createServiceAndNameBoxServiceName').value,
serviceHomeDirPath: document.getElementById('createServiceAndNameBoxDir').value,
isPathShared: window.isPathShared
};
Safe.dns.createServiceAndName(payload).then(function() {
node.nextElementSibling.innerHTML = 'Created new service and name.';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Delete Service For Name <small>deleteService(service, name)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="deleteServiceForNameBoxName" placeholder="Name"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="deleteServiceForNameBoxServiceName" placeholder="Service Name"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="deleteServiceForName(this)">Delete Service</button>
<p id="deleteServiceForNameArea" class="result-area"></p>
<script class="source">
function deleteServiceForName(node) {
var name = document.getElementById('deleteServiceForNameBoxName').value,
serviceName = document.getElementById('deleteServiceForNameBoxServiceName').value;
Safe.dns.deleteService(serviceName, name).then(function() {
node.nextElementSibling.innerHTML = 'Deleted service "' + serviceName + '".';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Get Service Directory
<small>getServiceDir(service, name)</small>
</h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="getServiceDirectoryServiceName" placeholder="Service Name"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="getServiceDirectoryName" placeholder="Name"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="getServiceDir(this)">Get Service Directory</button>
<p id="getServiceDirArea" class="result-area"></p>
<script class="source">
function getServiceDir(node) {
var name = document.getElementById('getServiceDirectoryName').value,
serviceName = document.getElementById('getServiceDirectoryServiceName').value;
Safe.dns.getServiceDir(serviceName, name).then(function(dir) {
node.nextElementSibling.innerHTML = 'Directory: <br /><pre><code>' + JSON.stringify(dir, null, 2) + '</code></pre>';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Get Service File <small>getFile(service, name, filePath, options = {})</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="getServiceFileServiceName" placeholder="Service Name"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="getServiceFileName" placeholder="Name"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="getServiceFilePath" placeholder="Path"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="getServiceFileOffset" placeholder="Integer offset (optional)"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="getServiceFileLength" placeholder="Integer length (optional)"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="getServiceFile(this)">Get Service File</button>
<p id="getServiceFileArea" class="result-area"></p>
<script class="source">
function getServiceFile(node) {
var name = document.getElementById('getServiceFileName').value,
serviceName = document.getElementById('getServiceFileServiceName').value,
filePath = document.getElementById('getServiceFilePath').value,
options = {
offset: document.getElementById('getServiceFileOffset').value,
length: document.getElementById('getServiceFileLength').value,
};
Safe.dns.getFile(serviceName, name, filePath, options).then(function(file) {
var link = '<a href="http://'+serviceName+'.'+name+'.safenet/'+filePath+'" target="_blank" class="file-link">Link to File</a>';
node.nextElementSibling.innerHTML = "<div>"+link+"<br><br>Raw file: <br /><pre><code>" + encode(file.body) + "</code></pre></div>";
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
</div>
<div class="tab-pane" id="nfs">
<h3>Create Directory <small>createDirectory(dir, options)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="createDirectory" placeholder="Directory"/>
</div>
<div class="form-group">
<label><input type="checkbox" id="isPrivate"> Encrypt containing files (isPrivate)</label>
</div>
<button type="button" class="btn btn-primary submit" onClick="createDirectory(this)">Create Directory</button>
<p id="createDirectoryArea" class="result-area"></p>
<script class="source">
function createDirectory(node) {
var createDirectory = document.getElementById('createDirectory').value,
isPrivate = document.getElementById('isPrivate').checked;
Safe.nfs.createDirectory(createDirectory, {
isPrivate: isPrivate,
metadata: 'some meta data was put here',
isVersioned: true,
isPathShared: window.isPathShared
}).then(function() {
node.nextElementSibling.innerHTML = 'Created directory.';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Get Directory <small>getDirectory(dir, options)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="getDirectory" placeholder="Directory"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="getDirectory(this)">Get Directory</button>
<p id="getDirectoryArea" class="result-area"></p>
<script class="source">
function getDirectory(node) {
var getDirectory = document.getElementById('getDirectory').value;
Safe.nfs.getDirectory(getDirectory, {isPathShared: window.isPathShared}).then(function(dir) {
node.nextElementSibling.innerHTML = '<pre><code>' + JSON.stringify(dir, null, 4) + '</code></pre>';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Delete Directory <small>deleteDirectory(dir, options)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="deleteDirectory" placeholder="Directory"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="deleteDirectory(this)">Delete Directory</button>
<p id="deleteDirectoryArea" class="result-area"></p>
<script class="source">
function deleteDirectory(node) {
var deleteDirectory = document.getElementById('deleteDirectory').value;
Safe.nfs.deleteDirectory(deleteDirectory, {isPathShared: window.isPathShared}).then(function() {
node.nextElementSibling.innerHTML = 'Deleted directory.';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Create File <small>createFile(filePath, options)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="createFile" placeholder="File name"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="createFile(this)">Create File</button>
<p id="createFileArea" class="result-area"></p>
<script class="source">
function createFile(node) {
var createFile = document.getElementById('createFile').value;
Safe.nfs.createFile(createFile, {
metadata: 'some meta data was put here',
isVersioned: true,
isPathShared: window.isPathShared
}).then(function() {
node.nextElementSibling.innerHTML = 'Created file. Now go add content!';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Update File <small>updateFile(filePath, content, options)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="updateFile" placeholder="File name"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="updateFileOffset" placeholder="Integer offset (optional)"/>
</div>
<div class="form-group">
<textarea class="form-control" style="width: 500px;" id="updateFileContents" placeholder="Contents" rows="7"></textarea>
</div>
<button type="button" class="btn btn-primary submit" onClick="updateFile(this)">Update File Contents</button>
<p id="updateFileArea" class="result-area"></p>
<script class="source">
function updateFile(node) {
var updateFile = document.getElementById('updateFile').value;
updateFileContents = document.getElementById('updateFileContents').value;
Safe.nfs.updateFile(updateFile, updateFileContents, {
isPathShared: window.isPathShared,
offset: document.getElementById('updateFileOffset').value
}).then(function() {
node.nextElementSibling.innerHTML = 'Updated file. Try fetching it now.';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Get File <small>getFile(filePath, options)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="getFile" placeholder="File Path"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="getFileOffset" placeholder="Integer offset (optional)"/>
</div>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="getFileLength" placeholder="Integer length (optional)"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="getFile(this)">Get File</button>
<p id="getFileArea" class="result-area"></p>
<script class="source">
function getFile(node) {
var getFile = document.getElementById('getFile').value,
options = {
isPathShared: window.isPathShared,
offset: document.getElementById('getFileOffset').value,
length: document.getElementById('getFileLength').value,
};
Safe.nfs.getFile(getFile, options).then(function(file) {
node.nextElementSibling.innerHTML = '<pre><code>' + encode(file.body) + '</code></pre>';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
<h3>Delete File <small>deleteFile(filePath, options)</small></h3>
<div class="form-group">
<input type="text" class="form-control" class="form-control" id="deleteFile" placeholder="File Path"/>
</div>
<button type="button" class="btn btn-primary submit" onClick="deleteFile(this)">Delete File</button>
<p id="deleteFileArea" class="result-area"></p>
<script class="source">
function deleteFile(node) {
var deleteFile = document.getElementById('deleteFile').value;
Safe.nfs.deleteFile(deleteFile, {isPathShared: window.isPathShared}).then(function() {
node.nextElementSibling.innerHTML = 'Deleted.';
}, addError.bind(null, node.nextElementSibling)).then(finalize.bind(null, node.nextElementSibling));
}
</script>
</div>
</div>
</div>
</div>
<div class="panel">
<div class="panel-body">
Created by Ephi Blanshey<br />
Email: hello@blanshey.com
</div>
</div>
</div>
<script>
// We'll put stuff not related to Safe down here, out of the way
$(document).ready(function() {
restoringForm();
tabRestore();
sourceCodeFunctionality();
initializeSafeOnLoad(); // called after restoring form
});
function initializeSafeOnLoad() {
if ($('#autoAuth').prop('checked')) {
initialize(document.getElementById('initializeButton'));
authorize(document.getElementById('authorizeButton'));
}
}
function sourceCodeFunctionality() {
// add source links
$('#playgroundTabs script').each(function(k, script) {
script = $(script);
// Get js source and make a new box
var source = script.text();
// Add a source link after each script area
var sourceLink = $('<div class="source-link">Source</div>').insertAfter(script);
// Add the (hidden) source code box
$('<div style="display: none"><pre><code>' + encode(source) + '</code></pre></div>').insertAfter(sourceLink);
});
// When source link is clicked, toggle the code area
$('.source-link').click(function() {
$(this).next().toggle();
});
}
// Save which tab we're in case we reload the page
function tabRestore() {
$('a[href="'+(location.hash || 'auth')+'"]').tab('show');
$(document.body).on("click", "a[data-toggle]", function(event) {
location.hash = this.getAttribute("href");
});
}
// Restoring form when returning
function restoringForm() {
var saveData = function(keyName, inputType) {
var valueType = inputType === 'checkbox' ? 'checked' : 'value';
var obj = {};
$('input[type='+inputType+']').each(function(i, el) {
obj[el.getAttribute('id')] = el[valueType];
});
localStorage.setItem(keyName, JSON.stringify(obj));
}
// Save all fields when a button or checkbox is clicked
$('button, input[type=checkbox]').on('change click', function() {
saveData('all-text-inputs', 'text');
saveData('all-checkbox-inputs', 'checkbox');
});
var restoreData = function(keyName, inputType) {
var valueType = inputType === 'checkbox' ? 'checked' : 'value';
var fields = localStorage.getItem(keyName);
if (fields) {
var obj = JSON.parse(fields);
for (var id in obj) {
var el = document.getElementById(id);
if (el) {
el[valueType] = obj[id];
}
}
}
}
restoreData('all-text-inputs', 'text');
restoreData('all-checkbox-inputs', 'checkbox');
window.isPathShared = $('#isPathShared').is(':checked');
}
// html encode (prevents JS from running in result areas)
function encode(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
</script>
</body>
</html>