|
| 1 | +import { |
| 2 | + AfterContentInit, |
| 3 | + Component, |
| 4 | + ContentChildren, |
| 5 | + ElementRef, |
| 6 | + EventEmitter, |
| 7 | + Host, |
| 8 | + HostBinding, |
| 9 | + Input, |
| 10 | + View, |
| 11 | + ViewEncapsulation, |
| 12 | + OnChanges, |
| 13 | + Optional, |
| 14 | + Output, |
| 15 | + Query, |
| 16 | + QueryList, |
| 17 | + SimpleChange |
| 18 | +} from 'angular2/core'; |
| 19 | +import {BaseException} from 'angular2/src/facade/exceptions'; |
| 20 | +import {Dir} from "../../directives/dir/dir"; |
| 21 | +import {MdMediaQuery} from "../../services/media-query/media-query"; |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * Exception thrown when a MdSidenavContainer is missing both sidenavs. |
| 26 | + */ |
| 27 | +export class MdMissingSidenavException extends BaseException {} |
| 28 | +export class MdDuplicatedSidenavException extends BaseException { |
| 29 | + constructor(align: string) { |
| 30 | + super(`A sidenav was already declared for 'align="${align}"'`); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +@Component({ |
| 36 | + selector: 'md-sidenav', |
| 37 | + template: '<ng-content></ng-content>', |
| 38 | +}) |
| 39 | +export class MdSidenav { |
| 40 | + @Input('align') private align_: string = 'start'; |
| 41 | + @Input('mode') private mode_: string = 'over'; |
| 42 | + |
| 43 | + @Input('opened') private opened_: boolean = false; |
| 44 | + |
| 45 | + @Input() get width() { |
| 46 | + if (this.elementRef_.nativeElement) { |
| 47 | + return this.elementRef_.nativeElement.offsetWidth; |
| 48 | + } |
| 49 | + return 0; |
| 50 | + } |
| 51 | + |
| 52 | + @Output('open-start') onOpenStart = new EventEmitter<void>(); |
| 53 | + @Output('open') onOpen = new EventEmitter<void>(); |
| 54 | + @Output('close-start') onCloseStart = new EventEmitter<void>(); |
| 55 | + @Output('close') onClose = new EventEmitter<void>(); |
| 56 | + |
| 57 | + @HostBinding('class.md-sidenav-closing') get isClosing() { |
| 58 | + return !this.opened_ && this.transition_; |
| 59 | + } |
| 60 | + @HostBinding('class.md-sidenav-opening') get isOpening() { |
| 61 | + return this.opened_ && this.transition_; |
| 62 | + } |
| 63 | + @HostBinding('class.md-sidenav-closed') get isClosed() { |
| 64 | + return !this.opened_ && !this.transition_; |
| 65 | + } |
| 66 | + @HostBinding('class.md-sidenav-opened') get isOpened() { |
| 67 | + return this.opened_ && !this.transition_; |
| 68 | + } |
| 69 | + @HostBinding('class.md-sidenav-end') get isEnd() { |
| 70 | + return this.align_ == 'end'; |
| 71 | + } |
| 72 | + @HostBinding('class.md-sidenav-side') get modeSide() { |
| 73 | + return this.mode_ == 'side'; |
| 74 | + } |
| 75 | + @HostBinding('class.md-sidenav-over') get modeOver() { |
| 76 | + return this.mode_ == 'over'; |
| 77 | + } |
| 78 | + |
| 79 | + private transition_: boolean = false; |
| 80 | + |
| 81 | + private onTransitionEnd_(): void { |
| 82 | + this.transition_ = false; |
| 83 | + if (this.opened_) { |
| 84 | + this.onOpen.emit(null); |
| 85 | + } else { |
| 86 | + this.onClose.emit(null); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // TODO(hansl): Get rid of ElementRef. |
| 91 | + constructor(private mediaQuery_: MdMediaQuery, |
| 92 | + private elementRef_: ElementRef) { |
| 93 | + elementRef_.nativeElement.addEventListener('transitionend', |
| 94 | + (e: TransitionEvent) => { |
| 95 | + if (e.target == elementRef_.nativeElement && e.propertyName == 'transform') { |
| 96 | + return this.onTransitionEnd_(); |
| 97 | + } |
| 98 | + }, false); |
| 99 | + } |
| 100 | + |
| 101 | + get align(): string { return this.align_; } |
| 102 | + set align(v: string) { |
| 103 | + if (v != 'end') { |
| 104 | + v = 'start'; |
| 105 | + } |
| 106 | + this.align_ = v; |
| 107 | + } |
| 108 | + |
| 109 | + get mode(): string { return this.mode_; } |
| 110 | + set mode(v: string) { |
| 111 | + if (v != 'side') { |
| 112 | + v = 'over'; |
| 113 | + } |
| 114 | + this.mode_ = v; |
| 115 | + } |
| 116 | + |
| 117 | + get opened(): boolean { return this.opened_; } |
| 118 | + set opened(v: boolean) { |
| 119 | + this.opened_ = v; |
| 120 | + this.transition_ = true; |
| 121 | + if (v) { |
| 122 | + this.onOpenStart.emit(null); |
| 123 | + } else { |
| 124 | + this.onCloseStart.emit(null); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + open(): Promise<void> { |
| 129 | + return this.toggle(true); |
| 130 | + } |
| 131 | + close(): Promise<void> { |
| 132 | + return this.toggle(false); |
| 133 | + } |
| 134 | + |
| 135 | + toggle(isOpen: boolean = !this.opened): Promise<void> { |
| 136 | + if (isOpen === this.opened) { |
| 137 | + return Promise.resolve(); |
| 138 | + } |
| 139 | + |
| 140 | + this.opened = isOpen; |
| 141 | + |
| 142 | + // We hook up both onOpen and onClose, but we reject the promise if |
| 143 | + // the animation was cut or cancelled for some reason. |
| 144 | + return new Promise<void>((resolve, reject) => { |
| 145 | + const property = isOpen ? this.onOpen : this.onClose; |
| 146 | + const other = isOpen ? this.onClose : this.onOpen; |
| 147 | + const subscription = property.subscribe(() => { |
| 148 | + resolve(); |
| 149 | + property.remove(subscription); |
| 150 | + other.remove(otherSubscription); |
| 151 | + }); |
| 152 | + const otherSubscription = property.subscribe(() => { |
| 153 | + reject(); |
| 154 | + property.remove(subscription); |
| 155 | + other.remove(otherSubscription); |
| 156 | + }); |
| 157 | + }); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | + |
| 162 | +@Component({ |
| 163 | + selector: 'md-sidenav-container', |
| 164 | + directives: [MdSidenav], |
| 165 | + templateUrl: './components/sidenav/sidenav.html', |
| 166 | + styleUrls: ['./components/sidenav/sidenav.css'], |
| 167 | +}) |
| 168 | +export class MdSidenavContainer implements AfterContentInit { |
| 169 | + @ContentChildren(MdSidenav) private drawers_: QueryList<MdSidenav>; |
| 170 | + |
| 171 | + @Input() get start() { return this.start_; } |
| 172 | + @Input() get end() { return this.end_; } |
| 173 | + |
| 174 | + private start_: MdSidenav; |
| 175 | + private end_: MdSidenav; |
| 176 | + private right_: MdSidenav; |
| 177 | + private left_: MdSidenav; |
| 178 | + |
| 179 | + private validateDrawers_() { |
| 180 | + this.start_ = this.end_ = null; |
| 181 | + if (this.drawers_.length === 0) { |
| 182 | + throw new MdMissingSidenavException(); |
| 183 | + } |
| 184 | + |
| 185 | + for (const drawer of this.drawers_.toArray()) { |
| 186 | + if (drawer.align == 'end') { |
| 187 | + if (this.end_) { |
| 188 | + throw new MdDuplicatedSidenavException('end'); |
| 189 | + } |
| 190 | + this.end_ = drawer; |
| 191 | + } else { |
| 192 | + if (this.start_) { |
| 193 | + throw new MdDuplicatedSidenavException('start'); |
| 194 | + } |
| 195 | + this.start_ = drawer; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + this.right_ = this.left_ = null; |
| 200 | + this.left_ = this.start_; |
| 201 | + this.right_ = this.end_; |
| 202 | + |
| 203 | + // Detect if we're LTR or RTL. |
| 204 | + if (this.dir_.dir == 'ltr') { |
| 205 | + this.left_ = this.start_; |
| 206 | + this.right_ = this.end_; |
| 207 | + } else { |
| 208 | + this.left_ = this.end_; |
| 209 | + this.right_ = this.start_; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + constructor(@Optional() @Host() private dir_: Dir) { |
| 214 | + this.dir_.onDirChange.subscribe(() => this.validateDrawers_()); |
| 215 | + } |
| 216 | + |
| 217 | + ngAfterContentInit() { |
| 218 | + // On changes, assert on consistency. |
| 219 | + this.drawers_.changes.subscribe(() => this.validateDrawers_()); |
| 220 | + this.validateDrawers_(); |
| 221 | + } |
| 222 | +} |
0 commit comments