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