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, cascade, fat arrow, |
| 8 // named constructors. |
| 9 // optional parameters. |
| 10 // a class |
| 11 // getters, setters |
| 12 // httprequest, JSON |
| 13 // local storage |
| 14 // static class-level methods/fields |
| 15 // top-level variable and functions |
| 16 // typecasting with 'as' |
| 17 // futures |
| 18 // import, also with show |
| 19 |
| 20 import 'dart:html'; |
| 21 import 'dart:math' show Random; |
| 22 import 'dart:convert' show JSON; |
| 23 import 'dart:async' show Future; |
| 24 |
| 25 final String TREASUREKEY = 'pirateName'; |
| 26 |
| 27 SpanElement badgeNameElement; |
| 28 ButtonElement genButton; |
| 29 |
| 30 void main() { |
| 31 InputElement inputField = query('#inputName') |
| 32 ..onInput.listen(updateBadge); |
| 33 genButton = query('#generateButton') |
| 34 ..onClick.listen(generateBadge); |
| 35 |
| 36 badgeNameElement = query('#badgeName'); |
| 37 |
| 38 PirateName.readyThePirates() |
| 39 .then((_) { |
| 40 inputField.disabled = false; //enable |
| 41 genButton.disabled = false; //enable |
| 42 badgeName = pirateNameFromStorage; |
| 43 }) |
| 44 .catchError((arrr) { |
| 45 print('Error initializing pirate names: $arrr'); |
| 46 badgeNameElement.text = 'Arrr! No names.'; |
| 47 }); |
| 48 } |
| 49 |
| 50 void updateBadge(Event e) { |
| 51 String inputName = (e.target as InputElement).value; |
| 52 |
| 53 badgeName = new PirateName(firstName: inputName); |
| 54 if (inputName.trim().isEmpty) { |
| 55 genButton..disabled = false |
| 56 ..text = 'Generate badge'; |
| 57 } else { |
| 58 genButton..disabled = true |
| 59 ..text = 'Arrr! Remove the text!'; |
| 60 } |
| 61 } |
| 62 |
| 63 void generateBadge(Event e) { |
| 64 badgeName = new PirateName(); |
| 65 } |
| 66 |
| 67 set badgeName(PirateName newName) { |
| 68 badgeNameElement.text = newName.pirateName; |
| 69 window.localStorage[TREASUREKEY] = newName.toJsonString(); |
| 70 } |
| 71 |
| 72 PirateName get pirateNameFromStorage { |
| 73 String storedName = window.localStorage[TREASUREKEY]; |
| 74 if (storedName != null) { |
| 75 return new PirateName.fromJSON(storedName); |
| 76 } else { |
| 77 return null; |
| 78 } |
| 79 } |
| 80 |
| 81 class PirateName { |
| 82 |
| 83 static final Random indexGen = new Random(); |
| 84 |
| 85 static List<String> names = []; |
| 86 static List<String> appellations = []; |
| 87 |
| 88 String _firstName; |
| 89 String _appellation; |
| 90 |
| 91 PirateName({String firstName, String appellation}) { |
| 92 if (firstName == null) { |
| 93 _firstName = names[indexGen.nextInt(names.length)]; |
| 94 } else { |
| 95 _firstName = firstName; |
| 96 } |
| 97 if (appellation == null) { |
| 98 _appellation = appellations[indexGen.nextInt(appellations.length)]; |
| 99 } else { |
| 100 _appellation = appellation; |
| 101 } |
| 102 } |
| 103 |
| 104 PirateName.fromJSON(String jsonString) { |
| 105 Map storedName = JSON.decode(jsonString); |
| 106 _firstName = storedName['f']; |
| 107 _appellation = storedName['a']; |
| 108 } |
| 109 |
| 110 String toString() => pirateName; |
| 111 |
| 112 String toJsonString() => '{ "f": "$_firstName", "a": "$_appellation" } '; |
| 113 |
| 114 String get pirateName => '$_firstName the $_appellation'; |
| 115 |
| 116 static Future readyThePirates() { |
| 117 String path = 'piratenames.json'; |
| 118 return HttpRequest.getString(path) |
| 119 .then(_parsePirateNamesFromJSON); |
| 120 } |
| 121 |
| 122 static _parsePirateNamesFromJSON(String jsonString) { |
| 123 Map pirateNames = JSON.decode(jsonString); |
| 124 names = pirateNames['names']; |
| 125 appellations = pirateNames['appellations']; |
| 126 } |
| 127 } |
OLD | NEW |