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