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

Commit feeb76e

Browse files
committed
feat(panel): configurable animation duration
* Adds the ability to specify the duration of an animation the panel. * Fixes the `MdPanelAnimation` not showing up in the docs. Fixes #9177.
1 parent a3b3e7b commit feeb76e

File tree

5 files changed

+105
-14
lines changed

5 files changed

+105
-14
lines changed

src/components/panel/demoPanelAnimations/index.html

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<div class="demo-md-panel-animation md-padding" ng-controller="AnimationCtrl as ctrl">
22
<h2>Animations</h2>
33
<div layout="row">
4-
<div flex="33">
5-
OpenFrom:
4+
<div flex="25">
5+
<h3>OpenFrom:</h3>
66
<md-radio-group ng-model="ctrl.openFrom">
77
<md-radio-button value="button">Button</md-radio-button>
88
<md-radio-button value="corner">Top/Left Corner</md-radio-button>
99
<md-radio-button value="bottom">Bottom Center</md-radio-button>
1010
</md-radio-group>
1111
</div>
1212

13-
<div flex="33">
14-
CloseTo:
13+
<div flex="25">
14+
<h3>CloseTo:</h3>
1515
<md-radio-group ng-model="ctrl.closeTo">
1616
<md-radio-button value="button">Button</md-radio-button>
1717
<md-radio-button value="corner">Top/Left Corner</md-radio-button>
1818
<md-radio-button value="bottom">Bottom Center</md-radio-button>
1919
</md-radio-group>
2020
</div>
2121

22-
<div flex="33">
23-
AnimationType:
22+
<div flex="25">
23+
<h3>AnimationType:</h3>
2424
<md-radio-group ng-model="ctrl.animationType">
2525
<md-radio-button value="none">None</md-radio-button>
2626
<md-radio-button value="slide">Slide</md-radio-button>
@@ -29,6 +29,14 @@ <h2>Animations</h2>
2929
<md-radio-button value="custom">Custom</md-radio-button>
3030
</md-radio-group>
3131
</div>
32+
33+
<div flex="25">
34+
<h3>Duration:</h3>
35+
<md-input-container>
36+
<label>Animation duration</label>
37+
<input type="number" ng-model="ctrl.duration">
38+
</md-input-container>
39+
</div>
3240
</div>
3341

3442
<div class="demo-md-panel-content">

src/components/panel/demoPanelAnimations/script.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function AnimationCtrl($mdPanel) {
1111
this.openFrom = 'button';
1212
this.closeTo = 'button';
1313
this.animationType = 'none';
14+
this.duration = 300;
1415
}
1516

1617

