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 | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 40 | // Functions for creating a scrolling table body with fixed table header |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 41 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 42 | function _width(elem, width) { |
| 43 | elem.style('width', width); |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 44 | } |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 45 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 46 | function defaultSize(table, width) { |
| 47 | var thead = table.select('.table-header').select('table'), |
| 48 | tbody = table.select('.table-body').select('table'), |
| 49 | wpx = width + 'px'; |
| 50 | _width(thead, wpx); |
| 51 | _width(tbody, wpx); |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 52 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 53 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 54 | function adjustTable(table, width, height) { |
| 55 | var thead = table.select('.table-header').select('table'), |
| 56 | tbodyDiv = table.select('.table-body'), |
| 57 | tbody = tbodyDiv.select('table'), |
| 58 | cstmWidths = {}; |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 59 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 60 | function findCstmWidths() { |
| 61 | var headers = thead.selectAll('td'); |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 62 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 63 | headers.each(function (d, i) { |
| 64 | var h = d3.select(this), |
| 65 | index = i.toString(); |
| 66 | if (h.classed(tableIcon)) { |
| 67 | cstmWidths[index] = tableIconTdSize + 'px'; |
| 68 | } |
| 69 | if (h.attr(colWidth)) { |
| 70 | cstmWidths[index] = h.attr(colWidth); |
| 71 | } |
| 72 | }); |
| 73 | $log.debug('Headers with custom widths: ', cstmWidths); |
| 74 | } |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 75 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 76 | function setTdWidths(elem) { |
| 77 | var tds = elem.selectAll('tr:not(.ignore-width)').selectAll('td'); |
| 78 | _width(elem, width + 'px'); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 79 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 80 | tds.each(function (d, i) { |
| 81 | var td = d3.select(this), |
| 82 | index = i.toString(); |
| 83 | if (cstmWidths.hasOwnProperty(index)) { |
| 84 | _width(td, cstmWidths[index]); |
| 85 | } |
| 86 | }); |
| 87 | } |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 88 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 89 | function setHeight(body) { |
| 90 | var h = height - (mast.mastHeight() + |
| 91 | fs.noPxStyle(d3.select('.tabular-header'), 'height') + |
| 92 | fs.noPxStyle(thead, 'height') + pdg); |
| 93 | body.style('height', h + 'px'); |
| 94 | } |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 95 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 96 | findCstmWidths(); |
| 97 | setTdWidths(thead); |
| 98 | setTdWidths(tbody); |
| 99 | setHeight(tbodyDiv); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 100 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 101 | cstmWidths = {}; |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 104 | // Functions for sorting table rows by header |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 105 | |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 106 | function updateSortDirection(thElem) { |
| 107 | sortIconAPI.sortNone(thElem.select('div')); |
| 108 | currCol.div = thElem.append('div'); |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 109 | currCol.colId = thElem.attr('colId'); |
| 110 | |
| 111 | if (currCol.colId === prevCol.colId) { |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 112 | (currCol.dir === desc) ? currCol.dir = asc : currCol.dir = desc; |
| 113 | prevCol.dir = currCol.dir; |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 114 | } else { |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 115 | currCol.dir = asc; |
| 116 | prevCol.dir = none; |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 117 | } |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 118 | (currCol.dir === asc) ? |
| 119 | sortIconAPI.sortAsc(currCol.div) : sortIconAPI.sortDesc(currCol.div); |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 120 | |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 121 | if (prevCol.colId && prevCol.dir === none) { |
| 122 | sortIconAPI.sortNone(prevCol.div); |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | prevCol.colId = currCol.colId; |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 126 | prevCol.div = currCol.div; |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 129 | function sortRequestParams() { |
| 130 | return { |
| 131 | sortCol: currCol.colId, |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 132 | sortDir: currCol.dir |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 133 | }; |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 136 | function resetSortIcons() { |
| 137 | if (currCol.div) { |
| 138 | sortIconAPI.sortNone(currCol.div); |
| 139 | } |
| 140 | if (prevCol.div) { |
| 141 | sortIconAPI.sortNone(prevCol.div); |
| 142 | } |
| 143 | currCol = {}; |
| 144 | prevCol = {}; |
| 145 | } |
| 146 | |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 147 | angular.module('onosWidget') |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 148 | .directive('onosFixedHeader', ['$log','$window', |
| 149 | 'FnService', 'MastService', |
| 150 | |
| 151 | function (_$log_, _$window_, _fs_, _mast_) { |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 152 | return function (scope, element) { |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 153 | $log = _$log_; |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 154 | $window = _$window_; |
| 155 | fs = _fs_; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 156 | mast = _mast_; |
| 157 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 158 | var w = angular.element($window), |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 159 | table = d3.select(element[0]), |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 160 | canAdjust = false; |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 161 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 162 | scope.$watch(function () { |
| 163 | return { |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 164 | h: $window.innerHeight, |
| 165 | w: $window.innerWidth |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 166 | }; |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 167 | }, function () { |
| 168 | var wsz = fs.windowSize(0, 30), |
| 169 | wWidth = wsz.width, |
| 170 | wHeight = wsz.height; |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 171 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 172 | if (!scope.tableData.length) { |
| 173 | defaultSize(table, wWidth); |
| 174 | } |
Simon Hunt | 0c2c4c5 | 2015-04-02 17:42:45 -0700 | [diff] [blame] | 175 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 176 | scope.$on('LastElement', function () { |
| 177 | // only adjust the table once it's completely loaded |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 178 | adjustTable(table, wWidth, wHeight); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 179 | canAdjust = true; |
| 180 | }); |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 181 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 182 | if (canAdjust) { |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 183 | adjustTable(table, wWidth, wHeight); |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 184 | } |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 185 | }, true); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 186 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 187 | w.bind('onos-fixed-header', function () { |
| 188 | scope.$apply(); |
| 189 | }); |
| 190 | }; |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 191 | }]) |
| 192 | |
| 193 | .directive('onosSortableHeader', ['$log', 'IconService', |
| 194 | function (_$log_, _is_) { |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 195 | return { |
| 196 | scope: { |
| 197 | ctrlCallback: '&sortCallback' |
| 198 | }, |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 199 | link: function (scope, element) { |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 200 | $log = _$log_; |
| 201 | is = _is_; |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 202 | var header = d3.select(element[0]); |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 203 | sortIconAPI = is.sortIcons(); |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 204 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 205 | // when a header is clicked, change its sort direction |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 206 | // and get sorting order to send to the server. |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 207 | header.selectAll('td').on('click', function () { |
| 208 | var col = d3.select(this); |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 209 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 210 | if (col.attr('sortable') === '') { |
| 211 | updateSortDirection(col); |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 212 | scope.ctrlCallback({ |
Bri Prebilic Cole | 72eb6db | 2015-03-30 16:58:53 -0700 | [diff] [blame] | 213 | requestParams: sortRequestParams() |
| 214 | }); |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 215 | } |
| 216 | }); |
| 217 | } |
| 218 | }; |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 219 | }]) |
| 220 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 221 | .factory('TableService', ['IconService', |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 222 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 223 | function (is) { |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 224 | sortIconAPI = is.sortIcons(); |
| 225 | |
| 226 | return { |
| 227 | resetSortIcons: resetSortIcons |
| 228 | }; |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 229 | }]); |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 230 | |
| 231 | }()); |