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

Improve landing code

parent 5fb66a36
/*jslint nomen: true, indent: 2, maxerr: 3 */ /*jslint nomen: true, indent: 2, maxerr: 3 */
/*global window, rJS */ /*global window, rJS, RSVP, jIO, fetch, Promise, document, console, Blob
, JSZip */
(function (rJS, RSVP, JSZip, jIO) { (function (rJS, RSVP, JSZip, jIO) {
"use strict"; "use strict";
function getStorageGadget(gadget) { function getStorageGadget(gadget) {
var storage_gadget, site_editor_gadget_url, getURL = window.location; var getURL = window.location;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
var url = getURL.protocol + "//" + getURL.host + "/crib-enable.html"; var url = getURL.protocol + "//" + getURL.host + "/crib-enable.html";
if (gadget.props.storage_gadget_url == url) { if (gadget.props.storage_gadget_url === url) {
return gadget.getDeclaredGadget("storage") return gadget.getDeclaredGadget("storage")
.push(undefined, function () { .push(undefined, function () {
return gadget.declareGadget( return gadget.declareGadget(
...@@ -18,39 +19,38 @@ ...@@ -18,39 +19,38 @@
"sandbox": "iframe", "sandbox": "iframe",
"element": gadget.props.element "element": gadget.props.element
.querySelector('.storage-access') .querySelector('.storage-access')
}); }
}); );
} 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('.storage-access')
});
}); });
} }
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('.storage-access')
}
);
});
}) })
.push(undefined, function (e) { .push(undefined, function (e) {
// Ugly Hack to reload page and make service worker available // Ugly Hack to reload page and make service worker available
if (e && if (e &&
e.indexOf("Please reload this page to allow Service Worker to control this page") > -1) { e.toString()
console.log("reload"); .indexOf("Please reload this page to allow Service Worker to control this page") > -1) {
window.location.reload(false); window.location.reload(false);
throw (e); throw e;
} else {
console.log(e);
throw (e);
} }
throw e;
}); });
} }
function getParameterDict () { function getParameterDict() {
var hash = window.location.hash.substring(1), var hash = window.location.hash.substring(1),
params = {}; params = {};
hash.split('&').map(hk => { hash.split('&').map(hk => {
...@@ -59,28 +59,11 @@ ...@@ -59,28 +59,11 @@
}); });
return params; return params;
} }
function getExtension(url) {
var extension = url.split('.').pop();
if (extension.endsWith('/')) {
return ".html";
}
return "." + extension;
}
function getSetting(gadget, key, default_value) {
if (key === "site_editor_gadget_url") {
return window.location.protocol + "//" + window.location.host +
"/crib-enable.html";
}
return default_value;
}
function loadZipIntoCrib(gadget, zip, from_path, path_to_load) { function loadZipIntoCrib(gadget, zip, from_path) {
var promise_list = [], url_number = 0, var promise_list = [];
site_url = window.location.protocol + "//" + window.location.host;
zip.forEach(function (relativePath, zipEntry) { zip.forEach(function (relativePath, zipEntry) {
var end_url; var end_url;
url_number += 1;
if (zipEntry.dir) { if (zipEntry.dir) {
return; return;
} }
...@@ -88,7 +71,6 @@ ...@@ -88,7 +71,6 @@
return; return;
} }
relativePath = relativePath.substring(from_path.length); relativePath = relativePath.substring(from_path.length);
console.log(relativePath);
if (relativePath.startsWith("/")) { if (relativePath.startsWith("/")) {
end_url = relativePath.substring(1); end_url = relativePath.substring(1);
} else { } else {
...@@ -96,50 +78,48 @@ ...@@ -96,50 +78,48 @@
} }
promise_list.push( promise_list.push(
new RSVP.Queue() new RSVP.Queue()
.push(function () { .push(function () {
return zipEntry.async('blob'); return zipEntry.async('blob');
}) })
.push(function (result) { .push(function (result) {
if (end_url.endsWith(".js")) { if (end_url.endsWith(".js")) {
// 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, "application/javascript"); result = result.slice(0, result.size, "application/javascript");
} else if (end_url.endsWith(".html")) { } else if (end_url.endsWith(".html")) {
// 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/html"); result = result.slice(0, result.size, "text/html");
} 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.put(end_url, {blob: result}); return gadget.put(end_url, {blob: result});
}) })
); );
}); });
return RSVP.all(promise_list); return RSVP.all(promise_list);
} }
function loadContentFromZIPURL(gadget, options) { function loadContentFromZIPURL(gadget, options) {
var path_to_load = options.to_path, file_list, var path_to_load = options.to_path,
from_path = options.from_path, zip_url = options.zip_url, from_path = options.from_path,
url_list = [], url_number = 0; zip_url = options.zip_url;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return fetch(zip_url) return fetch(zip_url)
.then(function (response) { // 2) filter on 200 OK .then(function (response) { // 2) filter on 200 OK
if (response.status === 200 || response.status === 0) { if (response.status === 200 || response.status === 0) {
return Promise.resolve(response.blob()); return Promise.resolve(response.blob());
} else {
return Promise.reject(new Error(response.statusText));
} }
return Promise.reject(new Error(response.statusText));
}); });
}) })
.push(JSZip.loadAsync) .push(JSZip.loadAsync)
.push(function (zip) { .push(function (zip) {
return loadZipIntoCrib(gadget, zip, from_path, path_to_load); return loadZipIntoCrib(gadget, zip, from_path, path_to_load);
}) });
.push(console.log, console.log);
} }
function loadCribJSFromZipUrl (gadget, data) { function loadCribJSFromZipUrl(gadget, data) {
return loadContentFromZIPURL(gadget, { return loadContentFromZIPURL(gadget, {
path: document.location.href, path: document.location.href,
zip_url: data.zip_url, zip_url: data.zip_url,
...@@ -147,16 +127,16 @@ ...@@ -147,16 +127,16 @@
to_path: data.to_path, to_path: data.to_path,
application_id: "cribjs" application_id: "cribjs"
}) })
.push(function (url_list) { .push(function () {
document.location = data.redirect_url; document.location = data.redirect_url;
}) })
.push(console.log, console.log); .push(console.log, console.log);
} }
rJS(window) rJS(window)
.declareMethod('render', function (options) { .declareMethod('render', function () {
var gadget = this, var gadget = this,
getURL = window.location, getURL = window.location,
site = getURL.protocol + "//" + getURL.host, site = getURL.protocol + "//" + getURL.host,
params = getParameterDict(), params = getParameterDict(),
...@@ -167,24 +147,23 @@ ...@@ -167,24 +147,23 @@
"-/archive/master/cribjs-editor-master.zip", "-/archive/master/cribjs-editor-master.zip",
redirect_url: site redirect_url: site
}; };
if ( params.hasOwnProperty("from_path") ) { if (params.hasOwnProperty("from_path")) {
data.from_path = params.from_path; data.from_path = params.from_path;
} }
if ( params.hasOwnProperty("to_path") ) { if (params.hasOwnProperty("to_path")) {
data.to_path = params.to_path; data.to_path = params.to_path;
} }
if ( params.hasOwnProperty("zip_url") ) { if (params.hasOwnProperty("zip_url")) {
data.zip_url = params.zip_url; data.zip_url = params.zip_url;
} }
if ( params.hasOwnProperty("redirect_url") ) { if (params.hasOwnProperty("redirect_url")) {
data.redirect_url = decodeURIComponent(params.redirect_url); data.redirect_url = decodeURIComponent(params.redirect_url);
} }
return RSVP.Queue() return RSVP.Queue()
.push(function () { .push(function () {
return loadCribJSFromZipUrl(gadget, data); return loadCribJSFromZipUrl(gadget, data);
});
}) })
.fail(function(e){console.log(e)})
})
.declareMethod('put', function (url, parameter) { .declareMethod('put', function (url, parameter) {
var blob, gadget = this; var blob, gadget = this;
if (parameter.blob !== undefined) { if (parameter.blob !== undefined) {
...@@ -205,21 +184,20 @@ ...@@ -205,21 +184,20 @@
.push(function (result_list) { .push(function (result_list) {
return result_list[0].put(url, result_list[1].target.result); return result_list[0].put(url, result_list[1].target.result);
}) })
.push(console.log, console.log) .push(console.log, console.log);
;
}) })
.ready(function (g) { .ready(function (g) {
g.props = {}; g.props = {};
return g.getElement() return g.getElement()
.push(function (element) { .push(function (element) {
g.props.element = element; g.props.element = element;
}).push(function() { })
return RSVP.all([ .push(function () {
getStorageGadget(g), return RSVP.all([
g.render({}) getStorageGadget(g),
]); g.render({})
]);
});
}); });
});
}(rJS, RSVP, JSZip, jIO)); }(rJS, RSVP, JSZip, jIO));
\ 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