Bri Prebilic Cole
Committed by Gerrit Code Review

GUI -- Tables with the "onos-fixed-header" directive automatically resize to fit…

… the window (as well as having a fixed header row with scrolling body).

Change-Id: I230a9577f9a83ab9342414054e64afa181f4f1e9
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
21 /* ------ for summary-list tables ------ */ 21 /* ------ for summary-list tables ------ */
22 22
23 table.summary-list { 23 table.summary-list {
24 - margin: 20px 50px; 24 + margin: 0px 50px 50px 50px;
25 font-size: 10pt; 25 font-size: 10pt;
26 border-spacing: 0; 26 border-spacing: 0;
27 } 27 }
......
...@@ -20,7 +20,8 @@ ...@@ -20,7 +20,8 @@
20 (function () { 20 (function () {
21 'use strict'; 21 'use strict';
22 22
23 - var $log; 23 + var $window, fs,
24 + bottomMargin = 200;
24 25
25 // Render a plain d3 table by giving it the div, a config file, and data 26 // Render a plain d3 table by giving it the div, a config file, and data
26 27
...@@ -60,87 +61,92 @@ ...@@ -60,87 +61,92 @@
60 return table; 61 return table;
61 } 62 }
62 63
63 - // Functions for creating a fixed-header on a table (Angular Directive) 64 + // Functions for creating a fixed header on a table (Angular Directive)
64 65
65 - function setColWidth(t) { 66 + function setTableWidth(t) {
66 - var tHeaders, tdElement, colWidth; 67 + var tHeaders, tdElement, colWidth, numCols,
68 + winWidth = fs.windowSize().width;
67 69
68 tHeaders = t.selectAll('th'); 70 tHeaders = t.selectAll('th');
71 + numCols = tHeaders[0].length;
72 + colWidth = Math.floor(winWidth/numCols);
69 73
70 - // select each td in the first row and set the header's width to the 74 + tHeaders.each(function(thElement, index) {
71 - // corresponding td's width, if td is larger than header's width.
72 - tHeaders.each(function(thElement, index){
73 thElement = d3.select(this); 75 thElement = d3.select(this);
74 76
75 tdElement = t.select('td:nth-of-type(' + (index + 1) + ')'); 77 tdElement = t.select('td:nth-of-type(' + (index + 1) + ')');
76 - colWidth = tdElement.style('width');
77 78
78 - thElement.style('width', colWidth); 79 + // if the header has no text in it,
79 - tdElement.style('width', colWidth); 80 + // then make the column the width of the td element.
81 + // (This looks good for icons)
82 + if (!(thElement.html().length)) {
83 + var tdSize = tdElement.style('width');
84 + thElement.style('width', tdSize + 'px');
85 + tdElement.style('width', tdSize + 'px');
86 + }
87 + else {
88 + thElement.style('width', colWidth + 'px');
89 + tdElement.style('width', colWidth + 'px');
90 + }
80 }); 91 });
81 } 92 }
82 93
83 - function setCSS(thead, tbody, height) { 94 + function setTableHeight(thead, tbody) {
95 + var winHeight = fs.windowSize().height;
96 +
84 thead.style('display', 'block'); 97 thead.style('display', 'block');
85 tbody.style({'display': 'block', 98 tbody.style({'display': 'block',
86 - 'height': height || '500px', 99 + 'height': ((winHeight - bottomMargin) + 'px'),
87 'overflow': 'auto' 100 'overflow': 'auto'
88 }); 101 });
89 } 102 }
90 103
91 function fixTable(t, th, tb, height) { 104 function fixTable(t, th, tb, height) {
92 - setColWidth(t); 105 + setTableWidth(t);
93 - setCSS(th, tb, height); 106 + setTableHeight(th, tb, height);
94 } 107 }
95 108
96 angular.module('onosWidget') 109 angular.module('onosWidget')
97 - .factory('TableService', ['$log', function (_$log_) { 110 + .factory('TableService', [function () {
98 - $log = _$log_;
99 -
100 return { 111 return {
101 renderTable: renderTable 112 renderTable: renderTable
102 }; 113 };
103 }]) 114 }])
104 115
105 - .directive('fixedHeader', ['$timeout', function ($timeout) { 116 + // TODO: find another solution other than timeout for waiting for ng-repeat to end
117 + .directive('onosFixedHeader', ['$window', '$timeout',
118 + 'MastService', 'FnService',
119 + function (_$window_, $timeout, mast, _fs_) {
120 + return function (scope, element) {
121 + $window = _$window_;
122 + fs = _fs_;
123 + var w = angular.element($window),
124 + table = d3.select(element[0]);
125 +
126 + scope.$watch(function () {
106 return { 127 return {
107 - restrict: 'A', 128 + h: window.innerHeight,
108 - scope: { 129 + w: window.innerWidth
109 - tableHeight: '@' 130 + };
110 - }, 131 + }, function (newVal) {
111 - 132 + var thead = table.select('thead'),
112 - link: function (scope, element) {
113 - // TODO: look into other solutions than $timeout --
114 - // fixed-header directive called before ng-repeat was
115 - // finished; using $scope.$emit to notify this directive
116 - // to fire was not working.
117 - $timeout(function() {
118 - var table = d3.select(element[0]),
119 - thead = table.select('thead'),
120 tbody = table.select('tbody'); 133 tbody = table.select('tbody');
121 134
122 - // wait until the table is visible 135 + scope.windowHeight = newVal.h;
123 - // (offsetParent returns null if display is "none") 136 + scope.windowWidth = newVal.w;
124 - scope.$watch(
125 - function () {
126 - return (!(table.offsetParent === null));
127 - },
128 - function(newValue) {
129 - if (newValue === true) {
130 -
131 - // ensure thead and tbody have no display
132 - thead.style('display', null);
133 - tbody.style('display', null);
134 137
138 + scope.setTableHW = function () {
135 $timeout(function () { 139 $timeout(function () {
136 - fixTable(table, thead, tbody, 140 + fixTable(table, thead, tbody);
137 - scope.tableHeight); 141 + }, 250);
138 - }); 142 + };
139 - } 143 + }, true);
144 +
145 + w.bind('onos-fixed-header', function () {
146 + scope.$apply();
140 }); 147 });
141 - }, 200);
142 - }
143 }; 148 };
149 +
144 }]); 150 }]);
145 151
146 }()); 152 }());
......
1 <!-- Device partial HTML --> 1 <!-- Device partial HTML -->
2 <div id="ov-device"> 2 <div id="ov-device">
3 - <h2>Device View</h2> 3 + <h2>Devices</h2>
4 - <table class="summary-list" fixed-header> 4 + <table class="summary-list"
5 + onos-fixed-header
6 + ng-style="setTableHW()">
5 <thead> 7 <thead>
6 <tr> 8 <tr>
7 <th></th> 9 <th></th>
......