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

Side by Side Diff: src/site/articles/trydart/examples/5-localbadge/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, 1 month 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 import 'dart:convert' show JSON;
8
9 final String TREASUREKEY = 'pirateName';
10
11 ButtonElement genButton;
12
13 void main() {
14 query('#inputName').onInput.listen(updateBadge);
15 genButton = query('#generateButton')
16 ..onClick.listen(generateBadge);
17
18 badgeName = pirateNameFromStorage;
19 }
20
21 void updateBadge(Event e) {
22 String inputName = (e.target as InputElement).value;
23
24 badgeName = new PirateName(firstName: inputName);
25 if (inputName.trim().isEmpty) {
26 genButton..disabled = false
27 ..text = 'Generate badge';
28 } else {
29 genButton..disabled = true
30 ..text = 'Arrr! Remove the text!';
31 }
32 }
33
34 void generateBadge(Event e) {
35 badgeName = new PirateName();
36 }
37
38 set badgeName(PirateName newName) {
39 query('#badgeName').text = newName.pirateName;
40 window.localStorage[TREASUREKEY] = newName.toJsonString();
41 }
42
43 PirateName get pirateNameFromStorage {
44 String storedName = window.localStorage[TREASUREKEY];
45 if (storedName != null) {
46 return new PirateName.fromJSON(storedName);
47 } else {
48 return null;
49 }
50 }
51
52 class PirateName {
53
54 static final Random indexGen = new Random();
55
56 String _firstName;
57 String _appellation;
58
59 PirateName({String firstName, String appellation}) {
60 if (firstName == null) {
61 _firstName = names[indexGen.nextInt(names.length)];
62 } else {
63 _firstName = firstName;
64 }
65 if (appellation == null) {
66 _appellation = appellations[indexGen.nextInt(appellations.length)];
67 } else {
68 _appellation = appellation;
69 }
70 }
71
72 PirateName.fromJSON(String jsonString) {
73 Map storedName = JSON.decode(jsonString);
74 _firstName = storedName['f'];
75 _appellation = storedName['a'];
76 }
77
78 String toString() => pirateName;
79
80 String toJsonString() => '{ "f": "$_firstName", "a": "$_appellation" } ';
81
82 String get pirateName => '$_firstName the $_appellation';
83
84 static final List names = [
85 'Anne', 'Mary', 'Jack', 'Morgan', 'Roger',
86 'Bill', 'Ragnar', 'Ed', 'John', 'Jane' ];
87 static final List appellations = [
88 'Black','Damned', 'Jackal', 'Red', 'Stalwart', 'Axe',
89 'Young', 'Old', 'Angry', 'Brave', 'Crazy', 'Noble'];
90 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698