Commit b90b1087 authored by Boris Kocherov's avatar Boris Kocherov

erp5_officejs: form for editing json_document added

parent 2f447aa2
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>OfficeJS Add Json Document</title>
<script src="rsvp.js"></script>
<script src="renderjs.js"></script>
<script src="gadget_erp5_page_ojs_add_json_document.js"></script>
</head>
<body>
</body>
</html>
/*global window, rJS, RSVP */
/*jslint nomen: true, indent: 2, maxerr: 3 */
(function (window, rJS, RSVP, Blob) {
"use strict";
var content_type = {
Spreadsheet: 'application/x-asc-spreadsheet',
Presentation: 'application/x-asc-presentation',
Text: 'application/x-asc-text'
};
var file_ext = {
Spreadsheet: 'xlsy',
Presentation: 'ppty',
Text: 'docy'
};
rJS(window)
/////////////////////////////////////////////////////////////////
// Acquired methods
/////////////////////////////////////////////////////////////////
.declareAcquiredMethod("updateHeader", "updateHeader")
.declareAcquiredMethod("getSetting", "getSetting")
.declareAcquiredMethod("jio_putAttachment", "jio_putAttachment")
.declareAcquiredMethod("redirect", "redirect")
.declareAcquiredMethod("jio_post", "jio_post")
/////////////////////////////////////////////////////////////////
// declared methods
/////////////////////////////////////////////////////////////////
.declareMethod("render", function (options) {
var gadget = this;
return RSVP.Queue()
.push(function () {
var portal_type = options.portal_type,
ext = file_ext[portal_type],
ret = {
title: "Untitled Document",
portal_type: "JSON Document",
schema: options.schema,
parent_relative_url: "document_module",
content_type: content_type[portal_type] || undefined
};
if (ext) {
ret.filename = "default." + ext;
}
return gadget.jio_post(ret);
})
.push(function (id) {
return gadget.jio_putAttachment(id, 'data', new Blob(["{}"]))
.push(function () {
return gadget.redirect({
command: 'display',
options: {
jio_key: id,
editable: true
}
});
});
});
});
}(window, rJS, RSVP, Blob));
......@@ -65,14 +65,14 @@
});
})
.declareMethod("render", function () {
.declareMethod("render", function (options) {
var gadget = this;
return new RSVP.Queue()
.push(function () {
var portal_type = options.portal_type || gadget.getSetting("portal_type");
return RSVP.all([
gadget.getDeclaredGadget('form_list'),
gadget.getSetting("portal_type")
portal_type
]);
})
.push(function (result) {
......@@ -83,7 +83,12 @@
['description', 'Description'],
['version', 'Version'],
['modification_date', 'Modification Date']
];
],
query = 'portal_type:"' + result[1] + '"';
if (options.schema) {
query += ' AND schema:"' + options.schema + '"';
}
query = encodeURIComponent(query);
return result[0].render({
erp5_document: {
"_embedded": {"_view": {
......@@ -96,13 +101,12 @@
"key": "field_listbox",
"lines": 30,
"list_method": "portal_catalog",
"query": "urn:jio:allDocs?query=portal_type%3A%22" +
result[1] + "%22",
"query": "urn:jio:allDocs?query=" + query,
"portal_type": [],
"search_column_list": column_list,
"sort_column_list": column_list,
"sort": [['modification_date', 'descending']],
"title": "Schemas",
"title": options.schema_title || "Schemas",
"type": "ListBox"
}
}},
......@@ -122,17 +126,29 @@
});
})
.push(function () {
return RSVP.all([
gadget.getUrlFor({command: "index", options: {"page": "ojs_multi_upload"}}),
gadget.getSetting('document_title_plural')
]);
var tasks;
if (options.portal_type === "JSON Document") {
tasks = [
gadget.getUrlFor({command: "index", options: {
page: "ojs_add_json_document",
schema: options.schema
}}),
gadget.getSetting('document_title_plural')
];
} else {
tasks = [
gadget.getUrlFor({command: "index", options: {"page": "ojs_multi_upload"}}),
gadget.getSetting('document_title_plural')
];
}
return RSVP.all(tasks);
})
.push(function (result) {
return gadget.updateHeader({
page_title: result[1],
page_title: options.schema_title || result[1],
filter_action: true,
add_url: result[0]
});
});
});
}(window, rJS, RSVP));
}(window, rJS, RSVP));
\ No newline at end of file
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>OfficeJS Jio Document View</title>
<script src="rsvp.js"></script>
<script src="renderjs.js"></script>
<script src="handlebars.js"></script>
<script src="gadget_officejs_jio_json_document_view.js"></script>
</head>
<body>
<form class="save_form ui-body-c" novalidate>
<button type="submit" class="ui-btn ui-btn-b ui-btn-inline
ui-icon-edit ui-btn-icon-right ui-screen-hidden"></button>
<div data-gadget-url="gadget_erp5_form.html"
data-gadget-scope="form_view"
data-gadget-sandbox="public">
</div>
</form>
</body>
</html>
/*global window, rJS, RSVP, jIO, URL,
promiseEventListener, document*/
/*jslint nomen: true, indent: 2, maxerr: 3 */
(function (window, jIO, rJS, RSVP) {
"use strict";
rJS(window)
/////////////////////////////////////////////////////////////////
// Acquired methods
/////////////////////////////////////////////////////////////////
.declareAcquiredMethod("updateHeader", "updateHeader")
.declareAcquiredMethod("getUrlParameter", "getUrlParameter")
.declareAcquiredMethod("getUrlFor", "getUrlFor")
.declareAcquiredMethod("updateDocument", "updateDocument")
.declareAcquiredMethod("notifySubmitting", "notifySubmitting")
.declareAcquiredMethod("notifySubmitted", 'notifySubmitted')
.declareAcquiredMethod("redirect", "redirect")
.declareAcquiredMethod("jio_get", "jio_get")
.declareAcquiredMethod("jio_put", "jio_put")
.declareAcquiredMethod("jio_allDocs", "jio_allDocs")
.declareAcquiredMethod("jio_getAttachment", "jio_getAttachment")
.declareAcquiredMethod("jio_putAttachment", "jio_putAttachment")
/////////////////////////////////////////////////////////////////
// declared methods
/////////////////////////////////////////////////////////////////
.declareMethod("render", function (options) {
var gadget = this;
return gadget.changeState({
jio_key: options.jio_key,
doc: options.doc
});
})
.onEvent('submit', function () {
var gadget = this;
return gadget.notifySubmitting()
.push(function () {
return gadget.getDeclaredGadget('form_view');
})
.push(function (form_gadget) {
return form_gadget.getContent();
})
.push(function (content) {
var list = [],
blob;
if (content.file) {
blob = jIO.util.dataURItoBlob(content.file.url);
content.title = content.file.file_name;
delete content.file;
list = [
gadget.updateDocument(content),
gadget.jio_putAttachment(gadget.state.jio_key, 'data', blob)
];
} else if (content.data) {
blob = new Blob([content.data]);
delete content.data;
list = [
gadget.updateDocument(content),
gadget.jio_putAttachment(gadget.state.jio_key, 'data', blob)
];
} else {
list = [gadget.updateDocument(content)];
}
return RSVP.all(list);
})
.push(function () {
return gadget.notifySubmitted({message: 'Data Updated', status: 'success'});
}, function (error) {
if (error.target && error.target.error.name === 'NotReadableError') {
return gadget.notifySubmitted({message: error.target.error.message, status: 'fail'});
}
throw error;
}).push(function () {
// debugger;
return gadget.redirect({
command: 'reload'
});
});
})
.declareMethod("triggerSubmit", function () {
return this.element.querySelector('button[type="submit"]').click();
})
.allowPublicAcquisition("downloadJSON", function (arr) {
var g = this,
url = arr[0],
reference,
args;
// return g.jio_getAttachment(id, "data", {format: "json"});
if (url.startsWith("urn:jio:reference?")) {
reference = decodeURIComponent(url.replace("urn:jio:reference?", ""));
args = {
query: '(portal_type: "Json Schema") AND ((reference: "' + reference + '"))',
limit: [0, 1],
select_list: [],
sort_on: [["modification_date", "descending"]]
};
return g.jio_allDocs(args)
.push(function (result) {
return g.jio_getAttachment(result.data.rows[0].id, "data", {format: "json"});
});
}
})
.onStateChange(function () {
var gadget = this,
schema_obj;
return RSVP.Queue()
.push(function () {
return RSVP.all([
gadget.getDeclaredGadget('form_view'),
gadget.jio_getAttachment(gadget.state.jio_key, 'data', {format: "json"}),
gadget.jio_get(gadget.state.doc.schema)
]);
})
.push(function (result) {
schema_obj = result[2];
return result[0].render({
erp5_document: {
"_embedded": {
"_view": {
"my_title": {
"description": "",
"title": "Title",
"default": gadget.state.doc.title,
"css_class": "",
"required": 1,
"editable": 1,
"key": "title",
"hidden": 0,
"type": "StringField"
},
"my_file": {
"description": "",
"title": "Upload",
"default": "",
"css_class": "",
"required": 0,
"editable": 1,
"key": "file",
"hidden": 0,
"accept": "application/json",
"type": "FileField"
},
"my_content": {
"default": result[1],
"css_class": "",
"required": 0,
"editable": 1,
"key": "data",
"hidden": 0,
"type": "GadgetField",
"renderjs_extra": '{"name": "data",' +
' "schema_url": "urn:jio:reference?' + schema_obj.reference + '"}',
"url": "jsonform.gadget.html",
"sandbox": "public"
}
}
},
"_links": {
"type": {
// form_list display portal_type in header
name: ""
}
}
},
form_definition: {
group_list: [[
"left",
[["my_title"], ["my_reference"], ["my_file"]]
], [
"bottom",
[["my_content"]]
]]
}
});
})
.push(function () {
return RSVP.all([
gadget.getUrlFor({command: 'display', options: {
page: "ojs_schema_document_list",
portal_type: "JSON Document",
schema: gadget.state.doc.schema,
schema_title: schema_obj.title
}}),
gadget.getUrlFor({command: 'selection_previous'}),
gadget.getUrlFor({command: 'selection_next'})
]);
})
.push(function (url_list) {
return gadget.updateHeader({
page_title: gadget.state.doc.title,
save_action: true,
selection_url: url_list[0],
previous_url: url_list[1],
next_url: url_list[2]
});
});
});
}(window, jIO, rJS, RSVP));
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