mc-range.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { ChangeDetectorRef, Component, ElementRef, Renderer, Optional, forwardRef, Input } from '@angular/core';
  2. import { Platform, Config, DomController, Form, Haptic, Item, Range } from 'ionic-angular';
  3. import { NG_VALUE_ACCESSOR } from '@angular/forms';
  4. class TempRange extends Range {
  5. static decorators = undefined;
  6. }
  7. export const MCRANGE_VALUE_ACCESSOR: any = {
  8. provide: NG_VALUE_ACCESSOR,
  9. useExisting: forwardRef(() => McRangeComponent),
  10. multi: true
  11. };
  12. @Component({
  13. selector: 'mc-range',
  14. template:
  15. '<ng-content select="[range-left]"></ng-content>' +
  16. '<div class="range-slider" #slider>' +
  17. '<div class="range-tick" *ngFor="let t of _ticks" [style.left]="t.left" [class.range-tick-active]="t.active" role="presentation"></div>' +
  18. '<div class="range-bar" role="presentation"></div>' +
  19. '<div class="range-bar range-bar-active" [style.left]="_barL" [style.right]="_barR" #bar role="presentation"></div>' +
  20. '<div class="range-knob-handle" (ionIncrease)="_keyChg(true, false)" (ionDecrease)="_keyChg(false, false)" [ratio]="_ratioA" [val]="_valA" [pin]="_pin" [pressed]="true" [min]="_min" [max]="_max" [disabled]="_disabled" [labelId]="_lblId"></div>' +
  21. '<div class="range-knob-handle" (ionIncrease)="_keyChg(true, true)" (ionDecrease)="_keyChg(false, true)" [ratio]="_ratioB" [val]="_valB" [pin]="_pin" [pressed]="_pressedB" [min]="_min" [max]="_max" [disabled]="_disabled" [labelId]="_lblId" *ngIf="_dual"></div>' +
  22. '</div><span style="top: -1em; font-size: 80%; position: relative; float: left;">{{_min}}{{_unit}}</span><span style="top: -1em; font-size: 80%; position: relative;float:right;">{{_max}}{{_unit}}</span>' +
  23. '<ng-content select="[range-right]"></ng-content>',
  24. host: {
  25. '[class.range-disabled]': '_disabled',
  26. '[class.range-pressed]': '_pressed',
  27. '[class.range-has-pin]': '_pin'
  28. },
  29. providers: [MCRANGE_VALUE_ACCESSOR]
  30. })
  31. export class McRangeComponent extends TempRange {
  32. //text: string;
  33. _description: string;
  34. _unit: string;
  35. _plt_mf: Platform;
  36. constructor(
  37. _form: Form,
  38. _haptic: Haptic,
  39. @Optional() _item: Item,
  40. config: Config,
  41. _plt: Platform,
  42. elementRef: ElementRef,
  43. renderer: Renderer,
  44. _dom: DomController,
  45. _cd: ChangeDetectorRef
  46. ) {
  47. super( _form, _haptic, _item, config, _plt, elementRef, renderer, _dom, _cd );
  48. this._description = '';
  49. this._unit = '';
  50. this._plt_mf = _plt;
  51. // console.log('Hello McRange Component');
  52. // this.text = 'Hello World';
  53. }
  54. /**
  55. * @input {number} Maximum integer value of the range. Defaults to `100`.
  56. */
  57. @Input()
  58. get description(): string {
  59. return this._description;
  60. }
  61. set description(val: string) {
  62. this._description = val;
  63. }
  64. @Input()
  65. get step(): number {
  66. return this._step;
  67. }
  68. set step(val: number) {
  69. if (!isNaN(val) && val > 0) {
  70. this._step = val;
  71. }
  72. }
  73. @Input()
  74. get unit(): string {
  75. return this._unit;
  76. }
  77. set unit(val: string) {
  78. this._unit = ' ' + val;
  79. }
  80. _ratioToValue(ratio: number) {
  81. //this._step = 0.1;
  82. ratio = (this._max - this._min) * ratio;
  83. ratio = Math.round(ratio / this._step) * this._step + this._min;
  84. return Math.round( ratio * 100 ) / 100; // strange bug with 10th significant digit
  85. // return clamp(this._min, ratio, this._max) + 12.3;
  86. }
  87. _pointerDown(ev: UIEvent): boolean {
  88. // console.log( ev );
  89. if( ev.srcElement.className != "range-slider" || !this._plt_mf.is( 'ios' ) )
  90. return super._pointerDown( ev );
  91. return false;
  92. }
  93. }