OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, 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 library web_components.html_import_annotation; | |
5 | |
6 import 'dart:async'; | |
7 import 'dart:html'; | |
8 import 'package:initialize/initialize.dart'; | |
9 import 'src/normalize_path.dart'; | |
10 | |
11 /// Annotation for a dart library which injects an html import into the | |
12 /// current html document. The imported file must not contain any dart script | |
13 /// tags, as they cannot be dynamically loaded. | |
14 class HtmlImport implements Initializer<LibraryIdentifier> { | |
15 final String filePath; | |
16 | |
17 const HtmlImport(this.filePath); | |
18 | |
19 Future initialize(LibraryIdentifier library) { | |
20 var element = new LinkElement() | |
21 ..rel = 'import' | |
22 ..href = normalizeHtmlImportPath(filePath, library.package, library.path); | |
23 document.head.append(element); | |
24 var completer = new Completer(); | |
25 var listeners = [ | |
26 element.on['load'].listen((_) => completer.complete()), | |
27 element.on['error'].listen((_) { | |
28 print('Error loading html import from path `$filePath`'); | |
29 completer.complete(); | |
30 }), | |
31 ]; | |
32 return completer.future.then((_) { | |
33 listeners.forEach((listener) => listener.cancel()); | |
34 }); | |
35 } | |
36 } | |
OLD | NEW |