OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:html'; | 5 import 'dart:html'; |
6 | 6 |
7 /// Utility class for Custom Tags registration. | 7 /// Utility class for Custom Tags registration. |
8 class Tag<T extends HtmlElement> { | 8 class Tag<T extends HtmlElement> { |
9 /// Tag name. | 9 /// Tag name. |
10 final String name; | 10 final String name; |
11 | 11 |
12 /// Dependend tags that need to be registred for this tag to work properly. | 12 /// Dependent tags that need to be registred for this tag to work properly. |
13 final Iterable<Tag> dependencies; | 13 final Iterable<Tag> dependencies; |
14 | 14 |
15 const Tag(this.name, {this.dependencies: const []}); | 15 const Tag(this.name, {this.dependencies: const []}); |
16 | 16 |
17 static final Map<Type, String> _tagByClass = <Type, String>{}; | 17 static final Map<Type, String> _tagByClass = <Type, String>{}; |
18 static final Map<String, Type> _classByTag = <String, Type>{}; | 18 static final Map<String, Type> _classByTag = <String, Type>{}; |
19 | 19 |
20 /// Ensures that the Tag and all the dependencies are registered. | 20 /// Ensures that the Tag and all the dependencies are registered. |
21 void ensureRegistration() { | 21 void ensureRegistration() { |
22 if (!_tagByClass.containsKey(T) && !_classByTag.containsKey(name)) { | 22 if (!_tagByClass.containsKey(T) && !_classByTag.containsKey(name)) { |
23 document.registerElement(name, T); | 23 document.registerElement(name, T); |
24 _tagByClass[T] = name; | 24 _tagByClass[T] = name; |
25 _classByTag[name] = T; | 25 _classByTag[name] = T; |
26 dependencies.forEach((tag) => tag.ensureRegistration()); | 26 dependencies.forEach((tag) => tag.ensureRegistration()); |
27 } | 27 } |
28 var tag = _tagByClass[T]; | 28 var tag = _tagByClass[T]; |
29 if (tag != name) { | 29 if (tag != name) { |
30 throw new ArgumentError('Class $T is already registered to tag ${tag}'); | 30 throw new ArgumentError('Class $T is already registered to tag ${tag}'); |
31 } | 31 } |
32 var c = _classByTag[name]; | 32 var c = _classByTag[name]; |
33 if (c != T) { | 33 if (c != T) { |
34 throw new ArgumentError('Tag $name is already registered by class ${c}'); | 34 throw new ArgumentError('Tag $name is already registered by class ${c}'); |
35 } | 35 } |
36 } | 36 } |
37 } | 37 } |
OLD | NEW |