~matsubara/juju-gui/tarmac-test

« back to all changes in this revision

Viewing changes to app/views/topology/viewport.js

  • Committer: Matthew Scott
  • Date: 2013-01-09 17:23:33 UTC
  • mfrom: (307 juju-gui)
  • mto: This revision was merged to the branch mainline in revision 312.
  • Revision ID: matthew.scott@canonical.com-20130109172333-ad6ndevmn5p44dz0
Merging with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
YUI.add('juju-topology-viewport', function(Y) {
4
4
  var views = Y.namespace('juju.views'),
 
5
      utils = Y.namespace('juju.views.utils'),
5
6
      models = Y.namespace('juju.models'),
6
7
      d3ns = Y.namespace('d3');
7
8
 
26
27
 
27
28
    events: {
28
29
      yui: {
29
 
        windowresize: 'resized'
30
 
      }
31
 
    },
32
 
 
33
 
    initializer: function(options) {
34
 
      ViewportModule.superclass.constructor.apply(this, arguments);
35
 
    },
36
 
 
37
 
    render: function() {
38
 
      var topology = this.get('component'),
39
 
          value = 100,
40
 
          currentScale = topology.get('scale');
41
 
 
42
 
      ViewportModule.superclass.render.apply(this, arguments);
43
 
      // Build a slider to control zoom level
44
 
      if (currentScale) {
45
 
        value = currentScale * 100;
46
 
      }
47
 
      var slider = new Y.Slider({
48
 
        min: 25,
49
 
        max: 200,
50
 
        value: value
51
 
      });
52
 
      slider.render('#slider-parent');
53
 
      slider.after('valueChange', function(evt) {
54
 
        // Don't fire a zoom if there's a zoom event already in progress;
55
 
        // that will run rescale for us.
56
 
        if (d3.event && d3.event.scale && d3.event.translate) {
57
 
          return;
58
 
        }
59
 
        topology._fire_zoom((evt.newVal - evt.prevVal) / 100);
60
 
      });
61
 
      this.slider = slider;
62
 
 
63
 
      return this;
64
 
    },
65
 
 
66
 
    update: function() {
67
 
      ViewportModule.superclass.update.apply(this, arguments);
68
 
      return this;
69
 
    },
70
 
 
71
 
    /**
72
 
     * Event handler for windowresize events.
73
 
     *
74
 
     * Properly scale the component to take advantage of all the space
75
 
     * provided by the viewport.
76
 
     *
77
 
     * @method resized
78
 
     **/
79
 
    resized: function(evt) {
80
 
      // start with some reasonable defaults
81
 
      var topology = this.get('component'),
82
 
          vis = topology.vis,
 
30
        windowresize: 'resized',
 
31
        rendered: 'resized'
 
32
      }
 
33
    },
 
34
 
 
35
    /*
 
36
     * Set the visualization size based on the viewport.
 
37
     *
 
38
     * This event allows other page components that may unintentionally affect
 
39
     * the page size, such as the charm panel, to get out of the way before we
 
40
     * compute sizes.  Note the "afterPageSizeRecalculation" event at the end
 
41
     * of this function.
 
42
     */
 
43
    resized: function() {
 
44
      var topo = this.get('component'),
83
45
          container = this.get('container'),
84
 
          viewport_height = '100%',
85
 
          viewport_width = '100%',
 
46
          vis = topo.vis,
86
47
          svg = container.one('svg'),
87
 
          width = 800,
88
 
          height = 600;
89
 
 
90
 
      if (container.get('winHeight') &&
91
 
          Y.one('#overview-tasks') &&
92
 
          Y.one('.navbar')) {
93
 
        // Attempt to get the viewport height minus the navbar at top and
94
 
        // control bar at the bottom. Use Y.one() to ensure that the
95
 
        // container is attached first (provides some sensible defaults)
96
 
 
97
 
        viewport_height = container.get('winHeight') -
98
 
            styleToNumber('#overview-tasks', 'height', 22) - //XXX
99
 
            styleToNumber('.navbar', 'height', 87) - 1; //XXX
100
 
 
101
 
        // Attempt to get the viewport width from the overview-tasks bar.
102
 
        viewport_width = styleToNumber('#viewport', 'width', 800); //XXX
103
 
 
104
 
        // Make sure we don't get sized any smaller than 800x600
105
 
        viewport_height = Math.max(viewport_height, height);
106
 
        viewport_width = Math.max(viewport_width, width);
 
48
          canvas = container.one('.topology-canvas'),
 
49
          zoomPlane = container.one('.zoom-plane');
 
50
 
 
51
      if (!canvas || !svg) {
 
52
        return;
107
53
      }
108
 
      // Set the svg sizes.
109
 
      svg.setAttribute('width', viewport_width)
110
 
        .setAttribute('height', viewport_height);
111
 
 
112
 
      // Get the resulting computed sizes (in the case of 100%).
113
 
      width = parseInt(svg.getComputedStyle('width'), 10);
114
 
      height = parseInt(svg.getComputedStyle('height'), 10);
115
 
 
116
 
      // Set the internal rect's size.
117
 
      svg.one('rect')
118
 
        .setAttribute('width', width)
119
 
        .setAttribute('height', height);
120
 
      container.one('#canvas').setStyle('height', height);
121
 
      container.one('#canvas').setStyle('width', width);
122
 
 
 
54
      topo.fire('beforePageSizeRecalculation');
 
55
      // Get the canvas out of the way so we can calculate the size
 
56
      // correctly (the canvas contains the svg).  We want it to be the
 
57
      // smallest size we accept--no smaller or bigger--or else the
 
58
      // presence or absence of scrollbars may affect our calculations
 
59
      // incorrectly.
 
60
      canvas.setStyles({height: 600, width: 800});
 
61
      var dimensions = utils.getEffectiveViewportSize(true, 800, 600);
 
62
      svg.setAttribute('width', dimensions.width);
 
63
      svg.setAttribute('height', dimensions.height);
 
64
      vis.attr('width', dimensions.width);
 
65
      vis.attr('height', dimensions.height);
 
66
 
 
67
 
 
68
      zoomPlane.setAttribute('width', dimensions.width);
 
69
      zoomPlane.setAttribute('height', dimensions.height);
 
70
      canvas.setStyles({
 
71
        width: dimensions.width + 'px',
 
72
        height: dimensions.height + 'px'});
123
73
      // Reset the scale parameters
124
 
      topology.xscale.domain([-width / 2, width / 2])
125
 
        .range([0, width]);
126
 
      topology.yscale.domain([-height / 2, height / 2])
127
 
        .range([height, 0]);
128
 
 
129
 
      topology.width = width;
130
 
      topology.height = height;
 
74
      topo.set('size', [dimensions.width, dimensions.height]);
 
75
      topo.fire('afterPageSizeRecalculation');
131
76
    }
132
77
 
133
 
 
134
 
 
135
78
  }, {
136
79
    ATTRS: {}
137
80
  });