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

Commit eb94d64

Browse files
EladBezalelThomasBurleson
authored andcommitted
fix(toolbar): add support in scrollshrink to ngshow/hide
`updateToolbarHeight` was not been called when `ngShow` or `ngHide` value has change which caused the toolbar to wait for the debounce to call `updateToolbarHeight` (5 sec) * use `scope.$watch()` instead of `attrs.$observe( )` since ngShow/ngHide expect expressions instead of interpolated values. fixes #5706. closes #5863
1 parent 0740daa commit eb94d64

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

src/components/toolbar/toolbar.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ function mdToolbarDirective($$rAF, $mdConstant, $mdUtil, $mdTheming, $animate) {
101101

102102
attr.$observe('mdScrollShrink', onChangeScrollShrink);
103103

104+
// If the toolbar has ngShow or ngHide we need to update height immediately as it changed
105+
// and not wait for $mdUtil.debounce to happen
106+
107+
if (attr.ngShow) { scope.$watch(attr.ngShow, updateToolbarHeight); }
108+
if (attr.ngHide) { scope.$watch(attr.ngHide, updateToolbarHeight); }
109+
104110
// If the scope is destroyed (which could happen with ng-if), make sure
105111
// to disable scroll shrinking again
106112

src/components/toolbar/toolbar.spec.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,110 @@ describe('<md-toolbar>', function() {
118118
expect(element.find('md-toolbar').length).toBe(0);
119119
}));
120120

121+
it('works with ng-show', inject(function($$rAF) {
122+
var template =
123+
'<div layout="column" style="height: 600px;">' +
124+
' <md-toolbar md-scroll-shrink="true" ng-show="shouldShowToolbar">test</md-toolbar>' +
125+
' <md-content flex><div style="height: 5000px;"></div></md-content>' +
126+
'</div>';
127+
128+
// Build/append the element
129+
build(template);
130+
document.body.appendChild(element[0]);
131+
132+
// Flushing to get the actual height of toolbar
133+
$$rAF.flush();
134+
135+
//
136+
// Initial tests
137+
//
138+
139+
var toolbarStyles = getComputedStyle(element.find('md-toolbar')[0]);
140+
var contentStyles = getComputedStyle(element.find('md-content')[0]);
141+
142+
// Should start out hidden because we have not set shouldShowToolbar
143+
expect(toolbarStyles.display).toBeTruthy();
144+
expect(toolbarStyles.display).toEqual('none');
145+
146+
// Expect the content to have a zero margin top
147+
expect(contentStyles.marginTop).toBeTruthy();
148+
expect(contentStyles.marginTop).toEqual('0px');
149+
150+
//
151+
// After showing toolbar tests
152+
//
153+
154+
// Show the toolbar and ensure it is visible
155+
pageScope.$apply('shouldShowToolbar = true');
156+
pageScope.$digest();
157+
158+
toolbarStyles = getComputedStyle(element.find('md-toolbar')[0]);
159+
contentStyles = getComputedStyle(element.find('md-content')[0]);
160+
161+
// Expect the toolbar to be visible
162+
expect(toolbarStyles.display).toBeTruthy();
163+
expect(toolbarStyles.display).not.toEqual('none');
164+
165+
// Expect the content to have a non-zero margin top (because updateToolbarHeight() was called)
166+
expect(contentStyles.marginTop).toBeTruthy();
167+
expect(contentStyles.marginTop).not.toEqual('0px');
168+
169+
// Remove the element
170+
document.body.removeChild(element[0]);
171+
}));
172+
173+
it('works with ng-hide', inject(function($$rAF) {
174+
var template =
175+
'<div layout="column" style="height: 600px;">' +
176+
' <md-toolbar md-scroll-shrink="true" ng-hide="shouldNotShowToolbar">test</md-toolbar>' +
177+
' <md-content flex><div style="height: 5000px;"></div></md-content>' +
178+
'</div>';
179+
180+
// Build/append the element
181+
build(template);
182+
document.body.appendChild(element[0]);
183+
184+
// Flushing to get the actual height of toolbar
185+
$$rAF.flush();
186+
187+
//
188+
// Initial tests
189+
//
190+
191+
var toolbarStyles = getComputedStyle(element.find('md-toolbar')[0]);
192+
var contentStyles = getComputedStyle(element.find('md-content')[0]);
193+
194+
// Should start out visible because we have not set shouldNotShowToolbar
195+
expect(toolbarStyles.display).toBeTruthy();
196+
expect(toolbarStyles.display).not.toEqual('none');
197+
198+
// Expect the content to have a non-zero margin top
199+
expect(contentStyles.marginTop).toBeTruthy();
200+
expect(contentStyles.marginTop).not.toEqual('0px');
201+
202+
//
203+
// After showing toolbar tests
204+
//
205+
206+
// Show the toolbar and ensure it is hidden
207+
pageScope.$apply('shouldNotShowToolbar = true');
208+
pageScope.$digest();
209+
210+
toolbarStyles = getComputedStyle(element.find('md-toolbar')[0]);
211+
contentStyles = getComputedStyle(element.find('md-content')[0]);
212+
213+
// Expect the toolbar to be hidden
214+
expect(toolbarStyles.display).toBeTruthy();
215+
expect(toolbarStyles.display).toEqual('none');
216+
217+
// Expect the content to have a zero margin top (because updateToolbarHeight() was called)
218+
expect(contentStyles.marginTop).toBeTruthy();
219+
expect(contentStyles.marginTop).toEqual('0px');
220+
221+
// Remove the element
222+
document.body.removeChild(element[0]);
223+
}));
224+
121225
// The toolbar is like a container component, so we want to make sure it works with ng-controller
122226
it('works with ng-controller', inject(function($exceptionHandler) {
123227
build(

0 commit comments

Comments
 (0)