OLD | NEW |
(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 querySelector('#inputName').onInput.listen(updateBadge); |
| 12 genButton = querySelector('#generateButton'); |
| 13 genButton.onClick.listen(generateBadge); |
| 14 } |
| 15 |
| 16 void updateBadge(Event e) { |
| 17 String inputName = (e.target as InputElement).value; |
| 18 |
| 19 setBadgeName(new PirateName(firstName: inputName)); |
| 20 if (inputName.trim().isEmpty) { |
| 21 genButton..disabled = false |
| 22 ..text = 'Aye! Gimme a name!'; |
| 23 } else { |
| 24 genButton..disabled = true |
| 25 ..text = 'Arrr! Write yer name!'; |
| 26 } |
| 27 } |
| 28 |
| 29 void generateBadge(Event e) { |
| 30 setBadgeName(new PirateName()); |
| 31 } |
| 32 |
| 33 void setBadgeName(PirateName newName) { |
| 34 querySelector('#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.isEmpty ? '' : '$_firstName the $_appellat
ion'; |
| 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 } |
OLD | NEW |