Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(collpase): add collapsed and expanded events [Refs: #576] #779

Merged
merged 1 commit into from
Jul 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 64 additions & 60 deletions components/collapse/collapse.directive.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
// FIX: in order to update to rc.1 had to disable animation, sorry
import {Directive, OnInit, ElementRef, Input, HostBinding, Renderer} from '@angular/core';
import {Directive, OnInit, ElementRef, Input, Output, HostBinding, Renderer, EventEmitter} from '@angular/core';
// import {AnimationBuilder} from '@angular/platform-browser/src/animate/animation_builder';

// import {animation, style, animate, state, transition} from '@angular/core';

/*@Directive({
selector: '[collapse]',
// templateUrl: 'app/panel.html',
// styleUrls: ['app/panel.css'],
animations: [
animation('active', [
state('void', style({ height: 0 })),
state('closed', style({ height: 0 })),
state('open', style({ height: '*' })),
transition('void => closed', [ animate(0) ]),
transition('closed => open', [ animate('350ms ease-out') ]),
transition('open => closed', [ animate('350ms ease-out') ])
])
]
})*/
selector: '[collapse]',
// templateUrl: 'app/panel.html',
// styleUrls: ['app/panel.css'],
animations: [
animation('active', [
state('void', style({ height: 0 })),
state('closed', style({ height: 0 })),
state('open', style({ height: '*' })),
transition('void => closed', [ animate(0) ]),
transition('closed => open', [ animate('350ms ease-out') ]),
transition('open => closed', [ animate('350ms ease-out') ])
])
]
})*/
// fix: replace with // '@angular/animate';
// when https://github.com/angular/angular/issues/5984 will be fixed

// TODO: remove ElementRef
// TODO: add on change
// TODO: #576 add callbacks: expanding, expanded, collapsing, collapsed
// TODO: #576 add callbacks: expanding, collapsing after adding animation
@Directive({selector: '[collapse]'})
export class CollapseDirective implements OnInit {
// private animation:any;

@Output() public collapsed:EventEmitter<any> = new EventEmitter<any>(false);
@Output() public expanded:EventEmitter<any> = new EventEmitter<any>(false);
// style
// @HostBinding('style.height')
// private height:string;
Expand Down Expand Up @@ -59,12 +60,13 @@ export class CollapseDirective implements OnInit {
private get collapse():boolean {
return this.isExpanded;
}

// private open: boolean;
// private _ab:AnimationBuilder;
private _el:ElementRef;
private _renderer:Renderer;

public constructor(/*_ab:AnimationBuilder, */_el:ElementRef, _renderer: Renderer) {
public constructor(/*_ab:AnimationBuilder, */_el:ElementRef, _renderer:Renderer) {
// this._ab = _ab;
this._el = _el;
this._renderer = _renderer;
Expand Down Expand Up @@ -95,30 +97,31 @@ export class CollapseDirective implements OnInit {
this.isCollapsing = false;

this.display = 'none';
this.collapsed.emit(event);

/* setTimeout(() => {
// this.height = '0';
// this.isCollapse = true;
// this.isCollapsing = false;
this.animation
.setFromStyles({
height: this._el.nativeElement.scrollHeight + 'px'
})
.setToStyles({
height: '0',
overflow: 'hidden'
});

this.animation.start(this._el.nativeElement)
.onComplete(() => {
if (this._el.nativeElement.offsetHeight === 0) {
this.display = 'none';
}

this.isCollapse = true;
this.isCollapsing = false;
});
}, 4);*/
// this.height = '0';
// this.isCollapse = true;
// this.isCollapsing = false;
this.animation
.setFromStyles({
height: this._el.nativeElement.scrollHeight + 'px'
})
.setToStyles({
height: '0',
overflow: 'hidden'
});

this.animation.start(this._el.nativeElement)
.onComplete(() => {
if (this._el.nativeElement.offsetHeight === 0) {
this.display = 'none';
}

this.isCollapse = true;
this.isCollapsing = false;
});
}, 4);*/
}

public show():void {
Expand All @@ -134,26 +137,27 @@ export class CollapseDirective implements OnInit {
this.isCollapsing = false;
this._renderer.setElementStyle(this._el.nativeElement, 'overflow', 'visible');
this._renderer.setElementStyle(this._el.nativeElement, 'height', 'auto');
this.expanded.emit(event);
/*setTimeout(() => {
// this.height = 'auto';
// this.isCollapse = true;
// this.isCollapsing = false;
this.animation
.setFromStyles({
height: this._el.nativeElement.offsetHeight,
overflow: 'hidden'
})
.setToStyles({
height: this._el.nativeElement.scrollHeight + 'px'
});

this.animation.start(this._el.nativeElement)
.onComplete(() => {
this.isCollapse = true;
this.isCollapsing = false;
this._renderer.setElementStyle(this._el.nativeElement, 'overflow', 'visible');
this._renderer.setElementStyle(this._el.nativeElement, 'height', 'auto');
});
}, 4);*/
// this.height = 'auto';
// this.isCollapse = true;
// this.isCollapsing = false;
this.animation
.setFromStyles({
height: this._el.nativeElement.offsetHeight,
overflow: 'hidden'
})
.setToStyles({
height: this._el.nativeElement.scrollHeight + 'px'
});

this.animation.start(this._el.nativeElement)
.onComplete(() => {
this.isCollapse = true;
this.isCollapsing = false;
this._renderer.setElementStyle(this._el.nativeElement, 'overflow', 'visible');
this._renderer.setElementStyle(this._el.nativeElement, 'height', 'auto');
});
}, 4);*/
}
}
5 changes: 4 additions & 1 deletion demo/components/collapse/collapse-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
(click)="isCollapsed = !isCollapsed">Toggle collapse
</button>
<hr>
<div [collapse]="isCollapsed" class="card card-block card-header">
<div (collapsed)="collapsed($event)"
(expanded)="expanded($event)"
[collapse]="isCollapsed"
class="card card-block card-header">
<div class="well well-lg">Some content</div>
</div>
8 changes: 8 additions & 0 deletions demo/components/collapse/collapse-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ let template = require('./collapse-demo.html');
})
export class CollapseDemoComponent {
public isCollapsed:boolean = false;

public collapsed(event:any):void {
console.log(event);
}

public expanded(event:any):void {
console.log(event);
}
}