-
Notifications
You must be signed in to change notification settings - Fork 1
/
dom-else.html
189 lines (168 loc) · 5.8 KB
/
dom-else.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
This tag is based on and contains much of the original code on the dom-if template tag.
Thus I include the original license from that file.
-->
<!-- Imports polymer -->
<link rel="import" href="../polymer/polymer.html">
<!-- Registers custom element -->
<script>
/**
* Stamps the template iff the `if` property of the parent dom-if template is falsey.
*
* When `if` becomes truthy, the stamped content is hidden but not
* removed from dom. When `if` subsequently becomes falsey again, the content
* is simply re-shown. This approach is used due to its favorable performance
* characteristics: the expense of creating template content is paid only
* once and lazily.
*
* Set the `restamp` property to true to force the stamped content to be
* created / destroyed when the `if` condition changes.
*/
Polymer({
is: 'dom-else',
extends: 'template',
/**
* Fired whenever DOM is added or removed/hidden by this template (by
* default, rendering occurs lazily). To force immediate rendering, call
* `render`.
*
* @event dom-change
*/
properties: {
/**
* When true, elements will be removed from DOM and discarded when `if`
* becomes false and re-created and added back to the DOM when `if`
* becomes true. By default, stamped elements will be hidden but left
* in the DOM when `if` becomes false, which is generally results
* in better performance.
*/
"restamp": {
type: Boolean,
value: false,
observer: '_queueRender'
},
"if": {
type: Boolean,
value: true,
notify: true,
observer: '_elseChange'
},
},
"_else": false,
behaviors: [
Polymer.Templatizer
],
_elseChange: function(c) {
this._else = !c;
this._queueRender();
},
_queueRender: function() {
this._debounceTemplate(this._render);
},
detached: function() {
this._teardownInstance();
},
attached: function() {
this._parent = this.dataHost;
this._parent._template.addEventListener('dom-change',this._toggleIf.bind(this));
if (this._else && this.ctor) {
this.async(this._ensureInstance);
}
},
/**
* Forces the element to render its content. Normally rendering is
* asynchronous to a provoking change. This is done for efficiency so
* that multiple changes trigger only a single render. The render method
* should be called if, for example, template rendering is required to
* validate application state.
*/
render: function() {
this._flushTemplates();
},
_render: function() {
if (this._else) {
if (!this.ctor) {
this._wrapTextNodes(this._content || this.content);
this.templatize(this);
}
this._ensureInstance();
this._showHideChildren();
} else if (this.restamp) {
this._teardownInstance();
}
if (!this.restamp && this._instance) {
this._showHideChildren();
}
if (this._else != this._lastIf) {
//this.fire('dom-change');
this._lastIf = this._else;
}
},
_wrapTextNodes: function (root) {
for (var n = root.firstChild.nextSibling; n; n = n.nextSibling) {
if (n.nodeType === Node.TEXT_NODE && n.textContent.trim()) {
var s = document.createElement('span');
root.insertBefore(s, n);
s.appendChild(n);
n = s;
}
}
},
_toggleIf: function() {
this.set('if',this._parent._template.get('if'));
},
_ensureInstance: function() {
if (!this._instance) {
//parent = parent.parentNode;
if (!this._parent._template || (this._parent._template.hasAttribute('is')&&this._parent._template.is!=='dom-if')) return console.err('Must be a direct child of dom-if');
this._instance = this.stamp();
var parent=this._parent._template.parentNode;
var root = this._instance.root;
parent.insertBefore(root, this._parent._template);
}
},
_teardownInstance: function() {
if (this._instance) {
var c = this._instance._children;
this._parent.removeEventListener('dom-change',this._toggleIf);
if (c) {
// use first child parent, for case when dom-if may have been detached
var parent = Polymer.dom(Polymer.dom(c[0]).parentNode);
c.forEach(function(n) {
parent.removeChild(n);
});
}
this._instance = null;
}
},
_showHideChildren: function() {
var hidden = !this._else;
if (this._instance) {
this._instance._showHideChildren(hidden);
}
},
// Implements extension point from Templatizer mixin
// Called as side-effect of a host property change, responsible for
// notifying parent.<prop> path change on instance
_forwardParentProp: function(prop, value) {
if (this._instance) {
this._instance[prop] = value;
}
},
// Implements extension point from Templatizer
// Called as side-effect of a host path change, responsible for
// notifying parent.<path> path change on each row
_forwardParentPath: function(path, value) {
if (this._instance) {
this._instance.notifyPath(path, value, true);
}
}
});
</script>