forked from mgcrea/angular-strap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab.js
More file actions
137 lines (136 loc) · 4.73 KB
/
tab.js
File metadata and controls
137 lines (136 loc) · 4.73 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
134
135
136
137
/**
* angular-strap
* @version v2.3.6 - 2015-11-14
* @link http://mgcrea.github.io/angular-strap
* @author Olivier Louvignes <olivier@mg-crea.com> (https://github.com/mgcrea)
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
'use strict';
angular.module('mgcrea.ngStrap.tab', []).provider('$tab', function() {
var defaults = this.defaults = {
animation: 'am-fade',
template: 'tab/tab.tpl.html',
navClass: 'nav-tabs',
activeClass: 'active'
};
var controller = this.controller = function($scope, $element, $attrs) {
var self = this;
self.$options = angular.copy(defaults);
angular.forEach([ 'animation', 'navClass', 'activeClass' ], function(key) {
if (angular.isDefined($attrs[key])) self.$options[key] = $attrs[key];
});
$scope.$navClass = self.$options.navClass;
$scope.$activeClass = self.$options.activeClass;
self.$panes = $scope.$panes = [];
self.$activePaneChangeListeners = self.$viewChangeListeners = [];
self.$push = function(pane) {
if (angular.isUndefined(self.$panes.$active)) {
$scope.$setActive(pane.name || 0);
}
self.$panes.push(pane);
};
self.$remove = function(pane) {
var index = self.$panes.indexOf(pane);
var active = self.$panes.$active;
var activeIndex;
if (angular.isString(active)) {
activeIndex = self.$panes.map(function(pane) {
return pane.name;
}).indexOf(active);
} else {
activeIndex = self.$panes.$active;
}
self.$panes.splice(index, 1);
if (index < activeIndex) {
activeIndex--;
} else if (index === activeIndex && activeIndex === self.$panes.length) {
activeIndex--;
}
if (activeIndex >= 0 && activeIndex < self.$panes.length) {
self.$setActive(self.$panes[activeIndex].name || activeIndex);
} else {
self.$setActive();
}
};
self.$setActive = $scope.$setActive = function(value) {
self.$panes.$active = value;
self.$activePaneChangeListeners.forEach(function(fn) {
fn();
});
};
self.$isActive = $scope.$isActive = function($pane, $index) {
return self.$panes.$active === $pane.name || self.$panes.$active === $index;
};
};
this.$get = function() {
var $tab = {};
$tab.defaults = defaults;
$tab.controller = controller;
return $tab;
};
}).directive('bsTabs', [ '$window', '$animate', '$tab', '$parse', function($window, $animate, $tab, $parse) {
var defaults = $tab.defaults;
return {
require: [ '?ngModel', 'bsTabs' ],
transclude: true,
scope: true,
controller: [ '$scope', '$element', '$attrs', $tab.controller ],
templateUrl: function(element, attr) {
return attr.template || defaults.template;
},
link: function postLink(scope, element, attrs, controllers) {
var ngModelCtrl = controllers[0];
var bsTabsCtrl = controllers[1];
if (ngModelCtrl) {
bsTabsCtrl.$activePaneChangeListeners.push(function() {
ngModelCtrl.$setViewValue(bsTabsCtrl.$panes.$active);
});
ngModelCtrl.$formatters.push(function(modelValue) {
bsTabsCtrl.$setActive(modelValue);
return modelValue;
});
}
if (attrs.bsActivePane) {
var parsedBsActivePane = $parse(attrs.bsActivePane);
bsTabsCtrl.$activePaneChangeListeners.push(function() {
parsedBsActivePane.assign(scope, bsTabsCtrl.$panes.$active);
});
scope.$watch(attrs.bsActivePane, function(newValue, oldValue) {
bsTabsCtrl.$setActive(newValue);
}, true);
}
}
};
} ]).directive('bsPane', [ '$window', '$animate', '$sce', function($window, $animate, $sce) {
return {
require: [ '^?ngModel', '^bsTabs' ],
scope: true,
link: function postLink(scope, element, attrs, controllers) {
var ngModelCtrl = controllers[0];
var bsTabsCtrl = controllers[1];
element.addClass('tab-pane');
attrs.$observe('title', function(newValue, oldValue) {
scope.title = $sce.trustAsHtml(newValue);
});
scope.name = attrs.name;
if (bsTabsCtrl.$options.animation) {
element.addClass(bsTabsCtrl.$options.animation);
}
attrs.$observe('disabled', function(newValue, oldValue) {
scope.disabled = scope.$eval(newValue);
});
bsTabsCtrl.$push(scope);
scope.$on('$destroy', function() {
bsTabsCtrl.$remove(scope);
});
function render() {
var index = bsTabsCtrl.$panes.indexOf(scope);
$animate[bsTabsCtrl.$isActive(scope, index) ? 'addClass' : 'removeClass'](element, bsTabsCtrl.$options.activeClass);
}
bsTabsCtrl.$activePaneChangeListeners.push(function() {
render();
});
render();
}
};
} ]);