forked from valor-software/ngx-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datepicker): Add directive for inline datepicker
Closes valor-software#3955
- Loading branch information
Martin Troedsson
committed
Mar 8, 2018
1 parent
65d0475
commit afcf540
Showing
12 changed files
with
275 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { | ||
ComponentRef, Directive, ElementRef, EventEmitter, Input, OnChanges, | ||
OnDestroy, OnInit, Output, Renderer2, SimpleChanges, ViewContainerRef | ||
} from '@angular/core'; | ||
import { ComponentLoader } from '../component-loader/component-loader.class'; | ||
import { ComponentLoaderFactory } from '../component-loader/component-loader.factory'; | ||
import { BsDatepickerInlineContainerComponent } from './themes/bs/bs-datepicker-inline-container.component'; | ||
import { Subscription } from 'rxjs/Subscription'; | ||
import 'rxjs/add/operator/filter'; | ||
import { BsDatepickerInlineConfig } from './bs-datepicker-inline.config'; | ||
import { BsLocaleService } from './bs-locale.service'; | ||
import { BsDatepickerConfig } from './bs-datepicker.config'; | ||
|
||
@Directive({ | ||
selector: 'bs-datepicker-inline', | ||
exportAs: 'bsDatepickerInline' | ||
}) | ||
export class BsDatepickerInlineDirective implements OnInit, OnDestroy, OnChanges { | ||
_bsValue: Date; | ||
/** | ||
* Initial value of datepicker | ||
*/ | ||
@Input() | ||
set bsValue(value: Date) { | ||
if (this._bsValue === value) { | ||
return; | ||
} | ||
this._bsValue = value; | ||
this.bsValueChange.emit(value); | ||
} | ||
|
||
/** | ||
* Config object for datepicker | ||
*/ | ||
@Input() bsConfig: Partial<BsDatepickerInlineConfig>; | ||
/** | ||
* Indicates whether datepicker is enabled or not | ||
*/ | ||
@Input() isDisabled: boolean; | ||
/** | ||
* Minimum date which is available for selection | ||
*/ | ||
@Input() minDate: Date; | ||
/** | ||
* Maximum date which is available for selection | ||
*/ | ||
@Input() maxDate: Date; | ||
/** | ||
* Emits when datepicker value has been changed | ||
*/ | ||
@Output() bsValueChange: EventEmitter<Date> = new EventEmitter(); | ||
|
||
protected _subs: Subscription[] = []; | ||
|
||
private _datepicker: ComponentLoader<BsDatepickerInlineContainerComponent>; | ||
private _datepickerRef: ComponentRef<BsDatepickerInlineContainerComponent>; | ||
|
||
constructor(public _config: BsDatepickerInlineConfig, | ||
private _elementRef: ElementRef, | ||
_renderer: Renderer2, | ||
_viewContainerRef: ViewContainerRef, | ||
cis: ComponentLoaderFactory) { | ||
// todo: assign only subset of fields | ||
Object.assign(this, this._config); | ||
this._datepicker = cis.createLoader<BsDatepickerInlineContainerComponent>( | ||
_elementRef, | ||
_viewContainerRef, | ||
_renderer | ||
); | ||
} | ||
|
||
ngOnInit(): any { | ||
this.setConfig(); | ||
|
||
this._datepickerRef = this._datepicker | ||
.provide({provide: BsDatepickerConfig, useValue: this._config}) | ||
.attach(BsDatepickerInlineContainerComponent) | ||
.to(this._elementRef) | ||
.show(); | ||
|
||
// if date changes from external source (model -> view) | ||
this._subs.push( | ||
this.bsValueChange.subscribe((value: Date) => { | ||
this._datepickerRef.instance.value = value; | ||
}) | ||
); | ||
|
||
// if date changes from picker (view -> model) | ||
this._subs.push( | ||
this._datepickerRef.instance.valueChange.subscribe((value: Date) => { | ||
this.bsValue = value; | ||
}) | ||
); | ||
} | ||
|
||
ngOnChanges(changes: SimpleChanges): void { | ||
if (!this._datepickerRef || !this._datepickerRef.instance) { | ||
return; | ||
} | ||
|
||
if (changes.minDate) { | ||
this._datepickerRef.instance.minDate = this.minDate; | ||
} | ||
|
||
if (changes.maxDate) { | ||
this._datepickerRef.instance.maxDate = this.maxDate; | ||
} | ||
|
||
if (changes.isDisabled) { | ||
this._datepickerRef.instance.isDisabled = this.isDisabled; | ||
} | ||
} | ||
|
||
/** | ||
* Set config for datepicker | ||
*/ | ||
setConfig(): void { | ||
this._config = Object.assign({}, this._config, this.bsConfig, { | ||
value: this._bsValue, | ||
isDisabled: this.isDisabled, | ||
minDate: this.minDate || this.bsConfig && this.bsConfig.minDate, | ||
maxDate: this.maxDate || this.bsConfig && this.bsConfig.maxDate | ||
}); | ||
} | ||
|
||
ngOnDestroy(): any { | ||
this._datepicker.dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { BsDatepickerConfig } from './bs-datepicker.config'; | ||
|
||
@Injectable() | ||
export class BsDatepickerInlineConfig extends BsDatepickerConfig { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/datepicker/themes/bs/bs-datepicker-inline-container.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Component, OnDestroy, OnInit } from '@angular/core'; | ||
import { BsDatepickerContainerComponent } from './bs-datepicker-container.component'; | ||
|
||
import { BsDatepickerConfig } from '../../bs-datepicker.config'; | ||
import { BsDatepickerActions } from '../../reducer/bs-datepicker.actions'; | ||
import { BsDatepickerEffects } from '../../reducer/bs-datepicker.effects'; | ||
import { BsDatepickerStore } from '../../reducer/bs-datepicker.store'; | ||
|
||
@Component({ | ||
selector: 'bs-datepicker-inline-container', | ||
providers: [BsDatepickerStore, BsDatepickerEffects], | ||
templateUrl: './bs-datepicker-view.html', | ||
host: { | ||
'(click)': '_stopPropagation($event)', | ||
style: 'display: inline-block;' | ||
} | ||
}) | ||
export class BsDatepickerInlineContainerComponent extends BsDatepickerContainerComponent | ||
implements OnInit, OnDestroy { | ||
constructor( | ||
_config: BsDatepickerConfig, | ||
_store: BsDatepickerStore, | ||
_actions: BsDatepickerActions, | ||
_effects: BsDatepickerEffects | ||
) { | ||
super(_config, _store, _actions, _effects); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters