toggle.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { Component, ElementRef, HostListener, Input, NgZone, Optional, Renderer, ViewEncapsulation } from '@angular/core';
  2. import { NG_VALUE_ACCESSOR } from '@angular/forms';
  3. import { Config } from '../../config/config';
  4. import { DomController } from '../../platform/dom-controller';
  5. import { Form } from '../../util/form';
  6. import { GestureController } from '../../gestures/gesture-controller';
  7. import { Haptic } from '../../tap-click/haptic';
  8. import { isTrueProperty } from '../../util/util';
  9. import { BaseInput } from '../../util/base-input';
  10. import { Item } from '../item/item';
  11. import { KEY_ENTER, KEY_SPACE } from '../../platform/key';
  12. import { Platform } from '../../platform/platform';
  13. import { ToggleGesture } from './toggle-gesture';
  14. /**
  15. * @name Toggle
  16. * @description
  17. * A toggle technically is the same thing as an HTML checkbox input,
  18. * except it looks different and is easier to use on a touch device.
  19. * Toggles can also have colors assigned to them, by adding any color
  20. * attribute.
  21. *
  22. * See the [Angular Docs](https://angular.io/docs/ts/latest/guide/forms.html)
  23. * for more info on forms and inputs.
  24. *
  25. * @usage
  26. * ```html
  27. *
  28. * <ion-list>
  29. *
  30. * <ion-item>
  31. * <ion-label>Pepperoni</ion-label>
  32. * <ion-toggle [(ngModel)]="pepperoni"></ion-toggle>
  33. * </ion-item>
  34. *
  35. * <ion-item>
  36. * <ion-label>Sausage</ion-label>
  37. * <ion-toggle [(ngModel)]="sausage" disabled="true"></ion-toggle>
  38. * </ion-item>
  39. *
  40. * <ion-item>
  41. * <ion-label>Mushrooms</ion-label>
  42. * <ion-toggle [(ngModel)]="mushrooms"></ion-toggle>
  43. * </ion-item>
  44. *
  45. * </ion-list>
  46. * ```
  47. *
  48. * @demo /docs/demos/src/toggle/
  49. * @see {@link /docs/components#toggle Toggle Component Docs}
  50. */
  51. export class Toggle extends BaseInput {
  52. constructor(form, config, _plt, elementRef, renderer, _haptic, item, _gestureCtrl, _domCtrl, _zone) {
  53. super(config, elementRef, renderer, 'toggle', false, form, item, null);
  54. this._plt = _plt;
  55. this._haptic = _haptic;
  56. this._gestureCtrl = _gestureCtrl;
  57. this._domCtrl = _domCtrl;
  58. this._zone = _zone;
  59. this._activated = false;
  60. }
  61. /**
  62. * @input {boolean} If true, the element is selected.
  63. */
  64. get checked() {
  65. return this.value;
  66. }
  67. set checked(val) {
  68. this.value = val;
  69. }
  70. /**
  71. * @hidden
  72. */
  73. ngAfterContentInit() {
  74. this._initialize();
  75. this._gesture = new ToggleGesture(this._plt, this, this._gestureCtrl, this._domCtrl);
  76. this._gesture.listen();
  77. }
  78. /**
  79. * @hidden
  80. */
  81. _inputUpdated() { }
  82. /**
  83. * @hidden
  84. */
  85. _inputNormalize(val) {
  86. return isTrueProperty(val);
  87. }
  88. /**
  89. * @hidden
  90. */
  91. _onDragStart(startX) {
  92. (void 0) /* assert */;
  93. (void 0) /* console.debug */;
  94. this._zone.run(() => {
  95. this._startX = startX;
  96. this._fireFocus();
  97. this._activated = true;
  98. });
  99. }
  100. /**
  101. * @hidden
  102. */
  103. _onDragMove(currentX) {
  104. if (!this._startX) {
  105. (void 0) /* assert */;
  106. return;
  107. }
  108. if (this._shouldToggle(currentX, -15)) {
  109. this._zone.run(() => {
  110. this.value = !this.value;
  111. this._startX = currentX;
  112. this._haptic.selection();
  113. });
  114. }
  115. }
  116. /**
  117. * @hidden
  118. */
  119. _onDragEnd(endX) {
  120. if (!this._startX) {
  121. (void 0) /* assert */;
  122. return;
  123. }
  124. (void 0) /* console.debug */;
  125. this._zone.run(() => {
  126. if (this._shouldToggle(endX, 4)) {
  127. this.value = !this.value;
  128. this._haptic.selection();
  129. }
  130. this._activated = false;
  131. this._fireBlur();
  132. this._startX = null;
  133. });
  134. }
  135. /**
  136. * @hidden
  137. */
  138. _shouldToggle(currentX, margin) {
  139. const isLTR = !this._plt.isRTL;
  140. const startX = this._startX;
  141. if (this._value) {
  142. return (isLTR && (startX + margin > currentX)) ||
  143. (!isLTR && (startX - margin < currentX));
  144. }
  145. else {
  146. return (isLTR && (startX - margin < currentX)) ||
  147. (!isLTR && (startX + margin > currentX));
  148. }
  149. }
  150. /**
  151. * @hidden
  152. */
  153. _keyup(ev) {
  154. if (ev.keyCode === KEY_SPACE || ev.keyCode === KEY_ENTER) {
  155. (void 0) /* console.debug */;
  156. ev.preventDefault();
  157. ev.stopPropagation();
  158. this.value = !this.value;
  159. }
  160. }
  161. /**
  162. * @hidden
  163. */
  164. ngOnDestroy() {
  165. super.ngOnDestroy();
  166. this._gesture && this._gesture.destroy();
  167. }
  168. }
  169. Toggle.decorators = [
  170. { type: Component, args: [{
  171. selector: 'ion-toggle',
  172. template: '<div class="toggle-icon">' +
  173. '<div class="toggle-inner"></div>' +
  174. '</div>' +
  175. '<button role="checkbox" ' +
  176. 'type="button" ' +
  177. 'ion-button="item-cover" ' +
  178. '[id]="id" ' +
  179. '[attr.aria-checked]="_value" ' +
  180. '[attr.aria-labelledby]="_labelId" ' +
  181. '[attr.aria-disabled]="_disabled" ' +
  182. 'class="item-cover" disable-activated>' +
  183. '</button>',
  184. host: {
  185. '[class.toggle-disabled]': '_disabled',
  186. '[class.toggle-checked]': '_value',
  187. '[class.toggle-activated]': '_activated',
  188. },
  189. providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: Toggle, multi: true }],
  190. encapsulation: ViewEncapsulation.None,
  191. },] },
  192. ];
  193. /** @nocollapse */
  194. Toggle.ctorParameters = () => [
  195. { type: Form, },
  196. { type: Config, },
  197. { type: Platform, },
  198. { type: ElementRef, },
  199. { type: Renderer, },
  200. { type: Haptic, },
  201. { type: Item, decorators: [{ type: Optional },] },
  202. { type: GestureController, },
  203. { type: DomController, },
  204. { type: NgZone, },
  205. ];
  206. Toggle.propDecorators = {
  207. 'checked': [{ type: Input },],
  208. '_keyup': [{ type: HostListener, args: ['keyup', ['$event'],] },],
  209. };
  210. //# sourceMappingURL=toggle.js.map