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,9 +19,10 @@ ...@@ -18,9 +19,10 @@
"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; gadget.props.storage_gadget_url = url;
return gadget.dropGadget("storage") return gadget.dropGadget("storage")
.push(function () {}, function () {}) .push(function () {}, function () {})
...@@ -32,25 +34,23 @@ ...@@ -32,25 +34,23 @@
"sandbox": "iframe", "sandbox": "iframe",
"element": gadget.props.element "element": gadget.props.element
.querySelector('.storage-access') .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 {
...@@ -118,28 +100,26 @@ ...@@ -118,28 +100,26 @@
} }
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,7 +127,7 @@ ...@@ -147,7 +127,7 @@
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);
...@@ -155,7 +135,7 @@ ...@@ -155,7 +135,7 @@
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,
...@@ -167,23 +147,22 @@ ...@@ -167,23 +147,22 @@
"-/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;
...@@ -205,8 +184,7 @@ ...@@ -205,8 +184,7 @@
.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) {
...@@ -214,12 +192,12 @@ ...@@ -214,12 +192,12 @@
return g.getElement() return g.getElement()
.push(function (element) { .push(function (element) {
g.props.element = element; g.props.element = element;
}).push(function() { })
.push(function () {
return RSVP.all([ return RSVP.all([
getStorageGadget(g), getStorageGadget(g),
g.render({}) 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