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 |
| 6 // Demonstrates: |
| 7 // list, maps, random, strings, string interpolation |
| 8 // cascade, fat arrow, ternary operator |
| 9 // named constructors |
| 10 // optional parameters |
| 11 // a class |
| 12 // getters |
| 13 // httprequest, JSON |
| 14 // local storage |
| 15 // static class-level methods/fields |
| 16 // top-level variables and functions |
| 17 // typecasting with 'as' |
| 18 // futures |
| 19 // import, also with show |
| 20 // dart:core, html, math, convert and async libraries |
| 21 |
| 22 import 'dart:html'; |
| 23 import 'dart:math' show Random; |
| 24 import 'dart:convert' show JSON; |
| 25 import 'dart:async' show Future; |
| 26 |
| 27 final String TREASURE_KEY = 'pirateName'; |
| 28 |
| 29 ButtonElement genButton; |
| 30 SpanElement badgeNameElement; |
| 31 |
| 32 void main() { |
| 33 InputElement inputField = querySelector('#inputName'); |
| 34 inputField.onInput.listen(updateBadge); |
| 35 genButton = querySelector('#generateButton'); |
| 36 genButton.onClick.listen(generateBadge); |
| 37 |
| 38 badgeNameElement = querySelector('#badgeName'); |
| 39 |
| 40 PirateName.readyThePirates() |
| 41 .then((_) { |
| 42 //on success |
| 43 inputField.disabled = false; //enable |
| 44 genButton.disabled = false; //enable |
| 45 setBadgeName(getBadgeNameFromStorage()); |
| 46 }) |
| 47 .catchError((arrr) { |
| 48 print('Error initializing pirate names: $arrr'); |
| 49 badgeNameElement.text = 'Arrr! No names.'; |
| 50 }); |
| 51 } |
| 52 |
| 53 void updateBadge(Event e) { |
| 54 String inputName = (e.target as InputElement).value; |
| 55 |
| 56 setBadgeName(new PirateName(firstName: inputName)); |
| 57 if (inputName.trim().isEmpty) { |
| 58 genButton..disabled = false |
| 59 ..text = 'Aye! Gimme a name!'; |
| 60 } else { |
| 61 genButton..disabled = true |
| 62 ..text = 'Arrr! Write yer name!'; |
| 63 } |
| 64 } |
| 65 |
| 66 void generateBadge(Event e) { |
| 67 setBadgeName(new PirateName()); |
| 68 } |
| 69 |
| 70 void setBadgeName(PirateName newName) { |
| 71 if (newName == null) { |
| 72 return; |
| 73 } |
| 74 badgeNameElement.text = newName.pirateName; |
| 75 window.localStorage[TREASURE_KEY] = newName.jsonString; |
| 76 } |
| 77 |
| 78 PirateName getBadgeNameFromStorage() { |
| 79 String storedName = window.localStorage[TREASURE_KEY]; |
| 80 if (storedName != null) { |
| 81 return new PirateName.fromJSON(storedName); |
| 82 } else { |
| 83 return null; |
| 84 } |
| 85 } |
| 86 |
| 87 /* |
| 88 * A class declaration. |
| 89 */ |
| 90 class PirateName { |
| 91 |
| 92 // static variables are shared by all instances. |
| 93 // Random is a random number generator in dart:math. |
| 94 static final Random indexGen = new Random(); |
| 95 |
| 96 // List is a parameterized type. You can declare the type of the objects it co
ntains. |
| 97 static List<String> names = []; |
| 98 static List<String> appellations = []; |
| 99 |
| 100 // Instance variables. Private variables have names that start with underscore
'_'. |
| 101 String _firstName; |
| 102 String _appellation; |
| 103 |
| 104 // A constructor with two optional, named parameters. |
| 105 PirateName({String firstName, String appellation}) { |
| 106 |
| 107 // Use nextInt to get a random integer from a Random object. |
| 108 // Use length to get the number of items in a list. |
| 109 if (firstName == null) { |
| 110 _firstName = names[indexGen.nextInt(names.length)]; |
| 111 } else { |
| 112 _firstName = firstName; |
| 113 } |
| 114 if (appellation == null) { |
| 115 _appellation = appellations[indexGen.nextInt(appellations.length)]; |
| 116 } else { |
| 117 _appellation = appellation; |
| 118 } |
| 119 } |
| 120 |
| 121 // A named constructor. |
| 122 PirateName.fromJSON(String jsonString) { |
| 123 // JSON is the default implementation of a JSON encoder/decoder. |
| 124 // Map is a collection of key-value pairs. |
| 125 Map storedName = JSON.decode(jsonString); |
| 126 _firstName = storedName['f']; |
| 127 _appellation = storedName['a']; |
| 128 } |
| 129 |
| 130 // Fat arrow syntax is shorthand for a one-line function that returns a value. |
| 131 String toString() => pirateName; |
| 132 |
| 133 // A getter provides read access to the member of an object. |
| 134 String get jsonString => '{ "f": "$_firstName", "a": "$_appellation" } '; |
| 135 |
| 136 // The ternary operator is shorthand for if-then-else. |
| 137 // String interpolation lets you easily build strings from other objects. |
| 138 String get pirateName => _firstName.isEmpty ? '' : '$_firstName the $_appellat
ion'; |
| 139 |
| 140 static Future readyThePirates() { |
| 141 String path = 'piratenames.json'; |
| 142 return HttpRequest.getString(path) |
| 143 .then(_parsePirateNamesFromJSON); |
| 144 } |
| 145 |
| 146 static _parsePirateNamesFromJSON(String jsonString) { |
| 147 Map pirateNames = JSON.decode(jsonString); |
| 148 names = pirateNames['names']; |
| 149 appellations = pirateNames['appellations']; |
| 150 } |
| 151 } |
OLD | NEW |