OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 library trydart.userOption; | |
6 | |
7 /// Persistent user-configurable option. | |
8 /// | |
9 /// Options included in [options] in settings.dart will automatically be | |
10 /// included in the settings UI unless [isHidden] is true. | |
11 /// | |
12 /// The value of an option is persisted in [storage] which is normally the | |
13 /// browser's "localStorage", and [name] is a key in "localStorage". This | |
14 /// means that hidden options can be controlled by opening the JavaScript | |
15 /// console and evaluate: | |
16 /// | |
17 /// localStorage['name'] = value // or | |
18 /// localStorage.name = value | |
19 /// | |
20 /// An option can be reset to the default value using: | |
21 /// | |
22 /// delete localStorage['name'] // or | |
23 /// delete localStorage.name | |
24 class UserOption { | |
25 final String name; | |
26 | |
27 final bool isHidden; | |
28 | |
29 static var storage; | |
30 | |
31 const UserOption(this.name, {this.isHidden: false}); | |
32 | |
33 get value => storage[name]; | |
34 | |
35 void set value(newValue) { | |
36 storage[name] = newValue; | |
37 } | |
38 | |
39 void setIfNotInitialized(newValueEvaluator()) { | |
40 if (storage[name] == null) { | |
41 value = newValueEvaluator(); | |
42 } | |
43 } | |
44 } | |
45 | |
46 class BooleanUserOption extends UserOption { | |
47 const BooleanUserOption(String name, {bool isHidden: false}) | |
48 : super(name, isHidden: isHidden); | |
49 | |
50 bool get value => super.value == 'true'; | |
51 | |
52 void set value(bool newValue) { | |
53 super.value = '$newValue'; | |
54 } | |
55 } | |
56 | |
57 class StringUserOption extends UserOption { | |
58 const StringUserOption(String name, {bool isHidden: false}) | |
59 : super(name, isHidden: isHidden); | |
60 | |
61 String get value => super.value == null ? '' : super.value; | |
62 | |
63 void set value(String newValue) { | |
64 super.value = newValue; | |
65 } | |
66 } | |
OLD | NEW |