Commit 007d8fc1 authored by Jérome Perrin's avatar Jérome Perrin

Implement getContent on fieldset gadget and use it

parent 86024186
......@@ -4,39 +4,13 @@
"use strict";
function saveForm(gadget) {
var general = {},
field_gadget_list;
var general;
return gadget.getDeclaredGadget('fieldset')
.push(function (fieldset_gadget) {
return fieldset_gadget.getFieldGadgetList();
return fieldset_gadget.getContent();
})
.push(function (field_gadgets) {
field_gadget_list = field_gadgets;
})
.push(function () {
var i,
promise_list = [];
for (i = 0; i < field_gadget_list.length; i += 1) {
promise_list.push(field_gadget_list[i].getContent());
}
return RSVP.all(promise_list);
})
.push(function (result_list) {
var i,
result,
key;
for (i = 0; i < result_list.length; i += 1) {
result = result_list[i];
for (key in result) {
if (result.hasOwnProperty(key)) {
// Drop empty
if (result[key]) {
general[key] = result[key];
}
}
}
}
.push(function (content) {
general = content;
// Always get a fresh version, to prevent deleting spreadsheet & co
return gadget.aq_getAttachment({
"_id": gadget.props.jio_key,
......
......@@ -79,8 +79,25 @@
return queue;
})
.declareMethod("getFieldGadgetList", function () {
return this.props.field_gadget_list;
// getContent of all subfields
.declareMethod("getContent", function () {
var i, promise_list = [];
for (i = 0; i < this.props.field_gadget_list.length; i += 1) {
promise_list.push(this.props.field_gadget_list[i].getContent());
}
return RSVP.Queue()
.push(function () { return RSVP.all(promise_list); })
.push(function (result_list) {
var name, result = {};
for (i = 0; i < result_list.length; i += 1) {
for (name in result_list[i]) {
if (result_list[i].hasOwnProperty(name)) {
result[name] = result_list[i][name];
}
}
}
return result;
});
});
......
......@@ -405,10 +405,10 @@
function updateElementData(gadget, node_id, data) {
var element_id = gadget.props.node_container[node_id].element_id,
new_id = data.id;
if (data.name) {
$(gadget.props.element).find("#" + element_id).text(data.name)
if (data.data.name) {
$(gadget.props.element).find("#" + element_id).text(data.data.name)
.append('<div class="ep"></div></div>');
gadget.props.node_container[node_id].name = data.name;
gadget.props.node_container[node_id].name = data.data.name;
}
delete data.id;
$.extend(gadget.props.node_container[node_id], data.data);
......@@ -485,19 +485,6 @@
// onDataChange();
// }
function saveNode(evt, gadget, node_id) {
var i,
data = {
"id": $(evt.target[1]).val(),
"name": $(evt.target[2]).val()
};
data.data = {};
for (i = 3; i < evt.target.length - 1; i += 1) {
data.data[evt.target[i].name] = $(evt.target[i]).val();
}
updateElementData(gadget, node_id, data);
}
function openNodeDialog(gadget, element, config_dict) {
var node_id = getNodeId(gadget.props.node_container, element.id),
node_data = gadget.props.node_container[node_id],
......@@ -505,7 +492,6 @@
property_list = config_dict[element_type].property_list || [],
node_edit_popup = $(gadget.props.element).find('#popup-edit-template'),
fieldset_element,
save_promise,
delete_promise;
if (node_edit_popup.length !== 0) {
......@@ -521,6 +507,7 @@
node_data.id = node_id;
if (property_list.length === 0 || property_list[0].id !== "id") {
// XXX name & id should not be handled differently in form.
property_list.unshift({
"_class": "Dream.Property",
"id": "name",
......@@ -536,17 +523,27 @@
});
}
save_promise = new RSVP.Queue()
.push(function () {
return promiseEventListener(
node_edit_popup.find("form")[0],
'submit',
false
);
})
.push(function (evt) {
return saveNode(evt, gadget, node_id);
});
function save_promise(fieldset_gadget, node_id) {
return RSVP.Queue()
.push(function () {
return promiseEventListener(
node_edit_popup.find("form")[0],
'submit',
false
);
})
.push(function (evt) {
var data = {
// XXX id should not be handled differently ...
"id": $(evt.target[1]).val(),
"data": {}
};
return fieldset_gadget.getContent().then(function (r) {
$.extend(data.data, r);
updateElementData(gadget, node_id, data);
});
});
}
delete_promise = new RSVP.Queue()
.push(function () {
......@@ -565,15 +562,17 @@
scope: 'fieldset'
})
.push(function (fieldset_gadget) {
return fieldset_gadget.render(property_list, node_data);
return RSVP.all([fieldset_gadget,
fieldset_gadget.render(property_list, node_data)]);
})
.push(function () {
.push(function (fieldset_gadget) {
node_edit_popup.enhanceWithin();
node_edit_popup.popup('open');
return fieldset_gadget[0];
})
.push(function () {
.push(function (fieldset_gadget) {
return RSVP.any([
save_promise,
save_promise(fieldset_gadget, node_id),
delete_promise
]);
})
......
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