Index: src/site/codelabs/darrrt/examples/4-classbadge/piratebadge.dart |
diff --git a/src/site/codelabs/darrrt/examples/4-classbadge/piratebadge.dart b/src/site/codelabs/darrrt/examples/4-classbadge/piratebadge.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..18de097bfd8c1321c975b23cc4f125d6d53f025e |
--- /dev/null |
+++ b/src/site/codelabs/darrrt/examples/4-classbadge/piratebadge.dart |
@@ -0,0 +1,67 @@ |
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+import 'dart:html'; |
+import 'dart:math' show Random; |
+ |
+ButtonElement genButton; |
+ |
+void main() { |
+ querySelector('#inputName').onInput.listen(updateBadge); |
+ genButton = querySelector('#generateButton'); |
+ genButton.onClick.listen(generateBadge); |
+} |
+ |
+void updateBadge(Event e) { |
+ String inputName = (e.target as InputElement).value; |
+ |
+ setBadgeName(new PirateName(firstName: inputName)); |
+ if (inputName.trim().isEmpty) { |
+ genButton..disabled = false |
+ ..text = 'Aye! Gimme a name!'; |
+ } else { |
+ genButton..disabled = true |
+ ..text = 'Arrr! Write yer name!'; |
+ } |
+} |
+ |
+void generateBadge(Event e) { |
+ setBadgeName(new PirateName()); |
+} |
+ |
+void setBadgeName(PirateName newName) { |
+ querySelector('#badgeName').text = newName.pirateName; |
+} |
+ |
+class PirateName { |
+ |
+ static final Random indexGen = new Random(); |
+ |
+ String _firstName; |
+ String _appellation; |
+ |
+ PirateName({String firstName, String appellation}) { |
+ if (firstName == null) { |
+ _firstName = names[indexGen.nextInt(names.length)]; |
+ } else { |
+ _firstName = firstName; |
+ } |
+ if (appellation == null) { |
+ _appellation = appellations[indexGen.nextInt(appellations.length)]; |
+ } else { |
+ _appellation = appellation; |
+ } |
+ } |
+ |
+ String toString() => pirateName; |
+ |
+ String get pirateName => _firstName.isEmpty ? '' : '$_firstName the $_appellation'; |
+ |
+ static final List names = [ |
+ 'Anne', 'Mary', 'Jack', 'Morgan', 'Roger', |
+ 'Bill', 'Ragnar', 'Ed', 'John', 'Jane' ]; |
+ static final List appellations = [ |
+ 'Black','Damned', 'Jackal', 'Red', 'Stalwart', 'Axe', |
+ 'Young', 'Old', 'Angry', 'Brave', 'Crazy', 'Noble']; |
+} |