forked from jmesserly/dart-web-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathif.html
89 lines (81 loc) · 2.39 KB
/
if.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
87
88
89
<!--
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('if_component');
#import('dart:html');
#import('component.dart');
#import('watcher.dart');
#import('webcomponents.dart');
/**
* A web component implementing `<template instantiate="if ...">`.
*/
class IfComponent extends Component {
IfCondition shouldShow;
Element _childTemplate;
Element _parent;
Element _child;
String _childId;
WatcherDisposer _stopWatcher;
IfComponent(root, elem)
: super('if', root, elem);
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];
_childId = _childTemplate.id;
if (_childId != null && _childId != '') {
_childTemplate.id = '';
}
element.style.display = 'none';
element.nodes.clear();
}
void inserted() {
_stopWatcher = bind(() => shouldShow(scopedVariables), (e) {
bool showNow = e.newValue;
if (_child != null && !showNow) {
_child.remove();
_child = null;
} else if (_child == null && showNow) {
_child = _childTemplate.clone(true);
if (_childId != null && _childId != '') {
_child.id = _childId;
}
manager.expandDeclarations(_child).forEach((component) {
component.scopedVariables = scopedVariables;
component.created();
});
element.parent.nodes.add(_child);
}
});
}
void removed() {
_stopWatcher();
if (_child != null) {
_child.remove();
}
}
}
/**
* A condition whether the children of a '<template instantitate="if ...">' tag
* should be displayed or not.
*/
typedef bool IfCondition(Map<String, Dynamic> variables);
</script>
<element name="x-if" extends="template" constructor="IfComponent"
apply-author-styles>
<template>
<content></content>
</template>
</element>
</body>
</html>