Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 // TODO(jmesserly): more commentary here. | |
| 6 /** | |
| 7 * This library provides access to Model-Driven-Views APIs on HTML elements. | |
| 8 * More information can be found at: <https://github.com/toolkitchen/mdv>. | |
| 9 */ | |
| 10 library mdv; | |
| 11 | |
| 12 import 'dart:async'; | |
| 13 import 'dart:collection'; | |
| 14 import 'dart:html'; | |
| 15 import 'dart:math' as math; | |
| 16 import 'package:observe/observe.dart'; | |
| 17 | |
| 18 part 'src/bindings.dart'; | |
| 19 part 'src/element.dart'; | |
| 20 part 'src/input_element.dart'; | |
| 21 part 'src/node.dart'; | |
| 22 part 'src/template.dart'; | |
| 23 part 'src/text.dart'; | |
| 24 | |
| 25 /** Initialize the Model-Driven Views polyfill. */ | |
| 26 void initialize() { | |
| 27 mdvPackage = _mdv; | |
| 28 } | |
| 29 | |
| 30 StreamController<DocumentFragment> _instanceCreated; | |
| 31 | |
| 32 /** | |
| 33 * *Warning*: This is an implementation helper for Model-Driven Views and | |
| 34 * should not be used in your code. | |
| 35 * | |
| 36 * This event is fired whenever a template is instantiated via | |
| 37 * [Element.createInstance]. | |
| 38 */ | |
| 39 // TODO(rafaelw): This is a hack, and is neccesary for the polyfill | |
| 40 // because custom elements are not upgraded during clone() | |
| 41 Stream<DocumentFragment> get instanceCreated { | |
| 42 if (_instanceCreated == null) { | |
| 43 _instanceCreated = | |
| 44 new StreamController<DocumentFragment>(sync: true); | |
| 45 } | |
| 46 return _instanceCreated.stream; | |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Binds all mustaches recursively starting from the [root] node. | |
| 51 * | |
| 52 * Note: this is not an official Model-Driven-Views API; it is intended to | |
| 53 * support binding the [ShadowRoot]'s content to a model. | |
| 54 */ | |
| 55 // TODO(jmesserly): this is needed to avoid two <template> nodes when using | |
| 56 // bindings in a custom element's template. See also: | |
| 57 // https://github.com/polymer-project/polymer/blob/master/src/bindMDV.js#L68 | |
| 58 // Called from: | |
| 59 // https://github.com/polymer-project/polymer/blob/master/src/register.js#L99 | |
| 60 void bindModel(Node root, model, [CustomBindingSyntax syntax]) { | |
| 61 _Bindings._addBindings(root, model, syntax); | |
| 62 } | |
| 63 | |
| 64 | |
| 65 // TODO(jmesserly): investigate if expandos give us enough performance. | |
| 66 | |
| 67 // The expando for storing our MDV wrappers. | |
| 68 // | |
| 69 // In general, we need state associated with the nodes. Rather than having a | |
| 70 // bunch of individual expandos, we keep one per node. | |
| 71 // | |
| 72 // Aside from the potentially helping performance, it also keeps things simpler | |
| 73 // if we decide to integrate MDV into the DOM later, and means less code needs | |
| 74 // to worry about expandos. | |
| 75 final Expando _mdvExpando = new Expando('mdv'); | |
| 76 | |
| 77 _mdv(node) { | |
| 78 var wrapper = _mdvExpando[node]; | |
| 79 if (wrapper != null) return wrapper; | |
| 80 | |
| 81 // TODO(jmesserly): is using nodeType and localName faster than "is"? | |
|
floitsch
2013/06/26 18:06:39
`is` should be fast. Afaik dart2js compiles it to
Jennifer Messerly
2013/06/26 23:09:12
thanks for the info! removed TODO.
| |
| 82 if (node is InputElement) { | |
| 83 wrapper = new _InputElementExtension(node); | |
| 84 } else if (node is Element) { | |
| 85 if (node.isTemplate) { | |
| 86 wrapper = new _TemplateExtension(node); | |
| 87 } else { | |
| 88 wrapper = new _ElementExtension(node); | |
| 89 } | |
| 90 } else if (node is Text) { | |
| 91 wrapper = new _TextExtension(node); | |
| 92 } else if (node is Node) { | |
| 93 wrapper = new _NodeExtension(node); | |
| 94 } else { | |
| 95 // TODO(jmesserly): this happens for things like CompoundBinding. | |
| 96 wrapper = node; | |
| 97 } | |
| 98 | |
| 99 _mdvExpando[node] = wrapper; | |
| 100 return wrapper; | |
| 101 } | |
| OLD | NEW |