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