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