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