Commit b6b09964 authored by Boris Kocherov's avatar Boris Kocherov

erp5_officejs: schema editor: add fetch and upload field on upload page

parent 1c2f03a3
/*global window, rJS, RSVP, loopEventListener, /*global window, rJS, RSVP, loopEventListener,
jIO, document */ jIO, document, URL */
/*jslint nomen: true, indent: 2, maxerr: 3 */ /*jslint nomen: true, indent: 2, maxerr: 3 */
(function (window, rJS, jIO, RSVP) { (function (window, rJS, jIO, RSVP) {
"use strict"; "use strict";
...@@ -10,6 +10,17 @@ ...@@ -10,6 +10,17 @@
Text: 'application/x-asc-text' Text: 'application/x-asc-text'
}; };
function displayError(g, text) {
return g.notifySubmitted({
message: text,
status: 'fail'
})
.push(function () {
// return undefined for fail detection
return;
});
}
function endsWith(str, suffix) { function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1; return str.indexOf(suffix, str.length - suffix.length) !== -1;
} }
...@@ -109,8 +120,54 @@ ...@@ -109,8 +120,54 @@
return form_gadget.getContent(); return form_gadget.getContent();
}) })
.push(function (content) { .push(function (content) {
var data_array,
protocol,
i;
if (!content.data) {
content.data = [];
}
data_array = content.data;
for (i = 0; i < data_array.length; i += 1) {
data_array[i].blob = jIO.util.dataURItoBlob(data_array[i].url);
data_array[i].url = undefined;
}
if (content.data_url) {
protocol = (new URL(content.data_url, gadget.__path)).protocol;
if (protocol === "http:") {
if (window.location.protocol !== protocol) {
return displayError(gadget, "You cannot mixed http and https calls");
}
}
return RSVP.Queue()
.push(function () {
return jIO.util.ajax({
dataType: "blob",
url: content.data_url
});
})
.push(function (evt) {
content.data.push({
blob: evt.target.response,
file_name: content.data_url
});
return content;
}, function () {
return displayError(gadget, 'Download ' + content.data_url + ' Failed');
});
}
return content;
})
.push(function (content) {
if (!content) {
// fail while downloading
return;
}
if (content.data.length === 0) {
return gadget.notifySubmitted({message: 'You need enter data', status: 'fail'});
}
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return RSVP.all([ return RSVP.all([
gadget.getSetting('jio_storage_name'), gadget.getSetting('jio_storage_name'),
gadget.getSetting('jio_storage_description') gadget.getSetting('jio_storage_description')
...@@ -133,7 +190,7 @@ ...@@ -133,7 +190,7 @@
} }
for (i = 0; i < data_array.length; i += 1) { for (i = 0; i < data_array.length; i += 1) {
blob = jIO.util.dataURItoBlob(data_array[i].url); blob = data_array[i].blob;
if (endsWith(data_array[i].file_name, ".zip")) { if (endsWith(data_array[i].file_name, ".zip")) {
configuration = { configuration = {
type: "replicate", type: "replicate",
...@@ -172,7 +229,6 @@ ...@@ -172,7 +229,6 @@
} }
} }
return RSVP.all(promiseArray); return RSVP.all(promiseArray);
});
}) })
.push(function () { .push(function () {
return RSVP.all([ return RSVP.all([
...@@ -180,6 +236,7 @@ ...@@ -180,6 +236,7 @@
gadget.redirect({command: 'history_previous'}) gadget.redirect({command: 'history_previous'})
]); ]);
}); });
});
}) })
.onStateChange(function () { .onStateChange(function () {
...@@ -201,13 +258,24 @@ ...@@ -201,13 +258,24 @@
"title": "Upload files and Zip archive containing files", "title": "Upload files and Zip archive containing files",
"default": "", "default": "",
"css_class": "", "css_class": "",
"required": 1, "required": 0,
"editable": 1, "editable": 1,
"key": "data", "key": "data",
"hidden": 0, "hidden": 0,
"multiple": "true", "multiple": "true",
"accept": "application/zip," + result[1], "accept": "application/zip," + result[1],
"type": "FileField" "type": "FileField"
},
"my_url": {
"description": "",
"title": "Fetch and Upload files and Zip archive containing files",
"default": "",
"css_class": "",
"required": 0,
"editable": 1,
"key": "data_url",
"hidden": 0,
"type": "StringField"
} }
} }
}, },
...@@ -221,7 +289,7 @@ ...@@ -221,7 +289,7 @@
form_definition: { form_definition: {
group_list: [[ group_list: [[
"left", "left",
[["my_file"]] [["my_file"], ["my_url"]]
]] ]]
} }
}); });
......
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