forked from jmesserly/dart-web-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.html
86 lines (79 loc) · 2.39 KB
/
list.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!--
Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
for details. All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
-->
<!doctype html>
<html lang="en">
<body>
<script data-complete="true">
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#library('list_component');
#import('dart:html');
#import('component.dart');
#import('watcher.dart');
#import('webcomponents.dart');
/**
* A web component implementing `<template iterate=...>`.
*/
class ListComponent extends Component {
Getter<List> items;
final String _loopVar;
Element _childTemplate;
Element _parent;
WatcherDisposer _stopWatcher;
ListComponent(root, elem)
: super('list', root, elem),
_loopVar = const RegExp(@"{{(.*) in .*}}").firstMatch(
elem.attributes['iterate']).group(1);
void created() {
// TODO(sigmund): support document fragments, not just a single child.
// TODO(sigmund): use logging and not assertions.
assert(element.elements.length == 1);
_childTemplate = element.elements[0];
element.nodes.clear();
_parent = element.parent;
}
void inserted() {
root.nodes.clear();
_stopWatcher = bind(items, (_) {
for (var n in _parent.elements) {
var wrapper = manager[n];
if (wrapper != this) {
if (wrapper != null) wrapper.removed();
n.remove();
}
}
for (var x in items()) {
var child = _childTemplate.clone(true);
var component = manager.expandElement(child);
// TODO(jmesserly): should support children that aren't WebComponents
component.scopedVariables = new Map.from(scopedVariables);
component.scopedVariables[_loopVar] = x;
_parent.nodes.add(child);
}
});
}
void removed() {
_stopWatcher();
for (var n in _parent.elements) {
var wrapper = manager[n];
if (wrapper != null && wrapper != this) {
wrapper.removed();
}
}
}
}
</script>
<element name="x-list" extends="template" constructor="ListComponent"
apply-author-styles>
<template>
<template iterate="i in items">
<content></content>
</template>
</template>
</element>
</body>
</html>