Commit 201a9994 authored by Jérome Perrin's avatar Jérome Perrin

first step of recursive fieldsets

parent c2b365fd
...@@ -17,17 +17,20 @@ ...@@ -17,17 +17,20 @@
initGadgetMixin(gadget_klass); initGadgetMixin(gadget_klass);
gadget_klass gadget_klass
.declareMethod("render", function (property_list, data) { .declareMethod("render", function (property_list, data, key) {
var gadget = this, var gadget = this,
queue, queue,
value, value,
property; property;
gadget.key = key; // used for recursive fieldsets
gadget.props.field_gadget_list = []; gadget.props.field_gadget_list = [];
function addField(property, value) { function addField(property, value) {
var sub_gadget; var sub_gadget;
queue queue
.push(function () { .push(function () {
// XXX this is incorrect for recursive fieldsets.
// we should use nested fieldset with legend
gadget.props.element.insertAdjacentHTML( gadget.props.element.insertAdjacentHTML(
'beforeend', 'beforeend',
label_template({ label_template({
...@@ -35,6 +38,10 @@ ...@@ -35,6 +38,10 @@
"name": (property.name || property.id) "name": (property.name || property.id)
}) })
); );
if (property._class === "Dream.PropertyList") {
// Create a recursive fieldset for this key.
return gadget.declareGadget("../fieldset/index.html");
}
if (property.type === "number") { if (property.type === "number") {
return gadget.declareGadget("../number_field/index.html"); return gadget.declareGadget("../number_field/index.html");
} }
...@@ -44,11 +51,15 @@ ...@@ -44,11 +51,15 @@
return gadget.declareGadget("../string_field/index.html"); return gadget.declareGadget("../string_field/index.html");
}) })
.push(function (gg) { .push(function (gg) {
sub_gadget = gg;
var choice = property.choice || [], var choice = property.choice || [],
default_opt = choice[0] ? [choice[0][1]] : [""]; default_opt = choice[0] ? [choice[0][1]] : [""];
sub_gadget = gg;
value = (data[property.id] === undefined ? value = (data[property.id] === undefined ?
value : data[property.id]); value : data[property.id]);
if (gg.__title === 'Fieldset') {
// XXX there must be a better way instead of using __title ?
return gg.render(property.property_list, value, property.id);
}
return sub_gadget.render({field_json: { return sub_gadget.render({field_json: {
title: (property.description || ''), title: (property.description || ''),
key: property.id, key: property.id,
...@@ -70,10 +81,8 @@ ...@@ -70,10 +81,8 @@
.push(function () { .push(function () {
Object.keys(property_list).forEach(function (i) { Object.keys(property_list).forEach(function (i) {
property = property_list[i]; property = property_list[i];
if (property._class === "Dream.Property") {
value = property._default || ""; value = property._default || "";
addField(property, value); addField(property, value);
}
}); });
}); });
...@@ -82,18 +91,21 @@ ...@@ -82,18 +91,21 @@
// getContent of all subfields // getContent of all subfields
.declareMethod("getContent", function () { .declareMethod("getContent", function () {
var i, promise_list = []; var i, promise_list = [], gadget = this;
for (i = 0; i < this.props.field_gadget_list.length; i += 1) { for (i = 0; i < this.props.field_gadget_list.length; i += 1) {
promise_list.push(this.props.field_gadget_list[i].getContent()); promise_list.push(this.props.field_gadget_list[i].getContent());
} }
return RSVP.Queue() return RSVP.Queue()
.push(function () { return RSVP.all(promise_list); }) .push(function () { return RSVP.all(promise_list); })
.push(function (result_list) { .push(function (result_list) {
var name, result = {}; var name, result = {}, content = result;
if (gadget.key) {
content = result[gadget.key] = {};
}
for (i = 0; i < result_list.length; i += 1) { for (i = 0; i < result_list.length; i += 1) {
for (name in result_list[i]) { for (name in result_list[i]) {
if (result_list[i].hasOwnProperty(name)) { if (result_list[i].hasOwnProperty(name)) {
result[name] = result_list[i][name]; content[name] = result_list[i][name];
} }
} }
} }
......
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