marko/components/util-browser.js
2017-02-20 16:30:16 -07:00

109 lines
2.9 KiB
JavaScript

var componentLookup = {};
var defaultDocument = document;
function getComponentForEl(el, doc) {
if (el) {
var node = typeof el === 'string' ? (doc || defaultDocument).getElementById(el) : el;
if (node) {
var component = node._w;
while(component) {
var rootFor = component.$__rootFor;
if (rootFor) {
component = rootFor;
} else {
break;
}
}
return component;
}
}
}
var lifecycleEventMethods = {};
[
'create',
'render',
'update',
'mount',
'destroy',
].forEach(function(eventName) {
lifecycleEventMethods[eventName] = 'on' + eventName[0].toUpperCase() + eventName.substring(1);
});
/**
* This method handles invoking a component's event handler method
* (if present) while also emitting the event through
* the standard EventEmitter.prototype.emit method.
*
* Special events and their corresponding handler methods
* include the following:
*
* beforeDestroy --> onBeforeDestroy
* destroy --> onDestroy
* beforeUpdate --> onBeforeUpdate
* update --> onUpdate
* render --> onRender
*/
function emitLifecycleEvent(component, eventType, eventArg1, eventArg2) {
var listenerMethod = component[lifecycleEventMethods[eventType]];
if (listenerMethod) {
listenerMethod.call(component, eventArg1, eventArg2);
}
component.emit(eventType, eventArg1, eventArg2);
}
function destroyComponentForEl(el) {
var componentToDestroy = el._w;
if (componentToDestroy) {
componentToDestroy.$__destroyShallow();
el._w = null;
while ((componentToDestroy = componentToDestroy.$__rootFor)) {
componentToDestroy.$__rootFor = null;
componentToDestroy.$__destroyShallow();
}
}
}
function destroyElRecursive(el) {
var curChild = el.firstChild;
while(curChild) {
if (curChild.nodeType == 1) {
destroyComponentForEl(curChild);
destroyElRecursive(curChild);
}
curChild = curChild.nextSibling;
}
}
var nextUniqueId = 0;
function nextComponentId() {
return 'wc' + (nextUniqueId++);
}
function getElementById(doc, id) {
return doc.getElementById(id);
}
function attachBubblingEvent(componentDef, handlerMethodName, extraArgs) {
if (handlerMethodName) {
return extraArgs ?
[handlerMethodName, componentDef.id, extraArgs] :
[handlerMethodName, componentDef.id];
}
}
exports.$__componentLookup = componentLookup;
exports.$__getComponentForEl = getComponentForEl;
exports.$__emitLifecycleEvent = emitLifecycleEvent;
exports.$__destroyComponentForEl = destroyComponentForEl;
exports.$__destroyElRecursive = destroyElRecursive;
exports.$__nextComponentId = nextComponentId;
exports.$__getElementById = getElementById;
exports.$__attachBubblingEvent = attachBubblingEvent;