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 | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame^] | 23 | var $log, $window, fs, is, |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 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 | |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame^] | 104 | function fixTable(t, th, tb) { |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 105 | setTableWidth(t); |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame^] | 106 | setTableHeight(th, tb); |
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 | .directive('onosFixedHeader', ['$window', '$timeout', |
| 117 | 'MastService', 'FnService', |
| 118 | function (_$window_, $timeout, mast, _fs_) { |
| 119 | return function (scope, element) { |
| 120 | $window = _$window_; |
| 121 | fs = _fs_; |
| 122 | var w = angular.element($window), |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 123 | table = d3.select(element[0]), |
| 124 | shouldResize = false; |
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 () { |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 139 | scope.$on('LastElement', function (event) { |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame^] | 140 | // only adjust the table once it's completely loaded |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 141 | fixTable(table, thead, tbody); |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 142 | shouldResize = true; |
| 143 | }); |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 144 | }; |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 145 | |
| 146 | if (shouldResize) { |
| 147 | fixTable(table, thead, tbody); |
| 148 | } |
| 149 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 150 | }, true); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 151 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 152 | w.bind('onos-fixed-header', function () { |
| 153 | scope.$apply(); |
| 154 | }); |
| 155 | }; |
| 156 | |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame^] | 157 | }]) |
| 158 | |
| 159 | .directive('onosSortableHeader', ['$log', 'IconService', |
| 160 | function (_$log_, _is_) { |
| 161 | return function (scope, element, attrs) { |
| 162 | $log = _$log_; |
| 163 | is = _is_; |
| 164 | var table = d3.select(element[0]), |
| 165 | currCol = {}, |
| 166 | prevCol = {}, |
| 167 | sortIconAPI = is.createSortIcon(); |
| 168 | |
| 169 | // when a header is clicked, change its icon tag and get sorting |
| 170 | // order to send to the server. |
| 171 | table.selectAll('th').on('click', function () { |
| 172 | var thElem = d3.select(this), |
| 173 | div; |
| 174 | |
| 175 | currCol.colId = thElem.attr('colId'); |
| 176 | |
| 177 | if (currCol.colId === prevCol.colId) { |
| 178 | (currCol.icon === 'tableColSortDesc') ? |
| 179 | currCol.icon = 'tableColSortAsc' : |
| 180 | currCol.icon = 'tableColSortDesc'; |
| 181 | prevCol.icon = currCol.icon; |
| 182 | } else { |
| 183 | currCol.icon = 'tableColSortAsc'; |
| 184 | prevCol.icon = 'tableColSortNone'; |
| 185 | } |
| 186 | |
| 187 | $log.debug('currCol clicked: ' + currCol.colId + |
| 188 | ', with sorting icon: ' + currCol.icon); |
| 189 | $log.debug('prevCol clicked: ' + prevCol.colId + |
| 190 | ', with its current sorting icon as ' + prevCol.icon); |
| 191 | |
| 192 | div = thElem.select('div'); |
| 193 | div.remove(); |
| 194 | |
| 195 | div = thElem.append('div'); |
| 196 | |
| 197 | if (currCol.icon === 'tableColSortAsc') { |
| 198 | sortIconAPI.sortAsc(div); |
| 199 | } else { |
| 200 | sortIconAPI.sortDesc(div); |
| 201 | } |
| 202 | |
| 203 | if (prevCol.colId !== undefined && |
| 204 | prevCol.icon === 'tableColSortNone') { |
| 205 | sortIconAPI.sortNone(prevCol.elem.select('div')); |
| 206 | } |
| 207 | |
| 208 | prevCol.colId = currCol.colId; |
| 209 | prevCol.elem = thElem; |
| 210 | |
| 211 | }); |
| 212 | |
| 213 | // TODO: send the prev and currCol info to the server to use in sorting table |
| 214 | |
| 215 | // TODO: figure out timing of events: |
| 216 | // updating the icon |
| 217 | // sending the column sorting info to the server |
| 218 | // refreshing the table so that the new rows will be sorted |
| 219 | |
| 220 | } |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 221 | }]); |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 222 | |
| 223 | }()); |