summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLV-426 <lv-426@taproot.org.il>2014-11-17 13:52:28 +0200
committerAlon Levy <alon@pobox.com>2014-11-19 23:21:42 +0200
commit7cdbdbf8c5c823b66abd4e313e831671f2a0c370 (patch)
tree84eada8d574b37f05da09a1df765815f54fb643e
parent8f4a44e04b7b96e3cd4327faa6c4d147b91b2cb1 (diff)
addNode refactoring:
- rename, return value - add notify param - set id via create_node__set_random_id()
-rw-r--r--src/model/graph.js71
1 files changed, 33 insertions, 38 deletions
diff --git a/src/model/graph.js b/src/model/graph.js
index 714685eb..160a8dfe 100644
--- a/src/model/graph.js
+++ b/src/model/graph.js
@@ -19,50 +19,45 @@ function Graph(el) {
}
var id_generator = id_generator_generator();
- ///FUNCTIONS
- this.addNode = function(name, type, state) {
- if (type === undefined) {
- console.log('bug: adding undefined type');
- }
- var new_node = this._addNodeNoHistory(
- {name:name,
- type:type,
- state:state,
- start:0,
- end:0,
- status:"unknown"});
- if (new_node) {
- signal.signal(consts.APPLIED_GRAPH_DIFF, [{
- nodes: {add: [new_node]}}]);
- return new_node;
+ /**
+ * add node if no previous node is present whose id equals that of the node being added
+ *
+ * @return node if node was actually added
+ */
+ this.addNode = function(node) {
+ if (this.__addNode(node)) {
+ return node;
}
}
- this._addNodeNoHistory = function(spec) {
- // No history recorded - this is a helper for loading from files / constant graphs
- var node;
- if (spec.id === undefined) {
- node = findNodeByName(spec.name, null);
- } else {
- if (spec.id !== undefined) {
- node = findNode(spec.id, null);
+ /**
+ * Inner implementation
+ *
+ * @param notify whether or not a presenter notification will be sent, default = true
+ */
+ this.__addNode = function(node, notify) {
+ if (undefined == node.id) {
+ if (findNodeByName(node.name)){
+ // FIXME handle node-with-equal-name case
+ return;
+ }else{
+ node = model_core.create_node__set_random_id(node);
+ console.debug('__addNode: node id missing, generated: name: ' + node.name + ', id: ' + node.id);
}
}
- if (node === undefined) {
- node = {
- "id": spec.id || id_generator(),
- "name": spec.name,
- "type": spec.type,
- "state": spec.state,
- "start": spec.start,
- "end": spec.end,
- "status": spec.status,
- 'url': spec.url,
- 'x': spec.x,
- 'y': spec.y,
- };
- nodes.push(node);
+
+ if (findNode(node.id, null)){
+ return;
+ }
+
+ undefined === notify && (notify = true); // notify by default
+
+ nodes.push(node);
+
+ if (undefined == notify){
+ signal.signal(consts.APPLIED_GRAPH_DIFF, [{nodes: {add: [node]}}]);
}
+
return node;
}