marko/widgets/WidgetDef.js
2017-01-02 15:53:38 -07:00

126 lines
4.5 KiB
JavaScript

'use strict';
var nextRepeatedId = require('./nextRepeatedId');
var repeatedRegExp = /\[\]$/;
var nextWidgetId = require('./util').$__nextWidgetId;
/**
* A WidgetDef is used to hold the metadata collected at runtime for
* a single widget and this information is used to instantiate the widget
* later (after the rendered HTML has been added to the DOM)
*/
function WidgetDef(id, out, widgetStack, widgetStackLen) {
this.id = id;
this.$__out = out; // The AsyncWriter that this widget is associated with
this.$__widgetStack = widgetStack;
this.$__widgetStackLen = widgetStackLen;
this.$__type = // The widget module type name that is passed to the factory
this.$__config = // Widget config object (may be null)
this.$__state = // Widget state object (may be null)
this.$__scope = // The ID of the widget that this widget is scoped within
this.$__customEvents = // An array containing information about custom events
this.$__roots = // IDs of root elements if there are multiple root elements
this.b =
this.$__existingWidget =
this.$__children = // An array of nested WidgetDef instances
this.$__domEvents = // An array of DOM events that need to be added (in sets of three)
this.$__widget = // This is used by RenderResult to reference the associated widget instance after creation
null;
this.$__nextIdIndex = 0; // The unique integer to use for the next scoped ID
}
WidgetDef.prototype = {
$__end: function() {
this.$__widgetStack.length = this.$__widgetStackLen;
},
t: function(typeName) {
this.$__type = typeName;
},
c: function(config) {
this.$__config = config;
},
/**
* Register a nested widget for this widget. We maintain a tree of widgets
* so that we can instantiate nested widgets before their parents.
*/
$__addChild: function (widgetDef) {
var children = this.$__children;
if (children) {
children.push(widgetDef);
} else {
this.$__children = [widgetDef];
}
},
/**
* This helper method generates a unique and fully qualified DOM element ID
* that is unique within the scope of the current widget. This method prefixes
* the the nestedId with the ID of the current widget. If nestedId ends
* with `[]` then it is treated as a repeated ID and we will generate
* an ID with the current index for the current nestedId.
* (e.g. "myParentId-foo[0]", "myParentId-foo[1]", etc.)
*/
elId: function (nestedId) {
if (nestedId == null) {
return this.id;
} else {
if (typeof nestedId === 'string' && repeatedRegExp.test(nestedId)) {
return nextRepeatedId(this.$__out, this.id, nestedId);
} else {
return this.id + '-' + nestedId;
}
}
},
/**
* Registers a DOM event for a nested HTML element associated with the
* widget. This is only done for non-bubbling events that require
* direct event listeners to be added.
* @param {String} type The DOM event type ("mouseover", "mousemove", etc.)
* @param {String} targetMethod The name of the method to invoke on the scoped widget
* @param {String} elId The DOM element ID of the DOM element that the event listener needs to be added too
*/
e: function(type, targetMethod, elId, extraArgs) {
if (!targetMethod) {
// The event handler method is allowed to be conditional. At render time if the target
// method is null then we do not attach any direct event listeners.
return;
}
var domEvents = this.$__domEvents;
this.$__domEvents = (domEvents || (this.$__domEvents = [])).concat([
type,
targetMethod,
elId,
extraArgs]);
},
/**
* Returns the next auto generated unique ID for a nested DOM element or nested DOM widget
*/
$__nextId: function() {
return this.id ?
this.id + '-w' + (this.$__nextIdIndex++) :
nextWidgetId(this.$__out);
}
};
WidgetDef.$__deserialize = function(o, types) {
var extra = o[4];
var typeIndex = o[1];
return {
id: o[0],
$__type: types[typeIndex],
$__roots: o[2],
$__state: o[3],
$__scope: extra.p,
$__domEvents: extra.e,
$__customEvents: extra.ce,
$__config: extra.c
};
};
module.exports = WidgetDef;