From 365735289e899b4d4ee209506e465d420d24e978 Mon Sep 17 00:00:00 2001 From: Alon Levy Date: Thu, 30 Jul 2015 18:03:04 +0300 Subject: client: record position to db when dragging + fixed Record position when dragging. Record fixed status. this is still not clear - what fixed means exactly. --- src/client/model/graph.js | 35 +++++++++++++++++++++++++++-------- src/client/view/graph_view.js | 35 ++++++++++++++++++++++++++++++----- src/client/view/layouts.js | 7 +++++++ 3 files changed, 64 insertions(+), 13 deletions(-) (limited to 'src/client') diff --git a/src/client/model/graph.js b/src/client/model/graph.js index 5e54d228..40df0896 100644 --- a/src/client/model/graph.js +++ b/src/client/model/graph.js @@ -641,6 +641,11 @@ function Graph(spec) { } this.layout_y_key = layout_y_key; + function layout_fixed_key(layout_name) { + return 'layouts_' + layout_name + '_fixed'; + } + this.layout_fixed_key = layout_fixed_key; + function close_to(a, b, eps) { return Math.abs(a - b) < eps; } @@ -650,16 +655,28 @@ function Graph(spec) { /** * Do an attribute commit with x, y for the current layout */ - this.nodes__update_positions = function(layout_name) { + this.nodes__store_layout_positions = function(layout_name, node_ids) { // TODO: fix when attribute diff supports nested keys to use: // layout..{x,y} var x_key = layout_x_key(layout_name), y_key = layout_y_key(layout_name), - changes = 0; + fixed_key = layout_fixed_key(layout_name), + changes = 0, + fixed = false; + + if (node_ids && node_ids.forEach) { + node_ids = node_ids.filter(function (node_id) { + return id_to_node_map[node_id] !== undefined; + }); + fixed = true; + } else { + node_ids = _.keys(id_to_node_map); + } // commit x, y to layout - _.values(id_to_node_map).forEach(function (node) { - var db_x = node[x_key], + node_ids.forEach(function (node_id) { + var node = id_to_node_map[node_id], + db_x = node[x_key], db_y = node[y_key], x = node.x, y = node.y; @@ -676,19 +693,21 @@ function Graph(spec) { } console.log('sending ' + changes + ' nodes position for layout ' + layout_name); // commit all current nodes - nodes__commit_attributes(function (node) { + nodes__commit_attributes(node_ids, function (node) { var d = {}; d[x_key] = node.x; d[y_key] = node.y; + d[fixed_key] = fixed; return d; }); }; - function nodes__commit_attributes(getter) { + function nodes__commit_attributes(node_ids, getter) { var attr_diff = model_diff.new_attr_diff(); - _.values(id_to_node_map).forEach(function (node) { - var d = getter(node); + node_ids.forEach(function (node_id) { + var node = id_to_node_map[node_id], + d = getter(node); _.keys(d).forEach(function (k) { attr_diff.add_node_attr_write(node.id, k, d[k]); }); diff --git a/src/client/view/graph_view.js b/src/client/view/graph_view.js index 09af8016..6da21015 100644 --- a/src/client/view/graph_view.js +++ b/src/client/view/graph_view.js @@ -207,29 +207,51 @@ function GraphView(spec) { var relayout = !temporary && (false === model_diff.is_attr_diff(diff)), have_position = 0, layout_x_key = graph.layout_x_key(layout.name), - layout_y_key = graph.layout_y_key(layout.name); + layout_y_key = graph.layout_y_key(layout.name), + node_set_add = diff.node_set_add || [], + is_full_graph_update = false; // copy position from diff based on current layout - if (layout.name && diff.node_set_add) { - diff.node_set_add.forEach(function (node) { + if (layout.name) { + node_set_add.forEach(function (node) { if (node[layout_x_key] && node[layout_y_key]) { node.x = node[layout_x_key]; node.y = node[layout_y_key]; have_position += 1; } }); + is_full_graph_update = (have_position === graph.nodes().length); if (have_position > 0) { console.log('loading layout last position from database for layout ' + layout.name); layout__load_graph(); - if (have_position === graph.nodes().length) { + if (is_full_graph_update) { console.log('no relayout because diff contains all graph nodes'); relayout = false; } } } + if (is_full_graph_update) { + layouts__set_from_nodes(node_set_add); + } update_view(relayout); }); + function layouts__set_from_nodes(nodes) { + layouts.forEach(function (layout_) { + var layout_x_key = graph.layout_x_key(layout_.name), + layout_y_key = graph.layout_y_key(layout_.name); + layout_.save_from_arr_id_x_y(nodes.map(function (node) { + var x = node[layout_x_key], + y = node[layout_y_key]; + + if (!x || !y) { + return undefined; + } + return {id: node.id, x: x, y: y}; + }).filter(function (datum) { return datum !== undefined; })); + }); + } + function transformOnSelection(data) { var selection = data[0], selection_inner_radius = Math.max(30, data[1]); @@ -352,6 +374,9 @@ function GraphView(spec) { d.px = d.x; d.py = d.y; tick(); + if (layout.name === 'custom') { + graph.nodes__store_layout_positions(layout.name, [d.id]); + } } } @@ -1161,7 +1186,7 @@ function GraphView(spec) { } function record_position_to_database() { - graph.nodes__update_positions(layout.name); + graph.nodes__store_layout_positions(layout.name); } var layouts = view_layouts.layouts.map(function (layout_data) { diff --git a/src/client/view/layouts.js b/src/client/view/layouts.js index 76422c0b..5fb750a9 100644 --- a/src/client/view/layouts.js +++ b/src/client/view/layouts.js @@ -177,6 +177,12 @@ function(consts, $, d3, _) { return layout; } + function save_from_arr_id_x_y(data) { + layout.saved_nodes_position = _.object(data.map(function (d) { + return [d.id, {x: d.x, y: d.y, px: d.x, py: d.y, fixed: true}]; + })); + } + function restore() { if (layout.saved_nodes_position === undefined) { return layout; @@ -219,6 +225,7 @@ function(consts, $, d3, _) { links: links, save: save, restore: restore, + save_from_arr_id_x_y: save_from_arr_id_x_y, stop: save, start: restore, nodes_links: function (nodes, links) { -- cgit v1.3.1