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

Side by Side Diff: src/site/articles/trydart/examples/4-classbadge/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: rough draft ready for review (2nd try after errors) 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 import 'dart:html';
6 import 'dart:math' show Random;
7
8 ButtonElement genButton;
9
10 void main() {
11 query('#inputName').onInput.listen(updateBadge);
12 genButton = query('#generateButton')
13 ..onClick.listen(generateBadge);
14 }
15
16 void updateBadge(Event e) {
17 String inputName = (e.target as InputElement).value;
18
19 badgeName = new PirateName(firstName: inputName);
20 if (inputName.trim().isEmpty) {
21 genButton..disabled = false
22 ..text = 'Generate badge';
23 } else {
24 genButton..disabled = true
25 ..text = 'Arrr! Remove the text!';
26 }
27 }
28
29 void generateBadge(Event e) {
30 badgeName = new PirateName();
31 }
32
33 set badgeName(PirateName newName) {
34 query('#badgeName').text = newName.pirateName;
35 }
36
37 class PirateName {
38
39 static final Random indexGen = new Random();
40
41 String _firstName;
42 String _appellation;
43
44 PirateName({String firstName, String appellation}) {
45 if (firstName == null) {
46 _firstName = names[indexGen.nextInt(names.length)];
47 } else {
48 _firstName = firstName;
49 }
50 if (appellation == null) {
51 _appellation = appellations[indexGen.nextInt(appellations.length)];
52 } else {
53 _appellation = appellation;
54 }
55 }
56
57 String toString() => pirateName;
58
59 String get pirateName => '$_firstName the $_appellation';
60
61 static final List names = [
62 'Anne', 'Mary', 'Jack', 'Morgan', 'Roger',
63 'Bill', 'Ragnar', 'Ed', 'John', 'Jane' ];
64 static final List appellations = [
65 'Black','Damned', 'Jackal', 'Red', 'Stalwart', 'Axe',
66 'Young', 'Old', 'Angry', 'Brave', 'Crazy', 'Noble'];
67 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698