Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit ab76dc7

Browse files
committed
feat(showHide): Introduce directives to notify child directives on hide/show.
Features extreme tail-hooking technology. review Change the update to use nextTick so that the hide/show has already taken place.
1 parent 1ae16cb commit ab76dc7

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/components/showHide/showHide.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @ngdoc module
3+
* @name material.components.showHide
4+
*/
5+
6+
// Add additional handlers to ng-show and ng-hide that notify directives
7+
// contained within that they should recompute their size.
8+
// These run in addition to Angular's built-in ng-hide and ng-show directives.
9+
angular.module('material.components.showHide', [
10+
'material.core'
11+
])
12+
.directive('ngShow', createDirective('ngShow', true))
13+
.directive('ngHide', createDirective('ngHide', false));
14+
15+
16+
function createDirective(name, targetValue) {
17+
return ['$mdUtil', function($mdUtil) {
18+
return {
19+
restrict: 'A',
20+
multiElement: true,
21+
link: function($scope, $element, $attr) {
22+
$scope.$watch($attr[name], function(value) {
23+
if (!!value === targetValue) {
24+
$mdUtil.nextTick(function() {
25+
$scope.$broadcast('$md-resize');
26+
});
27+
$mdUtil.dom.animator.waitTransitionEnd($element).then(function() {
28+
$scope.$broadcast('$md-resize');
29+
});
30+
}
31+
});
32+
}
33+
};
34+
}];
35+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
describe('showHide', function() {
2+
var $compile, $timeout, defered, scope, spy;
3+
4+
beforeEach(module('material.components.showHide'));
5+
6+
beforeEach(inject(function(_$compile_, $mdUtil, $q, $rootScope, _$timeout_) {
7+
$compile = _$compile_;
8+
$timeout = _$timeout_;
9+
defered = $q.defer();
10+
scope = $rootScope.$new();
11+
spy = jasmine.createSpy();
12+
13+
scope.$on('$md-resize', spy);
14+
spyOn($mdUtil.dom.animator, 'waitTransitionEnd').and.returnValue(defered.promise);
15+
}));
16+
17+
afterEach(function() {
18+
scope.$destroy();
19+
});
20+
21+
describe('ng-hide', function() {
22+
it('should notify when the node unhides', function() {
23+
scope.hide = true;
24+
var element = $compile('<div ng-hide="hide"></div>')(scope);
25+
scope.$apply();
26+
expect(spy).not.toHaveBeenCalled();
27+
28+
// Expect a $broadcast when showing.
29+
scope.hide = false;
30+
scope.$apply();
31+
$timeout.flush();
32+
expect(spy).toHaveBeenCalled();
33+
34+
// Expect a $broadcast on transitionEnd after showing.
35+
spy.calls.reset();
36+
defered.resolve();
37+
scope.$apply();
38+
expect(spy).toHaveBeenCalled();
39+
});
40+
41+
it('should not notify on hide', function() {
42+
scope.hide = true;
43+
var element = $compile('<div ng-hide="hide"></div>')(scope);
44+
scope.$apply();
45+
46+
// Expect no $broadcasts when hiding.
47+
expect(spy).not.toHaveBeenCalled();
48+
defered.resolve();
49+
scope.$apply();
50+
expect(spy).not.toHaveBeenCalled();
51+
});
52+
});
53+
54+
describe('ng-show', function() {
55+
it('should notify when the node unhides', function() {
56+
scope.show = false;
57+
var element = $compile('<div ng-show="show"></div>')(scope);
58+
scope.$apply();
59+
expect(spy).not.toHaveBeenCalled();
60+
61+
// Expect a $broadcast when showing.
62+
scope.show = true;
63+
scope.$apply();
64+
$timeout.flush();
65+
expect(spy).toHaveBeenCalled();
66+
67+
// Expect a $broadcast on transitionEnd after showing.
68+
spy.calls.reset();
69+
defered.resolve();
70+
scope.$apply();
71+
expect(spy).toHaveBeenCalled();
72+
});
73+
74+
it('should not notify on hide', function() {
75+
scope.show = false;
76+
var element = $compile('<div ng-show="show"></div>')(scope);
77+
scope.$apply();
78+
79+
// Expect no $broadcasts when hiding.
80+
expect(spy).not.toHaveBeenCalled();
81+
defered.resolve();
82+
scope.$apply();
83+
expect(spy).not.toHaveBeenCalled();
84+
});
85+
});
86+
});

0 commit comments

Comments
 (0)