Bri Prebilic Cole | 4f0d977 | 2015-01-21 16:45:14 -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 -- Showing Icons Test Module |
| 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
| 23 | |
| 24 | function setColWidth($log, t) { |
| 25 | var tHeaders, tdElement; |
| 26 | |
| 27 | // for each th, set its width to td's width |
| 28 | // do this by looping through each td in the first row |
| 29 | // and finding its column width |
| 30 | tHeaders = t.selectAll('th'); |
| 31 | //loop through selectAll array |
| 32 | tHeaders.forEach(function(thElement){ |
| 33 | tdElement = t.filter('nth-child()'); |
| 34 | $log.log(thElement); |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | function setCSS(thead, tbody) { |
| 39 | |
| 40 | } |
| 41 | |
| 42 | function trimTable(tbody) { |
| 43 | |
| 44 | } |
| 45 | |
| 46 | function fixTable($log, t, th, tb) { |
| 47 | setColWidth($log, t); |
| 48 | setCSS(th, tb); |
| 49 | trimTable(tb); |
| 50 | } |
| 51 | |
| 52 | angular.module('practiceTable', []) |
| 53 | |
| 54 | .directive('fixedHeader', ['$log', '$timeout', function ($log, $timeout) { |
| 55 | return { |
| 56 | restrict: 'A', |
| 57 | |
| 58 | link: function (scope, element, attrs) { |
| 59 | var table = d3.select(element[0]), |
| 60 | thead = table.select('thead'), |
| 61 | tbody = table.select('tbody'); |
| 62 | |
| 63 | // wait until the table is visible |
| 64 | scope.$watch( |
| 65 | function () { return (!(table.offsetParent === null)); }, |
| 66 | function (newValue, oldValue) { |
| 67 | if (newValue === true) { |
| 68 | $log.log('table is visible'); |
| 69 | |
| 70 | // ensure thead and tbody have no display |
| 71 | thead.style('display', null); |
| 72 | tbody.style('display', null); |
| 73 | |
| 74 | $timeout(function() { |
| 75 | $log.log('timeout is over'); |
| 76 | fixTable($log, table, thead, tbody); |
| 77 | }, 200); |
| 78 | } |
| 79 | }); |
| 80 | $log.log('fixedHeader directive has been created'); |
| 81 | } |
| 82 | }; |
| 83 | }]); |
| 84 | }()); |
| 85 | |
| 86 | $scope.$watch(function () { return $elem.find("tbody").is(':visible') }, |
| 87 | function (newValue, oldValue) { |
| 88 | if (newValue === true) { |
| 89 | // reset display styles so column widths are correct when measured below |
| 90 | $elem.find('thead, tbody, tfoot').css('display', ''); |
| 91 | |
| 92 | // wrap in $timeout to give table a chance to finish rendering |
| 93 | $timeout(function () { |
| 94 | // set widths of columns |
| 95 | $elem.find('th').each(function (i, thElem) { |
| 96 | thElem = $(thElem); |
| 97 | var tdElems = $elem.find('tbody tr:first td:nth-child(' + (i + 1) + ')'); |
| 98 | var tfElems = $elem.find('tfoot tr:first td:nth-child(' + (i + 1) + ')'); |
| 99 | |
| 100 | var columnWidth = tdElems.width(); |
| 101 | thElem.width(columnWidth); |
| 102 | tdElems.width(columnWidth); |
| 103 | tfElems.width(columnWidth); |
| 104 | }); |
| 105 | |
| 106 | // set css styles on thead and tbody |
| 107 | $elem.find('thead, tfoot').css({ |
| 108 | 'display': 'block', |
| 109 | }); |
| 110 | |
| 111 | $elem.find('tbody').css({ |
| 112 | 'display': 'block', |
| 113 | 'height': $scope.tableHeight || '400px', |
| 114 | 'overflow': 'auto' |
| 115 | }); |
| 116 | |
| 117 | // reduce width of last column by width of scrollbar |
| 118 | var scrollBarWidth = $elem.find('thead').width() - $elem.find('tbody')[0].clientWidth; |
| 119 | if (scrollBarWidth > 0) { |
| 120 | // for some reason trimming the width by 2px lines everything up better |
| 121 | scrollBarWidth -= 2; |
| 122 | $elem.find('tbody tr:first td:last-child').each(function (i, elem) { |
| 123 | $(elem).width($(elem).width() - scrollBarWidth); |
| 124 | }); |
| 125 | } |
| 126 | }); |
| 127 | } |
| 128 | }); |
| 129 | } |