slider.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. import { FocusMonitor } from '@angular/cdk/a11y';
  9. import { Directionality } from '@angular/cdk/bidi';
  10. import { coerceBooleanProperty, coerceNumberProperty } from '@angular/cdk/coercion';
  11. import { DOWN_ARROW, END, HOME, LEFT_ARROW, PAGE_DOWN, PAGE_UP, RIGHT_ARROW, UP_ARROW, hasModifierKey } from '@angular/cdk/keycodes';
  12. import { Attribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, EventEmitter, forwardRef, Inject, Input, Optional, Output, ViewChild, ViewEncapsulation, NgModule } from '@angular/core';
  13. import { NG_VALUE_ACCESSOR } from '@angular/forms';
  14. import { mixinColor, mixinDisabled, mixinTabIndex, GestureConfig, MatCommonModule } from '@angular/material/core';
  15. import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations';
  16. import { Subscription } from 'rxjs';
  17. import { CommonModule } from '@angular/common';
  18. import { HAMMER_GESTURE_CONFIG } from '@angular/platform-browser';
  19. /**
  20. * @fileoverview added by tsickle
  21. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  22. */
  23. /**
  24. * Visually, a 30px separation between tick marks looks best. This is very subjective but it is
  25. * the default separation we chose.
  26. * @type {?}
  27. */
  28. const MIN_AUTO_TICK_SEPARATION = 30;
  29. /**
  30. * The thumb gap size for a disabled slider.
  31. * @type {?}
  32. */
  33. const DISABLED_THUMB_GAP = 7;
  34. /**
  35. * The thumb gap size for a non-active slider at its minimum value.
  36. * @type {?}
  37. */
  38. const MIN_VALUE_NONACTIVE_THUMB_GAP = 7;
  39. /**
  40. * The thumb gap size for an active slider at its minimum value.
  41. * @type {?}
  42. */
  43. const MIN_VALUE_ACTIVE_THUMB_GAP = 10;
  44. /**
  45. * Provider Expression that allows mat-slider to register as a ControlValueAccessor.
  46. * This allows it to support [(ngModel)] and [formControl].
  47. * \@docs-private
  48. * @type {?}
  49. */
  50. const MAT_SLIDER_VALUE_ACCESSOR = {
  51. provide: NG_VALUE_ACCESSOR,
  52. useExisting: forwardRef((/**
  53. * @return {?}
  54. */
  55. () => MatSlider)),
  56. multi: true
  57. };
  58. /**
  59. * A simple change event emitted by the MatSlider component.
  60. */
  61. class MatSliderChange {
  62. }
  63. // Boilerplate for applying mixins to MatSlider.
  64. /**
  65. * \@docs-private
  66. */
  67. class MatSliderBase {
  68. /**
  69. * @param {?} _elementRef
  70. */
  71. constructor(_elementRef) {
  72. this._elementRef = _elementRef;
  73. }
  74. }
  75. /** @type {?} */
  76. const _MatSliderMixinBase = mixinTabIndex(mixinColor(mixinDisabled(MatSliderBase), 'accent'));
  77. /**
  78. * Allows users to select from a range of values by moving the slider thumb. It is similar in
  79. * behavior to the native `<input type="range">` element.
  80. */
  81. class MatSlider extends _MatSliderMixinBase {
  82. /**
  83. * @param {?} elementRef
  84. * @param {?} _focusMonitor
  85. * @param {?} _changeDetectorRef
  86. * @param {?} _dir
  87. * @param {?} tabIndex
  88. * @param {?=} _animationMode
  89. */
  90. constructor(elementRef, _focusMonitor, _changeDetectorRef, _dir, tabIndex, _animationMode) {
  91. super(elementRef);
  92. this._focusMonitor = _focusMonitor;
  93. this._changeDetectorRef = _changeDetectorRef;
  94. this._dir = _dir;
  95. this._animationMode = _animationMode;
  96. this._invert = false;
  97. this._max = 100;
  98. this._min = 0;
  99. this._step = 1;
  100. this._thumbLabel = false;
  101. this._tickInterval = 0;
  102. this._value = null;
  103. this._vertical = false;
  104. /**
  105. * Event emitted when the slider value has changed.
  106. */
  107. this.change = new EventEmitter();
  108. /**
  109. * Event emitted when the slider thumb moves.
  110. */
  111. this.input = new EventEmitter();
  112. /**
  113. * Emits when the raw value of the slider changes. This is here primarily
  114. * to facilitate the two-way binding for the `value` input.
  115. * \@docs-private
  116. */
  117. this.valueChange = new EventEmitter();
  118. /**
  119. * onTouch function registered via registerOnTouch (ControlValueAccessor).
  120. */
  121. this.onTouched = (/**
  122. * @return {?}
  123. */
  124. () => { });
  125. this._percent = 0;
  126. /**
  127. * Whether or not the thumb is sliding.
  128. * Used to determine if there should be a transition for the thumb and fill track.
  129. */
  130. this._isSliding = false;
  131. /**
  132. * Whether or not the slider is active (clicked or sliding).
  133. * Used to shrink and grow the thumb as according to the Material Design spec.
  134. */
  135. this._isActive = false;
  136. /**
  137. * The size of a tick interval as a percentage of the size of the track.
  138. */
  139. this._tickIntervalPercent = 0;
  140. /**
  141. * The dimensions of the slider.
  142. */
  143. this._sliderDimensions = null;
  144. this._controlValueAccessorChangeFn = (/**
  145. * @return {?}
  146. */
  147. () => { });
  148. /**
  149. * Subscription to the Directionality change EventEmitter.
  150. */
  151. this._dirChangeSubscription = Subscription.EMPTY;
  152. this.tabIndex = parseInt(tabIndex) || 0;
  153. }
  154. /**
  155. * Whether the slider is inverted.
  156. * @return {?}
  157. */
  158. get invert() { return this._invert; }
  159. /**
  160. * @param {?} value
  161. * @return {?}
  162. */
  163. set invert(value) {
  164. this._invert = coerceBooleanProperty(value);
  165. }
  166. /**
  167. * The maximum value that the slider can have.
  168. * @return {?}
  169. */
  170. get max() { return this._max; }
  171. /**
  172. * @param {?} v
  173. * @return {?}
  174. */
  175. set max(v) {
  176. this._max = coerceNumberProperty(v, this._max);
  177. this._percent = this._calculatePercentage(this._value);
  178. // Since this also modifies the percentage, we need to let the change detection know.
  179. this._changeDetectorRef.markForCheck();
  180. }
  181. /**
  182. * The minimum value that the slider can have.
  183. * @return {?}
  184. */
  185. get min() { return this._min; }
  186. /**
  187. * @param {?} v
  188. * @return {?}
  189. */
  190. set min(v) {
  191. this._min = coerceNumberProperty(v, this._min);
  192. // If the value wasn't explicitly set by the user, set it to the min.
  193. if (this._value === null) {
  194. this.value = this._min;
  195. }
  196. this._percent = this._calculatePercentage(this._value);
  197. // Since this also modifies the percentage, we need to let the change detection know.
  198. this._changeDetectorRef.markForCheck();
  199. }
  200. /**
  201. * The values at which the thumb will snap.
  202. * @return {?}
  203. */
  204. get step() { return this._step; }
  205. /**
  206. * @param {?} v
  207. * @return {?}
  208. */
  209. set step(v) {
  210. this._step = coerceNumberProperty(v, this._step);
  211. if (this._step % 1 !== 0) {
  212. this._roundToDecimal = (/** @type {?} */ (this._step.toString().split('.').pop())).length;
  213. }
  214. // Since this could modify the label, we need to notify the change detection.
  215. this._changeDetectorRef.markForCheck();
  216. }
  217. /**
  218. * Whether or not to show the thumb label.
  219. * @return {?}
  220. */
  221. get thumbLabel() { return this._thumbLabel; }
  222. /**
  223. * @param {?} value
  224. * @return {?}
  225. */
  226. set thumbLabel(value) { this._thumbLabel = coerceBooleanProperty(value); }
  227. /**
  228. * How often to show ticks. Relative to the step so that a tick always appears on a step.
  229. * Ex: Tick interval of 4 with a step of 3 will draw a tick every 4 steps (every 12 values).
  230. * @return {?}
  231. */
  232. get tickInterval() { return this._tickInterval; }
  233. /**
  234. * @param {?} value
  235. * @return {?}
  236. */
  237. set tickInterval(value) {
  238. if (value === 'auto') {
  239. this._tickInterval = 'auto';
  240. }
  241. else if (typeof value === 'number' || typeof value === 'string') {
  242. this._tickInterval = coerceNumberProperty(value, (/** @type {?} */ (this._tickInterval)));
  243. }
  244. else {
  245. this._tickInterval = 0;
  246. }
  247. }
  248. /**
  249. * Value of the slider.
  250. * @return {?}
  251. */
  252. get value() {
  253. // If the value needs to be read and it is still uninitialized, initialize it to the min.
  254. if (this._value === null) {
  255. this.value = this._min;
  256. }
  257. return this._value;
  258. }
  259. /**
  260. * @param {?} v
  261. * @return {?}
  262. */
  263. set value(v) {
  264. if (v !== this._value) {
  265. /** @type {?} */
  266. let value = coerceNumberProperty(v);
  267. // While incrementing by a decimal we can end up with values like 33.300000000000004.
  268. // Truncate it to ensure that it matches the label and to make it easier to work with.
  269. if (this._roundToDecimal) {
  270. value = parseFloat(value.toFixed(this._roundToDecimal));
  271. }
  272. this._value = value;
  273. this._percent = this._calculatePercentage(this._value);
  274. // Since this also modifies the percentage, we need to let the change detection know.
  275. this._changeDetectorRef.markForCheck();
  276. }
  277. }
  278. /**
  279. * Whether the slider is vertical.
  280. * @return {?}
  281. */
  282. get vertical() { return this._vertical; }
  283. /**
  284. * @param {?} value
  285. * @return {?}
  286. */
  287. set vertical(value) {
  288. this._vertical = coerceBooleanProperty(value);
  289. }
  290. /**
  291. * The value to be used for display purposes.
  292. * @return {?}
  293. */
  294. get displayValue() {
  295. if (this.displayWith) {
  296. return this.displayWith(this.value);
  297. }
  298. // Note that this could be improved further by rounding something like 0.999 to 1 or
  299. // 0.899 to 0.9, however it is very performance sensitive, because it gets called on
  300. // every change detection cycle.
  301. if (this._roundToDecimal && this.value && this.value % 1 !== 0) {
  302. return this.value.toFixed(this._roundToDecimal);
  303. }
  304. return this.value || 0;
  305. }
  306. /**
  307. * set focus to the host element
  308. * @param {?=} options
  309. * @return {?}
  310. */
  311. focus(options) {
  312. this._focusHostElement(options);
  313. }
  314. /**
  315. * blur the host element
  316. * @return {?}
  317. */
  318. blur() {
  319. this._blurHostElement();
  320. }
  321. /**
  322. * The percentage of the slider that coincides with the value.
  323. * @return {?}
  324. */
  325. get percent() { return this._clamp(this._percent); }
  326. /**
  327. * Whether the axis of the slider is inverted.
  328. * (i.e. whether moving the thumb in the positive x or y direction decreases the slider's value).
  329. * @return {?}
  330. */
  331. get _invertAxis() {
  332. // Standard non-inverted mode for a vertical slider should be dragging the thumb from bottom to
  333. // top. However from a y-axis standpoint this is inverted.
  334. return this.vertical ? !this.invert : this.invert;
  335. }
  336. /**
  337. * Whether the slider is at its minimum value.
  338. * @return {?}
  339. */
  340. get _isMinValue() {
  341. return this.percent === 0;
  342. }
  343. /**
  344. * The amount of space to leave between the slider thumb and the track fill & track background
  345. * elements.
  346. * @return {?}
  347. */
  348. get _thumbGap() {
  349. if (this.disabled) {
  350. return DISABLED_THUMB_GAP;
  351. }
  352. if (this._isMinValue && !this.thumbLabel) {
  353. return this._isActive ? MIN_VALUE_ACTIVE_THUMB_GAP : MIN_VALUE_NONACTIVE_THUMB_GAP;
  354. }
  355. return 0;
  356. }
  357. /**
  358. * CSS styles for the track background element.
  359. * @return {?}
  360. */
  361. get _trackBackgroundStyles() {
  362. /** @type {?} */
  363. const axis = this.vertical ? 'Y' : 'X';
  364. /** @type {?} */
  365. const scale = this.vertical ? `1, ${1 - this.percent}, 1` : `${1 - this.percent}, 1, 1`;
  366. /** @type {?} */
  367. const sign = this._shouldInvertMouseCoords() ? '-' : '';
  368. return {
  369. // scale3d avoids some rendering issues in Chrome. See #12071.
  370. transform: `translate${axis}(${sign}${this._thumbGap}px) scale3d(${scale})`
  371. };
  372. }
  373. /**
  374. * CSS styles for the track fill element.
  375. * @return {?}
  376. */
  377. get _trackFillStyles() {
  378. /** @type {?} */
  379. const axis = this.vertical ? 'Y' : 'X';
  380. /** @type {?} */
  381. const scale = this.vertical ? `1, ${this.percent}, 1` : `${this.percent}, 1, 1`;
  382. /** @type {?} */
  383. const sign = this._shouldInvertMouseCoords() ? '' : '-';
  384. return {
  385. // scale3d avoids some rendering issues in Chrome. See #12071.
  386. transform: `translate${axis}(${sign}${this._thumbGap}px) scale3d(${scale})`
  387. };
  388. }
  389. /**
  390. * CSS styles for the ticks container element.
  391. * @return {?}
  392. */
  393. get _ticksContainerStyles() {
  394. /** @type {?} */
  395. let axis = this.vertical ? 'Y' : 'X';
  396. // For a horizontal slider in RTL languages we push the ticks container off the left edge
  397. // instead of the right edge to avoid causing a horizontal scrollbar to appear.
  398. /** @type {?} */
  399. let sign = !this.vertical && this._getDirection() == 'rtl' ? '' : '-';
  400. /** @type {?} */
  401. let offset = this._tickIntervalPercent / 2 * 100;
  402. return {
  403. 'transform': `translate${axis}(${sign}${offset}%)`
  404. };
  405. }
  406. /**
  407. * CSS styles for the ticks element.
  408. * @return {?}
  409. */
  410. get _ticksStyles() {
  411. /** @type {?} */
  412. let tickSize = this._tickIntervalPercent * 100;
  413. /** @type {?} */
  414. let backgroundSize = this.vertical ? `2px ${tickSize}%` : `${tickSize}% 2px`;
  415. /** @type {?} */
  416. let axis = this.vertical ? 'Y' : 'X';
  417. // Depending on the direction we pushed the ticks container, push the ticks the opposite
  418. // direction to re-center them but clip off the end edge. In RTL languages we need to flip the
  419. // ticks 180 degrees so we're really cutting off the end edge abd not the start.
  420. /** @type {?} */
  421. let sign = !this.vertical && this._getDirection() == 'rtl' ? '-' : '';
  422. /** @type {?} */
  423. let rotate = !this.vertical && this._getDirection() == 'rtl' ? ' rotate(180deg)' : '';
  424. /** @type {?} */
  425. let styles = {
  426. 'backgroundSize': backgroundSize,
  427. // Without translateZ ticks sometimes jitter as the slider moves on Chrome & Firefox.
  428. 'transform': `translateZ(0) translate${axis}(${sign}${tickSize / 2}%)${rotate}`
  429. };
  430. if (this._isMinValue && this._thumbGap) {
  431. /** @type {?} */
  432. let side = this.vertical ?
  433. (this._invertAxis ? 'Bottom' : 'Top') :
  434. (this._invertAxis ? 'Right' : 'Left');
  435. styles[`padding${side}`] = `${this._thumbGap}px`;
  436. }
  437. return styles;
  438. }
  439. /**
  440. * @return {?}
  441. */
  442. get _thumbContainerStyles() {
  443. /** @type {?} */
  444. let axis = this.vertical ? 'Y' : 'X';
  445. // For a horizontal slider in RTL languages we push the thumb container off the left edge
  446. // instead of the right edge to avoid causing a horizontal scrollbar to appear.
  447. /** @type {?} */
  448. let invertOffset = (this._getDirection() == 'rtl' && !this.vertical) ? !this._invertAxis : this._invertAxis;
  449. /** @type {?} */
  450. let offset = (invertOffset ? this.percent : 1 - this.percent) * 100;
  451. return {
  452. 'transform': `translate${axis}(-${offset}%)`
  453. };
  454. }
  455. /**
  456. * Whether mouse events should be converted to a slider position by calculating their distance
  457. * from the right or bottom edge of the slider as opposed to the top or left.
  458. * @private
  459. * @return {?}
  460. */
  461. _shouldInvertMouseCoords() {
  462. return (this._getDirection() == 'rtl' && !this.vertical) ? !this._invertAxis : this._invertAxis;
  463. }
  464. /**
  465. * The language direction for this slider element.
  466. * @private
  467. * @return {?}
  468. */
  469. _getDirection() {
  470. return (this._dir && this._dir.value == 'rtl') ? 'rtl' : 'ltr';
  471. }
  472. /**
  473. * @return {?}
  474. */
  475. ngOnInit() {
  476. this._focusMonitor
  477. .monitor(this._elementRef, true)
  478. .subscribe((/**
  479. * @param {?} origin
  480. * @return {?}
  481. */
  482. (origin) => {
  483. this._isActive = !!origin && origin !== 'keyboard';
  484. this._changeDetectorRef.detectChanges();
  485. }));
  486. if (this._dir) {
  487. this._dirChangeSubscription = this._dir.change.subscribe((/**
  488. * @return {?}
  489. */
  490. () => {
  491. this._changeDetectorRef.markForCheck();
  492. }));
  493. }
  494. }
  495. /**
  496. * @return {?}
  497. */
  498. ngOnDestroy() {
  499. this._focusMonitor.stopMonitoring(this._elementRef);
  500. this._dirChangeSubscription.unsubscribe();
  501. }
  502. /**
  503. * @return {?}
  504. */
  505. _onMouseenter() {
  506. if (this.disabled) {
  507. return;
  508. }
  509. // We save the dimensions of the slider here so we can use them to update the spacing of the
  510. // ticks and determine where on the slider click and slide events happen.
  511. this._sliderDimensions = this._getSliderDimensions();
  512. this._updateTickIntervalPercent();
  513. }
  514. /**
  515. * @param {?} event
  516. * @return {?}
  517. */
  518. _onMousedown(event) {
  519. // Don't do anything if the slider is disabled or the
  520. // user is using anything other than the main mouse button.
  521. if (this.disabled || event.button !== 0) {
  522. return;
  523. }
  524. /** @type {?} */
  525. const oldValue = this.value;
  526. this._isSliding = false;
  527. this._focusHostElement();
  528. this._updateValueFromPosition({ x: event.clientX, y: event.clientY });
  529. // Emit a change and input event if the value changed.
  530. if (oldValue != this.value) {
  531. this._emitInputEvent();
  532. this._emitChangeEvent();
  533. }
  534. }
  535. /**
  536. * @param {?} event
  537. * @return {?}
  538. */
  539. _onSlide(event) {
  540. if (this.disabled) {
  541. return;
  542. }
  543. // The slide start event sometimes fails to fire on iOS, so if we're not already in the sliding
  544. // state, call the slide start handler manually.
  545. if (!this._isSliding) {
  546. this._onSlideStart(null);
  547. }
  548. // Prevent the slide from selecting anything else.
  549. event.preventDefault();
  550. /** @type {?} */
  551. let oldValue = this.value;
  552. this._updateValueFromPosition({ x: event.center.x, y: event.center.y });
  553. // Native range elements always emit `input` events when the value changed while sliding.
  554. if (oldValue != this.value) {
  555. this._emitInputEvent();
  556. }
  557. }
  558. /**
  559. * @param {?} event
  560. * @return {?}
  561. */
  562. _onSlideStart(event) {
  563. if (this.disabled || this._isSliding) {
  564. return;
  565. }
  566. // Simulate mouseenter in case this is a mobile device.
  567. this._onMouseenter();
  568. this._isSliding = true;
  569. this._focusHostElement();
  570. this._valueOnSlideStart = this.value;
  571. if (event) {
  572. this._updateValueFromPosition({ x: event.center.x, y: event.center.y });
  573. event.preventDefault();
  574. }
  575. }
  576. /**
  577. * @return {?}
  578. */
  579. _onSlideEnd() {
  580. this._isSliding = false;
  581. if (this._valueOnSlideStart != this.value && !this.disabled) {
  582. this._emitChangeEvent();
  583. }
  584. this._valueOnSlideStart = null;
  585. }
  586. /**
  587. * @return {?}
  588. */
  589. _onFocus() {
  590. // We save the dimensions of the slider here so we can use them to update the spacing of the
  591. // ticks and determine where on the slider click and slide events happen.
  592. this._sliderDimensions = this._getSliderDimensions();
  593. this._updateTickIntervalPercent();
  594. }
  595. /**
  596. * @return {?}
  597. */
  598. _onBlur() {
  599. this.onTouched();
  600. }
  601. /**
  602. * @param {?} event
  603. * @return {?}
  604. */
  605. _onKeydown(event) {
  606. if (this.disabled || hasModifierKey(event)) {
  607. return;
  608. }
  609. /** @type {?} */
  610. const oldValue = this.value;
  611. switch (event.keyCode) {
  612. case PAGE_UP:
  613. this._increment(10);
  614. break;
  615. case PAGE_DOWN:
  616. this._increment(-10);
  617. break;
  618. case END:
  619. this.value = this.max;
  620. break;
  621. case HOME:
  622. this.value = this.min;
  623. break;
  624. case LEFT_ARROW:
  625. // NOTE: For a sighted user it would make more sense that when they press an arrow key on an
  626. // inverted slider the thumb moves in that direction. However for a blind user, nothing
  627. // about the slider indicates that it is inverted. They will expect left to be decrement,
  628. // regardless of how it appears on the screen. For speakers ofRTL languages, they probably
  629. // expect left to mean increment. Therefore we flip the meaning of the side arrow keys for
  630. // RTL. For inverted sliders we prefer a good a11y experience to having it "look right" for
  631. // sighted users, therefore we do not swap the meaning.
  632. this._increment(this._getDirection() == 'rtl' ? 1 : -1);
  633. break;
  634. case UP_ARROW:
  635. this._increment(1);
  636. break;
  637. case RIGHT_ARROW:
  638. // See comment on LEFT_ARROW about the conditions under which we flip the meaning.
  639. this._increment(this._getDirection() == 'rtl' ? -1 : 1);
  640. break;
  641. case DOWN_ARROW:
  642. this._increment(-1);
  643. break;
  644. default:
  645. // Return if the key is not one that we explicitly handle to avoid calling preventDefault on
  646. // it.
  647. return;
  648. }
  649. if (oldValue != this.value) {
  650. this._emitInputEvent();
  651. this._emitChangeEvent();
  652. }
  653. this._isSliding = true;
  654. event.preventDefault();
  655. }
  656. /**
  657. * @return {?}
  658. */
  659. _onKeyup() {
  660. this._isSliding = false;
  661. }
  662. /**
  663. * Increments the slider by the given number of steps (negative number decrements).
  664. * @private
  665. * @param {?} numSteps
  666. * @return {?}
  667. */
  668. _increment(numSteps) {
  669. this.value = this._clamp((this.value || 0) + this.step * numSteps, this.min, this.max);
  670. }
  671. /**
  672. * Calculate the new value from the new physical location. The value will always be snapped.
  673. * @private
  674. * @param {?} pos
  675. * @return {?}
  676. */
  677. _updateValueFromPosition(pos) {
  678. if (!this._sliderDimensions) {
  679. return;
  680. }
  681. /** @type {?} */
  682. let offset = this.vertical ? this._sliderDimensions.top : this._sliderDimensions.left;
  683. /** @type {?} */
  684. let size = this.vertical ? this._sliderDimensions.height : this._sliderDimensions.width;
  685. /** @type {?} */
  686. let posComponent = this.vertical ? pos.y : pos.x;
  687. // The exact value is calculated from the event and used to find the closest snap value.
  688. /** @type {?} */
  689. let percent = this._clamp((posComponent - offset) / size);
  690. if (this._shouldInvertMouseCoords()) {
  691. percent = 1 - percent;
  692. }
  693. // Since the steps may not divide cleanly into the max value, if the user
  694. // slid to 0 or 100 percent, we jump to the min/max value. This approach
  695. // is slightly more intuitive than using `Math.ceil` below, because it
  696. // follows the user's pointer closer.
  697. if (percent === 0) {
  698. this.value = this.min;
  699. }
  700. else if (percent === 1) {
  701. this.value = this.max;
  702. }
  703. else {
  704. /** @type {?} */
  705. const exactValue = this._calculateValue(percent);
  706. // This calculation finds the closest step by finding the closest
  707. // whole number divisible by the step relative to the min.
  708. /** @type {?} */
  709. const closestValue = Math.round((exactValue - this.min) / this.step) * this.step + this.min;
  710. // The value needs to snap to the min and max.
  711. this.value = this._clamp(closestValue, this.min, this.max);
  712. }
  713. }
  714. /**
  715. * Emits a change event if the current value is different from the last emitted value.
  716. * @private
  717. * @return {?}
  718. */
  719. _emitChangeEvent() {
  720. this._controlValueAccessorChangeFn(this.value);
  721. this.valueChange.emit(this.value);
  722. this.change.emit(this._createChangeEvent());
  723. }
  724. /**
  725. * Emits an input event when the current value is different from the last emitted value.
  726. * @private
  727. * @return {?}
  728. */
  729. _emitInputEvent() {
  730. this.input.emit(this._createChangeEvent());
  731. }
  732. /**
  733. * Updates the amount of space between ticks as a percentage of the width of the slider.
  734. * @private
  735. * @return {?}
  736. */
  737. _updateTickIntervalPercent() {
  738. if (!this.tickInterval || !this._sliderDimensions) {
  739. return;
  740. }
  741. if (this.tickInterval == 'auto') {
  742. /** @type {?} */
  743. let trackSize = this.vertical ? this._sliderDimensions.height : this._sliderDimensions.width;
  744. /** @type {?} */
  745. let pixelsPerStep = trackSize * this.step / (this.max - this.min);
  746. /** @type {?} */
  747. let stepsPerTick = Math.ceil(MIN_AUTO_TICK_SEPARATION / pixelsPerStep);
  748. /** @type {?} */
  749. let pixelsPerTick = stepsPerTick * this.step;
  750. this._tickIntervalPercent = pixelsPerTick / trackSize;
  751. }
  752. else {
  753. this._tickIntervalPercent = this.tickInterval * this.step / (this.max - this.min);
  754. }
  755. }
  756. /**
  757. * Creates a slider change object from the specified value.
  758. * @private
  759. * @param {?=} value
  760. * @return {?}
  761. */
  762. _createChangeEvent(value = this.value) {
  763. /** @type {?} */
  764. let event = new MatSliderChange();
  765. event.source = this;
  766. event.value = value;
  767. return event;
  768. }
  769. /**
  770. * Calculates the percentage of the slider that a value is.
  771. * @private
  772. * @param {?} value
  773. * @return {?}
  774. */
  775. _calculatePercentage(value) {
  776. return ((value || 0) - this.min) / (this.max - this.min);
  777. }
  778. /**
  779. * Calculates the value a percentage of the slider corresponds to.
  780. * @private
  781. * @param {?} percentage
  782. * @return {?}
  783. */
  784. _calculateValue(percentage) {
  785. return this.min + percentage * (this.max - this.min);
  786. }
  787. /**
  788. * Return a number between two numbers.
  789. * @private
  790. * @param {?} value
  791. * @param {?=} min
  792. * @param {?=} max
  793. * @return {?}
  794. */
  795. _clamp(value, min = 0, max = 1) {
  796. return Math.max(min, Math.min(value, max));
  797. }
  798. /**
  799. * Get the bounding client rect of the slider track element.
  800. * The track is used rather than the native element to ignore the extra space that the thumb can
  801. * take up.
  802. * @private
  803. * @return {?}
  804. */
  805. _getSliderDimensions() {
  806. return this._sliderWrapper ? this._sliderWrapper.nativeElement.getBoundingClientRect() : null;
  807. }
  808. /**
  809. * Focuses the native element.
  810. * Currently only used to allow a blur event to fire but will be used with keyboard input later.
  811. * @private
  812. * @param {?=} options
  813. * @return {?}
  814. */
  815. _focusHostElement(options) {
  816. this._elementRef.nativeElement.focus(options);
  817. }
  818. /**
  819. * Blurs the native element.
  820. * @private
  821. * @return {?}
  822. */
  823. _blurHostElement() {
  824. this._elementRef.nativeElement.blur();
  825. }
  826. /**
  827. * Sets the model value. Implemented as part of ControlValueAccessor.
  828. * @param {?} value
  829. * @return {?}
  830. */
  831. writeValue(value) {
  832. this.value = value;
  833. }
  834. /**
  835. * Registers a callback to be triggered when the value has changed.
  836. * Implemented as part of ControlValueAccessor.
  837. * @param {?} fn Callback to be registered.
  838. * @return {?}
  839. */
  840. registerOnChange(fn) {
  841. this._controlValueAccessorChangeFn = fn;
  842. }
  843. /**
  844. * Registers a callback to be triggered when the component is touched.
  845. * Implemented as part of ControlValueAccessor.
  846. * @param {?} fn Callback to be registered.
  847. * @return {?}
  848. */
  849. registerOnTouched(fn) {
  850. this.onTouched = fn;
  851. }
  852. /**
  853. * Sets whether the component should be disabled.
  854. * Implemented as part of ControlValueAccessor.
  855. * @param {?} isDisabled
  856. * @return {?}
  857. */
  858. setDisabledState(isDisabled) {
  859. this.disabled = isDisabled;
  860. }
  861. }
  862. MatSlider.decorators = [
  863. { type: Component, args: [{selector: 'mat-slider',
  864. exportAs: 'matSlider',
  865. providers: [MAT_SLIDER_VALUE_ACCESSOR],
  866. host: {
  867. '(focus)': '_onFocus()',
  868. '(blur)': '_onBlur()',
  869. '(mousedown)': '_onMousedown($event)',
  870. '(keydown)': '_onKeydown($event)',
  871. '(keyup)': '_onKeyup()',
  872. '(mouseenter)': '_onMouseenter()',
  873. '(slide)': '_onSlide($event)',
  874. '(slideend)': '_onSlideEnd()',
  875. '(slidestart)': '_onSlideStart($event)',
  876. // On Safari starting to slide temporarily triggers text selection mode which
  877. // show the wrong cursor. We prevent it by stopping the `selectstart` event.
  878. '(selectstart)': '$event.preventDefault()',
  879. 'class': 'mat-slider',
  880. 'role': 'slider',
  881. '[tabIndex]': 'tabIndex',
  882. '[attr.aria-disabled]': 'disabled',
  883. '[attr.aria-valuemax]': 'max',
  884. '[attr.aria-valuemin]': 'min',
  885. '[attr.aria-valuenow]': 'value',
  886. '[attr.aria-orientation]': 'vertical ? "vertical" : "horizontal"',
  887. '[class.mat-slider-disabled]': 'disabled',
  888. '[class.mat-slider-has-ticks]': 'tickInterval',
  889. '[class.mat-slider-horizontal]': '!vertical',
  890. '[class.mat-slider-axis-inverted]': '_invertAxis',
  891. '[class.mat-slider-sliding]': '_isSliding',
  892. '[class.mat-slider-thumb-label-showing]': 'thumbLabel',
  893. '[class.mat-slider-vertical]': 'vertical',
  894. '[class.mat-slider-min-value]': '_isMinValue',
  895. '[class.mat-slider-hide-last-tick]': 'disabled || _isMinValue && _thumbGap && _invertAxis',
  896. '[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
  897. },
  898. template: "<div class=\"mat-slider-wrapper\" #sliderWrapper><div class=\"mat-slider-track-wrapper\"><div class=\"mat-slider-track-background\" [ngStyle]=\"_trackBackgroundStyles\"></div><div class=\"mat-slider-track-fill\" [ngStyle]=\"_trackFillStyles\"></div></div><div class=\"mat-slider-ticks-container\" [ngStyle]=\"_ticksContainerStyles\"><div class=\"mat-slider-ticks\" [ngStyle]=\"_ticksStyles\"></div></div><div class=\"mat-slider-thumb-container\" [ngStyle]=\"_thumbContainerStyles\"><div class=\"mat-slider-focus-ring\"></div><div class=\"mat-slider-thumb\"></div><div class=\"mat-slider-thumb-label\"><span class=\"mat-slider-thumb-label-text\">{{displayValue}}</span></div></div></div>",
  899. styles: [".mat-slider{display:inline-block;position:relative;box-sizing:border-box;padding:8px;outline:0;vertical-align:middle}.mat-slider.mat-slider-sliding:not(.mat-slider-disabled),.mat-slider:not(.mat-slider-disabled):active{cursor:-webkit-grabbing;cursor:grabbing}.mat-slider-wrapper{position:absolute}.mat-slider-track-wrapper{position:absolute;top:0;left:0;overflow:hidden}.mat-slider-track-fill{position:absolute;transform-origin:0 0;transition:transform .4s cubic-bezier(.25,.8,.25,1),background-color .4s cubic-bezier(.25,.8,.25,1)}.mat-slider-track-background{position:absolute;transform-origin:100% 100%;transition:transform .4s cubic-bezier(.25,.8,.25,1),background-color .4s cubic-bezier(.25,.8,.25,1)}.mat-slider-ticks-container{position:absolute;left:0;top:0;overflow:hidden}.mat-slider-ticks{background-repeat:repeat;background-clip:content-box;box-sizing:border-box;opacity:0;transition:opacity .4s cubic-bezier(.25,.8,.25,1)}.mat-slider-thumb-container{position:absolute;z-index:1;transition:transform .4s cubic-bezier(.25,.8,.25,1)}.mat-slider-focus-ring{position:absolute;width:30px;height:30px;border-radius:50%;transform:scale(0);opacity:0;transition:transform .4s cubic-bezier(.25,.8,.25,1),background-color .4s cubic-bezier(.25,.8,.25,1),opacity .4s cubic-bezier(.25,.8,.25,1)}.mat-slider.cdk-keyboard-focused .mat-slider-focus-ring,.mat-slider.cdk-program-focused .mat-slider-focus-ring{transform:scale(1);opacity:1}.mat-slider:not(.mat-slider-disabled):not(.mat-slider-sliding) .mat-slider-thumb,.mat-slider:not(.mat-slider-disabled):not(.mat-slider-sliding) .mat-slider-thumb-label{cursor:-webkit-grab;cursor:grab}.mat-slider-thumb{position:absolute;right:-10px;bottom:-10px;box-sizing:border-box;width:20px;height:20px;border:3px solid transparent;border-radius:50%;transform:scale(.7);transition:transform .4s cubic-bezier(.25,.8,.25,1),background-color .4s cubic-bezier(.25,.8,.25,1),border-color .4s cubic-bezier(.25,.8,.25,1)}.mat-slider-thumb-label{display:none;align-items:center;justify-content:center;position:absolute;width:28px;height:28px;border-radius:50%;transition:transform .4s cubic-bezier(.25,.8,.25,1),border-radius .4s cubic-bezier(.25,.8,.25,1),background-color .4s cubic-bezier(.25,.8,.25,1)}@media (-ms-high-contrast:active){.mat-slider-thumb-label{outline:solid 1px}}.mat-slider-thumb-label-text{z-index:1;opacity:0;transition:opacity .4s cubic-bezier(.25,.8,.25,1)}.mat-slider-sliding .mat-slider-thumb-container,.mat-slider-sliding .mat-slider-track-background,.mat-slider-sliding .mat-slider-track-fill{transition-duration:0s}.mat-slider-has-ticks .mat-slider-wrapper::after{content:'';position:absolute;border-width:0;border-style:solid;opacity:0;transition:opacity .4s cubic-bezier(.25,.8,.25,1)}.mat-slider-has-ticks.cdk-focused:not(.mat-slider-hide-last-tick) .mat-slider-wrapper::after,.mat-slider-has-ticks:hover:not(.mat-slider-hide-last-tick) .mat-slider-wrapper::after{opacity:1}.mat-slider-has-ticks.cdk-focused:not(.mat-slider-disabled) .mat-slider-ticks,.mat-slider-has-ticks:hover:not(.mat-slider-disabled) .mat-slider-ticks{opacity:1}.mat-slider-thumb-label-showing .mat-slider-focus-ring{display:none}.mat-slider-thumb-label-showing .mat-slider-thumb-label{display:flex}.mat-slider-axis-inverted .mat-slider-track-fill{transform-origin:100% 100%}.mat-slider-axis-inverted .mat-slider-track-background{transform-origin:0 0}.mat-slider:not(.mat-slider-disabled).cdk-focused.mat-slider-thumb-label-showing .mat-slider-thumb{transform:scale(0)}.mat-slider:not(.mat-slider-disabled).cdk-focused .mat-slider-thumb-label{border-radius:50% 50% 0}.mat-slider:not(.mat-slider-disabled).cdk-focused .mat-slider-thumb-label-text{opacity:1}.mat-slider:not(.mat-slider-disabled).cdk-mouse-focused .mat-slider-thumb,.mat-slider:not(.mat-slider-disabled).cdk-program-focused .mat-slider-thumb,.mat-slider:not(.mat-slider-disabled).cdk-touch-focused .mat-slider-thumb{border-width:2px;transform:scale(1)}.mat-slider-disabled .mat-slider-focus-ring{transform:scale(0);opacity:0}.mat-slider-disabled .mat-slider-thumb{border-width:4px;transform:scale(.5)}.mat-slider-disabled .mat-slider-thumb-label{display:none}.mat-slider-horizontal{height:48px;min-width:128px}.mat-slider-horizontal .mat-slider-wrapper{height:2px;top:23px;left:8px;right:8px}.mat-slider-horizontal .mat-slider-wrapper::after{height:2px;border-left-width:2px;right:0;top:0}.mat-slider-horizontal .mat-slider-track-wrapper{height:2px;width:100%}.mat-slider-horizontal .mat-slider-track-fill{height:2px;width:100%;transform:scaleX(0)}.mat-slider-horizontal .mat-slider-track-background{height:2px;width:100%;transform:scaleX(1)}.mat-slider-horizontal .mat-slider-ticks-container{height:2px;width:100%}@media (-ms-high-contrast:active){.mat-slider-horizontal .mat-slider-ticks-container{height:0;outline:solid 2px;top:1px}}.mat-slider-horizontal .mat-slider-ticks{height:2px;width:100%}.mat-slider-horizontal .mat-slider-thumb-container{width:100%;height:0;top:50%}.mat-slider-horizontal .mat-slider-focus-ring{top:-15px;right:-15px}.mat-slider-horizontal .mat-slider-thumb-label{right:-14px;top:-40px;transform:translateY(26px) scale(.01) rotate(45deg)}.mat-slider-horizontal .mat-slider-thumb-label-text{transform:rotate(-45deg)}.mat-slider-horizontal.cdk-focused .mat-slider-thumb-label{transform:rotate(45deg)}@media (-ms-high-contrast:active){.mat-slider-horizontal.cdk-focused .mat-slider-thumb-label,.mat-slider-horizontal.cdk-focused .mat-slider-thumb-label-text{transform:none}}.mat-slider-vertical{width:48px;min-height:128px}.mat-slider-vertical .mat-slider-wrapper{width:2px;top:8px;bottom:8px;left:23px}.mat-slider-vertical .mat-slider-wrapper::after{width:2px;border-top-width:2px;bottom:0;left:0}.mat-slider-vertical .mat-slider-track-wrapper{height:100%;width:2px}.mat-slider-vertical .mat-slider-track-fill{height:100%;width:2px;transform:scaleY(0)}.mat-slider-vertical .mat-slider-track-background{height:100%;width:2px;transform:scaleY(1)}.mat-slider-vertical .mat-slider-ticks-container{width:2px;height:100%}@media (-ms-high-contrast:active){.mat-slider-vertical .mat-slider-ticks-container{width:0;outline:solid 2px;left:1px}}.mat-slider-vertical .mat-slider-focus-ring{bottom:-15px;left:-15px}.mat-slider-vertical .mat-slider-ticks{width:2px;height:100%}.mat-slider-vertical .mat-slider-thumb-container{height:100%;width:0;left:50%}.mat-slider-vertical .mat-slider-thumb{-webkit-backface-visibility:hidden;backface-visibility:hidden}.mat-slider-vertical .mat-slider-thumb-label{bottom:-14px;left:-40px;transform:translateX(26px) scale(.01) rotate(-45deg)}.mat-slider-vertical .mat-slider-thumb-label-text{transform:rotate(45deg)}.mat-slider-vertical.cdk-focused .mat-slider-thumb-label{transform:rotate(-45deg)}[dir=rtl] .mat-slider-wrapper::after{left:0;right:auto}[dir=rtl] .mat-slider-horizontal .mat-slider-track-fill{transform-origin:100% 100%}[dir=rtl] .mat-slider-horizontal .mat-slider-track-background{transform-origin:0 0}[dir=rtl] .mat-slider-horizontal.mat-slider-axis-inverted .mat-slider-track-fill{transform-origin:0 0}[dir=rtl] .mat-slider-horizontal.mat-slider-axis-inverted .mat-slider-track-background{transform-origin:100% 100%}.mat-slider._mat-animation-noopable .mat-slider-focus-ring,.mat-slider._mat-animation-noopable .mat-slider-has-ticks .mat-slider-wrapper::after,.mat-slider._mat-animation-noopable .mat-slider-thumb,.mat-slider._mat-animation-noopable .mat-slider-thumb-container,.mat-slider._mat-animation-noopable .mat-slider-thumb-label,.mat-slider._mat-animation-noopable .mat-slider-thumb-label-text,.mat-slider._mat-animation-noopable .mat-slider-ticks,.mat-slider._mat-animation-noopable .mat-slider-track-background,.mat-slider._mat-animation-noopable .mat-slider-track-fill{transition:none}"],
  900. inputs: ['disabled', 'color', 'tabIndex'],
  901. encapsulation: ViewEncapsulation.None,
  902. changeDetection: ChangeDetectionStrategy.OnPush,
  903. },] },
  904. ];
  905. /** @nocollapse */
  906. MatSlider.ctorParameters = () => [
  907. { type: ElementRef },
  908. { type: FocusMonitor },
  909. { type: ChangeDetectorRef },
  910. { type: Directionality, decorators: [{ type: Optional }] },
  911. { type: String, decorators: [{ type: Attribute, args: ['tabindex',] }] },
  912. { type: String, decorators: [{ type: Optional }, { type: Inject, args: [ANIMATION_MODULE_TYPE,] }] }
  913. ];
  914. MatSlider.propDecorators = {
  915. invert: [{ type: Input }],
  916. max: [{ type: Input }],
  917. min: [{ type: Input }],
  918. step: [{ type: Input }],
  919. thumbLabel: [{ type: Input }],
  920. tickInterval: [{ type: Input }],
  921. value: [{ type: Input }],
  922. displayWith: [{ type: Input }],
  923. vertical: [{ type: Input }],
  924. change: [{ type: Output }],
  925. input: [{ type: Output }],
  926. valueChange: [{ type: Output }],
  927. _sliderWrapper: [{ type: ViewChild, args: ['sliderWrapper', { static: false },] }]
  928. };
  929. /**
  930. * @fileoverview added by tsickle
  931. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  932. */
  933. class MatSliderModule {
  934. }
  935. MatSliderModule.decorators = [
  936. { type: NgModule, args: [{
  937. imports: [CommonModule, MatCommonModule],
  938. exports: [MatSlider, MatCommonModule],
  939. declarations: [MatSlider],
  940. providers: [{ provide: HAMMER_GESTURE_CONFIG, useClass: GestureConfig }]
  941. },] },
  942. ];
  943. /**
  944. * @fileoverview added by tsickle
  945. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  946. */
  947. /**
  948. * @fileoverview added by tsickle
  949. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  950. */
  951. export { MatSliderModule, MAT_SLIDER_VALUE_ACCESSOR, MatSliderChange, MatSlider };
  952. //# sourceMappingURL=slider.js.map