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

Commit b9b63f9

Browse files
committed
feat(showHide): Introduce directives to notify child directives on hide/show.
Features extreme tail-hooking technology.
1 parent 27c65fd commit b9b63f9

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

src/components/showHide/showHide.js

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

0 commit comments

Comments
 (0)