123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { ContentChildren, Directive, ElementRef, Optional, Renderer } from '@angular/core';
  2. import { NgControl } from '@angular/forms';
  3. import { Config } from '../../config/config';
  4. import { BaseInput } from '../../util/base-input';
  5. import { SegmentButton } from './segment-button';
  6. /**
  7. * @name Segment
  8. * @description
  9. * A Segment is a group of buttons, sometimes known as Segmented Controls, that allow the user to interact with a compact group of a number of controls.
  10. * Segments provide functionality similar to tabs, selecting one will unselect all others. You should use a tab bar instead of a segmented control when you want to let the user move back and forth between distinct pages in your app.
  11. * You could use Angular's `ngModel` or `FormBuilder` API. For an overview on how `FormBuilder` works, checkout [Angular Forms](http://learnangular2.com/forms/), or [Angular FormBuilder](https://angular.io/docs/ts/latest/api/forms/index/FormBuilder-class.html)
  12. *
  13. *
  14. * ```html
  15. * <!-- Segment in a header -->
  16. * <ion-header>
  17. * <ion-toolbar>
  18. * <ion-segment [(ngModel)]="icons" color="secondary">
  19. * <ion-segment-button value="camera">
  20. * <ion-icon name="camera"></ion-icon>
  21. * </ion-segment-button>
  22. * <ion-segment-button value="bookmark">
  23. * <ion-icon name="bookmark"></ion-icon>
  24. * </ion-segment-button>
  25. * </ion-segment>
  26. * </ion-toolbar>
  27. * </ion-header>
  28. *
  29. * <ion-content>
  30. * <!-- Segment in content -->
  31. * <ion-segment [(ngModel)]="relationship" color="primary" (ionChange)="segmentChanged($event)">
  32. * <ion-segment-button value="friends">
  33. * Friends
  34. * </ion-segment-button>
  35. * <ion-segment-button value="enemies">
  36. * Enemies
  37. * </ion-segment-button>
  38. * </ion-segment>
  39. *
  40. * <!-- Segment in a form -->
  41. * <form [formGroup]="myForm">
  42. * <ion-segment formControlName="mapStyle" color="danger">
  43. * <ion-segment-button value="standard">
  44. * Standard
  45. * </ion-segment-button>
  46. * <ion-segment-button value="hybrid">
  47. * Hybrid
  48. * </ion-segment-button>
  49. * <ion-segment-button value="sat">
  50. * Satellite
  51. * </ion-segment-button>
  52. * </ion-segment>
  53. * </form>
  54. * </ion-content>
  55. * ```
  56. *
  57. *
  58. * @demo /docs/demos/src/segment/
  59. * @see {@link /docs/components#segment Segment Component Docs}
  60. * @see [Angular Forms](http://learnangular2.com/forms/)
  61. */
  62. export class Segment extends BaseInput {
  63. constructor(config, elementRef, renderer, ngControl) {
  64. super(config, elementRef, renderer, 'segment', null, null, null, ngControl);
  65. }
  66. /**
  67. * @hidden
  68. */
  69. ngAfterContentInit() {
  70. this._initialize();
  71. this._buttons.forEach(button => {
  72. button.ionSelect.subscribe((selectedButton) => {
  73. this.value = selectedButton.value;
  74. this._fireTouched();
  75. });
  76. });
  77. }
  78. /**
  79. * @hidden
  80. * Write a new value to the element.
  81. */
  82. _inputUpdated() {
  83. if (!this._buttons) {
  84. (void 0) /* assert */;
  85. return;
  86. }
  87. const buttons = this._buttons.toArray();
  88. const value = this.value;
  89. for (var button of buttons) {
  90. button.isActive = (button.value === value);
  91. }
  92. }
  93. }
  94. Segment.decorators = [
  95. { type: Directive, args: [{
  96. selector: 'ion-segment',
  97. host: {
  98. '[class.segment-disabled]': '_disabled'
  99. }
  100. },] },
  101. ];
  102. /** @nocollapse */
  103. Segment.ctorParameters = () => [
  104. { type: Config, },
  105. { type: ElementRef, },
  106. { type: Renderer, },
  107. { type: NgControl, decorators: [{ type: Optional },] },
  108. ];
  109. Segment.propDecorators = {
  110. '_buttons': [{ type: ContentChildren, args: [SegmentButton,] },],
  111. };
  112. //# sourceMappingURL=segment.js.map