Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 23 | var $window, fs, |
| 24 | bottomMargin = 200; |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 25 | |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 26 | // Render a plain d3 table by giving it the div, a config file, and data |
| 27 | |
Bri Prebilic Cole | 4c89177 | 2015-01-27 11:38:51 -0800 | [diff] [blame] | 28 | function renderTable(div, config, data) { |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 29 | var table = div.append('table'), |
Bri Prebilic Cole | 4c89177 | 2015-01-27 11:38:51 -0800 | [diff] [blame] | 30 | colIds = config.colIds, |
| 31 | colText = config.colText, |
| 32 | dataObjects = data[Object.keys(data)[0]], |
| 33 | thead, tbody, tRows; |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 34 | |
Bri Prebilic Cole | 4c89177 | 2015-01-27 11:38:51 -0800 | [diff] [blame] | 35 | thead = table.append('thead'); |
| 36 | tbody = table.append('tbody'); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 37 | |
Bri Prebilic Cole | 4c89177 | 2015-01-27 11:38:51 -0800 | [diff] [blame] | 38 | thead.append('tr').selectAll('th') |
| 39 | .data(colText) |
| 40 | .enter() |
| 41 | .append('th') |
| 42 | .text(function(d) { return d }); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 43 | |
Bri Prebilic Cole | 4c89177 | 2015-01-27 11:38:51 -0800 | [diff] [blame] | 44 | tRows = tbody.selectAll('tr') |
| 45 | .data(dataObjects) |
| 46 | .enter() |
| 47 | .append('tr'); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 48 | |
Bri Prebilic Cole | 4c89177 | 2015-01-27 11:38:51 -0800 | [diff] [blame] | 49 | tRows.selectAll('td') |
| 50 | .data(function(row) { |
| 51 | return colIds.map(function(headerId) { |
| 52 | return { |
| 53 | column: headerId, value: row[headerId] |
| 54 | }; |
| 55 | }); |
| 56 | }) |
| 57 | .enter() |
| 58 | .append('td') |
| 59 | .html(function(d) { return d.value }); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 60 | |
Bri Prebilic Cole | 4c89177 | 2015-01-27 11:38:51 -0800 | [diff] [blame] | 61 | return table; |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 64 | // Functions for creating a fixed header on a table (Angular Directive) |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 65 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 66 | function setTableWidth(t) { |
| 67 | var tHeaders, tdElement, colWidth, numCols, |
| 68 | winWidth = fs.windowSize().width; |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 69 | |
| 70 | tHeaders = t.selectAll('th'); |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 71 | numCols = tHeaders[0].length; |
| 72 | colWidth = Math.floor(winWidth/numCols); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 73 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 74 | tHeaders.each(function(thElement, index) { |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 75 | thElement = d3.select(this); |
| 76 | |
| 77 | tdElement = t.select('td:nth-of-type(' + (index + 1) + ')'); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 78 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 79 | // if the header has no text in it, |
| 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 | } |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 91 | }); |
| 92 | } |
| 93 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 94 | function setTableHeight(thead, tbody) { |
| 95 | var winHeight = fs.windowSize().height; |
| 96 | |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 97 | thead.style('display', 'block'); |
| 98 | tbody.style({'display': 'block', |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 99 | 'height': ((winHeight - bottomMargin) + 'px'), |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 100 | 'overflow': 'auto' |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | function fixTable(t, th, tb, height) { |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 105 | setTableWidth(t); |
| 106 | setTableHeight(th, tb, height); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 109 | angular.module('onosWidget') |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 110 | .factory('TableService', [function () { |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 111 | return { |
Bri Prebilic Cole | 4c89177 | 2015-01-27 11:38:51 -0800 | [diff] [blame] | 112 | renderTable: renderTable |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 113 | }; |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 114 | }]) |
| 115 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 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]); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 125 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 126 | scope.$watch(function () { |
| 127 | return { |
| 128 | h: window.innerHeight, |
| 129 | w: window.innerWidth |
| 130 | }; |
| 131 | }, function (newVal) { |
| 132 | var thead = table.select('thead'), |
| 133 | tbody = table.select('tbody'); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 134 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 135 | scope.windowHeight = newVal.h; |
| 136 | scope.windowWidth = newVal.w; |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 137 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 138 | scope.setTableHW = function () { |
| 139 | $timeout(function () { |
| 140 | fixTable(table, thead, tbody); |
| 141 | }, 250); |
| 142 | }; |
| 143 | }, true); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 144 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame^] | 145 | w.bind('onos-fixed-header', function () { |
| 146 | scope.$apply(); |
| 147 | }); |
| 148 | }; |
| 149 | |
| 150 | }]); |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 151 | |
| 152 | }()); |