| Index: src/site/articles/trydart/examples/7-piratebadge_json/piratebadge.dart
|
| diff --git a/src/site/articles/trydart/examples/7-piratebadge_json/piratebadge.dart b/src/site/articles/trydart/examples/7-piratebadge_json/piratebadge.dart
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..3c11f3d52fadbdef1e44a902c93524c119c54915
|
| --- /dev/null
|
| +++ b/src/site/articles/trydart/examples/7-piratebadge_json/piratebadge.dart
|
| @@ -0,0 +1,129 @@
|
| +// 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.
|
| +
|
| +
|
| +// Demonstrates:
|
| +// list, random, string interpolation, cascade, fat arrow,
|
| +// named constructors.
|
| +// optional parameters.
|
| +// getters, setters,
|
| +// httprequest, JSON
|
| +// static methods/fields
|
| +// cascades (onclick listen...piratename.male.name
|
| +
|
| +import 'dart:html';
|
| +import 'dart:math';
|
| +import 'dart:convert';
|
| +
|
| +InputElement inputNameElement;
|
| +ButtonElement genButtonElement;
|
| +
|
| +bool useRandomName = false;
|
| +
|
| +RadioButtonInputElement captainRadioElement;
|
| +RadioButtonInputElement scallywagRadioElement;
|
| +
|
| +void main() {
|
| + inputNameElement = query('#inputName');
|
| + inputNameElement.onChange.listen(generateBadge);
|
| +
|
| + genButtonElement = query('#generateButton');
|
| + genButtonElement.onClick.listen(generateBadge);
|
| +
|
| + query('#useRandomName').onClick.listen(useRandomNameClickHandler);
|
| +
|
| + captainRadioElement = query('#captain');
|
| + scallywagRadioElement = query('#scallywag');
|
| +
|
| + PirateName.initialize();
|
| +}
|
| +
|
| +void generateBadge(Event event) {
|
| + if (!useRandomName) {
|
| + query('#badgeName').text = new PirateName(firstName: inputNameElement.value).name;
|
| + } else if (scallywagRadioElement.checked) {
|
| + query('#badgeName').text = new PirateName.scallywag().name;
|
| + } else {
|
| + query('#badgeName').text = new PirateName.captain().name;
|
| + }
|
| +}
|
| +
|
| +void useRandomNameClickHandler(MouseEvent e) {
|
| + if ((e.target as CheckboxInputElement).checked) {
|
| + genButtonElement.disabled = false;
|
| + captainRadioElement.disabled = false;
|
| + scallywagRadioElement.disabled = false;
|
| + inputNameElement
|
| + ..disabled = true
|
| + ..value = ''
|
| + ..placeholder = '';
|
| + useRandomName = true;
|
| + } else {
|
| + genButtonElement.disabled = true;
|
| + captainRadioElement.disabled = true;
|
| + scallywagRadioElement.disabled = true;
|
| + inputNameElement
|
| + ..disabled = false
|
| + ..placeholder = 'Your name here';
|
| + useRandomName = false;
|
| + }
|
| +}
|
| +
|
| +//library models;
|
| +class PirateName {
|
| +
|
| + Random indexGen = new Random();
|
| +
|
| + String _pirateName;
|
| +
|
| + String get name => _pirateName;
|
| + set name(String value) => _pirateName = value;
|
| +
|
| + String toString() => name;
|
| +
|
| + PirateName({String firstName}) {
|
| + if (firstName == null) {
|
| + firstName = names[indexGen.nextInt(names.length)];
|
| + }
|
| + _pirateName = '$firstName';
|
| + }
|
| +
|
| + PirateName.captain() {
|
| + String firstName = names[indexGen.nextInt(names.length)];
|
| + String appellation = captains[indexGen.nextInt(captains.length)];
|
| + _pirateName = '$firstName the $appellation';
|
| + }
|
| +
|
| + PirateName.scallywag() {
|
| + String firstName = names[indexGen.nextInt(names.length)];
|
| + String appellation = scallywags[indexGen.nextInt(scallywags.length)];
|
| + _pirateName = '$firstName the $appellation';
|
| + }
|
| +
|
| + static List<String> names = [];
|
| + static List<String> captains = [];
|
| + static List<String> scallywags = [];
|
| +
|
| + static void initialize() {
|
| + makeRequest();
|
| + }
|
| +
|
| + static void makeRequest(/*Event e*/) {
|
| + var path = 'piratenames.json';
|
| + HttpRequest.getString(path)
|
| + .then(processString)
|
| + .catchError(handleError);
|
| + }
|
| +
|
| + static handleError(Exception error) {
|
| + print('Request failed.');
|
| + }
|
| +
|
| + static processString(String jsonString) {
|
| + List<List> pirateNames = JSON.decode(jsonString);
|
| + names = pirateNames[0];
|
| + captains = pirateNames[1];
|
| + scallywags = pirateNames[2];
|
| + }
|
| +}
|
|
|