/
index.es6
203 lines (177 loc) Β· 5.95 KB
/
index.es6
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
import {bootstrap, NgIf, NgFor, EventEmitter} from 'angular2/angular2';
import {FormBuilder, Validators, formDirectives, ControlGroup} from 'angular2/forms';
// TODO(radokirov): Once the application is transpiled by TS instead of Traceur,
// add those imports back into 'angular2/angular2';
import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations';
import {View} from 'angular2/src/core/annotations_impl/view';
// HeaderFields renders the bound header control group. It can used as follows:
//
// <survey-header [header]="header"></survey-header>
//
// This component is self-contained and can be tested in isolation.
@Component({
selector: 'survey-header',
properties: {
"header" : "header"
}
})
@View({
template: `
<div [control-group]="header">
<div>
<label>Title:</label> <br/>
<input type="text" control="title"/>
<div *ng-if="! header.controls.title.valid && header.controls.title.dirty">
Title is required
</div>
</div>
<div>
<label>Description:</label> <br/>
<textarea control="description"></textarea>
<div *ng-if="! header.controls.description.valid && header.controls.description.dirty">
Description is required
</div>
</div>
<div>
<label>Publish Date:</label> <br/>
<input type="date" control="date"/>
</div>
</div>
`,
directives: [formDirectives, NgIf]
})
class HeaderFields {
header:ControlGroup;
}
// SurveyQuestion renders an individual question. It can used as follows:
//
// <survey-question [question]="question" [index]="i" (delete)="onDelete()"></survey-question>
//
// SurveyQuestion uses EventEmitter to fire the delete action.
// This component is self-contained and can be tested in isolation.
@Component({
selector: 'survey-question',
events: ['destroy'],
properties: {
"question" : "question",
"index" : "index"
}
})
@View({
template: `
<h2>Question #{{index}}</h2>
<button (click)="deleteQuestion()">Delete</button>
<div [control-group]="question">
<div>
<label>Type:</label> <br/>
<select control="type">
<option value=""></option>
<option value="text">Text</option>
<option value="checkbox">Checkbox</option>
<option value="textarea">Textarea</option>
</select>
<div *ng-if="! question.controls.type.valid && question.controls.type.dirty">
Type is required
</div>
</div>
<div>
<label>Question:</label> <br/>
<input type="text" control="questionText">
<div *ng-if="! question.controls.questionText.valid && question.controls.questionText.dirty">
Question is required
</div>
</div>
<div *ng-if="question.contains('responseLength')">
<label>Response Length:</label> <br/>
<input type="number" control="responseLength">
<div *ng-if="! question.controls.responseLength.valid && question.controls.responseLength.dirty">
Length is required
</div>
</div>
</div>
`,
directives: [formDirectives, If]
})
class SurveyQuestion {
question:ControlGroup;
index:number;
destroy:EventEmitter;
constructor() {
this.destroy = new EventEmitter();
}
deleteQuestion() {
// Invoking an injected event emitter will fire an event,
// which in this case will result in calling `deleteQuestion(i)`
this.destroy.next(null);
}
}
// SurveyBuilder is a form that allows you to create a survey.
@Component({
selector: 'survey-builder-app',
appInjector: [FormBuilder]
})
@View({
template: `
<h1>Create New Survey</h1>
<div [control-group]="form">
<survey-header [header]="form.controls.header"></survey-header>
<button (click)="addQuestion()">Add Question</button>
<survey-question
*ng-for="var q of form.controls.questions.controls; var i=index"
[question]="q"
[index]="i + 1"
(destroy)="destroyQuestion(i)">
</survey-question>
<button (click)="submitForm()">Submit</button>
</div>
`,
directives: [formDirectives, NgFor, HeaderFields, SurveyQuestion]
})
class SurveyBuilder {
form:ControlGroup;
builder:FormBuilder;
constructor(b:FormBuilder) {
this.builder = b;
this.form = b.group({
"header" : b.group({
"title" : ["", Validators.required],
"description" : ["", Validators.required],
"date" : ""
}),
"questions": b.array([])
});
}
addQuestion() {
var newQuestion = this.builder.group({
"type": ["", Validators.required],
"questionText": ["", Validators.required],
"responseLength": [100, Validators.required]
}, {
// Optional controls can be dynamically added or removed from the form.
// Here, the responseLength field is optional and not included by default.
"optionals": {
"responseLength": false
}
});
// Every Control has an observable of value changes. You can subscribe to this observable
// to update the form, update the application model, etc.
// These observables can also be transformed and combined. This enables implementing
// complex form interactions in a declarative fashion.
//
// We are disabling the responseLength control when the question type is checkbox.
newQuestion.controls.type.valueChanges.observer({
next: (v) => v == 'text' || v == 'textarea' ? newQuestion.include('responseLength') : newQuestion.exclude('responseLength')
});
this.form.controls.questions.push(newQuestion);
}
destroyQuestion(index:number) {
this.form.controls.questions.removeAt(index);
}
submitForm() {
console.log("Submitting a form")
console.log("value", this.form.value, "valid", this.form.valid, "errors", this.form.errors);
}
}
export function main() {
bootstrap(SurveyBuilder);
}