Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 4ea002b

Browse files
feat(accordion): add config object
1 parent 5e6613a commit 4ea002b

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/accordion/accordion.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse']);
2-
angular.module('ui.bootstrap.accordion').controller('AccordionController', ['$scope', '$attrs', function ($scope, $attrs) {
1+
angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
2+
3+
.constant('accordionConfig', {
4+
closeOthers: true
5+
})
6+
7+
.controller('AccordionController', ['$scope', '$attrs', 'accordionConfig', function ($scope, $attrs, accordionConfig) {
8+
39
// This array keeps track of the accordion groups
410
this.groups = [];
511

612
// Ensure that all the groups in this accordion are closed, unless close-others explicitly says not to
713
this.closeOthers = function(openGroup) {
8-
if ( angular.isUndefined($attrs.closeOthers) || $scope.$eval($attrs.closeOthers) ) {
14+
var closeOthers = angular.isDefined($attrs.closeOthers) ? $scope.$eval($attrs.closeOthers) : accordionConfig.closeOthers;
15+
if ( closeOthers ) {
916
angular.forEach(this.groups, function (group) {
1017
if ( group !== openGroup ) {
1118
group.isOpen = false;

src/accordion/test/accordionSpec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,33 @@ describe('accordion', function () {
5050
expect(group2.isOpen).toBe(false);
5151
expect(group3.isOpen).toBe(true);
5252
});
53+
5354
it('should not close other groups if close-others attribute is false', function() {
5455
$attrs.closeOthers = 'false';
5556
ctrl.closeOthers(group2);
5657
expect(group1.isOpen).toBe(true);
5758
expect(group2.isOpen).toBe(true);
5859
expect(group3.isOpen).toBe(true);
5960
});
61+
62+
describe('setting accordionConfig', function() {
63+
var originalCloseOthers;
64+
beforeEach(inject(function(accordionConfig) {
65+
originalCloseOthers = accordionConfig.closeOthers;
66+
accordionConfig.closeOthers = false;
67+
}));
68+
afterEach(inject(function(accordionConfig) {
69+
// return it to the original value
70+
accordionConfig.closeOthers = originalCloseOthers;
71+
}));
72+
73+
it('should not close other groups if accordionConfig.closeOthers is false', function() {
74+
ctrl.closeOthers(group2);
75+
expect(group1.isOpen).toBe(true);
76+
expect(group2.isOpen).toBe(true);
77+
expect(group3.isOpen).toBe(true);
78+
});
79+
});
6080
});
6181

6282
describe('removeGroup', function() {

0 commit comments

Comments
 (0)