@@ -22,6 +23,8 @@ AnimationCtrl.prototype.showDialog = function() {
2223

2324
var animation = this._mdPanel.newPanelAnimation();
2425

26+
animation.duration(this.duration);
27+
2528
switch(this.openFrom) {
2629
case 'button':
2730
animation.openFrom('.animation-target');

src/components/panel/panel.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,24 +592,27 @@ angular
592592
* MdPanelAnimation *
593593
*****************************************************************************/
594594

595-
596595
/**
597596
* @ngdoc type
598597
* @name MdPanelAnimation
598+
* @module material.components.panel
599599
* @description
600600
* Animation configuration object. To use, create an MdPanelAnimation with the
601601
* desired properties, then pass the object as part of $mdPanel creation.
602602
*
603-
* Example:
603+
* @usage
604604
*
605+
* <hljs lang="js">
605606
* var panelAnimation = new MdPanelAnimation()
606607
* .openFrom(myButtonEl)
608+
* .duration(1337)
607609
* .closeTo('.my-button')
608610
* .withAnimation($mdPanel.animation.SCALE);
609611
*
610612
* $mdPanel.create({
611613
* animation: panelAnimation
612614
* });
615+
* </hljs>
613616
*/
614617

615618
/**
@@ -658,6 +661,18 @@ angular
658661
* @returns {!MdPanelAnimation}
659662
*/
660663

664+
/**
665+
* @ngdoc method
666+
* @name MdPanelAnimation#duration
667+
* @description
668+
* Specifies the duration of the animation in milliseconds. The `duration`
669+
* method accepts either a number or an object with separate open and close
670+
* durations.
671+
*
672+
* @param {number|{open: number, close: number}} duration
673+
* @returns {!MdPanelAnimation}
674+
*/
675+
661676

662677
/*****************************************************************************
663678
* IMPLEMENTATION *
@@ -2279,6 +2294,12 @@ function MdPanelAnimation($injector) {
22792294

22802295
/** @private {string|{open: string, close: string}} */
22812296
this._animationClass = '';
2297+
2298+
/** @private {number} */
2299+
this._openDuration;
2300+
2301+
/** @private {number} */
2302+
this._closeDuration;
22822303
}
22832304

22842305

@@ -2326,6 +2347,27 @@ MdPanelAnimation.prototype.closeTo = function(closeTo) {
23262347
return this;
23272348
};
23282349

2350+
/**
2351+
* Specifies the duration of the animation in milliseconds.
2352+
* @param {number|{open: number, close: number}} duration
2353+
* @returns {!MdPanelAnimation}
2354+
*/
2355+
MdPanelAnimation.prototype.duration = function(duration) {
2356+
if (duration) {
2357+
if (angular.isNumber(duration)) {
2358+
this._openDuration = this._closeDuration = toSeconds(duration);
2359+
} else if (angular.isObject(duration)) {
2360+
this._openDuration = toSeconds(duration.open);
2361+
this._closeDuration = toSeconds(duration.close);
2362+
}
2363+
}
2364+
2365+
return this;
2366+
2367+
function toSeconds(value) {
2368+
if (angular.isNumber(value)) return value / 1000;
2369+
}
2370+
};
23292371

23302372
/**
23312373
* Returns the element and bounds for the animation target.
@@ -2428,6 +2470,8 @@ MdPanelAnimation.prototype.animateOpen = function(panelEl) {
24282470
}
24292471
}
24302472

2473+
animationOptions.duration = this._openDuration;
2474+
24312475
return animator
24322476
.translate3d(panelEl, openFrom, openTo, animationOptions);
24332477
};
@@ -2491,6 +2535,8 @@ MdPanelAnimation.prototype.animateClose = function(panelEl) {
24912535
}
24922536
}
24932537

2538+
reverseAnimationOptions.duration = this._closeDuration;
2539+
24942540
return animator
24952541
.translate3d(panelEl, closeFrom, closeTo, reverseAnimationOptions);
24962542
};

src/components/panel/panel.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,38 @@ describe('$mdPanel', function() {
21492149
expect(animation._closeTo.bounds).toEqual(inputRect);
21502150
});
21512151
});
2152+
2153+
describe('should determine the animation duration when', function() {
2154+
it('provided a value in milliseconds', function() {
2155+
var animation = mdPanelAnimation.duration(1300);
2156+
2157+
expect(animation._openDuration).toBe(1.3);
2158+
});
2159+
2160+
it('provided a number', function() {
2161+
var animation = mdPanelAnimation.duration(2000);
2162+
2163+
expect(animation._openDuration).toEqual(animation._closeDuration);
2164+
expect(animation._openDuration).toBe(2);
2165+
});
2166+
2167+
it('provided an object', function() {
2168+
var animation = mdPanelAnimation.duration({
2169+
open: 1200,
2170+
close: 600
2171+
});
2172+
2173+
expect(animation._openDuration).toBe(1.2);
2174+
expect(animation._closeDuration).toBe(0.6);
2175+
});
2176+
2177+
it('provided an invalid value', function() {
2178+
var animation = mdPanelAnimation.duration('very fast');
2179+
2180+
expect(animation._openDuration).toBeFalsy();
2181+
expect(animation._closeDuration).toBeFalsy();
2182+
});
2183+
});
21522184
});
21532185

21542186
/**

src/core/util/animation/animate.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ function AnimateDomUtils($mdUtil, $q, $timeout, $mdConstant, $animateCss) {
2121
*
2222
*/
2323
translate3d : function( target, from, to, options ) {
24-
return $animateCss(target,{
25-
from:from,
26-
to:to,
27-
addClass:options.transitionInClass,
28-
removeClass:options.transitionOutClass
24+
return $animateCss(target, {
25+
from: from,
26+
to: to,
27+
addClass: options.transitionInClass,
28+
removeClass: options.transitionOutClass,
29+
duration: options.duration
2930
})
3031
.start()
3132
.then(function(){
@@ -40,7 +41,8 @@ function AnimateDomUtils($mdUtil, $q, $timeout, $mdConstant, $animateCss) {
4041
return $animateCss(target, {
4142
to: newFrom || from,
4243
addClass: options.transitionOutClass,
43-
removeClass: options.transitionInClass
44+
removeClass: options.transitionInClass,
45+
duration: options.duration
4446
}).start();
4547

4648
}

0 commit comments

Comments
 (0)