Commit 0e0e1c9b authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

Add Select Site Option in order to edit other sites

parent 17e5d71f
...@@ -178,9 +178,17 @@ function initHeaderOptions(gadget) { ...@@ -178,9 +178,17 @@ function initHeaderOptions(gadget) {
} }
function getSetting(gadget, key, default_value) { function getSetting(gadget, key, default_value) {
var setting_gadget;
return gadget.getDeclaredGadget("setting_gadget") return gadget.getDeclaredGadget("setting_gadget")
.push(function (jio_gadget) { .push(function (jio_gadget) {
return jio_gadget.get("setting"); setting_gadget = jio_gadget;
return setting_gadget.createJio({
type: "indexeddb",
database: "setting"
});
})
.push(function () {
return setting_gadget.get("setting");
}) })
.push(function (doc) { .push(function (doc) {
return doc[key] || default_value; return doc[key] || default_value;
......
...@@ -4,18 +4,38 @@ ...@@ -4,18 +4,38 @@
"use strict"; "use strict";
function getStorageGadget(gadget) { function getStorageGadget(gadget) {
var storage_gadget; var storage_gadget, site_editor_gadget_url;
return gadget.getDeclaredGadget("storage") return gadget.getSetting(
.push(undefined, function () { "site_editor_gadget_url",
return gadget.declareGadget( "https://cribjs.nexedi.net/gadget/crib-sw-storage-gadget.html"
gadget.props.storage_gadget_url, )
{ .push(function (url) {
"scope": "storage", if (gadget.props.storage_gadget_url == url) {
"sandbox": "iframe", return gadget.getDeclaredGadget("storage")
"element": gadget.props.element.querySelector('div') .push(undefined, function () {
} return gadget.declareGadget(
); url,
}) {
"scope": "storage",
"sandbox": "iframe",
"element": gadget.props.element.querySelector('div')
});
});
} else {
gadget.props.storage_gadget_url = url
return gadget.dropGadget("storage")
.push(function () {}, function () {})
.push(function () {
return gadget.declareGadget(
url,
{
"scope": "storage",
"sandbox": "iframe",
"element": gadget.props.element.querySelector('div')
});
})
}
});
} }
rJS(window) rJS(window)
...@@ -32,7 +52,7 @@ ...@@ -32,7 +52,7 @@
gadget.state_parameter_dict = {}; gadget.state_parameter_dict = {};
return getStorageGadget(gadget); return getStorageGadget(gadget);
}) })
.declareAcquiredMethod("getSetting", "getSetting")
.declareMethod('allDocs', function(params) { .declareMethod('allDocs', function(params) {
return getStorageGadget(this) return getStorageGadget(this)
.push(function (storage_gadget) { .push(function (storage_gadget) {
......
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
</div> </div>
<div class="collapse navbar-collapse"> <div class="collapse navbar-collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li><a href="#page=select_site">Select Site to Edit</a></li>
<li><a href="#page=url_list">URL List</a></li> <li><a href="#page=url_list">URL List</a></li>
<li><a href="#page=editor">Editor</a></li> <li><a href="#page=editor">Editor</a></li>
<li><a href="#page=save_load">Export/Import</a></li> <li><a href="#page=save_load">Export/Import</a></li>
......
...@@ -19,60 +19,6 @@ ...@@ -19,60 +19,6 @@
<body> <body>
<div class="nav_content save_load container"> <div class="nav_content save_load container">
<h3>Storage</h3> <h3>Storage</h3>
<h4>Dav Storage</h4>
<form class="form-horizontal form-use-jio-dav">
<div class="form-group">
<label for="url" class="col-sm-2 control-label">URL</label>
<div class="col-sm-10">
<input type="text" class="form-control url" id="url" value="https://yourdav.node.vifib.com/public/cribjs-storage/">
</div>
</div>
<div class="form-group">
<label for="credential" class="col-sm-2 control-label">Credential</label>
<div class="col-sm-10">
<input type="password" class="form-control credential" id="credential" placeholder="couscous:admin">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Use Dav Storage</button>
</div>
</div>
</form>
<h4>Local Storage</h4>
<form class="form-horizontal form-use-jio-local">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<button class="btn btn-default" type="submit">Local is Enough</button>
</div>
</div>
</form>
<form class="crib-save-to-jio form-inline">
<h3>Export</h3>
<div class="form-group">
<label>Export:
<input class="save-path form-control" name="save-path" type="text" size="30" value=""></label>
</div>
<div class="form-group">
<label> to:
<input name="save-id" class="save-id form-control" type="text" size="30" value="cribjs"></label>
</div>
<button name="save-contents" type="submit" class="btn btn-default">Export</button>
</form>
<div><span class="info crib-save-to-jio-status"></span></div>
<form class="crib-load-from-jio form-inline">
<h3>Import</h3>
<div class="form-group">
<label>Import :
<input name="load-id" class="load-id form-control" type="text" size="30" value="cribjs"></label>
</div>
<div class="form-group">
<label> to path:
<input name="load-path" class="load-path form-control" type="text" size="30" value="cribeditor/v1.1">
</label>
</div>
<button name="load-contents" class="load-contents btn btn-default" type="submit">Import</button>
</form>
<form class="crib-save-to-zip form-inline"> <form class="crib-save-to-zip form-inline">
<h3>Export to Zip</h3> <h3>Export to Zip</h3>
<div class="form-group"> <div class="form-group">
......
...@@ -9,131 +9,6 @@ ...@@ -9,131 +9,6 @@
return ".html"; return ".html";
} }
return "." + extension; return "." + extension;
}
function createJio(gadget, jio_config) {
var jio_gadget;
return gadget.getDeclaredGadget("jio_gadget")
.push(function (jio_g) {
jio_gadget = jio_g;
if (jio_config === undefined)
jio_config = {type: "indexeddb", database: "cribjs"};
return jio_gadget.createJio(jio_config);
});
}
function createDAVJio(gadget, event) {
return createJio(gadget, {
type:"dav",
url: gadget.props.element.querySelector('.form-use-jio-dav .url').value,
basic_login: btoa(gadget.props.element.querySelector(".form-use-jio-dav .credential").value)
});
}
function saveContentToJIO(gadget, event) {
var path_to_save, path_to_save_length, application_id, crib_sw_gadget,
jio_gadget, url_list = [], saved_number = 0;
path_to_save = gadget.props.element.querySelector('form.crib-save-to-jio .save-path').value
application_id = gadget.props.element.querySelector('form.crib-save-to-jio .save-id').value
path_to_save_length = path_to_save.length;
return new RSVP.Queue()
.push(function () {
return RSVP.all([
gadget.getDeclaredGadget('jio_gadget')]);
})
.push(function (gadget_list) {
jio_gadget = gadget_list[0];
return gadget.crib_sw_allDocs({cached_only: true});
})
.push(function(data) {
url_list = data.urls;
// This is buggy, it fails if the document already exists with dav storage
return RSVP.all(
[jio_gadget.put("/" + application_id + ".attachment/", {
// url: path_to_save
}),
jio_gadget.putAttachment("/", application_id, new Blob([JSON.stringify({url: path_to_save})],{type: "application/json"}))
]);
})
.push(function() {
var promise_list = [],
i, i_len, url;
for (i = 0, i_len = url_list.length; i < i_len; i += 1) {
url = new String(url_list[i]);
if (url.indexOf(path_to_save) === 0) {
saved_number += 1;
promise_list.push(gadget.crib_sw_get(url));
};
};
return RSVP.all(promise_list);
})
.push(function(response_list) {
var promise_list = [],
i, i_len, url, response, extension;
for (i = 0, i_len = response_list.length; i < i_len; i += 1) {
response = response_list[i];
url = response.responseURL.substr(path_to_save_length)
extension = getExtension(url)
promise_list.push(
jio_gadget.putAttachment("/" + application_id + ".attachment/", btoa(url) + extension,
// XXX Not sure it would work with images
new Blob([response.responseText], {type : response.responseType})
)
);
};
return RSVP.all(promise_list)
})
.push(function() {
gadget.props.element.querySelector(".crib-save-to-jio-status").textContent = "Saved " + saved_number + " files at "+ Date ()
})
}
function loadContentFromJIO(gadget, event) {
var path_to_load, path_to_load_length, application_id, crib_sw_gadget,
jio_gadget, url_list = [];
path_to_load = gadget.props.element.querySelector('form.crib-load-from-jio .load-path').value
application_id = gadget.props.element.querySelector('form.crib-load-from-jio .load-id').value
path_to_load_length = path_to_load.length;
return new RSVP.Queue()
.push(function () {
return RSVP.all([
gadget.getDeclaredGadget('jio_gadget')]);
})
.push(function (gadget_list) {
jio_gadget = gadget_list[0];
return jio_gadget.allAttachments("/" + application_id + ".attachment/");
})
.push(function(response) {
var promise_list = [],
key, extension;
for (key in response) {
if (response.hasOwnProperty(key)) {
extension = getExtension(key);
url_list.push(atob(key.substr(0, key.length - extension.length)))
promise_list.push(jio_gadget.getAttachment("/" + application_id + ".attachment/", key));
}
};
return RSVP.all(promise_list)
})
.push(function(response_list) {
var promise_list = [],
i, i_len, url, index, response, location, location_len;
location = document.location.origin;
location_len = location.length
for (i = 0, i_len = response_list.length; i < i_len; i += 1) {
url = url_list[i]
index = url.indexOf(location);
if (index != -1)
url = url.substr(index + location_len);
promise_list.push(
gadget.crib_sw_put(path_to_load + url, {blob: response_list[i]})
)
}
})
.push(function() {
gadget.props.element.querySelector(".crib-load-from-jio-status").textContent = "Loaded " + url_list.length + " files at "+ Date ()
});
} }
// Zip Methods // Zip Methods
...@@ -155,10 +30,15 @@ ...@@ -155,10 +30,15 @@
.push(function(data) { .push(function(data) {
var promise_list = [], var promise_list = [],
i, i_len, url; i, i_len, url;
if (Array.isArray(data.urls)) { if ( data.hasOwnProperty("urls") ) {
url_list = data.urls; data = data.urls;
} else { } else {
url_list = Object.keys(data.urls); data = data
}
if (Array.isArray(data)) {
url_list = data;
} else {
url_list = Object.keys(data);
} }
for (i = 0, i_len = url_list.length; i < i_len; i += 1) { for (i = 0, i_len = url_list.length; i < i_len; i += 1) {
url = new String(url_list[i]); url = new String(url_list[i]);
...@@ -176,9 +56,18 @@ ...@@ -176,9 +56,18 @@
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 = url_list[i].substr(path_to_save_length) url = url_list[i].substr(path_to_save_length)
if ( url.endsWith("//") ) {
url = url.substr(0, url.length - 1);
}
if ( url.endsWith("/./") ) {
url = url.substr(0, url.length - 2);
}
if ( url.endsWith("/") ) { if ( url.endsWith("/") ) {
url = url + "index.html"; url = url + "index.html";
} }
if ( url.startsWith("./") ) {
url = url.substr(1);
}
zip.file(url, response) zip.file(url, response)
}; };
return zip.generateAsync({type:"blob"}); return zip.generateAsync({type:"blob"});
...@@ -200,7 +89,7 @@ ...@@ -200,7 +89,7 @@
file_list = gadget.props.element.querySelector('form.crib-load-from-zip .load-zip-file').files file_list = gadget.props.element.querySelector('form.crib-load-from-zip .load-zip-file').files
if (file_list.length === 0) { if (file_list.length === 0) {
gadget.props.element.querySelector(".crib-load-from-zip-status").textContent = "Please put a Zip at "+ Date () gadget.props.element.querySelector(".crib-load-from-zip-status").textContent = "Please put a Zip at "+ Date ()
return return
} }
path_to_load_length = path_to_load.length; path_to_load_length = path_to_load.length;
file = file_list[0] file = file_list[0]
...@@ -217,11 +106,11 @@ ...@@ -217,11 +106,11 @@
return return
} }
if (!relativePath.startsWith("/") && !path_to_load.endsWith("/")) { if (!relativePath.startsWith("/") && !path_to_load.endsWith("/")) {
end_url = path_to_load + "/" + relativePath end_url = path_to_load + "/" + relativePath
} else if (relativePath.startsWith("/") && path_to_load.endsWith("/")) { } else if (relativePath.startsWith("/") && path_to_load.endsWith("/")) {
end_url = path_to_load + relativePath.substring(1); end_url = path_to_load + relativePath.substring(1);
} else { } else {
end_url = path_to_load + relativePath end_url = path_to_load + relativePath
} }
promise_list.push( promise_list.push(
new RSVP.Queue(). new RSVP.Queue().
...@@ -238,7 +127,7 @@ ...@@ -238,7 +127,7 @@
} else if ( end_url.endsWith(".css") ) { } else if ( end_url.endsWith(".css") ) {
// This is a ugly hack as mimetype needs to be correct for JS // This is a ugly hack as mimetype needs to be correct for JS
result = result.slice(0, result.size, "text/css") result = result.slice(0, result.size, "text/css")
} }
return gadget.crib_sw_put(end_url, {blob: result}) return gadget.crib_sw_put(end_url, {blob: result})
}) })
) )
...@@ -249,7 +138,7 @@ ...@@ -249,7 +138,7 @@
gadget.props.element.querySelector(".crib-load-from-zip-status").textContent = "Loaded " + url_number + " files at "+ Date () gadget.props.element.querySelector(".crib-load-from-zip-status").textContent = "Loaded " + url_number + " files at "+ Date ()
}) })
.push(console.log, console.log) .push(console.log, console.log)
} }
rJS(window) rJS(window)
.ready(function (g) { .ready(function (g) {
g.props = {}; g.props = {};
...@@ -268,13 +157,11 @@ ...@@ -268,13 +157,11 @@
if (options === undefined) if (options === undefined)
options = {}; options = {};
gadget.props.options = options; gadget.props.options = options;
gadget.props.element.querySelector('form.crib-save-to-jio .save-path').value = document.location.origin;
gadget.props.element.querySelector('form.crib-load-from-jio .load-path').value = document.location.origin;
gadget.props.element.querySelector('form.crib-save-to-zip .save-zip-path').value = document.location.origin; gadget.props.element.querySelector('form.crib-save-to-zip .save-zip-path').value = document.location.origin;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return gadget.props.start_deferred.resolve(); return gadget.props.start_deferred.resolve();
}) })
}) })
.declareService(function () { .declareService(function () {
var gadget = this; var gadget = this;
...@@ -284,34 +171,6 @@ ...@@ -284,34 +171,6 @@
}) })
.push(function () { .push(function () {
var promise_list = []; var promise_list = [];
// promise to save content to jIO
promise_list.push(loopEventListener(
gadget.props.element.querySelector(".crib-save-to-jio"),
'submit',
false,
function (event) {saveContentToJIO(gadget, event)}
));
// promise to load content from jIO
promise_list.push(loopEventListener(
gadget.props.element.querySelector(".crib-load-from-jio"),
'submit',
false,
function (event) {loadContentFromJIO(gadget, event)}
));
// promise to use dav storage for jIO
promise_list.push(loopEventListener(
gadget.props.element.querySelector(".form-use-jio-dav"),
'submit',
false,
function (event) {createDAVJio(gadget, event)}
));
// promise to use local storage for jIO
promise_list.push(loopEventListener(
gadget.props.element.querySelector(".form-use-jio-local"),
'submit',
false,
function (event) {createJio(gadget, {type: "indexeddb", database: "cribjs"})}
));
// promise to save content to ZIP // promise to save content to ZIP
promise_list.push(loopEventListener( promise_list.push(loopEventListener(
gadget.props.element.querySelector(".crib-save-to-zip"), gadget.props.element.querySelector(".crib-save-to-zip"),
...@@ -328,5 +187,5 @@ ...@@ -328,5 +187,5 @@
)); ));
return RSVP.all(promise_list); return RSVP.all(promise_list);
}); });
}) })
}(window, rJS, loopEventListener, JSZip, FileSaver)); }(window, rJS, loopEventListener, JSZip, FileSaver));
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no" />
<title>CribJS Header</title>
<!-- renderjs -->
<script src="/lib/rsvp.js" type="text/javascript"></script>
<script src="/lib/renderjs.js" type="text/javascript"></script>
<script src="/lib/jszip.js" type="text/javascript"></script>
<script src="/lib/FileSaver.js" type="text/javascript"></script>
<script src="gadget_global.js" type="text/javascript"></script>
<!-- Custom -->
<script src="gadget_cribjs_page_select_site.js" type="text/javascript"></script>
</head>
<body>
<div class="nav_content save_load container">
<h3>Site Editor Gadget URL</h3>
<p>Enter the URL of the Crib Gadget from the site you want to edit. If you want to edit this site use:
"https://cribjs.nexedi.net/gadget/crib-sw-storage-gadget.html"</p>
<div class="row">
<table class="table">
<tr>
<td class="success"><span class="crib-site-save-status"></span></td>
</tr>
</table>
</div>
<div class="row">
<form class="form-horizontal site-editor-gadget-url">
<div class="form-group">
<label for="url" class="col-sm-2 control-label">URL</label>
<div class="col-sm-10">
<input type="text" class="form-control url" id="url" value="https://cribjs.nexedi.net/gadget/crib-sw-storage-gadget.html">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default set-site-gadget-url">Set Site to edit</button>
</div>
</div>
</form>
</div>
</div>
<div data-gadget-url="/gadget/gadget_jio.html"
data-gadget-scope="jio_gadget"
data-gadget-sandbox="public"></div>
</body>
</html>
\ No newline at end of file
/*global window, rJS, loopEventListener */
/*jslint nomen: true, indent: 2, maxerr: 3*/
(function (window, rJS, loopEventListener) {
"use strict";
function setSiteGadgetUrl(gadget, event) {
var site_editor_gadget_url;
site_editor_gadget_url = gadget.props.element.querySelector('form.site-editor-gadget-url .url').value
return gadget.setSetting("site_editor_gadget_url", site_editor_gadget_url)
.push(function () {
gadget.props.element.querySelector(".crib-site-save-status").textContent = "Saved " + site_editor_gadget_url + " files at "+ Date ()
})
}
rJS(window)
.ready(function (g) {
g.props = {};
return g.getElement()
.push(function (element) {
g.props.element = element;
g.props.start_deferred = RSVP.defer();
});
})
.declareAcquiredMethod("getSetting", "getSetting")
.declareAcquiredMethod("setSetting", "setSetting")
.declareMethod('render', function (options) {
var gadget = this;
if (options === undefined)
options = {};
gadget.props.options = options;
return new RSVP.Queue()
.push(function () {
return gadget.getSetting(
"site_editor_gadget_url",
"https://cribjs.nexedi.net/gadget/crib-sw-storage-gadget.html"
)
})
.push(function (site_editor_gadget_url) {
gadget.props.element.querySelector('form.site-editor-gadget-url .url').value = site_editor_gadget_url;
return gadget.props.start_deferred.resolve();
})
})
.declareService(function () {
var gadget = this;
return new RSVP.Queue()
.push(function () {
return gadget.props.start_deferred.promise;
})
.push(function () {
var promise_list = [];
// promise to set Site to Edit
promise_list.push(loopEventListener(
gadget.props.element.querySelector(".site-editor-gadget-url"),
'submit',
false,
function (event) {setSiteGadgetUrl(gadget, event)}
));
return RSVP.all(promise_list);
});
})
}(window, rJS, loopEventListener));
\ No newline at end of file
...@@ -12,7 +12,11 @@ ...@@ -12,7 +12,11 @@
.push(function (data) { .push(function (data) {
var promise_list = [], var promise_list = [],
url; url;
url_list = data.urls; if ( data.hasOwnProperty("urls") ) {
url_list = data.urls;
} else {
url_list = data
}
for (url in url_list) { for (url in url_list) {
if (url_list.hasOwnProperty(url)) { if (url_list.hasOwnProperty(url)) {
promise_list.push(gadget.getUrlFor({page: 'editor', url: url})); promise_list.push(gadget.getUrlFor({page: 'editor', url: url}));
......
...@@ -12,6 +12,10 @@ ...@@ -12,6 +12,10 @@
return "." + extension; return "." + extension;
} }
function getSetting(gadget, key, default_value) {
return default_value;
}
function loadContentFromZIPURL(gadget, zip_url) { function loadContentFromZIPURL(gadget, zip_url) {
var path_to_load, path_to_load_length, file_list, crib_sw_gadget, var path_to_load, path_to_load_length, file_list, crib_sw_gadget,
jio_gadget, url_list = [], url_number = 0; jio_gadget, url_list = [], url_number = 0;
...@@ -92,5 +96,8 @@ ...@@ -92,5 +96,8 @@
}) })
.push(console.log, console.log) .push(console.log, console.log)
}) })
.allowPublicAcquisition("getSetting", function (argument_list) {
return getSetting(this, argument_list[0], argument_list[1]);
})
}(window, document, RSVP, rJS, location, console, JSZip)); }(window, document, RSVP, rJS, location, console, JSZip));
\ No newline at end of file
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