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