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 | |
Bri Prebilic Cole | e7399f3 | 2015-01-22 16:50:55 -0800 | [diff] [blame] | 24 | function setColWidth(t) { |
Bri Prebilic Cole | d36a90f | 2015-01-22 16:19:27 -0800 | [diff] [blame] | 25 | var tHeaders, tdElement, colWidth; |
Bri Prebilic Cole | 4f0d977 | 2015-01-21 16:45:14 -0800 | [diff] [blame] | 26 | |
Bri Prebilic Cole | 4f0d977 | 2015-01-21 16:45:14 -0800 | [diff] [blame] | 27 | tHeaders = t.selectAll('th'); |
Bri Prebilic Cole | d36a90f | 2015-01-22 16:19:27 -0800 | [diff] [blame] | 28 | |
| 29 | // select each td in the first row and set the header's width to the |
| 30 | // corresponding td's width, if td is larger than header's width |
| 31 | tHeaders.each(function(thElement, index){ |
| 32 | thElement = d3.select(this); |
| 33 | |
| 34 | tdElement = t.select('td:nth-of-type(' + (index + 1) + ')'); |
| 35 | colWidth = tdElement.style('width'); |
| 36 | |
| 37 | thElement.style('width', colWidth); |
| 38 | tdElement.style('width', colWidth); |
Bri Prebilic Cole | 4f0d977 | 2015-01-21 16:45:14 -0800 | [diff] [blame] | 39 | }); |
| 40 | } |
| 41 | |
Bri Prebilic Cole | e7399f3 | 2015-01-22 16:50:55 -0800 | [diff] [blame] | 42 | function setCSS(thead, tbody, height) { |
Bri Prebilic Cole | d36a90f | 2015-01-22 16:19:27 -0800 | [diff] [blame] | 43 | thead.style('display', 'block'); |
| 44 | tbody.style({'display': 'block', |
Bri Prebilic Cole | e7399f3 | 2015-01-22 16:50:55 -0800 | [diff] [blame] | 45 | 'height': height || '500px', |
Bri Prebilic Cole | d36a90f | 2015-01-22 16:19:27 -0800 | [diff] [blame] | 46 | 'overflow': 'auto' |
| 47 | }); |
Bri Prebilic Cole | 4f0d977 | 2015-01-21 16:45:14 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Bri Prebilic Cole | e7399f3 | 2015-01-22 16:50:55 -0800 | [diff] [blame] | 50 | function fixTable(t, th, tb, height) { |
| 51 | setColWidth(t); |
| 52 | setCSS(th, tb, height); |
Bri Prebilic Cole | 4f0d977 | 2015-01-21 16:45:14 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | angular.module('practiceTable', []) |
| 56 | |
| 57 | .directive('fixedHeader', ['$log', '$timeout', function ($log, $timeout) { |
| 58 | return { |
| 59 | restrict: 'A', |
Bri Prebilic Cole | e7399f3 | 2015-01-22 16:50:55 -0800 | [diff] [blame] | 60 | scope: { |
| 61 | tableHeight: '@' |
| 62 | }, |
Bri Prebilic Cole | 4f0d977 | 2015-01-21 16:45:14 -0800 | [diff] [blame] | 63 | |
| 64 | link: function (scope, element, attrs) { |
| 65 | var table = d3.select(element[0]), |
| 66 | thead = table.select('thead'), |
| 67 | tbody = table.select('tbody'); |
| 68 | |
| 69 | // wait until the table is visible |
| 70 | scope.$watch( |
| 71 | function () { return (!(table.offsetParent === null)); }, |
| 72 | function (newValue, oldValue) { |
| 73 | if (newValue === true) { |
Bri Prebilic Cole | 4f0d977 | 2015-01-21 16:45:14 -0800 | [diff] [blame] | 74 | |
| 75 | // ensure thead and tbody have no display |
| 76 | thead.style('display', null); |
| 77 | tbody.style('display', null); |
| 78 | |
| 79 | $timeout(function() { |
Bri Prebilic Cole | e7399f3 | 2015-01-22 16:50:55 -0800 | [diff] [blame] | 80 | fixTable(table, thead, tbody, scope.tableHeight); |
Bri Prebilic Cole | d36a90f | 2015-01-22 16:19:27 -0800 | [diff] [blame] | 81 | }); |
Bri Prebilic Cole | 4f0d977 | 2015-01-21 16:45:14 -0800 | [diff] [blame] | 82 | } |
| 83 | }); |
Bri Prebilic Cole | 4f0d977 | 2015-01-21 16:45:14 -0800 | [diff] [blame] | 84 | } |
| 85 | }; |
| 86 | }]); |
Bri Prebilic Cole | d36a90f | 2015-01-22 16:19:27 -0800 | [diff] [blame] | 87 | }()); |