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

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

Issue 34453003: Rename mdv -> template_binding, remove experiemntal apis from dart:html (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 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/template_binding/lib/src/text_area_element.dart ('k') | pkg/template_binding/pubspec.yaml » ('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) 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 /**
6 * This library provides access to the Polymer project's
7 * [Data Binding](http://www.polymer-project.org/docs/polymer/databinding.html)
8 * Find more information at the
9 * [Polymer.dart homepage](https://www.dartlang.org/polymer-dart/).
10 *
11 * Extends the capabilities of the HTML Template Element by enabling it to
12 * create, manage, and remove instances of content bound to data defined in
13 * Dart.
14 *
15 * Node.bind() is a new method added to all DOM nodes which instructs them to
16 * bind the named property to the data provided. These allows applications to
17 * create a data model in Dart or JavaScript that DOM reacts to.
18 */
19 library template_binding;
20
21 import 'dart:async';
22 import 'dart:collection';
23 import 'dart:html';
24 import 'dart:svg' show SvgSvgElement;
25 import 'package:observe/observe.dart';
26
27 import 'src/list_diff.dart' show calculateSplices, ListChangeDelta;
28
29 part 'src/element.dart';
30 part 'src/input_bindings.dart';
31 part 'src/input_element.dart';
32 part 'src/node.dart';
33 part 'src/select_element.dart';
34 part 'src/template.dart';
35 part 'src/template_iterator.dart';
36 part 'src/text.dart';
37 part 'src/text_area_element.dart';
38
39 // TODO(jmesserly): ideally we would split TemplateBinding and Node.bind into
40 // two packages, but this is not easy when we are faking extension methods.
41 // Since TemplateElement needs to override Node.bind, it seems like the
42 // Node.bind layer must have some innate knowledge of TemplateBinding.
43
44 /**
45 * Provides access to the data binding APIs for the [node]. For example:
46 *
47 * templateBind(node).model = new MyModel();
48 *
49 * This is equivalent to [nodeBind], but provides access to
50 * [TemplateBindExtension] APIs. [node] should be a [TemplateElement], or
51 * equivalent semantic template such as:
52 *
53 * <table template repeat="{{row in rows}}">
54 * <tr template repeat="{{item in row}}">
55 * <td>{{item}}</td>
56 * </tr>
57 * </table>
58 */
59 TemplateBindExtension templateBind(Element node) => nodeBind(node);
60
61 /**
62 * Like [templateBind], but intended to be used only within a custom element
63 * that implements [TemplateBindExtension]. This method can be used to simulate
64 * a super call. For example:
65 *
66 * class CoolTemplate extends TemplateElement
67 * implements TemplateBindExtension {
68 *
69 * createInstance(model, delegate) {
70 * // do something cool...
71 * // otherwise, fall back to superclass
72 * return templateBindFallback(this).createInstance(model, delegate);
73 * }
74 * ...
75 * }
76 */
77 TemplateBindExtension templateBindFallback(Element node) =>
78 nodeBindFallback(node);
79
80 /**
81 * Provides access to the data binding APIs for the [node]. For example:
82 *
83 * nodeBind(node).bind('checked', model, 'path.to.some.value');
84 */
85 NodeBindExtension nodeBind(Node node) {
86 return node is NodeBindExtension ? node : nodeBindFallback(node);
87 }
88
89 /**
90 * Like [nodeBind], but intended to be used only within a custom element that
91 * implements [NodeBindExtension]. This method can be used to simulate a super
92 * call. For example:
93 *
94 * class FancyButton extends ButtonElement implements NodeBindExtension {
95 * bind(name, model, path) {
96 * if (name == 'fancy-prop') ... // do fancy binding
97 * // otherwise, fall back to superclass
98 * return nodeBindFallback(this).bind(name, model, path);
99 * }
100 * ...
101 * }
102 */
103 NodeBindExtension nodeBindFallback(Node node) {
104 var extension = _expando[node];
105 if (extension != null) return extension;
106
107 if (node is InputElement) {
108 extension = new _InputElementExtension(node);
109 } else if (node is SelectElement) {
110 extension = new _SelectElementExtension(node);
111 } else if (node is TextAreaElement) {
112 extension = new _TextAreaElementExtension(node);
113 } else if (node is Element) {
114 if (isSemanticTemplate(node)) {
115 extension = new TemplateBindExtension._(node);
116 } else {
117 extension = new _ElementExtension(node);
118 }
119 } else if (node is Text) {
120 extension = new _TextExtension(node);
121 } else {
122 extension = new NodeBindExtension._(node);
123 }
124
125 _expando[node] = extension;
126 return extension;
127 }
128
129
130 bool _isAttributeTemplate(Element n) => n.attributes.containsKey('template') &&
131 _SEMANTIC_TEMPLATE_TAGS.containsKey(n.localName);
132
133 /**
134 * Returns true if this node is semantically a template.
135 *
136 * A node is a template if [tagName] is TEMPLATE, or the node has the
137 * 'template' attribute and this tag supports attribute form for backwards
138 * compatibility with existing HTML parsers. The nodes that can use attribute
139 * form are table elments (THEAD, TBODY, TFOOT, TH, TR, TD, CAPTION, COLGROUP
140 * and COL), OPTION, and OPTGROUP.
141 */
142 bool isSemanticTemplate(Node n) => n is Element &&
143 ((n as Element).localName == 'template' || _isAttributeTemplate(n));
144
145 // TODO(jmesserly): const set would be better
146 const _SEMANTIC_TEMPLATE_TAGS = const {
147 'caption': null,
148 'col': null,
149 'colgroup': null,
150 'option': null,
151 'optgroup': null,
152 'tbody': null,
153 'td': null,
154 'tfoot': null,
155 'th': null,
156 'thead': null,
157 'tr': null,
158 };
159
160
161 // TODO(jmesserly): investigate if expandos give us enough performance.
162
163 // The expando for storing our MDV extensions.
164 //
165 // In general, we need state associated with the nodes. Rather than having a
166 // bunch of individual expandos, we keep one per node.
167 //
168 // Aside from the potentially helping performance, it also keeps things simpler
169 // if we decide to integrate MDV into the DOM later, and means less code needs
170 // to worry about expandos.
171 final Expando _expando = new Expando('template_binding');
OLDNEW
« no previous file with comments | « pkg/template_binding/lib/src/text_area_element.dart ('k') | pkg/template_binding/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698