OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, 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 part of custom_element; | |
6 | |
7 /** | |
8 * Represents an attribute map of model values. If any items are added, | |
9 * removed, or replaced, then observers that are listening to [changes] | |
10 * will be notified. | |
11 */ | |
12 class _AttributeMap implements Map<String, String> { | |
13 final CustomElement _element; | |
14 final Map<String, String> _map; | |
15 | |
16 /** Creates an attribute map wrapping the host attributes. */ | |
17 _AttributeMap(CustomElement element) | |
18 : _element = element, _map = element.host.attributes; | |
19 | |
20 // Forward all read methods: | |
21 Iterable<String> get keys => _map.keys; | |
22 Iterable<String> get values => _map.values; | |
23 int get length =>_map.length; | |
24 bool get isEmpty => _map.isEmpty; | |
25 bool get isNotEmpty => _map.isNotEmpty; | |
26 bool containsValue(Object value) => _map.containsValue(value); | |
27 bool containsKey(Object key) => _map.containsKey(key); | |
28 String operator [](Object key) => _map[key]; | |
29 void forEach(void f(String key, String value)) => _map.forEach(f); | |
30 String toString() => _map.toString(); | |
31 | |
32 // Override the write methods and ensure attributeChanged is called: | |
33 void operator []=(String key, String value) { | |
34 int len = _map.length; | |
35 String oldValue = _map[key]; | |
36 _map[key] = value; | |
37 if (len != _map.length || !identical(oldValue, value)) { | |
38 _element.attributeChanged(key, oldValue); | |
39 } | |
40 } | |
41 | |
42 void addAll(Map<String, String> other) { | |
43 other.forEach((String key, String value) { this[key] = value; }); | |
44 } | |
45 | |
46 String putIfAbsent(String key, String ifAbsent()) { | |
47 int len = _map.length; | |
48 String result = _map.putIfAbsent(key, ifAbsent); | |
49 if (len != _map.length) { | |
50 _element.attributeChanged(key, null); | |
51 } | |
52 return result; | |
53 } | |
54 | |
55 String remove(Object key) { | |
56 int len = _map.length; | |
57 String result = _map.remove(key); | |
58 if (len != _map.length) { | |
59 _element.attributeChanged(key, result); | |
60 } | |
61 return result; | |
62 } | |
63 | |
64 void clear() { | |
65 int len = _map.length; | |
66 if (len > 0) { | |
67 _map.forEach((key, value) { | |
68 _element.attributeChanged(key, value); | |
69 }); | |
70 } | |
71 _map.clear(); | |
72 } | |
73 | |
74 /** | |
75 * This is not a [Map] method. We use it to implement "set attributes", which | |
76 * is a global replace operation. Rather than [clear] followed by [addAll], | |
77 * we try to be a bit smarter. | |
78 */ | |
79 void _replaceAll(Map<String, String> other) { | |
80 for (var key in keys) { | |
81 if (!other.containsKey(key)) remove(key); | |
82 } | |
83 addAll(other); | |
84 } | |
85 } | |
OLD | NEW |