|
1 | 1 | angular.module('ui.bootstrap.tabs', [])
|
2 |
| -.controller('TabsController', ['$scope', '$element', function($scope, $element) { |
3 |
| - var panes = $scope.panes = []; |
4 |
| - |
5 |
| - this.select = $scope.select = function selectPane(pane) { |
6 |
| - angular.forEach(panes, function(pane) { |
7 |
| - pane.selected = false; |
8 |
| - }); |
9 |
| - pane.selected = true; |
| 2 | + |
| 3 | +.controller('TabsetController', ['$scope', '$element', |
| 4 | +function TabsetCtrl($scope, $element) { |
| 5 | + var ctrl = this, |
| 6 | + tabs = ctrl.tabs = []; |
| 7 | + |
| 8 | + ctrl.select = function(tab) { |
| 9 | + angular.forEach(tabs, ctrl.deselect); |
| 10 | + tab.active = true; |
| 11 | + ctrl.activeTab = tab; |
| 12 | + }; |
| 13 | + |
| 14 | + ctrl.deselect = function(tab) { |
| 15 | + if (ctrl.activeTab === tab) { |
| 16 | + ctrl.activeTab = null; |
| 17 | + } |
| 18 | + tab.active = false; |
10 | 19 | };
|
11 | 20 |
|
12 |
| - this.addPane = function addPane(pane) { |
13 |
| - if (!panes.length) { |
14 |
| - $scope.select(pane); |
| 21 | + |
| 22 | + ctrl.addTab = function addTab(tab) { |
| 23 | + if (!tabs.length) { |
| 24 | + ctrl.select(tab); |
15 | 25 | }
|
16 |
| - panes.push(pane); |
| 26 | + tabs.push(tab); |
17 | 27 | };
|
18 | 28 |
|
19 |
| - this.removePane = function removePane(pane) { |
20 |
| - var index = panes.indexOf(pane); |
21 |
| - panes.splice(index, 1); |
22 |
| - //Select a new pane if removed pane was selected |
23 |
| - if (pane.selected && panes.length > 0) { |
24 |
| - $scope.select(panes[index < panes.length ? index : index-1]); |
| 29 | + ctrl.removeTab = function removeTab(tab) { |
| 30 | + var index = tabs.indexOf(tab); |
| 31 | + tabs.splice(index, 1); |
| 32 | + //Select a new tab if removed tab was selected |
| 33 | + if (tab.active && tabs.length > 0) { |
| 34 | + ctrl.select(tabs[index < tabs.length ? index : index-1]); |
25 | 35 | }
|
26 | 36 | };
|
27 | 37 | }])
|
28 |
| -.directive('tabs', function() { |
| 38 | + |
| 39 | +.directive('tabset', function() { |
29 | 40 | return {
|
30 | 41 | restrict: 'EA',
|
31 | 42 | transclude: true,
|
32 |
| - scope: {}, |
33 |
| - controller: 'TabsController', |
34 |
| - templateUrl: 'template/tabs/tabs.html', |
35 |
| - replace: true |
| 43 | + controller: 'TabsetController', |
| 44 | + templateUrl: 'template/tabs/tabset.html' |
36 | 45 | };
|
37 | 46 | })
|
38 |
| -.directive('pane', ['$parse', function($parse) { |
| 47 | + |
| 48 | +.directive('tab', ['$parse', '$http', '$templateCache', '$compile', |
| 49 | +function($parse, $http, $templateCache, $compile) { |
39 | 50 | return {
|
40 |
| - require: '^tabs', |
| 51 | + require: '^tabset', |
41 | 52 | restrict: 'EA',
|
| 53 | + replace: true, |
| 54 | + templateUrl: 'template/tabs/tab.html', |
42 | 55 | transclude: true,
|
43 |
| - scope:{ |
44 |
| - heading:'@' |
| 56 | + scope: { |
| 57 | + heading: '@' |
45 | 58 | },
|
46 |
| - link: function(scope, element, attrs, tabsCtrl) { |
47 |
| - var getSelected, setSelected; |
48 |
| - scope.selected = false; |
49 |
| - if (attrs.active) { |
50 |
| - getSelected = $parse(attrs.active); |
51 |
| - setSelected = getSelected.assign; |
52 |
| - scope.$watch( |
53 |
| - function watchSelected() {return getSelected(scope.$parent);}, |
54 |
| - function updateSelected(value) {scope.selected = value;} |
55 |
| - ); |
56 |
| - scope.selected = getSelected ? getSelected(scope.$parent) : false; |
57 |
| - } |
58 |
| - scope.$watch('selected', function(selected) { |
59 |
| - if(selected) { |
60 |
| - tabsCtrl.select(scope); |
| 59 | + controller: ['$scope', function TabCtrl($scope) { |
| 60 | + this.getHeadingElement = function() { |
| 61 | + return $scope.headingElement; |
| 62 | + }; |
| 63 | + }], |
| 64 | + compile: function(elm, attrs, transclude) { |
| 65 | + return function postLink(scope, elm, attrs, tabsetCtrl) { |
| 66 | + var getActive, setActive; |
| 67 | + scope.active = false; // default value |
| 68 | + if (attrs.active) { |
| 69 | + getActive = $parse(attrs.active); |
| 70 | + setActive = getActive.assign; |
| 71 | + scope.$watch(function watchActive() { |
| 72 | + return getActive(scope.$parent); |
| 73 | + }, function updateActive(value) { |
| 74 | + scope.active = !!value; |
| 75 | + }); |
| 76 | + } else { |
| 77 | + setActive = getActive = angular.noop; |
61 | 78 | }
|
62 |
| - if(setSelected) { |
63 |
| - setSelected(scope.$parent, selected); |
| 79 | + |
| 80 | + scope.$watch('active', function(active) { |
| 81 | + setActive(scope.$parent, active); |
| 82 | + if (active) { |
| 83 | + tabsetCtrl.select(scope); |
| 84 | + if (attrs.select) { |
| 85 | + scope.$parent.$eval(attrs.select); |
| 86 | + } |
| 87 | + } else { |
| 88 | + tabsetCtrl.deselect(scope); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + scope.select = function() { |
| 93 | + scope.active = true; |
| 94 | + }; |
| 95 | + |
| 96 | + tabsetCtrl.addTab(scope); |
| 97 | + scope.$on('$destroy', function() { |
| 98 | + tabsetCtrl.removeTab(scope); |
| 99 | + }); |
| 100 | + //If the tabset sets this guy to active, set the parent too. We have to |
| 101 | + //do this because the parent watcher will fire and set active to false |
| 102 | + //again before the watcher on line 82 can go |
| 103 | + if (scope.active) { |
| 104 | + setActive(scope.$parent, true); |
| 105 | + } |
| 106 | + |
| 107 | + //Transclude the collection of sibling elements. Use forEach to find |
| 108 | + //the heading if it exists. We don't use a directive for tab-heading |
| 109 | + //because it is problematic. Discussion @ http://git.io/MSNPwQ |
| 110 | + transclude(scope.$parent, function(clone) { |
| 111 | + //Look at every element in the clone collection. If it's tab-heading, |
| 112 | + //mark it as that. If it's not tab-heading, mark it as tab contents |
| 113 | + var contents = [], heading; |
| 114 | + angular.forEach(clone, function(el) { |
| 115 | + //See if it's a tab-heading attr or element directive |
| 116 | + //First make sure it's a normal element, one that has a tagName |
| 117 | + if (el.tagName && |
| 118 | + (el.hasAttribute("tab-heading") || |
| 119 | + el.hasAttribute("data-tab-heading") || |
| 120 | + el.tagName.toLowerCase() == "tab-heading" || |
| 121 | + el.tagName.toLowerCase() == "data-tab-heading" |
| 122 | + )) { |
| 123 | + heading = el; |
| 124 | + } else { |
| 125 | + contents.push(el); |
| 126 | + } |
| 127 | + }); |
| 128 | + if (heading) { |
| 129 | + scope.headingElement = angular.element(heading); |
| 130 | + } |
| 131 | + //Everything else is the content we need, since we removed |
| 132 | + scope.contentElement = angular.element(contents); |
| 133 | + }); |
| 134 | + }; |
| 135 | + } |
| 136 | + }; |
| 137 | +}]) |
| 138 | + |
| 139 | +.directive('tabHeadingTransclude', [function() { |
| 140 | + return { |
| 141 | + restrict: 'A', |
| 142 | + require: '^tab', |
| 143 | + link: function(scope, elm, attrs, tabCtrl) { |
| 144 | + scope.$watch(function getHeadingElement() { |
| 145 | + return tabCtrl.getHeadingElement(); |
| 146 | + }, function(heading) { |
| 147 | + if (heading) { |
| 148 | + elm.html(''); |
| 149 | + elm.append(heading); |
64 | 150 | }
|
65 | 151 | });
|
| 152 | + } |
| 153 | + }; |
| 154 | +}]) |
66 | 155 |
|
67 |
| - tabsCtrl.addPane(scope); |
68 |
| - scope.$on('$destroy', function() { |
69 |
| - tabsCtrl.removePane(scope); |
| 156 | +.directive('tabContentTransclude', [function() { |
| 157 | + return { |
| 158 | + restrict: 'A', |
| 159 | + require: '^tabset', |
| 160 | + link: function(scope, elm, attrs, tabsetCtrl) { |
| 161 | + scope.$watch(function() { |
| 162 | + return tabsetCtrl.activeTab; |
| 163 | + }, function(activeTab, previousActiveTab) { |
| 164 | + if (previousActiveTab) { |
| 165 | + previousActiveTab.contentElement.remove(); |
| 166 | + } |
| 167 | + if (activeTab) { |
| 168 | + elm.append(activeTab.contentElement); |
| 169 | + } |
70 | 170 | });
|
71 |
| - }, |
72 |
| - templateUrl: 'template/tabs/pane.html', |
73 |
| - replace: true |
| 171 | + } |
74 | 172 | };
|
75 |
| -}]); |
| 173 | +}]) |
| 174 | + |
| 175 | +; |
| 176 | + |
0 commit comments