Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(583)

Side by Side Diff: pkg/polymer/lib/polymer.dart

Issue 23224003: move polymer.dart into dart svn (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: add --deploy to todomvc sample Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/polymer/lib/observe_html.dart ('k') | pkg/polymer/lib/polymer_element.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, 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 /**
6 * This library exports all of the commonly used functions and types for
7 * building UI's.
8 *
9 * See this article for more information:
10 * <http://www.dartlang.org/articles/dart-web-components/>.
11 */
12 library polymer;
13
14 import 'dart:async';
15 import 'dart:mirrors';
16
17 import 'package:mdv/mdv.dart' as mdv;
18 import 'package:observe/src/microtask.dart';
19 import 'package:path/path.dart' as path;
20 import 'polymer_element.dart' show registerPolymerElement;
21
22 export 'package:custom_element/custom_element.dart';
23 export 'package:observe/observe.dart';
24 export 'package:observe/src/microtask.dart';
25
26 export 'observe.dart';
27 export 'observe_html.dart';
28 export 'polymer_element.dart';
29 export 'safe_html.dart';
30
31
32 /** Annotation used to automatically register polymer elements. */
33 class CustomTag {
34 final String tagName;
35 const CustomTag(this.tagName);
36 }
37
38 /**
39 * Metadata used to label static or top-level methods that are called
40 * automatically when loading the library of a custom element.
41 */
42 const polymerInitMethod = const _InitPolymerAnnotation();
43
44 /**
45 * Initializes a polymer application by: setting up polling for observable
46 * changes, initializing MDV, registering and initializing custom elements from
47 * each library in [elementLibraries], and finally invoking [userMain].
48 *
49 * There are two mechanisms by which custom elements can be initialized:
50 * annotating the class that declares a custom element with [CustomTag] or
51 * programatically registering the element in a static or top-level function and
52 * annotating that function with [polymerInitMethod].
53 *
54 * The urls in [elementLibraries] can be absolute or relative to [srcUrl].
55 */
56 void initPolymer(List<String> elementLibraries, void userMain(), [String srcUrl] ) {
57 wrapMicrotask(() {
58 // DOM events don't yet go through microtasks, so we catch those here.
59 new Timer.periodic(new Duration(milliseconds: 125),
60 (_) => performMicrotaskCheckpoint());
61
62 // TODO(jmesserly): mdv should use initMdv instead of mdv.initialize.
63 mdv.initialize();
64 for (var lib in elementLibraries) {
65 _registerPolymerElementsOf(lib, srcUrl);
66 }
67 userMain();
68 })();
69 }
70
71 /** All libraries in the current isolate. */
72 final _libs = currentMirrorSystem().libraries;
73
74 /**
75 * Reads the library at [uriString] (which can be an absolute URI or a relative
76 * URI from [srcUrl]), and:
77 *
78 * * Invokes top-level and static functions marked with the
79 * [polymerInitMethod] annotation.
80 *
81 * * Registers any [PolymerElement] that is marked with the [CustomTag]
82 * annotation.
83 */
84 void _registerPolymerElementsOf(String uriString, [String srcUrl]) {
85 var uri = Uri.parse(uriString);
86 if (uri.scheme == '' && srcUrl != null) {
87 uri = Uri.parse(path.normalize(path.join(path.dirname(srcUrl), uriString)));
88 }
89 var lib = _libs[uri];
90 if (lib == null) {
91 print('warning: $uri library not found');
92 return;
93 }
94
95 // Search top-level functions marked with @polymerInitMethod
96 for (var f in lib.functions.values) {
97 _maybeInvoke(lib, f);
98 }
99
100 for (var c in lib.classes.values) {
101 // Search for @CustomTag on classes
102 for (var m in c.metadata) {
103 var meta = m.reflectee;
104 if (meta is CustomTag) {
105 registerPolymerElement(meta.tagName,
106 () => c.newInstance(const Symbol(''), const []).reflectee);
107 }
108 }
109
110 // TODO(sigmund): check also static methods marked with @polymerInitMethod.
111 // This is blocked on two bugs:
112 // - dartbug.com/12133 (static methods are incorrectly listed as top-level
113 // in dart2js, so they end up being called twice)
114 // - dartbug.com/12134 (sometimes "method.metadata" throws an exception,
115 // we could wrap and hide those exceptions, but it's not ideal).
116 }
117 }
118
119 void _maybeInvoke(ObjectMirror obj, MethodMirror method) {
120 var annotationFound = false;
121 for (var meta in method.metadata) {
122 if (identical(meta.reflectee, polymerInitMethod)) {
123 annotationFound = true;
124 break;
125 }
126 }
127 if (!annotationFound) return;
128 if (!method.isStatic) {
129 print("warning: methods marked with @polymerInitMethod should be static,"
130 " ${method.simpleName} is not.");
131 return;
132 }
133 if (!method.parameters.where((p) => !p.isOptional).isEmpty) {
134 print("warning: methods marked with @polymerInitMethod should take no "
135 "arguments, ${method.simpleName} expects some.");
136 return;
137 }
138 obj.invoke(method.simpleName, const []);
139 }
140
141 class _InitPolymerAnnotation {
142 const _InitPolymerAnnotation();
143 }
OLDNEW
« no previous file with comments | « pkg/polymer/lib/observe_html.dart ('k') | pkg/polymer/lib/polymer_element.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698