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,9 +19,10 @@
"sandbox": "iframe",
"element": gadget.props.element
.querySelector('.storage-access')
}
);
});
});
} else {
}
gadget.props.storage_gadget_url = url;
return gadget.dropGadget("storage")
.push(function () {}, function () {})
......@@ -32,25 +34,23 @@
"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 {
......@@ -118,28 +100,26 @@
}
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
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);
});
}
function loadCribJSFromZipUrl (gadget, data) {
function loadCribJSFromZipUrl(gadget, data) {
return loadContentFromZIPURL(gadget, {
path: document.location.href,
zip_url: data.zip_url,
......@@ -147,7 +127,7 @@
to_path: data.to_path,
application_id: "cribjs"
})
.push(function (url_list) {
.push(function () {
document.location = data.redirect_url;
})
.push(console.log, console.log);
......@@ -155,7 +135,7 @@
rJS(window)
.declareMethod('render', function (options) {
.declareMethod('render', function () {
var gadget = this,
getURL = window.location,
site = getURL.protocol + "//" + getURL.host,
......@@ -167,23 +147,22 @@
"-/archive/master/cribjs-editor-master.zip",
redirect_url: site
};
if ( params.hasOwnProperty("from_path") ) {
if (params.hasOwnProperty("from_path")) {
data.from_path = params.from_path;
}
if ( params.hasOwnProperty("to_path") ) {
if (params.hasOwnProperty("to_path")) {
data.to_path = params.to_path;
}
if ( params.hasOwnProperty("zip_url") ) {
if (params.hasOwnProperty("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);
}
return RSVP.Queue()
.push(function () {
return loadCribJSFromZipUrl(gadget, data);
})
.fail(function(e){console.log(e)})
});
})
.declareMethod('put', function (url, parameter) {
var blob, gadget = this;
......@@ -205,8 +184,7 @@
.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) {
......@@ -214,12 +192,12 @@
return g.getElement()
.push(function (element) {
g.props.element = element;
}).push(function() {
})
.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