OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library chat; | 5 library chat; |
6 import 'dart:html'; | 6 import 'dart:html'; |
7 import 'dart:json'; | 7 import 'dart:json'; |
8 | 8 |
9 void main() { | 9 void main() { |
10 new Chat().start(); | 10 new Chat().start(); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); | 172 request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); |
173 request.send(JSON.stringify(json)); | 173 request.send(JSON.stringify(json)); |
174 return request; | 174 return request; |
175 } | 175 } |
176 | 176 |
177 void uiJoin() { | 177 void uiJoin() { |
178 enableButton(_joinButton); | 178 enableButton(_joinButton); |
179 showElement(_joinSection); | 179 showElement(_joinSection); |
180 hideElement(_chatSection); | 180 hideElement(_chatSection); |
181 _nextMessage = 0; | 181 _nextMessage = 0; |
182 _messages.elements.clear(); | 182 _messages.children.clear(); |
183 showStatus("Welcome to dart chat sample. " | 183 showStatus("Welcome to dart chat sample. " |
184 "This chat service is build using Dart for both the server and th
e client. " | 184 "This chat service is build using Dart for both the server and th
e client. " |
185 "Enter your handle to join."); | 185 "Enter your handle to join."); |
186 _handleInput.focus(); | 186 _handleInput.focus(); |
187 } | 187 } |
188 | 188 |
189 void uiJoining() { | 189 void uiJoining() { |
190 disableButton(_joinButton); | 190 disableButton(_joinButton); |
191 showElement(_joinSection); | 191 showElement(_joinSection); |
192 hideElement(_chatSection); | 192 hideElement(_chatSection); |
(...skipping 27 matching lines...) Expand all Loading... |
220 text.add("joined"); | 220 text.add("joined"); |
221 } else if (message["type"] == "message") { | 221 } else if (message["type"] == "message") { |
222 text.add(message["message"]); | 222 text.add(message["message"]); |
223 } else if (message["type"] == "leave") { | 223 } else if (message["type"] == "leave") { |
224 text.add("left"); | 224 text.add("left"); |
225 } else { | 225 } else { |
226 text.add("timeout"); | 226 text.add("timeout"); |
227 } | 227 } |
228 p.text = text.toString(); | 228 p.text = text.toString(); |
229 _messages.insertAdjacentElement('afterBegin', p); | 229 _messages.insertAdjacentElement('afterBegin', p); |
230 if (_messages.elements.length > 20) { | 230 if (_messages.children.length > 20) { |
231 _messages.elements.removeLast(); | 231 _messages.children.removeLast(); |
232 } | 232 } |
233 } | 233 } |
234 | 234 |
235 String formatMessageTime(String received) { | 235 String formatMessageTime(String received) { |
236 Date date = new Date.fromString(received); | 236 Date date = new Date.fromString(received); |
237 StringBuffer formattedTime = new StringBuffer(); | 237 StringBuffer formattedTime = new StringBuffer(); |
238 if (date.hour < 10) formattedTime.add("0"); | 238 if (date.hour < 10) formattedTime.add("0"); |
239 formattedTime.add(date.hour); | 239 formattedTime.add(date.hour); |
240 formattedTime.add(":"); | 240 formattedTime.add(":"); |
241 if (date.minute < 10) formattedTime.add("0"); | 241 if (date.minute < 10) formattedTime.add("0"); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 } | 287 } |
288 | 288 |
289 void disableButton(ButtonElement element) { | 289 void disableButton(ButtonElement element) { |
290 element.disabled = true; | 290 element.disabled = true; |
291 } | 291 } |
292 | 292 |
293 void write(String message) { | 293 void write(String message) { |
294 Document doc = window.document; | 294 Document doc = window.document; |
295 ParagraphElement p = new Element.tag('p'); | 295 ParagraphElement p = new Element.tag('p'); |
296 p.text = message; | 296 p.text = message; |
297 doc.body.elements.add(p); | 297 doc.body.children.add(p); |
298 } | 298 } |
299 | 299 |
300 ButtonElement _joinButton; | 300 ButtonElement _joinButton; |
301 ButtonElement _leaveButton; | 301 ButtonElement _leaveButton; |
302 ButtonElement _postButton; | 302 ButtonElement _postButton; |
303 InputElement _handleInput; | 303 InputElement _handleInput; |
304 | 304 |
305 Element _joinSection; | 305 Element _joinSection; |
306 Element _chatSection; | 306 Element _chatSection; |
307 InputElement _messageInput; | 307 InputElement _messageInput; |
308 Element _messages; | 308 Element _messages; |
309 Element _statusText; | 309 Element _statusText; |
310 | 310 |
311 String _session = null; | 311 String _session = null; |
312 int _nextMessage = 0; | 312 int _nextMessage = 0; |
313 HttpRequest _pollRequest = null; | 313 HttpRequest _pollRequest = null; |
314 | 314 |
315 } | 315 } |
OLD | NEW |