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; |
| 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]), |
| 124 | shouldResize = false; |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 125 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 126 | scope.$watch(function () { |
| 127 | return { |
| 128 | h: window.innerHeight, |
| 129 | w: window.innerWidth |
| 130 | }; |
| 131 | }, function (newVal) { |
| 132 | var thead = table.select('thead'), |
| 133 | tbody = table.select('tbody'); |
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.windowHeight = newVal.h; |
| 136 | scope.windowWidth = newVal.w; |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 137 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 138 | scope.setTableHW = function () { |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 139 | scope.$on('LastElement', function (event) { |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 140 | // only adjust the table once it's completely loaded |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 141 | fixTable(table, thead, tbody); |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 142 | shouldResize = true; |
| 143 | }); |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 144 | }; |
Bri Prebilic Cole | b0e66be | 2015-02-10 10:53:15 -0800 | [diff] [blame] | 145 | |
| 146 | if (shouldResize) { |
| 147 | fixTable(table, thead, tbody); |
| 148 | } |
| 149 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 150 | }, true); |
Bri Prebilic Cole | 9ca0f9c | 2015-01-29 15:44:20 -0800 | [diff] [blame] | 151 | |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 152 | w.bind('onos-fixed-header', function () { |
| 153 | scope.$apply(); |
| 154 | }); |
| 155 | }; |
| 156 | |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 157 | }]) |
| 158 | |
| 159 | .directive('onosSortableHeader', ['$log', 'IconService', |
| 160 | function (_$log_, _is_) { |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 161 | return { |
| 162 | scope: { |
| 163 | ctrlCallback: '&sortCallback' |
| 164 | }, |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame^] | 165 | link: function (scope, element) { |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 166 | $log = _$log_; |
| 167 | is = _is_; |
| 168 | var table = d3.select(element[0]), |
| 169 | sortIconAPI = is.createSortIcon(); |
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 | // when a header is clicked, change its icon tag |
| 172 | // and get sorting order to send to the server. |
| 173 | table.selectAll('th').on('click', function () { |
| 174 | var thElem = d3.select(this); |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 175 | |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 176 | if (thElem.attr('sortable') === '') { |
| 177 | updateSortingIcons(thElem, sortIconAPI); |
Bri Prebilic Cole | 902cb04 | 2015-02-11 14:04:15 -0800 | [diff] [blame] | 178 | scope.ctrlCallback({ |
| 179 | urlSuffix: generateQueryParams() |
| 180 | }); |
| 181 | } |
| 182 | }); |
| 183 | } |
| 184 | }; |
Bri Prebilic Cole | e4d5c6c | 2015-02-02 15:52:45 -0800 | [diff] [blame] | 185 | }]); |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 186 | |
| 187 | }()); |