@angular/cdk
Version:
Angular Material Component Development Kit
538 lines (530 loc) • 16.4 kB
JavaScript
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Directive, TemplateRef, ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ContentChildren, EventEmitter, ElementRef, forwardRef, Inject, Input, Optional, Output, ViewChild, ViewEncapsulation, NgModule } from '@angular/core';
import { FocusKeyManager } from '@angular/cdk/a11y';
import { Directionality, BidiModule } from '@angular/cdk/bidi';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { END, ENTER, HOME, SPACE } from '@angular/cdk/keycodes';
import { DOCUMENT, CommonModule } from '@angular/common';
import '@angular/forms';
import { Subject, of } from 'rxjs';
import { startWith, takeUntil } from 'rxjs/operators';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
class CdkStepLabel {
/**
* @param {?} template
*/
constructor(template) {
this.template = template;
}
}
CdkStepLabel.decorators = [
{ type: Directive, args: [{
selector: '[cdkStepLabel]',
},] },
];
/** @nocollapse */
CdkStepLabel.ctorParameters = () => [
{ type: TemplateRef, },
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* Used to generate unique ID for each stepper component.
*/
let /** @type {?} */ nextId = 0;
/**
* Change event emitted on selection changes.
*/
class StepperSelectionEvent {
}
class CdkStep {
/**
* @param {?} _stepper
*/
constructor(_stepper) {
this._stepper = _stepper;
/**
* Whether user has seen the expanded step content or not.
*/
this.interacted = false;
this._editable = true;
this._optional = false;
this._customCompleted = null;
}
/**
* Whether the user can return to this step once it has been marked as complted.
* @return {?}
*/
get editable() { return this._editable; }
/**
* @param {?} value
* @return {?}
*/
set editable(value) {
this._editable = coerceBooleanProperty(value);
}
/**
* Whether the completion of step is optional.
* @return {?}
*/
get optional() { return this._optional; }
/**
* @param {?} value
* @return {?}
*/
set optional(value) {
this._optional = coerceBooleanProperty(value);
}
/**
* Whether step is marked as completed.
* @return {?}
*/
get completed() {
return this._customCompleted == null ? this._defaultCompleted : this._customCompleted;
}
/**
* @param {?} value
* @return {?}
*/
set completed(value) {
this._customCompleted = coerceBooleanProperty(value);
}
/**
* @return {?}
*/
get _defaultCompleted() {
return this.stepControl ? this.stepControl.valid && this.interacted : this.interacted;
}
/**
* Selects this step component.
* @return {?}
*/
select() {
this._stepper.selected = this;
}
/**
* Resets the step to its initial state. Note that this includes resetting form data.
* @return {?}
*/
reset() {
this.interacted = false;
if (this._customCompleted != null) {
this._customCompleted = false;
}
if (this.stepControl) {
this.stepControl.reset();
}
}
/**
* @return {?}
*/
ngOnChanges() {
// Since basically all inputs of the MatStep get proxied through the view down to the
// underlying MatStepHeader, we have to make sure that change detection runs correctly.
this._stepper._stateChanged();
}
}
CdkStep.decorators = [
{ type: Component, args: [{selector: 'cdk-step',
exportAs: 'cdkStep',
template: '<ng-template><ng-content></ng-content></ng-template>',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
},] },
];
/** @nocollapse */
CdkStep.ctorParameters = () => [
{ type: CdkStepper, decorators: [{ type: Inject, args: [forwardRef(() => CdkStepper),] },] },
];
CdkStep.propDecorators = {
"stepLabel": [{ type: ContentChild, args: [CdkStepLabel,] },],
"content": [{ type: ViewChild, args: [TemplateRef,] },],
"stepControl": [{ type: Input },],
"label": [{ type: Input },],
"ariaLabel": [{ type: Input, args: ['aria-label',] },],
"ariaLabelledby": [{ type: Input, args: ['aria-labelledby',] },],
"editable": [{ type: Input },],
"optional": [{ type: Input },],
"completed": [{ type: Input },],
};
class CdkStepper {
/**
* @param {?} _dir
* @param {?} _changeDetectorRef
* @param {?=} _elementRef
* @param {?=} _document
*/
constructor(_dir, _changeDetectorRef, _elementRef, _document) {
this._dir = _dir;
this._changeDetectorRef = _changeDetectorRef;
this._elementRef = _elementRef;
/**
* Emits when the component is destroyed.
*/
this._destroyed = new Subject();
this._linear = false;
this._selectedIndex = 0;
/**
* Event emitted when the selected step has changed.
*/
this.selectionChange = new EventEmitter();
this._orientation = 'horizontal';
this._groupId = nextId++;
this._document = _document;
}
/**
* Whether the validity of previous steps should be checked or not.
* @return {?}
*/
get linear() { return this._linear; }
/**
* @param {?} value
* @return {?}
*/
set linear(value) { this._linear = coerceBooleanProperty(value); }
/**
* The index of the selected step.
* @return {?}
*/
get selectedIndex() { return this._selectedIndex; }
/**
* @param {?} index
* @return {?}
*/
set selectedIndex(index) {
if (this._steps) {
// Ensure that the index can't be out of bounds.
if (index < 0 || index > this._steps.length - 1) {
throw Error('cdkStepper: Cannot assign out-of-bounds value to `selectedIndex`.');
}
if (this._selectedIndex != index &&
!this._anyControlsInvalidOrPending(index) &&
(index >= this._selectedIndex || this._steps.toArray()[index].editable)) {
this._updateSelectedItemIndex(index);
}
}
else {
this._selectedIndex = index;
}
}
/**
* The step that is selected.
* @return {?}
*/
get selected() {
// @breaking-change 7.0.0 Change return type to `CdkStep | undefined`.
return this._steps ? this._steps.toArray()[this.selectedIndex] : /** @type {?} */ ((undefined));
}
/**
* @param {?} step
* @return {?}
*/
set selected(step) {
this.selectedIndex = this._steps ? this._steps.toArray().indexOf(step) : -1;
}
/**
* @return {?}
*/
ngAfterViewInit() {
this._keyManager = new FocusKeyManager(this._stepHeader)
.withWrap()
.withVerticalOrientation(this._orientation === 'vertical');
(this._dir ? /** @type {?} */ (this._dir.change) : of())
.pipe(startWith(this._layoutDirection()), takeUntil(this._destroyed))
.subscribe(direction => this._keyManager.withHorizontalOrientation(direction));
this._keyManager.updateActiveItemIndex(this._selectedIndex);
this._steps.changes.pipe(takeUntil(this._destroyed)).subscribe(() => {
if (!this.selected) {
this._selectedIndex = Math.max(this._selectedIndex - 1, 0);
}
});
}
/**
* @return {?}
*/
ngOnDestroy() {
this._destroyed.next();
this._destroyed.complete();
}
/**
* Selects and focuses the next step in list.
* @return {?}
*/
next() {
this.selectedIndex = Math.min(this._selectedIndex + 1, this._steps.length - 1);
}
/**
* Selects and focuses the previous step in list.
* @return {?}
*/
previous() {
this.selectedIndex = Math.max(this._selectedIndex - 1, 0);
}
/**
* Resets the stepper to its initial state. Note that this includes clearing form data.
* @return {?}
*/
reset() {
this._updateSelectedItemIndex(0);
this._steps.forEach(step => step.reset());
this._stateChanged();
}
/**
* Returns a unique id for each step label element.
* @param {?} i
* @return {?}
*/
_getStepLabelId(i) {
return `cdk-step-label-${this._groupId}-${i}`;
}
/**
* Returns unique id for each step content element.
* @param {?} i
* @return {?}
*/
_getStepContentId(i) {
return `cdk-step-content-${this._groupId}-${i}`;
}
/**
* Marks the component to be change detected.
* @return {?}
*/
_stateChanged() {
this._changeDetectorRef.markForCheck();
}
/**
* Returns position state of the step with the given index.
* @param {?} index
* @return {?}
*/
_getAnimationDirection(index) {
const /** @type {?} */ position = index - this._selectedIndex;
if (position < 0) {
return this._layoutDirection() === 'rtl' ? 'next' : 'previous';
}
else if (position > 0) {
return this._layoutDirection() === 'rtl' ? 'previous' : 'next';
}
return 'current';
}
/**
* Returns the type of icon to be displayed.
* @param {?} index
* @return {?}
*/
_getIndicatorType(index) {
const /** @type {?} */ step = this._steps.toArray()[index];
if (!step.completed || this._selectedIndex == index) {
return 'number';
}
else {
return step.editable ? 'edit' : 'done';
}
}
/**
* Returns the index of the currently-focused step header.
* @return {?}
*/
_getFocusIndex() {
return this._keyManager ? this._keyManager.activeItemIndex : this._selectedIndex;
}
/**
* @param {?} newIndex
* @return {?}
*/
_updateSelectedItemIndex(newIndex) {
const /** @type {?} */ stepsArray = this._steps.toArray();
this.selectionChange.emit({
selectedIndex: newIndex,
previouslySelectedIndex: this._selectedIndex,
selectedStep: stepsArray[newIndex],
previouslySelectedStep: stepsArray[this._selectedIndex],
});
// If focus is inside the stepper, move it to the next header, otherwise it may become
// lost when the active step content is hidden. We can't be more granular with the check
// (e.g. checking whether focus is inside the active step), because we don't have a
// reference to the elements that are rendering out the content.
this._containsFocus() ? this._keyManager.setActiveItem(newIndex) :
this._keyManager.updateActiveItemIndex(newIndex);
this._selectedIndex = newIndex;
this._stateChanged();
}
/**
* @param {?} event
* @return {?}
*/
_onKeydown(event) {
const /** @type {?} */ keyCode = event.keyCode;
if (this._keyManager.activeItemIndex != null && (keyCode === SPACE || keyCode === ENTER)) {
this.selectedIndex = this._keyManager.activeItemIndex;
event.preventDefault();
}
else if (keyCode === HOME) {
this._keyManager.setFirstItemActive();
event.preventDefault();
}
else if (keyCode === END) {
this._keyManager.setLastItemActive();
event.preventDefault();
}
else {
this._keyManager.onKeydown(event);
}
}
/**
* @param {?} index
* @return {?}
*/
_anyControlsInvalidOrPending(index) {
const /** @type {?} */ steps = this._steps.toArray();
steps[this._selectedIndex].interacted = true;
if (this._linear && index >= 0) {
return steps.slice(0, index).some(step => {
const /** @type {?} */ control = step.stepControl;
const /** @type {?} */ isIncomplete = control ?
(control.invalid || control.pending || !step.interacted) :
!step.completed;
return isIncomplete && !step.optional;
});
}
return false;
}
/**
* @return {?}
*/
_layoutDirection() {
return this._dir && this._dir.value === 'rtl' ? 'rtl' : 'ltr';
}
/**
* Checks whether the stepper contains the focused element.
* @return {?}
*/
_containsFocus() {
if (!this._document || !this._elementRef) {
return false;
}
const /** @type {?} */ stepperElement = this._elementRef.nativeElement;
const /** @type {?} */ focusedElement = this._document.activeElement;
return stepperElement === focusedElement || stepperElement.contains(focusedElement);
}
}
CdkStepper.decorators = [
{ type: Directive, args: [{
selector: '[cdkStepper]',
exportAs: 'cdkStepper',
},] },
];
/** @nocollapse */
CdkStepper.ctorParameters = () => [
{ type: Directionality, decorators: [{ type: Optional },] },
{ type: ChangeDetectorRef, },
{ type: ElementRef, },
{ type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] },] },
];
CdkStepper.propDecorators = {
"_steps": [{ type: ContentChildren, args: [CdkStep,] },],
"linear": [{ type: Input },],
"selectedIndex": [{ type: Input },],
"selected": [{ type: Input },],
"selectionChange": [{ type: Output },],
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* Button that moves to the next step in a stepper workflow.
*/
class CdkStepperNext {
/**
* @param {?} _stepper
*/
constructor(_stepper) {
this._stepper = _stepper;
/**
* Type of the next button. Defaults to "submit" if not specified.
*/
this.type = 'submit';
}
}
CdkStepperNext.decorators = [
{ type: Directive, args: [{
selector: 'button[cdkStepperNext]',
host: {
'(click)': '_stepper.next()',
'[type]': 'type',
}
},] },
];
/** @nocollapse */
CdkStepperNext.ctorParameters = () => [
{ type: CdkStepper, },
];
CdkStepperNext.propDecorators = {
"type": [{ type: Input },],
};
/**
* Button that moves to the previous step in a stepper workflow.
*/
class CdkStepperPrevious {
/**
* @param {?} _stepper
*/
constructor(_stepper) {
this._stepper = _stepper;
/**
* Type of the previous button. Defaults to "button" if not specified.
*/
this.type = 'button';
}
}
CdkStepperPrevious.decorators = [
{ type: Directive, args: [{
selector: 'button[cdkStepperPrevious]',
host: {
'(click)': '_stepper.previous()',
'[type]': 'type',
}
},] },
];
/** @nocollapse */
CdkStepperPrevious.ctorParameters = () => [
{ type: CdkStepper, },
];
CdkStepperPrevious.propDecorators = {
"type": [{ type: Input },],
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
class CdkStepperModule {
}
CdkStepperModule.decorators = [
{ type: NgModule, args: [{
imports: [BidiModule, CommonModule],
exports: [CdkStep, CdkStepper, CdkStepLabel, CdkStepperNext, CdkStepperPrevious],
declarations: [CdkStep, CdkStepper, CdkStepLabel, CdkStepperNext, CdkStepperPrevious]
},] },
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
export { StepperSelectionEvent, CdkStep, CdkStepper, CdkStepLabel, CdkStepperNext, CdkStepperPrevious, CdkStepperModule };
//# sourceMappingURL=stepper.js.map