Skip to content

Commit

Permalink
webdav experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
ondras committed Feb 5, 2014
1 parent 3c790ec commit 56aea71
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SOURCES = src/mm.js \
src/format.mup.js \
src/backend.js \
src/backend.local.js \
src/backend.webdav.js \
src/backend.image.js \
src/backend.file.js \
src/backend.firebase.js \
Expand All @@ -39,6 +40,7 @@ SOURCES = src/mm.js \
src/ui.io.js \
src/ui.backend.js \
src/ui.backend.file.js \
src/ui.backend.webdav.js \
src/ui.backend.image.js \
src/ui.backend.local.js \
src/ui.backend.firebase.js \
Expand Down
18 changes: 18 additions & 0 deletions src/backend.webdav.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
MM.Backend.WebDAV = Object.create(MM.Backend, {
id: {value: "webdav"},
label: {value: "WebDAV"}
});

MM.Backend.WebDAV.save = function(data, url) {
return this._request("post", url, data);
}

MM.Backend.WebDAV.load = function(url) {
return this._request("get", url);
}

MM.Backend.WebDAV._request = function(method, url, data) {
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
return Promise.send(xhr, data);
}
49 changes: 49 additions & 0 deletions src/ui.backend.webdav.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
MM.UI.Backend.WebDAV = Object.create(MM.UI.Backend, {
id: {value: "webdav"}
});

MM.UI.Backend.WebDAV.init = function(select) {
MM.UI.Backend.init.call(this, select);

this._url = this._node.querySelector(".url");
this._url.value = localStorage.getItem(this._prefix + "url") || "";
}

MM.UI.Backend.WebDAV._action = function() {
localStorage.setItem(this._prefix + "url", this._url.value);

MM.UI.Backend._action.call(this);
}

MM.UI.Backend.WebDAV.save = function() {
MM.App.setThrobber(true);

var map = MM.App.map;
var url = this._url.value;
if (url.charCodeAt(url.length-1) != "/") { url += "/"; }
url += map.getName() + MM.Format.JSON.extension;

this._backend.save(map.toJSON(), url).then(
this._saveDone.bind(this),
this._error.bind(this)
);
}

MM.UI.Backend.WebDAV.load = function() {
MM.App.setThrobber(true);

this._backend.load().then(
this._loadDone.bind(this),
this._error.bind(this)
);
}

MM.UI.Backend.File._loadDone = function(xhr) {
try {
var json = MM.Format.JSON.from(xhr.responseText);
} catch (e) {
this._error(e);
}

MM.UI.Backend._loadDone.call(this, json);
}
5 changes: 4 additions & 1 deletion src/ui.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ MM.UI.IO = function() {
this._backend = this._node.querySelector("#backend");
this._currentBackend = null;
this._backends = {};
var ids = ["local", "firebase", "gdrive", "file", "image"];
var ids = ["local", "firebase", "gdrive", "file", "webdav", "image"];
ids.forEach(function(id) {
var ui = MM.UI.Backend.getById(id);
ui.init(this._backend);
Expand All @@ -29,6 +29,9 @@ MM.UI.IO.prototype.restore = function() {
parts[decodeURIComponent(keyvalue[0])] = decodeURIComponent(keyvalue[1]);
});

/* just URL means webdav backend */
if ("url" in parts && !("b" in parts)) { parts.b = "webdav"; }

var backend = MM.UI.Backend.getById(parts.b);
if (backend) { /* saved backend info */
backend.setState(parts);
Expand Down

0 comments on commit 56aea71

Please sign in to comment.