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

72 lines
2.0 KiB
JavaScript

'use strict';
var WidgetDef = require('./WidgetDef');
var initWidgets = require('./init-widgets');
var EMPTY_OBJECT = {};
function WidgetsContext(out, root) {
if (!root) {
root = new WidgetDef(null, out);
}
this.$__out = out;
this.$__widgetStack = [root];
this.$__preserved = EMPTY_OBJECT;
this.$__widgetsById = {};
}
WidgetsContext.prototype = {
get $__widgets() {
return this.$__widgetStack[0].$__children;
},
$__beginWidget: function(widgetId) {
var self = this;
var widgetStack = self.$__widgetStack;
var origLength = widgetStack.length;
var parent = widgetStack[origLength - 1];
if (!widgetId) {
widgetId = parent.$__nextId();
}
var widgetDef = new WidgetDef(widgetId, this.$__out, widgetStack, origLength);
this.$__widgetsById[widgetId] = widgetDef;
parent.$__addChild(widgetDef);
widgetStack.push(widgetDef);
return widgetDef;
},
$__clearWidgets: function () {
this.$__widgetStack = [new WidgetDef(null /* id */, this.$__out)];
},
$__initWidgets: function (doc) {
var widgetDefs = this.$__widgets;
if (widgetDefs) {
initWidgets.$__initClientRendered(widgetDefs, doc);
this.$__clearWidgets();
}
},
$__nextWidgetId: function() {
var widgetStack = this.$__widgetStack;
var parent = widgetStack[widgetStack.length - 1];
return parent.$__nextId();
},
$__preserveDOMNode: function(elId, bodyOnly) {
var preserved = this.$__preserved ;
if (preserved === EMPTY_OBJECT) {
preserved = this.$__preserved = {};
}
preserved[elId] = { $__bodyOnly: bodyOnly };
}
};
WidgetsContext.$__getWidgetsContext = function (out) {
var global = out.global;
return out.data.widgets ||
global.widgets ||
(global.widgets = new WidgetsContext(out));
};
module.exports = WidgetsContext;