blob: 5a76e0f3acaa8c7938db2e24969bd837c374579d [file] [log] [blame]
Bri Prebilic Cole093739a2015-01-23 10:22:50 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Widget -- Table Service
19 */
20(function () {
21 'use strict';
22
23 var $log;
24
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080025 // Render a plain d3 table by giving it the div, a config file, and data
26
Bri Prebilic Cole4c891772015-01-27 11:38:51 -080027 function renderTable(div, config, data) {
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080028 var table = div.append('table'),
Bri Prebilic Cole4c891772015-01-27 11:38:51 -080029 colIds = config.colIds,
30 colText = config.colText,
31 dataObjects = data[Object.keys(data)[0]],
32 thead, tbody, tRows;
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080033
Bri Prebilic Cole4c891772015-01-27 11:38:51 -080034 thead = table.append('thead');
35 tbody = table.append('tbody');
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080036
Bri Prebilic Cole4c891772015-01-27 11:38:51 -080037 thead.append('tr').selectAll('th')
38 .data(colText)
39 .enter()
40 .append('th')
41 .text(function(d) { return d });
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080042
Bri Prebilic Cole4c891772015-01-27 11:38:51 -080043 tRows = tbody.selectAll('tr')
44 .data(dataObjects)
45 .enter()
46 .append('tr');
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080047
Bri Prebilic Cole4c891772015-01-27 11:38:51 -080048 tRows.selectAll('td')
49 .data(function(row) {
50 return colIds.map(function(headerId) {
51 return {
52 column: headerId, value: row[headerId]
53 };
54 });
55 })
56 .enter()
57 .append('td')
58 .html(function(d) { return d.value });
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080059
Bri Prebilic Cole4c891772015-01-27 11:38:51 -080060 return table;
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080061 }
62
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080063 // Functions for creating a fixed-header on a table (Angular Directive)
64
65 function setColWidth(t) {
66 var tHeaders, tdElement, colWidth;
67
68 tHeaders = t.selectAll('th');
69
70 // select each td in the first row and set the header's width to the
71 // corresponding td's width, if td is larger than header's width.
72 tHeaders.each(function(thElement, index){
73 thElement = d3.select(this);
74
75 tdElement = t.select('td:nth-of-type(' + (index + 1) + ')');
76 colWidth = tdElement.style('width');
77
78 thElement.style('width', colWidth);
79 tdElement.style('width', colWidth);
80 });
81 }
82
83 function setCSS(thead, tbody, height) {
84 thead.style('display', 'block');
85 tbody.style({'display': 'block',
86 'height': height || '500px',
87 'overflow': 'auto'
88 });
89 }
90
91 function fixTable(t, th, tb, height) {
92 setColWidth(t);
93 setCSS(th, tb, height);
94 }
95
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080096 angular.module('onosWidget')
97 .factory('TableService', ['$log', function (_$log_) {
98 $log = _$log_;
99
100 return {
Bri Prebilic Cole4c891772015-01-27 11:38:51 -0800101 renderTable: renderTable
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800102 };
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800103 }])
104
105 .directive('fixedHeader', ['$timeout', function ($timeout) {
106 return {
107 restrict: 'A',
108 scope: {
109 tableHeight: '@'
110 },
111
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');
121
122 // wait until the table is visible
123 // (offsetParent returns null if display is "none")
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
135 $timeout(function () {
136 fixTable(table, thead, tbody,
137 scope.tableHeight);
138 });
139 }
140 });
141 }, 200);
142 }
143 };
144 }]);
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800145
146}());