Commit 5352ccfb authored by Jérome Perrin's avatar Jérome Perrin Committed by Ioannis Papagiannopoulos

lots of cleanups

parent 2e4812c0
......@@ -3,9 +3,9 @@
"use strict";
var gadget_klass = rJS(window);
// TODO: save on parent gadget
function saveGraph(evt) {
var gadget = this,
graph_data;
var gadget = this;
return new RSVP.Queue()
.push(function () {
// Prevent double click
......@@ -18,11 +18,10 @@
return graph_gadget.getContent();
})
.push(function (body) {
console.log("saving", JSON.parse(body));
return gadget.aq_putAttachment({
"_id": gadget.props.jio_key,
"_attachment": "body.json",
"_data": body, //JSON.stringify(data, null, 2),
"_data": JSON.stringify(JSON.parse(body), null, 2),
"_mimetype": "application/json"
});
})
......@@ -44,6 +43,7 @@
gadget_klass
// TODO Mixin
.ready(function (g) {
g.props = {};
})
......
......@@ -17,15 +17,17 @@
initGadgetMixin(gadget_klass);
gadget_klass
.declareMethod("render", function (property_list, data, key) {
.declareMethod("render", function (options, node_id) {
// XXX node_id is added like a property so that one can change the node
// id
var gadget = this,
queue,
value,
property;
gadget.key = key; // used for recursive fieldsets
gadget.props.key = options.key; // used for recursive fieldsets
gadget.props.field_gadget_list = [];
function addField(property, value) {
function addField(property_id, property_definition, value) {
var sub_gadget;
queue
.push(function () {
......@@ -34,39 +36,29 @@
gadget.props.element.insertAdjacentHTML(
'beforeend',
label_template({
"for": property.id,
"name": (property.name || property.id)
"for": property_id,
"name": (property_definition.name || property_id)
})
);
if (property._class === "Dream.PropertyList") {
if (property_definition.type === "object") {
// Create a recursive fieldset for this key.
return gadget.declareGadget("../fieldset/index.html");
}
if (property.type === "number") {
if (property_definition.type === "number") {
return gadget.declareGadget("../number_field/index.html");
}
if (property.choice) {
if (property_definition.enum) {
return gadget.declareGadget("../list_field/index.html");
}
return gadget.declareGadget("../string_field/index.html");
})
.push(function (gg) {
sub_gadget = gg;
var choice = property.choice || [],
default_opt = choice[0] ? [choice[0][1]] : [""];
value = (data[property.id] === undefined ?
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: {
title: (property.description || ''),
key: property.id,
return sub_gadget.render({
key: property_id,
value: value,
items: choice,
default: default_opt
}});
property_definition: property_definition
});
})
.push(function () {
return sub_gadget.getElement();
......@@ -79,13 +71,21 @@
queue = new RSVP.Queue()
.push(function () {
Object.keys(property_list).forEach(function (i) {
property = property_list[i];
value = property._default === undefined ? "" : property._default;
addField(property, value);
if (node_id) {
addField('id', {'type': 'string'}, node_id);
}
Object.keys(options.property_definition.properties
).forEach(function (property_name) {
var property_definition =
options.property_definition.properties[property_name],
value = (options.value || {})[property_name] === undefined
? property_definition._default : options.value[property_name];
// XXX some properties are not editable
if (property_name !== 'coordinate' && property_name !== '_class') {
addField(property_name, property_definition, value);
}
});
});
return queue;
})
......@@ -99,8 +99,8 @@
.push(function () { return RSVP.all(promise_list); })
.push(function (result_list) {
var name, result = {}, content = result;
if (gadget.key) {
content = result[gadget.key] = {};
if (gadget.props.key) {
content = result[gadget.props.key] = {};
}
for (i = 0; i < result_list.length; i += 1) {
for (name in result_list[i]) {
......
This diff is collapsed.
......@@ -28,18 +28,18 @@
var select = this.element.getElementsByTagName('select')[0],
i,
template,
field_json = options.field_json,
tmp = '';
select.setAttribute('name', field_json.key);
for (i = 0; i < field_json.items.length; i += 1) {
if (field_json.items[i][1] === field_json.value) {
select.setAttribute('name', options.key);
for (i = 0; i < options.property_definition.enum.length; i += 1) {
if (options.property_definition.enum[i] === options.value) {
template = selected_option_template;
} else {
template = option_template;
}
// XXX value and text are always same in json schema
tmp += template({
value: field_json.items[i][1],
text: field_json.items[i][0]
value: options.property_definition.enum[i],
text: options.property_definition.enum[i]
});
}
select.innerHTML += tmp;
......
......@@ -10,11 +10,10 @@
});
})
.declareMethod('render', function (options) {
var input = this.element.querySelector('input'),
field_json = options.field_json || {};
input.setAttribute('value', field_json.value);
input.setAttribute('name', field_json.key);
input.setAttribute('title', field_json.title);
var input = this.element.querySelector('input');
input.setAttribute('value', options.value);
input.setAttribute('name', options.key);
input.setAttribute('title', options.title || options.key);
})
.declareMethod('getContent', function () {
......
......@@ -10,11 +10,10 @@
});
})
.declareMethod('render', function (options) {
var input = this.element.querySelector('input'),
field_json = options.field_json || {};
input.setAttribute('value', field_json.value || "");
input.setAttribute('name', field_json.key);
input.setAttribute('title', field_json.title);
var input = this.element.querySelector('input');
input.setAttribute('value', options.value || "");
input.setAttribute('name', options.key);
input.setAttribute('title', options.title || options.key);
})
.declareMethod('getContent', function () {
......
......@@ -2,7 +2,6 @@
(function (window, document, RSVP, rJS, initGadgetMixin) {
"use strict";
// XXX use a renderjs utility function for that
/*jslint nomen: true*/
var gadget_klass = rJS(window);
function waitForDragstart(tool) {
......@@ -19,7 +18,7 @@
callback = function (evt) {
try {
evt.dataTransfer.setData('application/json',
tool.dataset.class_definition);
tool.dataset.class_name);
} catch (e) {
reject(e);
}
......@@ -49,7 +48,7 @@
tool.className = "tool " + key;
tool.textContent = _class.name || key;
tool.draggable = true;
tool.dataset.class_definition = JSON.stringify([key, _class]);
tool.dataset.class_name = JSON.stringify(key);
Object.keys(_class.css || {}).forEach(function (k) {
tool.style[k] = _class.css[k];
});
......
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