This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
/
DomRenderer.dart
217 lines (160 loc) · 7.27 KB
/
DomRenderer.dart
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Copyright (c) 2015, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
part of mdlapplication;
/// Store strings for class names defined by this component that are used in
/// Dart. This allows us to simply change it in one place should we
/// decide to modify at a later date.
class _RendererCssClasses {
final String LOADING = 'mdl-content__loading';
final String LOADED = 'mdl-content__loaded';
const _RendererCssClasses();
}
typedef void _DomRenderFunction();
/**
* DomRenderer converts a String into HtmlNodes
*/
@di.Injectable()
class DomRenderer {
final Logger _logger = new Logger('mdlapplication.DomRenderer');
static const _RendererCssClasses _cssClasses = const _RendererCssClasses();
final List<_DomRenderFunction> _renderFunctions = new List<_DomRenderFunction>();
/// Renders the {content} String into the given {parent} - {content} must have ONE! top level element.
/// If {replaceNode} is false {content} will be added to {parent} otherwise th new
/// {content} replaces the old content
///
/// Returns the rendered child
Future<dom.Element> render(final dom.Element parent, final String content,{ final bool replaceNode: true}) {
Validate.notNull(parent);
Validate.notBlank(content);
//_logger.info("Content: $content");
final Completer completer = new Completer();
// add the render-function to the list where the "new Future" can pick it
_renderFunctions.insert(0, () {
parent.classes.remove(_cssClasses.LOADED);
parent.classes.add(_cssClasses.LOADING);
try {
final dom.Element child = new dom.Element.html(content, validator: _validator());
componentFactory().upgradeElement(child).then( (_) {
dom.window.requestAnimationFrame( (_) {
if(replaceNode && parent.childNodes.length > 0 && parent.childNodes.last != null) {
var oldElement = parent.childNodes.last;
if(oldElement is dom.Element) {
oldElement.style.display = "none";
(componentHandler().downgradeElement(oldElement));
}
oldElement.remove();
//_logger.info("Old element removed!");
}
//element.append(child);
parent.insertAdjacentElement("beforeEnd",child);
_callAttached(child);
parent.classes.remove(_cssClasses.LOADING);
parent.classes.add(_cssClasses.LOADED);
completer.complete(child);
});
});
} on Error {
_logger.shout("Invalid content:\n\t$content\n"
"Usually this error occures if content has not just ONE single root element.");
//throw e;
}
});
new Future(() {
final _DomRenderFunction renderfunction = _renderFunctions.last;
_renderFunctions.remove(renderfunction);
renderfunction();
});
return completer.future;
}
/// Renders {content} and inserts the generated node before {reference}
/// If {reference} is null the new node will be added as last child to its {parent}
Future<dom.Element> renderBefore(final dom.Element parent,final dom.Element reference, final String content) {
Validate.notNull(parent);
Validate.notBlank(content);
//_logger.info("Content: $content");
final Completer completer = new Completer();
// add the render-function to the list where the "new Future" can pick it
_renderFunctions.insert(0, () {
parent.classes.remove(_cssClasses.LOADED);
parent.classes.add(_cssClasses.LOADING);
final dom.Element child = new dom.Element.html(content,validator: _validator());
componentFactory().upgradeElement(child).then( (_) {
dom.window.requestAnimationFrame( (_) {
if(reference != null) {
parent.insertBefore(child,reference);
} else {
parent.insertAdjacentElement("beforeEnd",child);
}
_callAttached(child);
parent.classes.remove(_cssClasses.LOADING);
parent.classes.add(_cssClasses.LOADED);
completer.complete(child);
});
});
});
new Future(() {
final _DomRenderFunction renderfunction = _renderFunctions.last;
_renderFunctions.remove(renderfunction);
renderfunction();
});
return completer.future;
}
//- private -----------------------------------------------------------------------------------
dom.NodeValidator _validator() {
final dom.NodeValidator validator = new dom.NodeValidatorBuilder.common() // html5 + Templating
..allowNavigation()
..allowImages()
..allowTextElements()
..allowInlineStyles()
..allowSvg()
..add(new _AllowAllAttributesNodeValidator());
return validator;
}
/// Calls the MdlComponents attached-function for the given [element] and all it's children.
void _callAttached(final dom.Element element) {
//final Logger _logger = new Logger('mdlcore.callAttached');
if(element is dom.HtmlElement) {
var jsElement = new JsObject.fromBrowserObject(element);
if(jsElement.hasProperty(MDL_COMPONENT_PROPERTY)) {
final List<String> componentsForElement = (jsElement[MDL_COMPONENT_PROPERTY] as String).split(",");
componentsForElement.forEach((final String componentName) {
final MdlComponent component = jsElement[componentName] as MdlComponent;
component.attached();
//_logger.info("Attached ${component}");
});
}
}
// Iterate through all children
element.children.forEach( (final dom.Element child) {
_callAttached(child);
});
}
}
class _AllowAllAttributesNodeValidator implements dom.NodeValidator {
bool allowsAttribute(dom.Element element, String attributeName, String value) {
// if (attributeName == 'is' || attributeName.startsWith('on')) {
// return false;
// }
return true;
}
@override
bool allowsElement(dom.Element element) {
return true;
}
}