Commit 69199231 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

Possibility to save and load from Remote Dav with jIO

parent b5ef611c
...@@ -24,15 +24,22 @@ if ('serviceWorker' in navigator) { ...@@ -24,15 +24,22 @@ if ('serviceWorker' in navigator) {
document.querySelector('#status').appendChild(aElement); document.querySelector('#status').appendChild(aElement);
} }
jio = jIO.createJIO({ //jio = jIO.createJIO({type: "indexeddb", database: "cribjs"});
type: "indexeddb",
database: "cribjs" jio = jIO.createJIO({type:"dav", url: "https://cedriclendav.node.vifib.com/toto", basic_login: btoa("cedriclen:foo")})
})
function setStatus(statusMessage) { function setStatus(statusMessage) {
document.querySelector('#status').textContent = statusMessage; document.querySelector('#status').textContent = statusMessage;
} }
function getExtension(url) {
var extension = url.split('.').pop();
if (extension === "/") {
return ".html";
}
return "." + extension;
}
function showCommands() { function showCommands() {
document.querySelector('#add').addEventListener('click', function() { document.querySelector('#add').addEventListener('click', function() {
var url = document.querySelector('#url').value; var url = document.querySelector('#url').value;
...@@ -44,7 +51,7 @@ function showCommands() { ...@@ -44,7 +51,7 @@ function showCommands() {
}) })
}).then(function() { }).then(function() {
// If the promise resolves, just display a success message. // If the promise resolves, just display a success message.
setStatus('Added to cache: ' + url + ' at ' +Date()); setStatus('Added to cache: ' + url + ' at ' + Date());
}).catch(setStatus); // If the promise rejects, then setStatus will be called with the error. }).catch(setStatus); // If the promise rejects, then setStatus will be called with the error.
}); });
...@@ -70,8 +77,11 @@ function showCommands() { ...@@ -70,8 +77,11 @@ function showCommands() {
// Add each cached URL to the list, one by one. // Add each cached URL to the list, one by one.
data.urls.forEach(function(url) { data.urls.forEach(function(url) {
var liElement = document.createElement('li'); var liElement = document.createElement('li'),
liElement.textContent = url; aElement = document.createElement('a');
aElement.setAttribute('href', url);
aElement.textContent = url;
liElement.innerHTML = aElement.outerHTML;
contentsElement.appendChild(liElement); contentsElement.appendChild(liElement);
}); });
}).catch(setStatus); // If the promise rejects, then setStatus will be called with the error. }).catch(setStatus); // If the promise rejects, then setStatus will be called with the error.
...@@ -87,69 +97,91 @@ function showCommands() { ...@@ -87,69 +97,91 @@ function showCommands() {
); );
}) })
document.querySelector('#save-path').value = document.location.origin;
document.querySelector('#save-contents').addEventListener('click', function() { document.querySelector('#save-contents').addEventListener('click', function() {
console.log('asking for the keys'); console.log('asking for the keys');
var path_to_save, path_to_save_length, application_id;
path_to_save = document.querySelector('#save-path').value
application_id = document.querySelector('#save-id').value
path_to_save_length = path_to_save.length
sendMessage({ sendMessage({
command: 'keys' command: 'keys'
}).then(function(data) { }).then(function(data) {
// Add each cached URL to the list, one by one. // Add each cached URL to the list, one by one.
console.log("foo"); console.log("foo");
return new RSVP.Queue() return new RSVP.Queue()
.push(function(){ .push(function() {
console.log("put"); console.log("put");
return jio.put("cribjs", {}) return RSVP.all(
[jio.put("/" + application_id + ".attachment/", {
// url: path_to_save
}),
jio.putAttachment("/", application_id, new Blob([JSON.stringify({url: path_to_save})],{type: "application/json"}))
]);
}) })
.push(function(){ .push(function() {
var promise_list = [], i, i_len, url; var promise_list = [],
i, i_len, url;
for (i = 0, i_len = data.urls.length; i < i_len; i += 1) { for (i = 0, i_len = data.urls.length; i < i_len; i += 1) {
url = new String(data.urls[i]); url = new String(data.urls[i]);
console.log(url); if (url.indexOf(path_to_save) === 0) {
promise_list.push(jIO.util.ajax({'url': url, dataType: "blob"})) promise_list.push(jIO.util.ajax({
'url': url,
dataType: "blob"
}));
}
}; };
return RSVP.all(promise_list) return RSVP.all(promise_list)
}) })
.push(function (response_list) { .push(function(response_list) {
console.log(response_list); var promise_list = [],
var promise_list = [], i, i_len, url, response; i, i_len, url, response, extension;
for (i = 0, i_len = response_list.length; i < i_len; i += 1) { for (i = 0, i_len = response_list.length; i < i_len; i += 1) {
response = response_list[i]; response = response_list[i];
url = response.target.responseURL.substr(path_to_save_length)
extension = getExtension(url)
console.log("Pushing attachment " + url + " to " + path_to_save);
promise_list.push( promise_list.push(
jio.putAttachment('cribjs', response.target.responseURL, jio.putAttachment("/" + application_id + ".attachment/", btoa(url) + extension,
response.target.response response.target.response
) )
); );
}; };
return RSVP.all(promise_list) return RSVP.all(promise_list)
}) })
.push(undefined, function (error) { .push(undefined, function(error) {
console.log( error ); console.log(error);
}); });
}).catch(setStatus); // If the promise rejects, then setStatus will be called with the error. }).catch(setStatus); // If the promise rejects, then setStatus will be called with the error.
}); });
document.querySelector('#load-path').value = document.location.origin;
document.querySelector('#load-contents').addEventListener('click', function() { document.querySelector('#load-contents').addEventListener('click', function() {
console.log('asking for the keys'); console.log('asking for the keys');
var url_list = []; var url_list = [],
application_id;
application_id = document.querySelector('#load-id').value
return new RSVP.Queue() return new RSVP.Queue()
.push(function(){ .push(function() {
console.log("put"); console.log("put");
return jio.allAttachments("cribjs") return jio.allAttachments("/" + application_id + ".attachment/")
}) })
.push(function(response){ .push(function(response) {
var promise_list = [], key; var promise_list = [],
key, extension;
for (key in response) { for (key in response) {
if (response.hasOwnProperty(key)) { if (response.hasOwnProperty(key)) {
extension = getExtension(key);
console.log(key); console.log(key);
url_list.push(key) url_list.push(atob(key.substr(0, key.length - extension.length)))
promise_list.push(jio.getAttachment("cribjs", key)); promise_list.push(jio.getAttachment("/" + application_id + ".attachment/", key));
} }
}; };
return RSVP.all(promise_list) return RSVP.all(promise_list)
}) })
.push(function (response_list) { .push(function(response_list) {
var promise_list = [], i, i_len, url, index, response, location, location_len; var promise_list = [],
i, i_len, url, index, response, location, location_len;
location = document.location.origin; location = document.location.origin;
location_len = location.length location_len = location.length
console.log(url_list); console.log(url_list);
...@@ -157,20 +189,37 @@ function showCommands() { ...@@ -157,20 +189,37 @@ function showCommands() {
for (i = 0, i_len = response_list.length; i < i_len; i += 1) { for (i = 0, i_len = response_list.length; i < i_len; i += 1) {
url = url_list[i] url = url_list[i]
index = url.indexOf(location); index = url.indexOf(location);
if(index != -1) if (index != -1)
url = url.substr(index + location_len); url = url.substr(index + location_len);
sendMessage({ sendMessage({
command: 'add', command: 'add',
url: 'crib' + url, url: document.querySelector('#load-path').value + url,
information: response_list[i] information: response_list[i]
}) })
} }
}) })
.push(undefined, function (error) { .push(undefined, function(error) {
console.log( error ); console.log(error);
}); });
}); });
document.querySelector('#mass-remove').addEventListener('click', function() {
var url_list = document.querySelector('#mass-remove-list').value.match(/[^\r\n]+/g),
url_list_length = url_list.length,
i;
console.log(url_list)
for (i = 0; i < url_list_length; i += 1) {
console.log(url_list[i])
sendMessage({
command: 'delete',
url: url_list[i],
}).then(function() {
// If the promise resolves, just display a success message.
setStatus('Deleted from cache.');
}).catch(setStatus); // If the promise rejects, then setStatus will be called with the error.
}
});
document.querySelector('#commands').style.display = 'block'; document.querySelector('#commands').style.display = 'block';
} }
......
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
...@@ -11,7 +12,6 @@ ...@@ -11,7 +12,6 @@
<script src="lib/sha256.js"></script> <script src="lib/sha256.js"></script>
<script src="lib/sha256.amd.js"></script> <script src="lib/sha256.amd.js"></script>
<script src="lib/rsvp.js"></script> <script src="lib/rsvp.js"></script>
<script src="lib/utils/promise.js"></script>
<script src="lib/jio-latest.js"></script> <script src="lib/jio-latest.js"></script>
<!--[if lt IE 9]> <!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
<div id="status"></div> <div id="status"></div>
<div id="commands" style="display: none"> <div id="commands" style="display: none">
<h3>Edit</h3>
<div> <div>
<label for="url">Resource URL:</label> <label for="url">Resource URL:</label>
<input id="url" type="text" size="50" value="test.html"> <input id="url" type="text" size="50" value="test.html">
...@@ -43,11 +44,33 @@ ...@@ -43,11 +44,33 @@
<button id="add">Add to Cache</button> <button id="add">Add to Cache</button>
<button id="delete">Delete from Cache</button> <button id="delete">Delete from Cache</button>
</div> </div>
<h3>Save</h3>
<div> <div>
<button id="list-contents">List Cache Contents</button> <label for="save-path">Save:</label>
<input id="save-path" type="text" size="30" value="">
<label for="save-id"> to:</label>
<input id="save-id" type="text" size="30" value="cribjs">
<button id="save-contents">Save Cache</button> <button id="save-contents">Save Cache</button>
</div>
<h3>Load</h3>
<div>
<label for="load-id">Load :</label>
<input id="load-id" type="text" size="30" value="cribjs">
<label for="load-path"> to path:</label>
<input id="load-path" type="text" size="30" value="cribeditor/v1.1">
<button id="load-contents">Load Cache</button> <button id="load-contents">Load Cache</button>
</div> </div>
<h3>Mass removal</h3>
<div>
<textarea id="mass-remove-list" cols="35" wrap="soft"></textarea>
</div>
<div>
<button id="mass-remove">Mass remove from Cache</button>
</div>
<h3>List Cache content</h3>
<div>
<button id="list-contents">List Cache Contents</button>
</div>
<ul id="contents"></ul> <ul id="contents"></ul>
</div> </div>
</div> </div>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment