Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. <!doctype html>
  2. <html ng-app="mainModule">
  3. <body>
  4. <tab-control id="MainTabControl" class="red">
  5. <tab name="FirstTab" id="Tab1">
  6. Tab 1 stuff
  7. </tab>
  8. <tab name="SecondTab" id="Tab2" selected>
  9. Tab 2 stuff
  10. </tab>
  11. <tab name="ThirdTab" id="Tab3">
  12. <strong>Tab 3</strong>
  13. </tab>
  14. </tab-control>
  15.  
  16. <div ng-controller="DynamicTabController">
  17. <tab-control>
  18. <tab ng-repeat="tab in tabs" name="{{tab.name}}" id="{{tab.id}}" selected="{{ $index == 1 }}">
  19. {{tab.name}}
  20. </tab>
  21. </tab-control>
  22. </div>
  23.  
  24. <script type="text/ng-template" id="tabControlTemplate">
  25. <ul id="{{id}}" class="{{klass}} tab-control">
  26. <li ng-repeat="tab in tabs" ng-class="{ selected: tab.selected }" class="tab" ng-click="selectTab(tab)">{{tab.name}}</li>
  27. </ul>
  28.  
  29. <section ng-transclude>
  30. </section>
  31. </script>
  32.  
  33. <script type="text/ng-template" id="tabTemplate">
  34. <article id="{{id}}" ng-class="{ selected: selected}" class="{{klass}} tab-content">
  35. <header>
  36. <h1>{{ name }}</h1>
  37. </header>
  38. <section ng-transclude>
  39. </section>
  40. </article>
  41. </script>
  42. </body>
  43. </html>
  44.  
  45. var mainModule = angular.module("mainModule", []);
  46.  
  47. mainModule.directive('tabControl', function() {
  48. return {
  49. restrict: 'E',
  50. templateUrl: 'tabControlTemplate',
  51. scope: {
  52. id: '@id',
  53. klass: '@class',
  54. },
  55. transclude: true,
  56. controller: ['$scope', function($scope) {
  57. $scope.tabs = []
  58.  
  59. this.addTab = function(tab){
  60. $scope.tabs.push(tab);
  61. }
  62.  
  63. $scope.selectTab = function(tab){
  64. for(var i=0; i<$scope.tabs.length; i++){
  65. if(tab.name != $scope.tabs[i].name){
  66. $scope.tabs[i].selected = false;
  67. }
  68. else {
  69. $scope.tabs[i].selected = true;
  70. }
  71. }
  72. }
  73. }]
  74. };
  75. });
  76.  
  77. mainModule.directive('tab', function(){
  78. return {
  79. restrict: 'E',
  80. templateUrl: 'tabTemplate',
  81. transclude: true,
  82. replace: true,
  83. scope: {
  84. id: '@id',
  85. name: '@name',
  86. },
  87. require: '^tabControl',
  88. link: function(scope, element, attrs, ctrl) {
  89. scope.selected = attrs.selected == "" || attrs.selected == true
  90. ctrl.addTab(scope);
  91. }
  92. };
  93. });
  94.  
  95. mainModule.controller("DynamicTabController", ['$scope', function($scope){
  96. $scope.tabs = [
  97. { name: 'Tab1', id: 'Tab1'},
  98. { name: 'Tab2', id: 'Tab2'},
  99. { name: 'Tab3', id: 'Tab3'},
  100. { name: 'Tab4', id: 'Tab4'},
  101. { name: 'Tab5', id: 'Tab5'},
  102. { name: 'Tab6', id: 'Tab6'},
  103. { name: 'Tab7', id: 'Tab7'}
  104. ]
  105. }]);
  106.  
  107. .tab-control {
  108. list-style: none;
  109. padding: 0;
  110. margin: 5px;
  111. }
  112.  
  113. .tab-control li {
  114. display: inline;
  115. padding: 5px 20px;
  116. border: 1px solid grey;
  117. border-bottom: 0;
  118. border-radius: 5;
  119. cursor: pointer;
  120. }
  121.  
  122. .tab-control li.selected {
  123. background-color: lightblue;
  124. }
  125.  
  126. .tab-content header {
  127. display : none;
  128. }
  129.  
  130. .tab-content {
  131. border: 1px solid black;
  132. width: 100%;
  133. height: 100%;
  134. display: none;
  135. }
  136.  
  137. .tab-content.selected {
  138. display: block;
  139. }
  140.  
  141. if (tab.name != $scope.tabs[i].name) {
  142. $scope.tabs[i].selected = false;
  143. } else {
  144. $scope.tabs[i].selected = true;
  145. }
  146.  
  147. $scope.tabs[i].selected = ( tab.name == $scope.tabs[i].name );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement