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

Commit

Permalink
feat(collapse): use uib- prefix
Browse files Browse the repository at this point in the history
Closes #4370
  • Loading branch information
Foxandxss authored and wesleycho committed Sep 8, 2015
1 parent 79e38cb commit 9bdb32e
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 6 deletions.
87 changes: 86 additions & 1 deletion src/collapse/collapse.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular.module('ui.bootstrap.collapse', [])

.directive('collapse', ['$animate', '$injector', function($animate, $injector) {
.directive('uibCollapse', ['$animate', '$injector', function($animate, $injector) {
var $animateCss = $injector.has('$animateCss') ? $injector.get('$animateCss') : null;
return {
link: function(scope, element, attrs) {
Expand Down Expand Up @@ -64,6 +64,91 @@ angular.module('ui.bootstrap.collapse', [])
.addClass('collapse');
}

scope.$watch(attrs.uibCollapse, function(shouldCollapse) {
if (shouldCollapse) {
collapse();
} else {
expand();
}
});
}
};
}]);

/* Deprecated collapse below */

angular.module('ui.bootstrap.collapse')

.value('$collapseSuppressWarning', false)

.directive('collapse', ['$animate', '$injector', '$log', '$collapseSuppressWarning', function($animate, $injector, $log, $collapseSuppressWarning) {
var $animateCss = $injector.has('$animateCss') ? $injector.get('$animateCss') : null;
return {
link: function(scope, element, attrs) {
if (!$collapseSuppressWarning) {
$log.warn('collapse is now deprecated. Use uib-collapse instead.');
}

function expand() {
element.removeClass('collapse')
.addClass('collapsing')
.attr('aria-expanded', true)
.attr('aria-hidden', false);

if ($animateCss) {
$animateCss(element, {
addClass: 'in',
easing: 'ease',
to: { height: element[0].scrollHeight + 'px' }
}).start().done(expandDone);
} else {
$animate.addClass(element, 'in', {
to: { height: element[0].scrollHeight + 'px' }
}).then(expandDone);
}
}

function expandDone() {
element.removeClass('collapsing')
.addClass('collapse')
.css({height: 'auto'});
}

function collapse() {
if (!element.hasClass('collapse') && !element.hasClass('in')) {
return collapseDone();
}

element
// IMPORTANT: The height must be set before adding "collapsing" class.
// Otherwise, the browser attempts to animate from height 0 (in
// collapsing class) to the given height here.
.css({height: element[0].scrollHeight + 'px'})
// initially all panel collapse have the collapse class, this removal
// prevents the animation from jumping to collapsed state
.removeClass('collapse')
.addClass('collapsing')
.attr('aria-expanded', false)
.attr('aria-hidden', true);

if ($animateCss) {
$animateCss(element, {
removeClass: 'in',
to: {height: '0'}
}).start().done(collapseDone);
} else {
$animate.removeClass(element, 'in', {
to: {height: '0'}
}).then(collapseDone);
}
}

function collapseDone() {
element.css({height: '0'}); // Required so that collapse works when animation is disabled
element.removeClass('collapsing')
.addClass('collapse');
}

scope.$watch(attrs.collapse, function(shouldCollapse) {
if (shouldCollapse) {
collapse();
Expand Down
6 changes: 3 additions & 3 deletions src/collapse/docs/demo.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div ng-controller="CollapseDemoCtrl">
<button type="button" class="btn btn-default" ng-click="isCollapsed = !isCollapsed">Toggle collapse</button>
<hr>
<div collapse="isCollapsed">
<div class="well well-lg">Some content</div>
<div uib-collapse="isCollapsed">
<div class="well well-lg">Some content</div>
</div>
</div>
</div>
37 changes: 35 additions & 2 deletions src/collapse/test/collapse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('collapse directive', function() {
}));

beforeEach(function() {
element = $compile('<div collapse="isCollapsed">Some Content</div>')(scope);
element = $compile('<div uib-collapse="isCollapsed">Some Content</div>')(scope);
angular.element(document.body).append(element);
});

Expand Down Expand Up @@ -98,7 +98,7 @@ describe('collapse directive', function() {
var element;

beforeEach(function() {
element = angular.element('<div collapse="isCollapsed"><p>Initial content</p><div ng-show="exp">Additional content</div></div>');
element = angular.element('<div uib-collapse="isCollapsed"><p>Initial content</p><div ng-show="exp">Additional content</div></div>');
$compile(element)(scope);
angular.element(document.body).append(element);
});
Expand Down Expand Up @@ -129,3 +129,36 @@ describe('collapse directive', function() {
});
});
});

/* Deprecation tests below */

describe('collapse deprecation', function() {
beforeEach(module('ui.bootstrap.collapse'));
beforeEach(module('ngAnimateMock'));

it('should suppress warning', function() {
module(function($provide) {
$provide.value('$collapseSuppressWarning', true);
});

inject(function($compile, $log, $rootScope) {
spyOn($log, 'warn');

var element = $compile('<div collapse="isCollapsed">Some Content</div>')($rootScope);
$rootScope.$digest();

expect($log.warn.calls.count()).toBe(0);
});
});

it('should give warning by default', inject(function($compile, $log, $rootScope) {
spyOn($log, 'warn');

var element = $compile('<div collapse="isCollapsed">Some Content</div>')($rootScope);
$rootScope.$digest();

expect($log.warn.calls.count()).toBe(1);
expect($log.warn.calls.argsFor(0)).toEqual(['collapse is now deprecated. Use uib-collapse instead.']);

}));
});

0 comments on commit 9bdb32e

Please sign in to comment.