| OLD | NEW |
| 1 part of angular.core.dom_internal; | 1 part of angular.core.dom; |
| 2 | 2 |
| 3 /// Callback function used to notify of attribute changes. | |
| 4 typedef void _AttributeChanged(String newValue); | |
| 5 | |
| 6 /// Callback function used to notify of observer changes. | |
| 7 typedef void Mustache(bool hasObservers); | |
| 8 | |
| 9 /** | 3 /** |
| 10 * NodeAttrs is a facade for element attributes. | 4 * Callback function used to notify of attribute changes. |
| 11 * | 5 */ |
| 12 * This facade allows reading and writing the attribute values as well as | 6 typedef AttributeChanged(String newValue); |
| 13 * adding observers triggered when: | 7 |
| 14 * - The value of an attribute changes, | 8 /** |
| 15 * - An element becomes observed. | 9 * NodeAttrs is a facade for element attributes. The facade is responsible |
| 10 * for normalizing attribute names as well as allowing access to the |
| 11 * value of the directive. |
| 16 */ | 12 */ |
| 17 class NodeAttrs { | 13 class NodeAttrs { |
| 18 final dom.Element element; | 14 final dom.Element element; |
| 19 | 15 |
| 20 Map<String, List<_AttributeChanged>> _observers; | 16 Map<String, List<AttributeChanged>> _observers; |
| 21 final _mustacheAttrs = <String, _MustacheAttr>{}; | |
| 22 | 17 |
| 23 NodeAttrs(this.element); | 18 NodeAttrs(this.element); |
| 24 | 19 |
| 25 operator [](String attrName) => element.attributes[attrName]; | 20 operator [](String attributeName) => |
| 21 element.attributes[attributeName]; |
| 26 | 22 |
| 27 void operator []=(String attrName, String value) { | 23 operator []=(String attributeName, String value) { |
| 28 if (_mustacheAttrs.containsKey(attrName)) { | 24 if (value == null) { |
| 29 _mustacheAttrs[attrName].isComputed = true; | 25 element.attributes.remove(attributeName); |
| 26 } else { |
| 27 element.attributes[attributeName] = value; |
| 30 } | 28 } |
| 31 if (value == null) { | 29 if (_observers != null && _observers.containsKey(attributeName)) { |
| 32 element.attributes.remove(attrName); | 30 _observers[attributeName].forEach((fn) => fn(value)); |
| 33 } else { | |
| 34 element.attributes[attrName] = value; | |
| 35 } | |
| 36 | |
| 37 if (_observers != null && _observers.containsKey(attrName)) { | |
| 38 _observers[attrName].forEach((notifyFn) => notifyFn(value)); | |
| 39 } | 31 } |
| 40 } | 32 } |
| 41 | 33 |
| 42 /** | 34 /** |
| 43 * Observes changes to the attribute by invoking the [notifyFn] | 35 * Observe changes to the attribute by invoking the [AttributeChanged] |
| 44 * function. On registration the [notifyFn] function gets invoked in order to | 36 * function. On registration the [AttributeChanged] function gets invoked |
| 45 * synchronize with the current value. | 37 * synchronise with the current value. |
| 46 * | |
| 47 * When an observed is registered on an attributes any existing | |
| 48 * [_observerListeners] will be called with the first parameter set to | |
| 49 * [:true:] | |
| 50 */ | 38 */ |
| 51 observe(String attrName, notifyFn(String value)) { | 39 observe(String attributeName, AttributeChanged notifyFn) { |
| 52 if (_observers == null) _observers = <String, List<_AttributeChanged>>{}; | 40 if (_observers == null) { |
| 53 _observers.putIfAbsent(attrName, () => <_AttributeChanged>[]) | 41 _observers = new Map<String, List<AttributeChanged>>(); |
| 54 .add(notifyFn); | |
| 55 | |
| 56 if (_mustacheAttrs.containsKey(attrName)) { | |
| 57 if (_mustacheAttrs[attrName].isComputed) notifyFn(this[attrName]); | |
| 58 _mustacheAttrs[attrName].notifyFn(true); | |
| 59 } else { | |
| 60 notifyFn(this[attrName]); | |
| 61 } | 42 } |
| 43 if (!_observers.containsKey(attributeName)) { |
| 44 _observers[attributeName] = new List<AttributeChanged>(); |
| 45 } |
| 46 _observers[attributeName].add(notifyFn); |
| 47 notifyFn(this[attributeName]); |
| 62 } | 48 } |
| 63 | 49 |
| 64 void forEach(void f(String k, String v)) { | 50 void forEach(void f(String k, String v)) { |
| 65 element.attributes.forEach(f); | 51 element.attributes.forEach(f); |
| 66 } | 52 } |
| 67 | 53 |
| 68 bool containsKey(String attrName) => element.attributes.containsKey(attrName); | 54 bool containsKey(String attributeName) => |
| 55 element.attributes.containsKey(attributeName); |
| 69 | 56 |
| 70 Iterable<String> get keys => element.attributes.keys; | 57 Iterable<String> get keys => |
| 71 | 58 element.attributes.keys; |
| 72 /** | |
| 73 * Registers a listener to be called when the attribute [attrName] becomes | |
| 74 * observed. On registration [notifyFn] function gets invoked with [:false:] | |
| 75 * as the first argument. | |
| 76 */ | |
| 77 void listenObserverChanges(String attrName, Mustache notifyFn) { | |
| 78 _mustacheAttrs[attrName] = new _MustacheAttr(notifyFn); | |
| 79 notifyFn(false); | |
| 80 } | |
| 81 } | 59 } |
| 82 | 60 |
| 83 /** | 61 /** |
| 84 * [TemplateLoader] is an asynchronous access to ShadowRoot which is | 62 * TemplateLoader is an asynchronous access to ShadowRoot which is |
| 85 * loaded asynchronously. It allows a Component to be notified when its | 63 * loaded asynchronously. It allows a Component to be notified when its |
| 86 * ShadowRoot is ready. | 64 * ShadowRoot is ready. |
| 87 */ | 65 */ |
| 88 class TemplateLoader { | 66 class TemplateLoader { |
| 89 final async.Future<dom.ShadowRoot> template; | 67 final async.Future<dom.ShadowRoot> _template; |
| 90 | 68 |
| 91 TemplateLoader(this.template); | 69 async.Future<dom.ShadowRoot> get template => _template; |
| 70 |
| 71 TemplateLoader(this._template); |
| 92 } | 72 } |
| 93 | |
| 94 class _MustacheAttr { | |
| 95 // Listener trigger when the attribute becomes observed | |
| 96 final Mustache notifyFn; | |
| 97 // Whether the value has first been computed | |
| 98 bool isComputed = false; | |
| 99 | |
| 100 _MustacheAttr(this.notifyFn); | |
| 101 } | |
| OLD | NEW |