On 02/17/2016 06:29 PM, Petr Vobornik wrote:
On 02/15/2016 04:20 PM, Pavel Vomacka wrote:


On 02/12/2016 01:52 PM, Pavel Vomacka wrote:


On 02/11/2016 12:31 PM, Pavel Vomacka wrote:
Hello,

The canvas of the graph had static size. This patch fixes this issue
and from now the graph canvas is resized according to the window size.

Pavel Vomacka


Because of changes in previous patch I'm sending also this one again.
Plus I fixed some jslint warnings.

And again a link to the ticket:
https://fedorahosted.org/freeipa/ticket/5647 .

--
Pavel^3 Vomacka


And another change in the code. This patch adds checking whether a svg
element even exists. And don't add 'col-sm-12' class to the svg element
any more. This class just added useless paddings to the element.

--
Pavel^3 Vomacka


Hi,

thanks for the patch.
Hi,

thank you for reviewing.

1. I don't like the fact that the resize handler registered in initialize method is active forever, even when viewing other facets.
I moved the handler to the topology graph facet. It is also removed after hide event is emited.
2. The code will probably fail if there is other svg element present on the page.

$('svg') searches for all svg elements in DOM, such search is usually slow and undeterministic. It is better to use a stored reference(if possible) or limit the search to some parent element, e.g. TopoGraph can store and then use its container.

Would be funny if there were 2 graphs.
Yep, you are right. I avoid using this type of searching in this patch.


3. Why is there the toFixed(1) call? Or more specifically on that position? It hides the fact that toFixed transforms Number to String and then '-' operator with Number on the right casts it back to Number.
The toFixed(1) was used just because we don't need so accurate numbers, but in this patch this function is not used any more.

4. width could be just: this._svg.parent().width()
The width is now solved by using this.content.width() in topology graph facet. I think that the calculating of width and height should be at the same place. That is why I didn't put calculating of width into the TopoGraph.

5. Your approach for bottom padding works well but I don't like that the component assumes that there is some col-sm-12 element on a page whose right padding is actually equal to space on the left of the svg.
I agree, fixed.

#1 and #5 makes me think that the resize logic should be moved topology facet. Something like:

* register resize handler on facet's 'show' event
* unregister resize handler on facet's 'hide' event (will solve #1)
* on window resize, compute the size in topology facet, call new .resize(width, height) method of TopoGraph

Then, we wouldn't have to search whole DOM for 'svg' elements to check if page is visible. The bottom padding can be obtained by: parseInt(this.content.css('paddingLeft')) where 'this' is facet.

I followed these tips and here is a new patch.

--
Pavel^3 Vomacka
>From 597997b97bd48d1ec92c977fc5429c5c2711a8c1 Mon Sep 17 00:00:00 2001
From: Pavel Vomacka <pvoma...@redhat.com>
Date: Thu, 25 Feb 2016 15:02:04 +0100
Subject: [PATCH] Resize topology graph canvas according to window size

The size of svg element is calculated when the topology graph facet is load
and then every time when the window is resized. The resize event listener
is removed after the topology graph facet emits hide event.

https://fedorahosted.org/freeipa/ticket/5647
---
 install/ui/src/freeipa/topology.js       | 41 ++++++++++++++++++++++++++++----
 install/ui/src/freeipa/topology_graph.js | 15 ++++++++++--
 2 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/install/ui/src/freeipa/topology.js b/install/ui/src/freeipa/topology.js
index 6e67484cc7542765056f0887928a64e4dc576836..a01296ee4b9a991c4655a004f947177bee6bc64f 100644
--- a/install/ui/src/freeipa/topology.js
+++ b/install/ui/src/freeipa/topology.js
@@ -431,14 +431,39 @@ topology.TopologyGraphFacet = declare([Facet, ActionMixin, HeaderMixin], {
     init: function(spec) {
         this.inherited(arguments);
         var graph = this.get_widget('topology-graph');
+        var listener = this.resize_listener.bind(this, graph);
 
         on(this, 'show', lang.hitch(this, function(args) {
-            graph.update();
+            var size = this.calculate_canvas_size();
+            graph.update(size.height, size.width);
+
+            $(window).on('resize', null, listener);
         }));
 
         on(graph, 'link-selected', lang.hitch(this, function(data) {
             this.set_selected_link(data.link);
         }));
+
+        on(this, 'hide', function () {
+            $(window).off('resize', null, listener);
+        });
+    },
+
+    resize_listener: function(graph) {
+        var size = this.calculate_canvas_size();
+
+        graph.resize(size.height, size.width);
+    },
+
+    calculate_canvas_size: function() {
+        var space = parseInt(this.content.css('paddingLeft'), 10);
+        var height = $(window).height() - this.content.offset().top - space;
+        var width = this.content.width();
+
+        return {
+            height: height,
+            width: width
+        };
     },
 
     set_selected_link: function(link) {
@@ -448,7 +473,8 @@ topology.TopologyGraphFacet = declare([Facet, ActionMixin, HeaderMixin], {
 
     refresh: function() {
         var graph = this.get_widget('topology-graph');
-        graph.update();
+        var size = this.calculate_canvas_size();
+        graph.update(size.height, size.width);
     }
 });
 
@@ -802,7 +828,7 @@ topology.TopologyGraphWidget = declare([Stateful, Evented], {
         return deferred.promise;
     },
 
-    update: function() {
+    update: function(height, width) {
         this._update_view();
 
         if (IPA.domain_level < topology.required_domain_level) return;
@@ -815,9 +841,10 @@ topology.TopologyGraphWidget = declare([Stateful, Evented], {
                     suffixes: data.suffixes
                 });
                 this._bind_graph_events(this.graph);
+                this.resize(height, width);
                 this.graph.initialize(this.visualization_cnt_el);
-
             } else {
+                this.resize(height, width);
                 this.graph.update(data.nodes, data.links, data.suffixes);
             }
         }), function(error) {
@@ -897,6 +924,12 @@ topology.TopologyGraphWidget = declare([Stateful, Evented], {
         return this.topology_view_el;
     },
 
+    resize: function(height, width) {
+        if (this.graph) {
+            this.graph.resize(height, width);
+        }
+    },
+
     _init_widgets: function() {
     },
 
diff --git a/install/ui/src/freeipa/topology_graph.js b/install/ui/src/freeipa/topology_graph.js
index 3ca00f6f6fee5fbb0706b7dbb86d746a89a74209..3e1084f2a0f8fa239743e96f9a8f1cd94321a53d 100644
--- a/install/ui/src/freeipa/topology_graph.js
+++ b/install/ui/src/freeipa/topology_graph.js
@@ -27,8 +27,6 @@ var topology_graph = {
  * @class
  */
 topology_graph.TopoGraph = declare([Evented], {
-    width: 960,
-    height: 500,
     _colors: d3.scale.category10(),
     _svg : null,
     _path: null,
@@ -472,6 +470,19 @@ topology_graph.TopoGraph = declare([Evented], {
             .attr("transform", transform);
     },
 
+    resize: function(height, width) {
+        if (!(isNaN(height) || isNaN(width))) {
+            this.height = height < 0 ? 0 : height;
+            this.width = width < 0 ? 0 : width;
+
+            if (this._svg) {
+                this._svg
+                    .attr('width', this.width)
+                    .attr('height', this.height);
+            }
+        }
+    },
+
     constructor: function(spec) {
         lang.mixin(this, spec);
     }
-- 
2.5.0

-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to