forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment.ts
More file actions
225 lines (200 loc) · 5.65 KB
/
segment.ts
File metadata and controls
225 lines (200 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import {Directive, Component, ElementRef, Renderer, Optional, EventEmitter, Input, Output, HostListener, ContentChildren, QueryList} from 'angular2/core';
import {NgControl} from 'angular2/common';
import {isPresent} from '../../util/util';
/**
* @name SegmentButton
* @description
* The child buttons of the `ion-segment` component. Each `ion-segment-button` must have a value.
* @usage
* ```html
* <ion-segment [(ngModel)]="relationship" primary>
* <ion-segment-button value="friends" (select)="selectedFriends()">
* Friends
* </ion-segment-button>
* <ion-segment-button value="enemies" (select)="selectedEnemies()">
* Enemies
* </ion-segment-button>
* </ion-segment>
*```
*
* Or with `FormBuilder`
*
*```html
* <form [ngFormModel]="myForm">
* <ion-segment ngControl="mapStyle" danger>
* <ion-segment-button value="standard">
* Standard
* </ion-segment-button>
* <ion-segment-button value="hybrid">
* Hybrid
* </ion-segment-button>
* <ion-segment-button value="sat">
* Satellite
* </ion-segment-button>
* </ion-segment>
* </form>
* ```
*
*
* @demo /docs/v2/demos/segment/
* @see {@link /docs/v2/components#segment Segment Component Docs}
* @see {@link /docs/v2/api/components/segment/Segment/ Segment API Docs}
*/
@Component({
selector: 'ion-segment-button',
template:
'<ng-content></ng-content>' +
'<ion-button-effect></ion-button-effect>',
host: {
'tappable': '',
'class': 'segment-button',
'role': 'button'
}
})
export class SegmentButton {
/**
* @input {string} the value of the segment button. Required.
*/
@Input() value: string;
/**
* @output {SegmentButton} expression to evaluate when a segment button has been clicked
*/
@Output() select: EventEmitter<SegmentButton> = new EventEmitter();
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {}
/**
* @private
* On click of a SegmentButton
*/
@HostListener('click', ['$event'])
private onClick(ev) {
console.debug('SegmentButton, select', this.value);
this.select.emit(this);
}
/**
* @private
*/
ngOnInit() {
if (!isPresent(this.value)) {
console.warn('<ion-segment-button> requires a "value" attribute');
}
}
/**
* @private
*/
set isActive(isActive) {
this._renderer.setElementClass(this._elementRef.nativeElement, 'segment-activated', isActive);
this._renderer.setElementAttribute(this._elementRef.nativeElement, 'aria-pressed', isActive);
}
}
/**
* @name Segment
* @description
* 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.
* 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.
* You could use Angular 2's `ngModel` or `FormBuilder` API. For an overview on how `FormBuilder` works, checkout [Angular 2 Forms](http://learnangular2.com/forms/), or [Angular FormBuilder](https://angular.io/docs/ts/latest/api/common/FormBuilder-class.html)
*
*
* @usage
* ```html
* <ion-segment [(ngModel)]="relationship" (change)="onSegmentChanged($event)" danger>
* <ion-segment-button value="friends">
* Friends
* </ion-segment-button>
* <ion-segment-button value="enemies">
* Enemies
* </ion-segment-button>
* </ion-segment>
*```
*
* Or with `FormBuilder`
*
*```html
* <form [ngFormModel]="myForm">
* <ion-segment ngControl="mapStyle" danger>
* <ion-segment-button value="standard">
* Standard
* </ion-segment-button>
* <ion-segment-button value="hybrid">
* Hybrid
* </ion-segment-button>
* <ion-segment-button value="sat">
* Satellite
* </ion-segment-button>
* </ion-segment>
* </form>
* ```
*
*
* @demo /docs/v2/demos/segment/
* @see {@link /docs/v2/components#segment Segment Component Docs}
* @see [Angular 2 Forms](http://learnangular2.com/forms/)
*/
@Directive({
selector: 'ion-segment'
})
export class Segment {
/**
* @private
*/
value: string;
/**
* @output {Any} expression to evaluate when a segment button has been changed
*/
@Output() change: EventEmitter<SegmentButton> = new EventEmitter();
/**
* @private
*/
@ContentChildren(SegmentButton) _buttons: QueryList<SegmentButton>;
constructor(@Optional() ngControl: NgControl) {
if (ngControl) {
ngControl.valueAccessor = this;
}
}
/**
* @private
* Write a new value to the element.
*/
writeValue(value) {
this.value = isPresent(value) ? value : '';
if (this._buttons) {
let buttons = this._buttons.toArray();
for (let button of buttons) {
button.isActive = (button.value === this.value);
}
}
}
/**
* @private
*/
ngAfterViewInit() {
let buttons = this._buttons.toArray();
for (let button of buttons) {
button.select.subscribe((selectedButton) => {
this.writeValue(selectedButton.value);
this.onChange(selectedButton.value);
this.change.emit(selectedButton);
});
if (isPresent(this.value)) {
button.isActive = (button.value === this.value);
}
}
}
/**
* @private
*/
onChange = (_) => {};
/**
* @private
*/
onTouched = (_) => {};
/**
* @private
* Set the function to be called when the control receives a change event.
*/
registerOnChange(fn) { this.onChange = fn; }
/**
* @private
* Set the function to be called when the control receives a touch event.
*/
registerOnTouched(fn) { this.onTouched = fn; }
}