Commit 3c163f1b authored by Romain Courteaud's avatar Romain Courteaud Committed by Jérome Perrin

Save simulation parameters before triggering calculations.

parent f2646b4a
......@@ -19,11 +19,9 @@
<script src="Input_viewSimulation.js" type="text/javascript"></script>
</head>
<body>
<form>
<form class="save_form">
<fieldset class="simulation_parameters">
</fieldset>
</form>
<form class="run_form">
<button type="submit" class="ui-btn ui-btn-b ui-btn-inline
ui-icon-refresh ui-btn-icon-right">Run Simulation</button>
</form>
......
......@@ -5,6 +5,124 @@
promiseEventListener, initGadgetMixin) {
"use strict";
function saveForm(gadget) {
var general = {};
return new RSVP.Queue()
.push(function () {
var i,
promise_list = [];
for (i = 0; i < gadget.props.gadget_list.length; i += 1) {
promise_list.push(gadget.props.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];
}
}
}
}
// Always get a fresh version, to prevent deleting spreadsheet & co
return gadget.aq_getAttachment({
"_id": gadget.props.jio_key,
"_attachment": "body.json"
});
})
.push(function (body) {
var data = JSON.parse(body);
data.general = general;
return gadget.aq_putAttachment({
"_id": gadget.props.jio_key,
"_attachment": "body.json",
"_data": JSON.stringify(data, null, 2),
"_mimetype": "application/json"
});
});
}
function runSimulation(gadget) {
return new RSVP.Queue()
.push(function () {
return gadget.aq_getAttachment({
"_id": gadget.props.jio_key,
"_attachment": "body.json"
});
})
.push(function (body_json) {
// XXX Hardcoded relative URL
return gadget.aq_ajax({
url: "../../runSimulation",
type: "POST",
data: body_json,
headers: {
"Content-Type": 'application/json'
}
});
})
.push(function (evt) {
var json_data = JSON.parse(evt.target.responseText);
if (json_data.success !== true) {
throw new Error(json_data.error);
}
return gadget.aq_putAttachment({
"_id": gadget.props.jio_key,
"_attachment": "simulation.json",
"_data": JSON.stringify(json_data.data, null, 2),
"_mimetype": "application/json"
});
})
.push(function () {
return gadget.whoWantToDisplayThisDocument(
gadget.props.jio_key,
"view_result"
);
})
.push(function (url) {
return gadget.pleaseRedirectMyHash(url);
});
}
function waitForRunSimulation(gadget) {
var submit_evt;
return new RSVP.Queue()
.push(function () {
return promiseEventListener(
gadget.props.element.getElementsByClassName("save_form")[0],
'submit',
false
);
})
.push(function (evt) {
submit_evt = evt;
// Prevent double click
evt.target.getElementsByClassName("ui-btn")[0].disabled = true;
$.mobile.loading('show');
return saveForm(gadget);
})
.push(function () {
return runSimulation(gadget);
})
.push(undefined, function (error) {
// Always drop the loader
$.mobile.loading('hide');
throw error;
})
.push(function () {
submit_evt.target.getElementsByClassName("ui-btn")[0].disabled = false;
$.mobile.loading('hide');
});
}
/////////////////////////////////////////////////////////////////
// Handlebars
/////////////////////////////////////////////////////////////////
......@@ -70,6 +188,7 @@
})
.push(function (sub_element) {
parent_element.appendChild(sub_element);
gadget.props.gadget_list.push(sub_gadget);
});
}
......@@ -84,6 +203,7 @@
.push(function (configuration_dict) {
var property_list =
configuration_dict['Dream-Configuration'].property_list;
gadget.props.gadget_list = [];
for (i = 0; i < property_list.length; i += 1) {
property = property_list[i];
if (property._class === "Dream.Property") {
......@@ -97,64 +217,7 @@
})
.declareMethod("startService", function () {
var gadget = this;
return new RSVP.Queue()
.push(function () {
return promiseEventListener(
gadget.props.element.getElementsByClassName("run_form")[0],
'submit',
false
);
})
.push(function () {
// Prevent double click
gadget.props.element
.getElementsByClassName("ui-btn")[0].disabled = true;
return gadget.aq_getAttachment({
"_id": gadget.props.jio_key,
"_attachment": "body.json"
});
})
.push(function (body_json) {
$.mobile.loading('show');
// XXX Hardcoded relative URL
return gadget.aq_ajax({
url: "../../runSimulation",
type: "POST",
data: body_json,
headers: {
"Content-Type": 'application/json'
}
});
})
.push(undefined, function (error) {
// Always drop the loader
$.mobile.loading('hide');
throw error;
})
.push(function (evt) {
$.mobile.loading('hide');
var json_data = JSON.parse(evt.target.responseText);
if (json_data.success !== true) {
throw new Error(json_data.error);
}
return gadget.aq_putAttachment({
"_id": gadget.props.jio_key,
"_attachment": "simulation.json",
"_data": JSON.stringify(json_data.data, null, 2),
"_mimetype": "application/json"
});
})
.push(function () {
return gadget.whoWantToDisplayThisDocument(
gadget.props.jio_key,
"view_result"
);
})
.push(function (url) {
return gadget.pleaseRedirectMyHash(url);
});
return waitForRunSimulation(this);
});
}(window, rJS, RSVP, jQuery, Handlebars,
promiseEventListener, initGadgetMixin));
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