marko/src/runtime/RenderResult.js
Patrick Steele-Idem b25f67fda9 Code size reduction
2017-05-19 16:03:19 -06:00

80 lines
2.1 KiB
JavaScript

var domInsert = require('./dom-insert');
function getComponentDefs(result) {
var componentDefs = result.___components;
if (!componentDefs) {
throw Error('No component');
}
return componentDefs;
}
function RenderResult(out) {
this.out = this.___out = out;
this.___components = undefined;
}
module.exports = RenderResult;
var proto = RenderResult.prototype = {
getComponent: function() {
return this.getComponents()[0];
},
getComponents: function(selector) {
if (this.___components === undefined) {
throw Error('Not added to DOM');
}
var componentDefs = getComponentDefs(this);
var components = [];
componentDefs.forEach(function(componentDef) {
var component = componentDef.___component;
if (!selector || selector(component)) {
components.push(component);
}
});
return components;
},
afterInsert: function(doc) {
var out = this.___out;
var globalComponentsContext = out.global.___components;
if (globalComponentsContext) {
this.___components = globalComponentsContext.___initComponents(doc);
} else {
this.___components = null;
}
return this;
},
getNode: function(doc) {
return this.___out.___getNode(doc);
},
getOutput: function() {
return this.___out.___getOutput();
},
toString: function() {
return this.___out.toString();
},
document: typeof document != 'undefined' && document
};
// Add all of the following DOM methods to Component.prototype:
// - appendTo(referenceEl)
// - replace(referenceEl)
// - replaceChildrenOf(referenceEl)
// - insertBefore(referenceEl)
// - insertAfter(referenceEl)
// - prependTo(referenceEl)
domInsert(
proto,
function getEl(renderResult, referenceEl) {
return renderResult.getNode(referenceEl.ownerDocument);
},
function afterInsert(renderResult, referenceEl) {
return renderResult.afterInsert(referenceEl.ownerDocument);
});