|
| 1 | +angular.module('ui.tab.scroll', []) |
| 2 | +.directive('scrollableTabset', [ |
| 3 | + '$window', '$interval', '$timeout', |
| 4 | + function($window, $interval, $timeout) { |
| 5 | + |
| 6 | + var timeoutId = null; |
| 7 | + |
| 8 | + var cancelId = function() { |
| 9 | + if(timeoutId) { |
| 10 | + $interval.cancel(timeoutId); |
| 11 | + timeoutId = null; |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + var unbindFunctions = function(el, cache) { |
| 16 | + if(!cache) return; |
| 17 | + el.off('mousedown', cache.mouseDown); |
| 18 | + el.off('mouseup', cache.mouseUp); |
| 19 | + }; |
| 20 | + |
| 21 | + var bindHoldFunctionTo = function(element, fn) { |
| 22 | + |
| 23 | + var isHolding = false; |
| 24 | + |
| 25 | + var mouseDown = function() { |
| 26 | + isHolding = true; |
| 27 | + |
| 28 | + fn(); |
| 29 | + |
| 30 | + timeoutId = $interval(function() { |
| 31 | + if(isHolding) { |
| 32 | + fn(); |
| 33 | + |
| 34 | + if($(element).is(":disabled")) { |
| 35 | + cancelId(); |
| 36 | + } |
| 37 | + } |
| 38 | + }, 100); |
| 39 | + }; |
| 40 | + |
| 41 | + var mouseUp = function() { |
| 42 | + isHolding = false; |
| 43 | + cancelId(); |
| 44 | + }; |
| 45 | + |
| 46 | + element.on('mousedown', mouseDown); |
| 47 | + element.on('mouseup', mouseUp); |
| 48 | + |
| 49 | + return {mouseDown: mouseDown, mouseUp: mouseUp}; |
| 50 | + |
| 51 | + }; |
| 52 | + |
| 53 | + return { |
| 54 | + restrict: 'AE', |
| 55 | + transclude: true, |
| 56 | + scope: { |
| 57 | + showTooltips: "=" |
| 58 | + }, |
| 59 | + template: [ |
| 60 | + '<div class="ui-tabs-scrollable">', |
| 61 | + '<button ng-hide="hideButtons" ng-disabled="disableLeft()" class="btn nav-button left-nav-button" tooltip-placement="bottom" tooltip-html-unsafe="{{tooltipLeftContent()}}">', |
| 62 | + '<span class="glyphicon glyphicon-chevron-left"></span>', |
| 63 | + '</button>', |
| 64 | + '<div class="spacer" ng-class="{\'hidden-buttons\': hideButtons}" ng-transclude></div>', |
| 65 | + '<button ng-hide="hideButtons" ng-disabled="disableRight()" class="btn nav-button right-nav-button" tooltip-placement="bottom" tooltip-html-unsafe="{{tooltipRightContent()}}">', |
| 66 | + '<span class="glyphicon glyphicon-chevron-right"></span>', |
| 67 | + '</button>', |
| 68 | + '</div>' |
| 69 | + ].join(''), |
| 70 | + link: function($scope, $el) { |
| 71 | + |
| 72 | + $scope.currentOffset = 0; |
| 73 | + $scope.leftFunction = null; |
| 74 | + $scope.rightFunction = null; |
| 75 | + |
| 76 | + $scope.toTheLeftHTML = ''; |
| 77 | + $scope.toTheRightHTML = ''; |
| 78 | + |
| 79 | + $scope.disableLeft = function() { |
| 80 | + return !$scope.toTheLeftHTML; |
| 81 | + }; |
| 82 | + |
| 83 | + $scope.disableRight = function() { |
| 84 | + return !$scope.toTheRightHTML; |
| 85 | + }; |
| 86 | + |
| 87 | + $scope.tooltipLeftContent = function() { |
| 88 | + return $scope.showTooltips ? $scope.toTheLeftHTML : ''; |
| 89 | + }; |
| 90 | + |
| 91 | + $scope.tooltipRightContent = function() { |
| 92 | + return $scope.showTooltips ? $scope.toTheRightHTML : ''; |
| 93 | + }; |
| 94 | + |
| 95 | + //select the innermost child that isn't a span |
| 96 | + //this way we cover getting <tab-heading> and <tab heading=''> |
| 97 | + //but leave other markup out of it, unless it's a span (commonly an icon) |
| 98 | + var selector = '*:not(:has("*:not(span)"))'; |
| 99 | + |
| 100 | + $scope.toTheLeft = function() { |
| 101 | + if(!$scope.tabContainer) return; |
| 102 | + var nodes = []; |
| 103 | + $scope.tabContainer.find(selector).each(function(index, node) { |
| 104 | + var nodeObj = $(node); |
| 105 | + var nodeContainer = nodeObj.parentsUntil("ul"); |
| 106 | + |
| 107 | + if(nodeContainer.offset().left > 0) return; |
| 108 | + |
| 109 | + nodes.push(nodeObj.html()); |
| 110 | + }); |
| 111 | + $scope.toTheLeftHTML = nodes.join('<br>'); |
| 112 | + }; |
| 113 | + |
| 114 | + $scope.toTheRight = function() { |
| 115 | + if(!$scope.tabContainer) return; |
| 116 | + var nodes = []; |
| 117 | + $scope.tabContainer.find(selector).each(function(index, node) { |
| 118 | + var nodeObj = $(node); |
| 119 | + var nodeContainer = nodeObj.parentsUntil("ul"); |
| 120 | + |
| 121 | + var nodeWidth = nodeContainer.offset().left; |
| 122 | + |
| 123 | + if(nodeWidth < $scope.tabWidth) return; |
| 124 | + |
| 125 | + nodes.push(nodeObj.html()); |
| 126 | + }); |
| 127 | + $scope.toTheRightHTML = nodes.join('<br>'); |
| 128 | + }; |
| 129 | + |
| 130 | + $scope.recalcSides = function() { |
| 131 | + $scope.toTheLeft(); |
| 132 | + $scope.toTheRight(); |
| 133 | + }; |
| 134 | + |
| 135 | + var generateScrollFunction = function(el, offset) { |
| 136 | + return function() { |
| 137 | + $scope.currentOffset = Math.min($scope.tabContainerWidth, Math.max(0, el.scrollLeft += offset)); |
| 138 | + $scope.recalcSides(); |
| 139 | + }; |
| 140 | + }; |
| 141 | + |
| 142 | + var init = function() { |
| 143 | + var $leftNav = $el.find(".left-nav-button"); |
| 144 | + var $rightNav = $el.find(".right-nav-button"); |
| 145 | + var $tabs = $scope.tabContainer = $el.find(".spacer").find("ul.nav.nav-tabs"); |
| 146 | + |
| 147 | + var tabContainerWidth = $scope.tabContainerWidth = $tabs[0].scrollWidth; |
| 148 | + var tabWidth = $scope.tabWidth = $tabs.width(); |
| 149 | + var tabScrollWidth = tabWidth / 6; |
| 150 | + var realTabs = $tabs[0]; |
| 151 | + |
| 152 | + $scope.hideButtons = tabContainerWidth === tabWidth; |
| 153 | + |
| 154 | + $scope.leftFunction = generateScrollFunction(realTabs, -tabScrollWidth); |
| 155 | + $scope.rightfunction = generateScrollFunction(realTabs, tabScrollWidth); |
| 156 | + |
| 157 | + unbindFunctions($leftNav, $scope.leftFunctionCache); |
| 158 | + unbindFunctions($rightNav, $scope.rightFunctionCache); |
| 159 | + |
| 160 | + $scope.leftFunctionCache = bindHoldFunctionTo($leftNav, $scope.leftFunction); |
| 161 | + $scope.rightfunctionCache = bindHoldFunctionTo($rightNav, $scope.rightfunction); |
| 162 | + |
| 163 | + $scope.recalcSides(); |
| 164 | + }; |
| 165 | + |
| 166 | + $timeout(init, 0); |
| 167 | + |
| 168 | + $(window).on('resize', function() { |
| 169 | + init(); |
| 170 | + $scope.$apply(); |
| 171 | + }); |
| 172 | + |
| 173 | + } |
| 174 | + }; |
| 175 | +}]); |
0 commit comments