1 /// Licensed to the Apache Software Foundation (ASF) under one
2 /// or more contributor license agreements. See the NOTICE file
3 /// distributed with this work for additional information
4 /// regarding copyright ownership. The ASF licenses this file
5 /// to you under the Apache License, Version 2.0 (the
6 /// "License"); you may not use this file except in compliance
7 /// with the License. You may obtain a copy of the License at
9 /// http://www.apache.org/licenses/LICENSE-2.0
11 /// Unless required by applicable law or agreed to in writing,
12 /// software distributed under the License is distributed on an
13 /// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 /// KIND, either express or implied. See the License for the
15 /// specific language governing permissions and limitations
16 /// under the License.
20 import 'package:thrift/thrift.dart';
21 import 'package:thrift/thrift_browser.dart';
22 import 'package:shared/shared.dart';
23 import 'package:tutorial/tutorial.dart';
25 /// Adapted from the AS3 tutorial
27 new CalculatorUI(querySelector('#output')).start();
31 final DivElement output;
33 CalculatorUI(this.output);
35 TTransport _transport;
36 Calculator _calculatorClient;
44 if (!_transport.isOpen) {
45 window.alert("The transport is not open!");
49 void _initConnection() {
50 _transport = new TAsyncClientSocketTransport(
51 new TWebSocket(Uri.parse('ws://127.0.0.1:9090/ws')),
52 new TMessageReader(new TBinaryProtocolFactory()));
53 TProtocol protocol = new TBinaryProtocol(_transport);
56 _calculatorClient = new CalculatorClient(protocol);
59 void _buildInterface() {
60 output.children.forEach((e) {
64 _buildPingComponent();
68 _buildCalculatorComponent();
70 _buildGetStructComponent();
73 void _buildPingComponent() {
74 output.append(new HeadingElement.h3()..text = "Ping");
75 ButtonElement pingButton = new ButtonElement()
77 ..onClick.listen(_onPingClick);
78 output.append(pingButton);
81 void _onPingClick(MouseEvent e) {
84 _calculatorClient.ping();
87 void _buildAddComponent() {
88 output.append(new HeadingElement.h3()..text = "Add");
89 InputElement num1 = new InputElement()
92 ..style.fontSize = "14px"
93 ..style.width = "50px";
95 SpanElement op = new SpanElement()
97 ..style.fontSize = "14px"
98 ..style.marginLeft = "10px";
100 InputElement num2 = new InputElement()
103 ..style.fontSize = "14px"
104 ..style.width = "50px"
105 ..style.marginLeft = "10px";
107 ButtonElement addButton = new ButtonElement()
109 ..style.fontSize = "14px"
110 ..style.marginLeft = "10px"
111 ..onClick.listen(_onAddClick);
112 output.append(addButton);
113 SpanElement result = new SpanElement()
115 ..style.fontSize = "14px"
116 ..style.marginLeft = "10px";
117 output.append(result);
120 void _onAddClick(MouseEvent e) {
123 InputElement num1 = querySelector("#add1");
124 InputElement num2 = querySelector("#add2");
125 SpanElement result = querySelector("#addResult");
128 .add(int.parse(num1.value), int.parse(num2.value))
134 void _buildCalculatorComponent() {
135 output.append(new HeadingElement.h3()..text = "Calculator");
136 InputElement num1 = new InputElement()
139 ..style.fontSize = "14px"
140 ..style.width = "50px";
142 SelectElement op = new SelectElement()
146 ..style.fontSize = "16px"
147 ..style.marginLeft = "10px"
148 ..style.width = "50px";
149 OptionElement addOp = new OptionElement()
151 ..value = Operation.ADD.toString();
153 OptionElement subtractOp = new OptionElement()
155 ..value = Operation.SUBTRACT.toString();
156 op.add(subtractOp, 1);
157 OptionElement multiplyOp = new OptionElement()
159 ..value = Operation.MULTIPLY.toString();
160 op.add(multiplyOp, 2);
161 OptionElement divideOp = new OptionElement()
163 ..value = Operation.DIVIDE.toString();
166 InputElement num2 = new InputElement()
169 ..style.fontSize = "14px"
170 ..style.width = "50px"
171 ..style.marginLeft = "10px";
173 ButtonElement calcButton = new ButtonElement()
175 ..style.fontSize = "14px"
176 ..style.marginLeft = "10px"
177 ..onClick.listen(_onCalcClick);
178 output.append(calcButton);
179 SpanElement result = new SpanElement()
181 ..style.fontSize = "14px"
182 ..style.marginLeft = "10px";
183 output.append(result);
184 output.append(new BRElement());
185 output.append(new BRElement());
186 LabelElement logIdLabel = new LabelElement()
188 ..style.fontSize = "14px";
189 output.append(logIdLabel);
190 InputElement logId = new InputElement()
194 ..style.fontSize = "14px"
195 ..style.width = "50px"
196 ..style.marginLeft = "10px";
197 output.append(logId);
198 LabelElement commentLabel = new LabelElement()
200 ..style.fontSize = "14px"
201 ..style.marginLeft = "10px";
202 output.append(commentLabel);
203 InputElement comment = new InputElement()
205 ..style.fontSize = "14px"
206 ..style.width = "100px"
207 ..style.marginLeft = "10px";
208 output.append(comment);
211 void _onCalcClick(MouseEvent e) {
214 InputElement num1 = querySelector("#calc1");
215 InputElement num2 = querySelector("#calc2");
216 SelectElement op = querySelector("#calcOp");
217 SpanElement result = querySelector("#calcResult");
218 InputElement logId = querySelector("#logId");
219 InputElement comment = querySelector("#comment");
221 int logIdValue = int.parse(logId.value);
222 logId.value = (logIdValue + 1).toString();
224 Work work = new Work();
225 work.num1 = int.parse(num1.value);
226 work.num2 = int.parse(num2.value);
227 work.op = int.parse(op.options[op.selectedIndex].value);
228 work.comment = comment.value;
230 _calculatorClient.calculate(logIdValue, work).then((int n) {
235 void _buildGetStructComponent() {
236 output.append(new HeadingElement.h3()..text = "Get Struct");
237 LabelElement logIdLabel = new LabelElement()
238 ..text = "Struct Key:"
239 ..style.fontSize = "14px";
240 output.append(logIdLabel);
241 InputElement logId = new InputElement()
245 ..style.fontSize = "14px"
246 ..style.width = "50px"
247 ..style.marginLeft = "10px";
248 output.append(logId);
249 ButtonElement getStructButton = new ButtonElement()
251 ..style.fontSize = "14px"
252 ..style.marginLeft = "10px"
253 ..onClick.listen(_onGetStructClick);
254 output.append(getStructButton);
255 output.append(new BRElement());
256 output.append(new BRElement());
257 TextAreaElement result = new TextAreaElement()
258 ..id = "getStructResult"
259 ..style.fontSize = "14px"
260 ..style.width = "300px"
261 ..style.height = "50px"
262 ..style.marginLeft = "10px";
263 output.append(result);
266 void _onGetStructClick(MouseEvent e) {
269 InputElement structKey = querySelector("#structKey");
270 TextAreaElement result = querySelector("#getStructResult");
273 .getStruct(int.parse(structKey.value))
274 .then((SharedStruct s) {
275 result.text = "${s.toString()}";