Commit 30ec359f authored by Roque's avatar Roque

erp5_officejs: moving json-form render method to a main gadget (controller for now)

parent 97b5c80f
...@@ -15,6 +15,73 @@ ...@@ -15,6 +15,73 @@
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// declared methods // declared methods
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
.declareMethod("getFormDefinition", function () {
//somehow the form_definition should come from the erp5-doc/form (jio?)
//for now, hardcoded form_definition for POST VIEW
return {
_debug: "traverse",
pt: "form_view",
title: "Post",
group_list: [[
"left",
[["my_title", {meta_type: "StringField"}]]
], [
"bottom",
[["my_text_content", {meta_type: "ProxyField"}]]
]],
//this field_info is totally made up, but somewhere in the definition there must be
//information about the fields. So, foreach field: key->info
field_info_dict: {
"my_title": {
"title": "Title",
"default": "Undefined title",
"editable": 1,
"key": "title",
"type": "StringField"
},
"my_text_content": {
"editable": 1,
"key": "text_content",
"renderjs_extra": '{"editor": "fck_editor",' +
'"maximize": true}',
"type": "GadgetField",
"url": "gadget_editor.html",
"sandbox": "public"
}
},
action: "Base_edit",
update_action: "",
_links: {}
};
})
.declareMethod("generateJsonRenderForm", function (form_definition, document) {
var form_json = {
erp5_document: {
"_embedded": {"_view": {}},
"_links": {}
},
form_definition: form_definition
};
for (var i = 0; i < form_definition.group_list.length; i++) {
var fields = form_definition.group_list[i][1];
for (var j = 0; j < fields.length; j++) {
var my_element = fields[j][0], element_id;
if (my_element.startsWith("my_")) {
element_id = my_element.replace("my_", "");
}
var field_info = form_definition.field_info_dict[my_element];
if (document && document.hasOwnProperty(element_id)) {
field_info["default"] = document[element_id];
}
form_json.erp5_document._embedded._view[my_element] = field_info;
form_json.erp5_document._links = form_definition._links;
}
}
return form_json;
})
.allowPublicAcquisition('notifySubmit', function () { .allowPublicAcquisition('notifySubmit', function () {
return this.triggerSubmit(); return this.triggerSubmit();
}) })
...@@ -41,10 +108,8 @@ ...@@ -41,10 +108,8 @@
.declareMethod("render", function (options) { .declareMethod("render", function (options) {
var gadget = this, var gadget = this,
child_gadget_url; child_gadget_url;
return gadget.jio_get(options.jio_key) return gadget.jio_get(options.jio_key)
.push(function (result) { .push(function (result) {
if (result.portal_type !== undefined) { if (result.portal_type !== undefined) {
child_gadget_url = 'gadget_officejs_jio_' + child_gadget_url = 'gadget_officejs_jio_' +
result.portal_type.replace(/ /g, '_').toLowerCase() + result.portal_type.replace(/ /g, '_').toLowerCase() +
...@@ -52,38 +117,47 @@ ...@@ -52,38 +117,47 @@
} else { } else {
throw new Error('Can not display document: ' + options.jio_key); throw new Error('Can not display document: ' + options.jio_key);
} }
//somehow the form_definition should come from the erp5-doc/form (jio?)
return gadget.getFormDefinition(options.jio_key)
.push(function (form_definition) {
return gadget.changeState({ return gadget.changeState({
jio_key: options.jio_key, jio_key: options.jio_key,
doc: result, doc: result,
child_gadget_url: child_gadget_url child_gadget_url: child_gadget_url,
form_definition: form_definition
});
}); });
}); });
}) })
.onStateChange(function (modification_dict) { .onStateChange(function (modification_dict) {
var fragment = document.createElement('div'), var fragment = document.createElement('div'),
gadget = this; gadget = this;
return gadget.generateJsonRenderForm(gadget.state.form_definition, gadget.state.doc)
.push(function (form_json) {
if (!modification_dict.hasOwnProperty('child_gadget_url')) { if (!modification_dict.hasOwnProperty('child_gadget_url')) {
return gadget.getDeclaredGadget('fg') return gadget.getDeclaredGadget('fg')
.push(function (child_gadget) { .push(function (child_gadget) {
return child_gadget.render({ return child_gadget.render({
jio_key: gadget.state.jio_key, jio_key: gadget.state.jio_key,
doc: gadget.state.doc doc: gadget.state.doc,
form_json: form_json
}); });
}); });
} }
// Clear first to DOM, append after to reduce flickering/manip // Clear first to DOM, append after to reduce flickering/manip
while (this.element.firstChild) { while (gadget.element.firstChild) {
this.element.removeChild(this.element.firstChild); gadget.element.removeChild(gadget.element.firstChild);
} }
this.element.appendChild(fragment); gadget.element.appendChild(fragment);
return gadget.declareGadget(gadget.state.child_gadget_url, {element: fragment, return gadget.declareGadget(gadget.state.child_gadget_url, {element: fragment,
scope: 'fg'}) scope: 'fg'})
.push(function (form_gadget) { .push(function (form_gadget) {
return form_gadget.render({ return form_gadget.render({
jio_key: gadget.state.jio_key, jio_key: gadget.state.jio_key,
doc: gadget.state.doc doc: gadget.state.doc,
form_json: form_json
});
}); });
}); });
}); });
......
...@@ -227,7 +227,7 @@ ...@@ -227,7 +227,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>967.6016.61211.59613</string> </value> <value> <string>973.30677.12698.50312</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -245,7 +245,7 @@ ...@@ -245,7 +245,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1524754712.51</float> <float>1549379093.01</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -18,72 +18,11 @@ ...@@ -18,72 +18,11 @@
// declared methods // declared methods
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
.declareMethod("generateJsonRenderForm", function (gadget) {
//hardcoded form_definition (this should come from erp5 form)
var form_definition = {
_debug: "traverse",
pt: "form_view",
title: "Post",
group_list: [[
"left",
[["my_title", {meta_type: "StringField"}]]
], [
"bottom",
[["my_text_content", {meta_type: "ProxyField"}]]
]],
//this field_info is totally made up, but somewhere in the definition there must be
//information about the fields. So, foreach field: key->info
field_info_dict: {
"my_title": {
"title": "Title",
"default": "Undefined title",
"editable": 1,
"key": "title",
"type": "StringField"
},
"my_text_content": {
"editable": 1,
"key": "text_content",
"renderjs_extra": '{"editor": "fck_editor",' +
'"maximize": true}',
"type": "GadgetField",
"url": "gadget_editor.html",
"sandbox": "public"
}
},
action: "Base_edit",
update_action: "",
_links: {}
},
form_json = {
erp5_document: {
"_embedded": {"_view": {}},
"_links": {}
},
form_definition: form_definition
};
for (var i = 0; i < form_definition.group_list.length; i++) {
var fields = form_definition.group_list[i][1];
for (var j = 0; j < fields.length; j++) {
var my_element = fields[j][0], element_id;
if (my_element.startsWith("my_")) {
element_id = my_element.replace("my_", "");
}
var field_info = form_definition.field_info_dict[my_element];
if (gadget.state.hasOwnProperty("doc") && gadget.state.doc.hasOwnProperty(element_id)) {
field_info["default"] = gadget.state.doc[element_id];
}
form_json.erp5_document._embedded._view[my_element] = field_info;
form_json.erp5_document._links = form_definition._links;
}
}
return form_json;
})
.declareMethod("render", function (options) { .declareMethod("render", function (options) {
return this.changeState({ return this.changeState({
jio_key: options.jio_key, jio_key: options.jio_key,
doc: options.doc doc: options.doc,
form_json: options.form_json
}); });
}) })
...@@ -109,14 +48,10 @@ ...@@ -109,14 +48,10 @@
}) })
.onStateChange(function () { .onStateChange(function () {
var gadget = this, var gadget = this;
form_json;
return gadget.generateJsonRenderForm(gadget)
.push(function (result) {
form_json = result;
return gadget.getDeclaredGadget('form_view') return gadget.getDeclaredGadget('form_view')
.push(function (form_gadget) { .push(function (form_gadget) {
return form_gadget.render(form_json); return form_gadget.render(gadget.state.form_json);
}) })
.push(function () { .push(function () {
return RSVP.all([ return RSVP.all([
...@@ -135,5 +70,4 @@ ...@@ -135,5 +70,4 @@
}); });
}); });
}); });
});
}(window, rJS, RSVP)); }(window, rJS, RSVP));
...@@ -236,7 +236,7 @@ ...@@ -236,7 +236,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>973.29142.36243.7697</string> </value> <value> <string>973.30699.43343.35106</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -254,7 +254,7 @@ ...@@ -254,7 +254,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1549285941.45</float> <float>1549379311.18</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
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