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;
...@@ -40,11 +47,11 @@ function showCommands() { ...@@ -40,11 +47,11 @@ function showCommands() {
command: 'add', command: 'add',
url: url, url: url,
information: new Blob([document.querySelector('#information').value], { information: new Blob([document.querySelector('#information').value], {
type: document.querySelector('#mimetype').value type: document.querySelector('#mimetype').value
}) })
}).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.
...@@ -80,96 +90,135 @@ function showCommands() { ...@@ -80,96 +90,135 @@ function showCommands() {
document.querySelector('#get').addEventListener('click', function() { document.querySelector('#get').addEventListener('click', function() {
console.log("bar"); console.log("bar");
getContent(document.querySelector('#url').value, getContent(document.querySelector('#url').value,
function() { function() {
document.querySelector('#mimetype').value = this.getResponseHeader("content-type"); document.querySelector('#mimetype').value = this.getResponseHeader("content-type");
document.querySelector('#information').value = this.responseText; document.querySelector('#information').value = this.responseText;
} }
); );
}) })
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];
promise_list.push( url = response.target.responseURL.substr(path_to_save_length)
jio.putAttachment('cribjs', response.target.responseURL, extension = getExtension(url)
response.target.response console.log("Pushing attachment " + url + " to " + path_to_save);
) promise_list.push(
); jio.putAttachment("/" + application_id + ".attachment/", btoa(url) + extension,
}; 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 = [],
return new RSVP.Queue() application_id;
.push(function(){ application_id = document.querySelector('#load-id').value
console.log("put"); return new RSVP.Queue()
return jio.allAttachments("cribjs") .push(function() {
}) console.log("put");
.push(function(response){ return jio.allAttachments("/" + application_id + ".attachment/")
var promise_list = [], key; })
for (key in response) { .push(function(response) {
if (response.hasOwnProperty(key)) { var promise_list = [],
console.log(key); key, extension;
url_list.push(key) for (key in response) {
promise_list.push(jio.getAttachment("cribjs", key)); if (response.hasOwnProperty(key)) {
} extension = getExtension(key);
}; console.log(key);
return RSVP.all(promise_list) url_list.push(atob(key.substr(0, key.length - extension.length)))
}) promise_list.push(jio.getAttachment("/" + application_id + ".attachment/", key));
.push(function (response_list) { }
var promise_list = [], i, i_len, url, index, response, location, location_len; };
location = document.location.origin; return RSVP.all(promise_list)
location_len = location.length })
console.log(url_list); .push(function(response_list) {
console.log(response_list); var promise_list = [],
for (i = 0, i_len = response_list.length; i < i_len; i += 1) { i, i_len, url, index, response, location, location_len;
url = url_list[i] location = document.location.origin;
index = url.indexOf(location); location_len = location.length
if(index != -1) console.log(url_list);
url = url.substr(index + location_len); console.log(response_list);
sendMessage({ for (i = 0, i_len = response_list.length; i < i_len; i += 1) {
command: 'add', url = url_list[i]
url: 'crib' + url, index = url.indexOf(location);
information: response_list[i] if (index != -1)
}) url = url.substr(index + location_len);
} sendMessage({
}) command: 'add',
.push(undefined, function (error) { url: document.querySelector('#load-path').value + url,
console.log( error ); information: response_list[i]
}); })
}); }
})
.push(undefined, function(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