Commit 82ed9470 authored by Sebastien Robin's avatar Sebastien Robin

gui prototype: simplify jsonPlumb and Dream objects, use inheritance between them

parent 70fae8cd
...@@ -19,8 +19,8 @@ ...@@ -19,8 +19,8 @@
(function (scope, $, jsPlumb, console, _) { (function (scope, $, jsPlumb, console, _) {
"use strict"; "use strict";
var dream = function (configuration) { scope.Dream = function (configuration) {
var that = {}, priv = {}, general = {}; var that = jsonPlumb(), priv = {};
priv.onError = function(error) { priv.onError = function(error) {
console.log("Error", error); console.log("Error", error);
...@@ -62,17 +62,13 @@ ...@@ -62,17 +62,13 @@
render_element.append('<p/><a id="clear_all">Clear All</a>'); render_element.append('<p/><a id="clear_all">Clear All</a>');
}; };
priv.removeElement = function(element_id) {
priv.plumb.removeElement(element_id);
};
priv.initDialog = function() { priv.initDialog = function() {
$( "#dialog-form" ).dialog({autoOpen: false}); $( "#dialog-form" ).dialog({autoOpen: false});
}; };
priv.initGeneralProperties = function() { priv.initGeneralProperties = function() {
var fieldset = $("#general-fieldset"), var fieldset = $("#general-fieldset"),
previous_data = priv.plumb.getData()['general'], previous_data = that.getData()['general'],
previous_value = "", previous_value = "",
prefix = "General-"; prefix = "General-";
fieldset.children().remove() fieldset.children().remove()
...@@ -108,7 +104,7 @@ ...@@ -108,7 +104,7 @@
$("#dialog-fieldset").children().remove() $("#dialog-fieldset").children().remove()
var element_id_prefix = element_id.split("_")[0]; var element_id_prefix = element_id.split("_")[0];
var property_list = configuration[element_id_prefix].property_list || []; var property_list = configuration[element_id_prefix].property_list || [];
var previous_data = priv.plumb.getData()["element"]; var previous_data = that.getData()["element"];
previous_data = previous_data[element_id] || {}; previous_data = previous_data[element_id] || {};
previous_data = previous_data.data || {}; previous_data = previous_data.data || {};
var previous_value; var previous_value;
...@@ -153,7 +149,7 @@ ...@@ -153,7 +149,7 @@
}, },
Delete: function() { Delete: function() {
if (confirm("Are you sure you want to delete " + element_id + " ?")) { if (confirm("Are you sure you want to delete " + element_id + " ?")) {
priv.removeElement(element_id); that.removeElement(element_id);
} }
$( this ).dialog( "close" ); $( this ).dialog( "close" );
}, },
...@@ -178,7 +174,7 @@ ...@@ -178,7 +174,7 @@
}); });
}; };
updateDataPropertyList(property_list, data); updateDataPropertyList(property_list, data);
priv.plumb.updateElementData(element_id, {data: data}); that.updateElementData(element_id, {data: data});
$( this ).dialog( "close" ); $( this ).dialog( "close" );
}, },
}, },
...@@ -188,13 +184,10 @@ ...@@ -188,13 +184,10 @@
}); });
}; };
Object.defineProperty(that, "newElement", { priv.super_newElement = that.newElement;
configurable: false, that.newElement = function (element) {
enumerable: false,
writable: false,
value: function (element) {
var element_prefix = element.id.split('_')[0] var element_prefix = element.id.split('_')[0]
priv.plumb.newElement(element, configuration[element_prefix]); priv.super_newElement(element, configuration[element_prefix]);
$("#" + element.id).bind('click', function() { $("#" + element.id).bind('click', function() {
console.log("bind click on window", $(this)); console.log("bind click on window", $(this));
$( "#dialog-form" ).dialog( "destroy" ) ; $( "#dialog-form" ).dialog( "destroy" ) ;
...@@ -217,18 +210,12 @@ ...@@ -217,18 +210,12 @@
}); });
} }
updateDefaultData(data, property_list); updateDefaultData(data, property_list);
priv.plumb.updateElementData(element.id, {data: data}); that.updateElementData(element.id, {data: data});
};
}
});
Object.defineProperty(that, "start", { priv.super_start = that.start;
configurable: false, that.start = function() {
enumerable: false, priv.super_start();
writable: false,
value: function () {
priv.plumb = jsonPlumb.newJsonPlumb();
priv.plumb.start();
priv.displayTool(); priv.displayTool();
priv.initDialog(); priv.initDialog();
// save general configuration default values // save general configuration default values
...@@ -237,48 +224,15 @@ ...@@ -237,48 +224,15 @@
console.log("dream.start, parsing general property", element.id); console.log("dream.start, parsing general property", element.id);
general_properties[element.id] = element.default; general_properties[element.id] = element.default;
}); });
priv.plumb.setGeneralProperties(general_properties); that.setGeneralProperties(general_properties);
priv.initGeneralProperties(); that.initGeneralProperties();
} };
});
Object.defineProperty(that, "initGeneralProperties", { that.initGeneralProperties = function() {
configurable: false,
enumerable: false,
writable: false,
value: function () {
priv.initGeneralProperties(); priv.initGeneralProperties();
} };
});
Object.defineProperty(that, "connect", {
configurable: false,
enumerable: false,
writable: false,
value: function (source_id, target_id) {
priv.plumb.connect(source_id, target_id);
}
});
Object.defineProperty(that, "updateElementData", {
configurable: false,
enumerable: false,
writable: false,
value: function (element_id, data) {
priv.plumb.updateElementData(element_id, data);
}
});
Object.defineProperty(that, "clearAll", {
configurable: false,
enumerable: false,
writable: false,
value: function () {
priv.plumb.clearAll();
}
});
function formatForManpy(data) { priv.formatForManpy = function(data) {
var manpy_dict = {}, coreObject = [], core_object_dict = {}; var manpy_dict = {}, coreObject = [], core_object_dict = {};
$.each(data['element'], function(idx, element) { $.each(data['element'], function(idx, element) {
var clone_element = {}; var clone_element = {};
...@@ -313,12 +267,6 @@ ...@@ -313,12 +267,6 @@
return manpy_dict; return manpy_dict;
} }
that.setGeneralProperties = function(properties) {
priv.plumb.setGeneralProperties(properties);
}
that.getData = function() { return priv.plumb.getData() };
that.runSimulation = function(callback) { that.runSimulation = function(callback) {
// handle Dream.General properties (in another function maybe ?) // handle Dream.General properties (in another function maybe ?)
var prefix = "General-", properties = {}, prefixed_property_id; var prefix = "General-", properties = {}, prefixed_property_id;
...@@ -330,9 +278,9 @@ ...@@ -330,9 +278,9 @@
properties[property.id] = $("#" + prefixed_property_id).val(); properties[property.id] = $("#" + prefixed_property_id).val();
} }
}); });
priv.plumb.setGeneralProperties(properties); that.setGeneralProperties(properties);
var model = formatForManpy(priv.plumb.getData()); var model = priv.formatForManpy(that.getData());
$.ajax( $.ajax(
'/runSimulation', { '/runSimulation', {
data: JSON.stringify({json: model}), data: JSON.stringify({json: model}),
...@@ -344,36 +292,7 @@ ...@@ -344,36 +292,7 @@
}); });
}; };
return that; return that;
}; };
var DreamNamespace = (function () {
var that = {};
/**
* Creates a new dream instance.
* @method newDream
* @param {object} model The model definition
* @return {object} The new Dream instance.
*/
Object.defineProperty(that, "newDream", {
configurable: false,
enumerable: false,
writable: false,
value: function (configuration) {
var instance = dream(configuration);
return instance;
}
});
return that;
})();
Object.defineProperty(scope, "DREAM", {
configurable: false,
enumerable: false,
writable: false,
value: DreamNamespace
});
}(window, jQuery, jsPlumb, console, _)); }(window, jQuery, jsPlumb, console, _));
...@@ -84,7 +84,7 @@ ...@@ -84,7 +84,7 @@
_class: 'Dream.Repairman', }, _class: 'Dream.Repairman', },
} }
dream_instance = DREAM.newDream(configuration) dream_instance = Dream(configuration)
dream_instance.start(); dream_instance.start();
$( ".tool" ).draggable({ opacity: 0.7, helper: "clone", $( ".tool" ).draggable({ opacity: 0.7, helper: "clone",
stop: function(tool) { stop: function(tool) {
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
(function (scope, $, jsPlumb, console, _) { (function (scope, $, jsPlumb, console, _) {
"use strict"; "use strict";
var jsonPlumb = function (model) { scope.jsonPlumb = function (model) {
var that = {}, priv = {}; var that = {}, priv = {};
priv.onError = function(error) { priv.onError = function(error) {
...@@ -150,83 +150,44 @@ ...@@ -150,83 +150,44 @@
priv.onDataChange(); priv.onDataChange();
}; };
Object.defineProperty(that, "updateElementData", { that.updateElementData = function (element_id, data) {
configurable: false,
enumerable: false,
writable: false,
value: function (element_id, data) {
_.extend(priv.element_container[element_id], data); _.extend(priv.element_container[element_id], data);
priv.onDataChange(); priv.onDataChange();
} };
});
Object.defineProperty(that, "start", { that.start = function () {
configurable: false,
enumerable: false,
writable: false,
value: function () {
priv.element_container = {}; priv.element_container = {};
priv.preference_container = {}; priv.preference_container = {};
priv.general_container = {}; priv.general_container = {};
priv.initJsPlumb(); priv.initJsPlumb();
} };
});
Object.defineProperty(that, "removeElement", { that.removeElement = function (element_id) {
configurable: false,
enumerable: false,
writable: false,
value: function (element_id) {
console.log("going to remove element", element_id); console.log("going to remove element", element_id);
priv.removeElement(element_id); priv.removeElement(element_id);
} };
});
Object.defineProperty(that, "getData", { that.getData = function () {
configurable: false,
enumerable: false,
writable: false,
value: function () {
return priv.getData(); return priv.getData();
} };
});
Object.defineProperty(that, "clearAll", { that.clearAll = function () {
configurable: false,
enumerable: false,
writable: false,
value: function () {
$("[id=render]").children().remove() $("[id=render]").children().remove()
_.each(_.pairs(priv.element_container), function(element, index) { _.each(_.pairs(priv.element_container), function(element, index) {
priv.removeElement(element[0]); priv.removeElement(element[0]);
}); });
} };
});
Object.defineProperty(that, "connect", { that.connect = function (source_id, target_id) {
configurable: false,
enumerable: false,
writable: false,
value: function (source_id, target_id) {
jsPlumb.connect({source: source_id, target: target_id}); jsPlumb.connect({source: source_id, target: target_id});
} };
});
Object.defineProperty(that, "setGeneralProperties", { that.setGeneralProperties = function (properties) {
configurable: false,
enumerable: false,
writable: false,
value: function (properties) { // XXX or k, v ?
priv.general_container = properties; priv.general_container = properties;
priv.onDataChange(); priv.onDataChange();
}, };
});
Object.defineProperty(that, "newElement", { that.newElement = function (element, option) {
configurable: false,
enumerable: false,
writable: false,
value: function (element, option) {
var render_element, style_string="", coordinate = {}; var render_element, style_string="", coordinate = {};
render_element = $("[id=render]"); render_element = $("[id=render]");
if (element.coordinate !== undefined) { if (element.coordinate !== undefined) {
...@@ -283,39 +244,9 @@ ...@@ -283,39 +244,9 @@
jsPlumb.addEndpoint(element.id, { anchor: anchor }, endpoint); jsPlumb.addEndpoint(element.id, { anchor: anchor }, endpoint);
}) })
priv.addElementToContainer(element); priv.addElementToContainer(element);
}
});
return that;
}; };
var JsonPlumbNamespace = (function () {
var that = {};
/**
* Creates a new dream instance.
* @method newDream
* @param {object} model The model definition
* @return {object} The new Dream instance.
*/
Object.defineProperty(that, "newJsonPlumb", {
configurable: false,
enumerable: false,
writable: false,
value: function (model) {
var instance = jsonPlumb(model);
return instance;
}
});
return that; return that;
})(); };
Object.defineProperty(scope, "jsonPlumb", {
configurable: false,
enumerable: false,
writable: false,
value: JsonPlumbNamespace
});
}(window, jQuery, jsPlumb, console, _)); }(window, jQuery, jsPlumb, console, _));
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