UNPKG

2.67 kBJavaScriptView Raw
1var domInsert = require("./dom-insert");
2var complain = "MARKO_DEBUG" && require("complain");
3
4function getComponentDefs(result) {
5 var componentDefs = result.___components;
6
7 if (!componentDefs) {
8 throw Error("No component");
9 }
10 return componentDefs;
11}
12
13function RenderResult(out) {
14 this.out = this.___out = out;
15 this.___components = undefined;
16}
17
18module.exports = RenderResult;
19
20var 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
66Object.defineProperty(proto, "html", {
67 get: function() {
68 // eslint-disable-next-line no-constant-condition
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
78Object.defineProperty(proto, "context", {
79 get: function() {
80 // eslint-disable-next-line no-constant-condition
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// Add all of the following DOM methods to Component.prototype:
91// - appendTo(referenceEl)
92// - replace(referenceEl)
93// - replaceChildrenOf(referenceEl)
94// - insertBefore(referenceEl)
95// - insertAfter(referenceEl)
96// - prependTo(referenceEl)
97domInsert(
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);