safenet
Version:
Low-level javascript API wrapper for communicating with the SAFE Network launcher
643 lines (564 loc) • 32 kB
HTML
<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');
}
// 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>