1 | var domInsert = require("./dom-insert");
|
2 | var complain = "MARKO_DEBUG" && require("complain");
|
3 |
|
4 | function getComponentDefs(result) {
|
5 | var componentDefs = result.___components;
|
6 |
|
7 | if (!componentDefs) {
|
8 | throw Error("No component");
|
9 | }
|
10 | return componentDefs;
|
11 | }
|
12 |
|
13 | function RenderResult(out) {
|
14 | this.out = this.___out = out;
|
15 | this.___components = undefined;
|
16 | }
|
17 |
|
18 | module.exports = RenderResult;
|
19 |
|
20 | var proto = (RenderResult.prototype = {
|
21 | getComponent: function() {
|
22 | return this.getComponents()[0];
|
23 | },
|
24 | getComponents: function(selector) {
|
25 | if (this.___components === undefined) {
|
26 | throw Error("Not added to DOM");
|
27 | }
|
28 |
|
29 | var componentDefs = getComponentDefs(this);
|
30 |
|
31 | var components = [];
|
32 |
|
33 | componentDefs.forEach(function(componentDef) {
|
34 | var component = componentDef.___component;
|
35 | if (!selector || selector(component)) {
|
36 | components.push(component);
|
37 | }
|
38 | });
|
39 |
|
40 | return components;
|
41 | },
|
42 |
|
43 | afterInsert: function(doc) {
|
44 | var out = this.___out;
|
45 | var componentsContext = out.___components;
|
46 | if (componentsContext) {
|
47 | this.___components = componentsContext.___initComponents(doc);
|
48 | } else {
|
49 | this.___components = null;
|
50 | }
|
51 |
|
52 | return this;
|
53 | },
|
54 | getNode: function(doc) {
|
55 | return this.___out.___getNode(doc);
|
56 | },
|
57 | getOutput: function() {
|
58 | return this.___out.___getOutput();
|
59 | },
|
60 | toString: function() {
|
61 | return this.___out.toString();
|
62 | },
|
63 | document: typeof document != "undefined" && document
|
64 | });
|
65 |
|
66 | Object.defineProperty(proto, "html", {
|
67 | get: function() {
|
68 |
|
69 | if ("MARKO_DEBUG") {
|
70 | complain(
|
71 | 'The "html" property is deprecated. Please use "toString" instead.'
|
72 | );
|
73 | }
|
74 | return this.toString();
|
75 | }
|
76 | });
|
77 |
|
78 | Object.defineProperty(proto, "context", {
|
79 | get: function() {
|
80 |
|
81 | if ("MARKO_DEBUG") {
|
82 | complain(
|
83 | 'The "context" property is deprecated. Please use "out" instead.'
|
84 | );
|
85 | }
|
86 | return this.___out;
|
87 | }
|
88 | });
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | domInsert(
|
98 | proto,
|
99 | function getEl(renderResult, referenceEl) {
|
100 | return renderResult.getNode(referenceEl.ownerDocument);
|
101 | },
|
102 | function afterInsert(renderResult, referenceEl) {
|
103 | var isShadow =
|
104 | typeof ShadowRoot === "function" && referenceEl instanceof ShadowRoot;
|
105 | return renderResult.afterInsert(
|
106 | isShadow ? referenceEl : referenceEl.ownerDocument
|
107 | );
|
108 | }
|
109 | );
|