UNPKG

@angular-mdc/web

Version:
739 lines (735 loc) 23.3 kB
/** * @license * Copyright (c) Dominic Carretto * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/trimox/angular-mdc-web/blob/master/LICENSE */ import { forwardRef, EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, NgZone, ChangeDetectorRef, ElementRef, Attribute, Input, Output, ViewChild, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { __extends, __awaiter, __generator } from 'tslib'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { coerceBooleanProperty, coerceNumberProperty } from '@angular/cdk/coercion'; import { Platform, supportsPassiveEventListeners } from '@angular/cdk/platform'; import { Subject, fromEvent } from 'rxjs'; import { auditTime, takeUntil } from 'rxjs/operators'; import { MDCComponent } from '@angular-mdc/web/base'; import { MDCSliderFoundation } from '@material/slider'; /** * @fileoverview added by tsickle * Generated from: slider/slider.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ var MDC_SLIDER_CONTROL_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef((/** * @return {?} */ function () { return MdcSlider; })), multi: true }; var MdcSliderChange = /** @class */ (function () { function MdcSliderChange(source, value) { this.source = source; this.value = value; } return MdcSliderChange; }()); var MdcSlider = /** @class */ (function (_super) { __extends(MdcSlider, _super); function MdcSlider(_platform, _ngZone, _changeDetectorRef, elementRef, tabIndex) { var _this = _super.call(this, elementRef) || this; _this._platform = _platform; _this._ngZone = _ngZone; _this._changeDetectorRef = _changeDetectorRef; _this.elementRef = elementRef; /** * Emits whenever the component is destroyed. */ _this._destroyed = new Subject(); _this._initialized = false; _this.tabIndex = 0; _this._discrete = false; _this._markers = false; _this._min = 0; _this._max = 100; _this._step = 1; _this._value = null; _this._disabled = false; _this.change = new EventEmitter(); _this.input = new EventEmitter(); /** * Emits when the raw value of the slider changes. This is here primarily * to facilitate the two-way binding for the `value` input. */ _this.valueChange = new EventEmitter(); /** * Function when touched */ _this._onTouched = (/** * @return {?} */ function () { }); /** * Function when changed */ _this._controlValueAccessorChangeFn = (/** * @return {?} */ function () { }); _this.tabIndex = parseInt(tabIndex, 10) || 0; _this._root = _this.elementRef.nativeElement; return _this; } Object.defineProperty(MdcSlider.prototype, "discrete", { get: /** * @return {?} */ function () { return this._discrete; }, set: /** * @param {?} value * @return {?} */ function (value) { this._discrete = coerceBooleanProperty(value); }, enumerable: true, configurable: true }); Object.defineProperty(MdcSlider.prototype, "markers", { get: /** * @return {?} */ function () { return this._markers; }, set: /** * @param {?} value * @return {?} */ function (value) { this._markers = coerceBooleanProperty(value); }, enumerable: true, configurable: true }); Object.defineProperty(MdcSlider.prototype, "min", { get: /** * @return {?} */ function () { return this._min; }, set: /** * @param {?} value * @return {?} */ function (value) { /** @type {?} */ var min = coerceNumberProperty(value); if (min !== this._min) { this._min = min; } }, enumerable: true, configurable: true }); Object.defineProperty(MdcSlider.prototype, "max", { get: /** * @return {?} */ function () { return this._max; }, set: /** * @param {?} value * @return {?} */ function (value) { /** @type {?} */ var max = coerceNumberProperty(value); if (max !== this._max) { this._max = max; } }, enumerable: true, configurable: true }); Object.defineProperty(MdcSlider.prototype, "step", { get: /** * @return {?} */ function () { return this._step; }, set: /** * @param {?} value * @return {?} */ function (value) { /** @type {?} */ var step = coerceNumberProperty(value, this._step); if (step !== this._step) { this._step = step; } }, enumerable: true, configurable: true }); Object.defineProperty(MdcSlider.prototype, "value", { get: /** * @return {?} */ function () { if (this._value === null) { this.value = this.min; } return this._value; }, set: /** * @param {?} newValue * @return {?} */ function (newValue) { this._value = coerceNumberProperty(newValue, null); }, enumerable: true, configurable: true }); Object.defineProperty(MdcSlider.prototype, "disabled", { get: /** * @return {?} */ function () { return this._disabled; }, set: /** * @param {?} value * @return {?} */ function (value) { this.setDisabledState(value); }, enumerable: true, configurable: true }); /** * @return {?} */ MdcSlider.prototype.getDefaultFoundation = /** * @return {?} */ function () { var _this = this; /** @type {?} */ var adapter = { hasClass: (/** * @param {?} className * @return {?} */ function (className) { return _this._root.classList.contains(className); }), addClass: (/** * @param {?} className * @return {?} */ function (className) { return _this._root.classList.add(className); }), removeClass: (/** * @param {?} className * @return {?} */ function (className) { return _this._root.classList.remove(className); }), getAttribute: (/** * @param {?} name * @return {?} */ function (name) { return _this._root.getAttribute(name); }), setAttribute: (/** * @param {?} name * @param {?} value * @return {?} */ function (name, value) { return _this._root.setAttribute(name, value); }), removeAttribute: (/** * @param {?} name * @return {?} */ function (name) { return _this._root.removeAttribute(name); }), computeBoundingRect: (/** * @return {?} */ function () { return _this._root.getBoundingClientRect(); }), getTabIndex: (/** * @return {?} */ function () { return ((/** @type {?} */ (_this._root))).tabIndex; }), registerInteractionHandler: (/** * @template K * @param {?} evtType * @param {?} handler * @return {?} */ function (evtType, handler) { return ((/** @type {?} */ (_this._root))).addEventListener(evtType, handler); }), deregisterInteractionHandler: (/** * @template K * @param {?} evtType * @param {?} handler * @return {?} */ function (evtType, handler) { return ((/** @type {?} */ (_this._root))).removeEventListener(evtType, handler); }), registerThumbContainerInteractionHandler: (/** * @template K * @param {?} evtType * @param {?} handler * @return {?} */ function (evtType, handler) { _this._ngZone.runOutsideAngular((/** * @return {?} */ function () { _this.thumbContainer.nativeElement.addEventListener(evtType, handler, supportsPassiveEventListeners()); })); }), deregisterThumbContainerInteractionHandler: (/** * @template K * @param {?} evtType * @param {?} handler * @return {?} */ function (evtType, handler) { return _this.thumbContainer.nativeElement.removeEventListener(evtType, handler, supportsPassiveEventListeners()); }), registerBodyInteractionHandler: (/** * @template K * @param {?} evtType * @param {?} handler * @return {?} */ function (evtType, handler) { return document.body.addEventListener(evtType, handler); }), deregisterBodyInteractionHandler: (/** * @template K * @param {?} evtType * @param {?} handler * @return {?} */ function (evtType, handler) { return document.body.removeEventListener(evtType, handler); }), registerResizeHandler: (/** * @return {?} */ function () { }), deregisterResizeHandler: (/** * @return {?} */ function () { }), notifyInput: (/** * @return {?} */ function () { /** @type {?} */ var newValue = _this._foundation.getValue(); if (newValue !== _this.value) { _this.value = newValue; _this.input.emit(_this._createChangeEvent(newValue)); } }), notifyChange: (/** * @return {?} */ function () { _this.value = _this._foundation.getValue(); _this._emitChangeEvent((/** @type {?} */ (_this.value))); }), setThumbContainerStyleProperty: (/** * @param {?} propertyName * @param {?} value * @return {?} */ function (propertyName, value) { return _this.thumbContainer.nativeElement.style.setProperty(propertyName, value); }), setTrackStyleProperty: (/** * @param {?} propertyName * @param {?} value * @return {?} */ function (propertyName, value) { return _this.track.nativeElement.style.setProperty(propertyName, value); }), setMarkerValue: (/** * @param {?} value * @return {?} */ function (value) { _this._changeDetectorRef.markForCheck(); (/** @type {?} */ (_this.pinValueMarker)).nativeElement.innerText = value !== null ? value.toString() : null; }), setTrackMarkers: (/** * @param {?} step * @param {?} max * @param {?} min * @return {?} */ function (step, max, min) { return (/** @type {?} */ (_this.trackMarkerContainer)).nativeElement.style.setProperty('background', _this._getTrackMarkersBackground(step, min, max)); }), isRTL: (/** * @return {?} */ function () { return _this._platform.isBrowser ? window.getComputedStyle(_this._root).getPropertyValue('direction') === 'rtl' : false; }), }; return new MDCSliderFoundation(adapter); }; /** * @param {?} changes * @return {?} */ MdcSlider.prototype.ngOnChanges = /** * @param {?} changes * @return {?} */ function (changes) { if (!this._initialized) { return; } if (changes['step']) { this._syncStepWithFoundation(); } if (changes['max']) { this._syncMaxWithFoundation(); } if (changes['min']) { this._syncMinWithFoundation(); } if (changes['value']) { this._syncValueWithFoundation(); } if (changes['markers'] || changes['discrete']) { this._refreshTrackMarkers(); } }; /** * @return {?} */ MdcSlider.prototype._asyncInitializeFoundation = /** * @return {?} */ function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { this._foundation.init(); return [2 /*return*/]; }); }); }; /** * @return {?} */ MdcSlider.prototype.ngAfterViewInit = /** * @return {?} */ function () { var _this = this; if (this._platform.isBrowser) { this._initialized = true; this._asyncInitializeFoundation() .then((/** * @return {?} */ function () { _this._syncStepWithFoundation(); _this._syncMaxWithFoundation(); _this._syncMinWithFoundation(); _this._syncValueWithFoundation(); _this._foundation.setupTrackMarker(); _this._loadListeners(); _this._changeDetectorRef.markForCheck(); })); } }; /** * @return {?} */ MdcSlider.prototype.ngOnDestroy = /** * @return {?} */ function () { this._destroyed.next(); this._destroyed.complete(); this.destroy(); }; /** * @param {?} value * @return {?} */ MdcSlider.prototype.writeValue = /** * @param {?} value * @return {?} */ function (value) { this.value = value; this._syncValueWithFoundation(); }; /** * @param {?} fn * @return {?} */ MdcSlider.prototype.registerOnChange = /** * @param {?} fn * @return {?} */ function (fn) { this._controlValueAccessorChangeFn = fn; }; /** * @param {?} fn * @return {?} */ MdcSlider.prototype.registerOnTouched = /** * @param {?} fn * @return {?} */ function (fn) { this._onTouched = fn; }; /** * @param {?} disabled * @return {?} */ MdcSlider.prototype.setDisabledState = /** * @param {?} disabled * @return {?} */ function (disabled) { this._disabled = coerceBooleanProperty(disabled); this._foundation.setDisabled(disabled); this._changeDetectorRef.markForCheck(); }; /** * @return {?} */ MdcSlider.prototype.layout = /** * @return {?} */ function () { this._foundation.layout(); }; /** * @private * @return {?} */ MdcSlider.prototype._loadListeners = /** * @private * @return {?} */ function () { var _this = this; this._ngZone.runOutsideAngular((/** * @return {?} */ function () { return fromEvent(window, 'resize') .pipe(auditTime(16), takeUntil(_this._destroyed)) .subscribe((/** * @return {?} */ function () { return _this.layout(); })); })); }; /** * @private * @return {?} */ MdcSlider.prototype._syncValueWithFoundation = /** * @private * @return {?} */ function () { this._foundation.setValue((/** @type {?} */ (this.value))); }; /** * @private * @return {?} */ MdcSlider.prototype._syncStepWithFoundation = /** * @private * @return {?} */ function () { this._foundation.setStep(this.step); }; /** * @private * @return {?} */ MdcSlider.prototype._syncMinWithFoundation = /** * @private * @return {?} */ function () { this._foundation.setMin(this.min); }; /** * @private * @return {?} */ MdcSlider.prototype._syncMaxWithFoundation = /** * @private * @return {?} */ function () { this._foundation.setMax(this.max); }; /** * @private * @param {?} newValue * @return {?} */ MdcSlider.prototype._createChangeEvent = /** * @private * @param {?} newValue * @return {?} */ function (newValue) { return new MdcSliderChange(this, newValue); }; /** * @private * @param {?} newValue * @return {?} */ MdcSlider.prototype._emitChangeEvent = /** * @private * @param {?} newValue * @return {?} */ function (newValue) { this._controlValueAccessorChangeFn(newValue); this.valueChange.emit(newValue); this.change.emit(this._createChangeEvent(newValue)); }; /** Keep calculation in css for better rounding/subpixel behavior. */ /** * Keep calculation in css for better rounding/subpixel behavior. * @private * @param {?} step * @param {?} min * @param {?} max * @return {?} */ MdcSlider.prototype._getTrackMarkersBackground = /** * Keep calculation in css for better rounding/subpixel behavior. * @private * @param {?} step * @param {?} min * @param {?} max * @return {?} */ function (step, min, max) { /** @type {?} */ var stepStr = step.toLocaleString(); /** @type {?} */ var maxStr = max.toLocaleString(); /** @type {?} */ var minStr = min.toLocaleString(); /** @type {?} */ var markerAmount = "((" + maxStr + " - " + minStr + ") / " + stepStr + ")"; /** @type {?} */ var markerWidth = "2px"; /** @type {?} */ var markerBkgdImage = "linear-gradient(to right, currentColor " + markerWidth + ", transparent 0)"; /** @type {?} */ var markerBkgdLayout = "0 center / calc((100% - " + markerWidth + ") / " + markerAmount + ") 100% repeat-x"; return markerBkgdImage + " " + markerBkgdLayout; }; /** Method that ensures that track markers are refreshed. */ /** * Method that ensures that track markers are refreshed. * @private * @return {?} */ MdcSlider.prototype._refreshTrackMarkers = /** * Method that ensures that track markers are refreshed. * @private * @return {?} */ function () { ((/** @type {?} */ (this._foundation))).hasTrackMarker_ = this.markers; this._foundation.setupTrackMarker(); }; MdcSlider.decorators = [ { type: Component, args: [{selector: 'mdc-slider', exportAs: 'mdcSlider', host: { 'role': 'slider', 'aria-orientation': 'horizontal', '[attr.tabindex]': 'tabIndex || 0', 'class': 'mdc-slider', '[class.mdc-slider--discrete]': 'discrete', '[class.mdc-slider--display-markers]': 'markers && discrete', '(blur)': '_onTouched()', }, template: "\n <div class=\"mdc-slider__track-container\">\n <div #track class=\"mdc-slider__track\"></div>\n <div #markercontainer *ngIf=\"markers\" class=\"mdc-slider__track-marker-container\"></div>\n </div>\n <div #thumbcontainer class=\"mdc-slider__thumb-container\">\n <div *ngIf=\"discrete\" class=\"mdc-slider__pin\">\n <span #pin class=\"mdc-slider__pin-value-marker\"></span>\n </div>\n <svg #sliderThumb\n class=\"mdc-slider__thumb\"\n width=\"21\" height=\"21\"\n focusable=\"false\">\n <circle cx=\"10.5\" cy=\"10.5\" r=\"7.875\"></circle>\n </svg>\n <div class=\"mdc-slider__focus-ring\"></div>\n </div>", providers: [MDC_SLIDER_CONTROL_VALUE_ACCESSOR], changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None },] }, ]; /** @nocollapse */ MdcSlider.ctorParameters = function () { return [ { type: Platform }, { type: NgZone }, { type: ChangeDetectorRef }, { type: ElementRef }, { type: String, decorators: [{ type: Attribute, args: ['tabindex',] }] } ]; }; MdcSlider.propDecorators = { tabIndex: [{ type: Input }], discrete: [{ type: Input }], markers: [{ type: Input }], min: [{ type: Input }], max: [{ type: Input }], step: [{ type: Input }], value: [{ type: Input }], disabled: [{ type: Input }], change: [{ type: Output }], input: [{ type: Output }], valueChange: [{ type: Output }], thumbContainer: [{ type: ViewChild, args: ['thumbcontainer', { static: false },] }], _sliderThumb: [{ type: ViewChild, args: ['sliderThumb', { static: false },] }], track: [{ type: ViewChild, args: ['track', { static: false },] }], pinValueMarker: [{ type: ViewChild, args: ['pin', { static: false },] }], trackMarkerContainer: [{ type: ViewChild, args: ['markercontainer', { static: false },] }] }; return MdcSlider; }(MDCComponent)); /** * @fileoverview added by tsickle * Generated from: slider/module.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var MdcSliderModule = /** @class */ (function () { function MdcSliderModule() { } MdcSliderModule.decorators = [ { type: NgModule, args: [{ imports: [CommonModule], exports: [MdcSlider], declarations: [MdcSlider] },] }, ]; return MdcSliderModule; }()); export { MDC_SLIDER_CONTROL_VALUE_ACCESSOR, MdcSlider, MdcSliderChange, MdcSliderModule }; //# sourceMappingURL=slider.es5.js.map