forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabs.js
More file actions
133 lines (108 loc) · 3.41 KB
/
tabs.js
File metadata and controls
133 lines (108 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* MUI Angular Tabs Component
* @module angular/tabs
*/
import angular from 'angular';
import * as jqLite from '../js/lib/jqLite';
const moduleName = 'mui.tabs';
angular.module(moduleName, [])
.directive('muiTabs', function() {
return {
restrict: 'EA',
transclude: true,
scope: {
selectedId: '=?selected',
onChange: '&?'
},
template: '' +
'<ul ' +
'class="mui-tabs__bar" ' +
'ng-class=\'{"mui-tabs__bar--justified": justified}\'>' +
'<li ' +
'ng-repeat="tab in tabs track by $index" ' +
'ng-class=\'{"mui--is-active": $index === selectedId}\'>' +
'<a ng-click="onClick($index)">{{tab.label}}</a>' +
'</li>' +
'</ul>',
controller: ['$scope', function($scope) {
var counter = 0;
// init scope
$scope.tabs = [];
// add tab
this.addTab = function(args) {
// user counter for tab id
var tabId = counter;
counter += 1;
// update tabs list
$scope.tabs.push({label: args.label});
// handle active tabs
if (args.isActive) $scope.selectedId = tabId;
// return id
return tabId;
};
}],
link: function(scope, element, attrs, ctrl, transcludeFn) {
var isUndef = angular.isUndefined;
// init scope
if (isUndef(scope.selectedId)) scope.selectedId = 0;
scope.justified = false;
// justified
if (!isUndef(attrs.justified)) scope.justified = true;
// click handler
scope.onClick = function(tabId) {
// check current tab
if (tabId === scope.selectedId) return;
// update active tab
scope.selectedId = tabId;
// execute onChange callback
if (scope.onChange) scope.$$postDigest(scope.onChange);
};
// use transcludeFn to pass ng-controller on parent element
transcludeFn(scope, function(clone) {
element.append(clone);
});
}
};
})
.directive('muiTab', ['$parse', function($parse) {
return {
require: '^?muiTabs',
restrict: 'AE',
scope: {
active: '&?',
label: '@?'
},
transclude: true,
template: '<div ' +
'class="mui-tabs__pane" ' +
'ng-class=\'{"mui--is-active": tabId === $parent.selectedId}\'></div>',
link: function(scope, element, attrs, ctrl, transcludeFn) {
var onSelectFn = $parse(attrs.onSelect),
onDeselectFn = $parse(attrs.onDeselect),
origScope = scope.$parent.$parent;
// init scope
scope.tabId = null;
// add to parent controller
if (ctrl) {
scope.tabId = ctrl.addTab({
label: scope.label,
isActive: Boolean(scope.active)
});
}
// use transcludeFn to pass ng-controller on parent element
transcludeFn(scope, function(clone) {
element.find('div').append(clone);
});
scope.$parent.$watch('selectedId', function(newVal, oldVal) {
// ignore initial load
if (newVal === oldVal) return;
// execute onSelect
if (newVal === scope.tabId) onSelectFn(origScope);
// execute onDeselect
if (oldVal === scope.tabId) onDeselectFn(origScope);
});
}
};
}]);
/** Define module API */
export default moduleName;