Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: src/site/articles/trydart/examples/7-piratebadge_json/piratebadge.dart

Issue 35913002: Try Dart 1 hour experience first draft (Closed) Base URL: https://github.com/dart-lang/dartlang.org.git@master
Patch Set: trying again to upload chnages. still getting an error Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5
6 // Demonstrates:
7 // list, random, string interpolation, cascade, fat arrow,
8 // named constructors.
9 // optional parameters.
10 // getters, setters,
11 // httprequest, JSON
12 // static methods/fields
13 // cascades (onclick listen...piratename.male.name
14
15 import 'dart:html';
16 import 'dart:math';
17 import 'dart:convert';
18
19 InputElement inputNameElement;
20 ButtonElement genButtonElement;
21
22 bool useRandomName = false;
23
24 RadioButtonInputElement captainRadioElement;
25 RadioButtonInputElement scallywagRadioElement;
26
27 void main() {
28 inputNameElement = query('#inputName');
29 inputNameElement.onChange.listen(generateBadge);
30
31 genButtonElement = query('#generateButton');
32 genButtonElement.onClick.listen(generateBadge);
33
34 query('#useRandomName').onClick.listen(useRandomNameClickHandler);
35
36 captainRadioElement = query('#captain');
37 scallywagRadioElement = query('#scallywag');
38
39 PirateName.initialize();
40 }
41
42 void generateBadge(Event event) {
43 if (!useRandomName) {
44 query('#badgeName').text = new PirateName(firstName: inputNameElement.value) .name;
45 } else if (scallywagRadioElement.checked) {
46 query('#badgeName').text = new PirateName.scallywag().name;
47 } else {
48 query('#badgeName').text = new PirateName.captain().name;
49 }
50 }
51
52 void useRandomNameClickHandler(MouseEvent e) {
53 if ((e.target as CheckboxInputElement).checked) {
54 genButtonElement.disabled = false;
55 captainRadioElement.disabled = false;
56 scallywagRadioElement.disabled = false;
57 inputNameElement
58 ..disabled = true
59 ..value = ''
60 ..placeholder = '';
61 useRandomName = true;
62 } else {
63 genButtonElement.disabled = true;
64 captainRadioElement.disabled = true;
65 scallywagRadioElement.disabled = true;
66 inputNameElement
67 ..disabled = false
68 ..placeholder = 'Your name here';
69 useRandomName = false;
70 }
71 }
72
73 //library models;
74 class PirateName {
75
76 Random indexGen = new Random();
77
78 String _pirateName;
79
80 String get name => _pirateName;
81 set name(String value) => _pirateName = value;
82
83 String toString() => name;
84
85 PirateName({String firstName}) {
86 if (firstName == null) {
87 firstName = names[indexGen.nextInt(names.length)];
88 }
89 _pirateName = '$firstName';
90 }
91
92 PirateName.captain() {
93 String firstName = names[indexGen.nextInt(names.length)];
94 String appellation = captains[indexGen.nextInt(captains.length)];
95 _pirateName = '$firstName the $appellation';
96 }
97
98 PirateName.scallywag() {
99 String firstName = names[indexGen.nextInt(names.length)];
100 String appellation = scallywags[indexGen.nextInt(scallywags.length)];
101 _pirateName = '$firstName the $appellation';
102 }
103
104 static List<String> names = [];
105 static List<String> captains = [];
106 static List<String> scallywags = [];
107
108 static void initialize() {
109 makeRequest();
110 }
111
112 static void makeRequest(/*Event e*/) {
113 var path = 'piratenames.json';
114 HttpRequest.getString(path)
115 .then(processString)
116 .catchError(handleError);
117 }
118
119 static handleError(Exception error) {
120 print('Request failed.');
121 }
122
123 static processString(String jsonString) {
124 List<List> pirateNames = JSON.decode(jsonString);
125 names = pirateNames[0];
126 captains = pirateNames[1];
127 scallywags = pirateNames[2];
128 }
129 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698