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 | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 23 | // injected refs |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 24 | var $log, $window, fs, mast, is; |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 25 | |
| 26 | // constants |
| 27 | var tableIconTdSize = 33, |
Bri Prebilic Cole | 4506938 | 2015-04-14 15:21:38 -0700 | [diff] [blame] | 28 | pdg = 12, |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 29 | colWidth = 'col-width', |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 30 | tableIcon = 'table-icon', |
| 31 | asc = 'asc', |
| 32 | desc = 'desc', |
| 33 | none = 'none'; |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 34 | |
| 35 | // internal state |
| 36 | var currCol = {}, |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 37 | prevCol = {}, |
| 38 | sortIconAPI; |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 39 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 40 | // Functions for creating a fixed header on a table (Angular Directive) |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 41 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 42 | function setElemWidth(elem, size) { |
| 43 | elem.style('width', size + 'px') |
| 44 | } |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 45 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 46 | function setColWidth(th, td, size) { |
| 47 | setElemWidth(th, size); |
| 48 | setElemWidth(td, size); |
| 49 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 50 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 51 | // count number of headers of |
| 52 | // - assigned width, |
| 53 | // - icon width, |
| 54 | // - and default width |
| 55 | // assumes assigned width is not given to icons |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 56 | // returns the width of all columns that are not icons |
| 57 | // or have an assigned width |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 58 | function getDefaultWidth(headers) { |
| 59 | var winWidth = fs.windowSize().width, |
| 60 | iconCols = 0, |
| 61 | regCols = 0, |
| 62 | cstmColWidth = 0; |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 63 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 64 | headers.each(function (d, i) { |
| 65 | var thElement = d3.select(this), |
| 66 | cstmWidth = thElement.attr(colWidth); |
| 67 | |
| 68 | if (cstmWidth) { |
| 69 | cstmColWidth += fs.noPx(cstmWidth); |
| 70 | } else if (thElement.classed(tableIcon)) { |
| 71 | iconCols += 1; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 72 | } else { |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 73 | regCols += 1; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 74 | } |
| 75 | }); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 76 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 77 | return Math.floor((winWidth - cstmColWidth - |
| 78 | (iconCols * tableIconTdSize)) / regCols); |
| 79 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 80 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 81 | function setTableWidth(t) { |
| 82 | var tHeaders = t.selectAll('th'), |
| 83 | defaultColWidth = getDefaultWidth(tHeaders); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 84 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 85 | tHeaders.each(function (d, i) { |
| 86 | var thElement = d3.select(this), |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 87 | tr = t.select('tr:nth-of-type(2)'), |
| 88 | tdElement = tr.select('td:nth-of-type(' + (i + 1) + ')'), |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 89 | custWidth = thElement.attr(colWidth); |
| 90 | |
| 91 | if (custWidth) { |
| 92 | setColWidth(thElement, tdElement, fs.noPx(custWidth)); |
| 93 | } else if (thElement.classed(tableIcon)) { |
| 94 | setColWidth(thElement, tdElement, tableIconTdSize); |
Bri Prebilic Cole | 357f7fd | 2015-02-12 17:03:42 -0800 | [diff] [blame] | 95 | } else { |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 96 | setColWidth(thElement, tdElement, defaultColWidth); |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 97 | } |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 98 | }); |
| 99 | } |
| 100 | |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 101 | // get the size of the window and then subtract the extra space at the top |
| 102 | // to get the height of the table |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 103 | function setTableHeight(thead, tbody) { |
Bri Prebilic Cole | 4506938 | 2015-04-14 15:21:38 -0700 | [diff] [blame] | 104 | var ttlHgt = fs.noPxStyle(d3.select('.tabular-header'), 'height'), |
| 105 | thHgt = fs.noPxStyle(thead, 'height'), |
| 106 | totalHgt = ttlHgt + thHgt + pdg, |
| 107 | tbleHgt = fs.windowSize(mast.mastHeight() + totalHgt).height; |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 108 | |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 109 | thead.style('display', 'block'); |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 110 | tbody.style({ |
| 111 | display: 'block', |
Bri Prebilic Cole | 4506938 | 2015-04-14 15:21:38 -0700 | [diff] [blame] | 112 | height: tbleHgt + 'px', |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 113 | overflow: 'auto' |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 114 | }); |
| 115 | } |
| 116 | |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 117 | function fixTable(t, th, tb) { |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 118 | setTableWidth(t); |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 119 | setTableHeight(th, tb); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 122 | // Functions for sorting table rows by header |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 123 | |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 124 | function updateSortDirection(thElem) { |
| 125 | sortIconAPI.sortNone(thElem.select('div')); |
| 126 | currCol.div = thElem.append('div'); |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 127 | currCol.colId = thElem.attr('colId'); |
| 128 | |
| 129 | if (currCol.colId === prevCol.colId) { |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 130 | (currCol.dir === desc) ? currCol.dir = asc : currCol.dir = desc; |
| 131 | prevCol.dir = currCol.dir; |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 132 | } else { |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 133 | currCol.dir = asc; |
| 134 | prevCol.dir = none; |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 135 | } |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 136 | (currCol.dir === asc) ? |
| 137 | sortIconAPI.sortAsc(currCol.div) : sortIconAPI.sortDesc(currCol.div); |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 138 | |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 139 | if (prevCol.colId && prevCol.dir === none) { |
| 140 | sortIconAPI.sortNone(prevCol.div); |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | prevCol.colId = currCol.colId; |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 144 | prevCol.div = currCol.div; |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 147 | function sortRequestParams() { |
| 148 | return { |
| 149 | sortCol: currCol.colId, |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 150 | sortDir: currCol.dir |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 151 | }; |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 154 | function resetSortIcons() { |
| 155 | if (currCol.div) { |
| 156 | sortIconAPI.sortNone(currCol.div); |
| 157 | } |
| 158 | if (prevCol.div) { |
| 159 | sortIconAPI.sortNone(prevCol.div); |
| 160 | } |
| 161 | currCol = {}; |
| 162 | prevCol = {}; |
| 163 | } |
| 164 | |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 165 | angular.module('onosWidget') |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 166 | .directive('onosFixedHeader', ['$window', 'FnService', 'MastService', |
| 167 | function (_$window_, _fs_, _mast_) { |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 168 | return function (scope, element) { |
| 169 | $window = _$window_; |
| 170 | fs = _fs_; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 171 | mast = _mast_; |
| 172 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 173 | var w = angular.element($window), |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 174 | table = d3.select(element[0]), |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 175 | thead = table.select('thead'), |
| 176 | tbody = table.select('tbody'), |
| 177 | canAdjust = false; |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 178 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 179 | scope.$watch(function () { |
| 180 | return { |
| 181 | h: window.innerHeight, |
| 182 | w: window.innerWidth |
| 183 | }; |
| 184 | }, function (newVal) { |
Simon Hunt | 0c2c4c5 | 2015-04-02 17:42:45 -0700 | [diff] [blame] | 185 | var wsz = fs.windowSize(0, 30); |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 186 | scope.windowHeight = newVal.h; |
| 187 | scope.windowWidth = newVal.w; |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 188 | |
Simon Hunt | 0c2c4c5 | 2015-04-02 17:42:45 -0700 | [diff] [blame] | 189 | // default table size in case no data elements |
| 190 | table.style('width', wsz.width + 'px'); |
| 191 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 192 | scope.$on('LastElement', function () { |
| 193 | // only adjust the table once it's completely loaded |
| 194 | fixTable(table, thead, tbody); |
| 195 | canAdjust = true; |
| 196 | }); |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 197 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 198 | if (canAdjust) { |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 199 | fixTable(table, thead, tbody); |
| 200 | } |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 201 | }, true); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 202 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 203 | w.bind('onos-fixed-header', function () { |
| 204 | scope.$apply(); |
| 205 | }); |
| 206 | }; |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 207 | }]) |
| 208 | |
| 209 | .directive('onosSortableHeader', ['$log', 'IconService', |
| 210 | function (_$log_, _is_) { |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 211 | return { |
| 212 | scope: { |
| 213 | ctrlCallback: '&sortCallback' |
| 214 | }, |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 215 | link: function (scope, element) { |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 216 | $log = _$log_; |
| 217 | is = _is_; |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 218 | var table = d3.select(element[0]); |
| 219 | sortIconAPI = is.sortIcons(); |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 220 | |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 221 | // when a header is clicked, change its icon tag |
| 222 | // and get sorting order to send to the server. |
| 223 | table.selectAll('th').on('click', function () { |
| 224 | var thElem = d3.select(this); |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 225 | |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 226 | if (thElem.attr('sortable') === '') { |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 227 | updateSortDirection(thElem); |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 228 | scope.ctrlCallback({ |
Bri Prebilic Cole | 72eb6db | 2015-03-30 16:58:53 -0700 | [diff] [blame] | 229 | requestParams: sortRequestParams() |
| 230 | }); |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 231 | } |
| 232 | }); |
| 233 | } |
| 234 | }; |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 235 | }]) |
| 236 | |
| 237 | .factory('TableService', ['$log', 'IconService', |
| 238 | |
| 239 | function ($log, is) { |
| 240 | sortIconAPI = is.sortIcons(); |
| 241 | |
| 242 | return { |
| 243 | resetSortIcons: resetSortIcons |
| 244 | }; |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 245 | }]); |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 246 | |
| 247 | }()); |