Created
December 2, 2014 15:54
-
-
Save glinesbdev/ecd67e1addca7715cc60 to your computer and use it in GitHub Desktop.
Pirate Name Badge Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
import 'dart:convert' show JSON; | |
ButtonElement genButton; | |
final String TREASURE_KEY = 'pirateName'; | |
void main() { | |
querySelector('#inputName').onInput.listen(updateBadge); | |
genButton = querySelector('#generateButton'); | |
genButton.onClick.listen(generateBadge); | |
setBadgeName(getBadgeNameFromStorage()); | |
} | |
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 setBadgeName(PirateName newName) { | |
if (newName == null) { | |
return; | |
} | |
querySelector('#badgeName').text = newName.pirateName; | |
window.localStorage[TREASURE_KEY] = newName.jsonString; | |
} | |
PirateName getBadgeNameFromStorage() { | |
String storedName = window.localStorage[TREASURE_KEY]; | |
if (storedName != null) { | |
return new PirateName.fromJSON(storedName); | |
} else { | |
return null; | |
} | |
} | |
void generateBadge(Event e) { | |
setBadgeName(new PirateName()); | |
} | |
class PirateName { | |
static final Random indexGen = new Random(); | |
String _firstName; | |
String _appellation; | |
static final List names = [ | |
'Anne', 'Mary', 'Jack', 'Morgan', 'Roger', | |
'Bill', 'Ragnar', 'Ed', 'John', 'Jane']; | |
static final List appellations = [ | |
'Jackal', 'King', 'Red', 'Stalwart', 'Axe', | |
'Young', 'Brave', 'Eager', 'Wily', 'Zesty']; | |
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 get pirateName => | |
_firstName.isEmpty ? '' : '$_firstName the $_appellation'; | |
String toString() => pirateName; | |
PirateName.fromJSON(String jsonString) { | |
Map storedName = JSON.decode(jsonString); | |
_firstName = storedName['f']; | |
_appellation = storedName['a']; | |
} | |
String get jsonString => JSON.encode({"f": _firstName, "a": _appellation}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment