mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Code size reduction
This commit is contained in:
parent
cbd95df50e
commit
2277d97c44
@ -131,5 +131,9 @@
|
||||
"version": "4.0.0-beta.4",
|
||||
"logo": {
|
||||
"url": "https://raw.githubusercontent.com/marko-js/branding/master/marko-logo-small.png"
|
||||
},
|
||||
"minprops": {
|
||||
"exclude": ["c", "ca", "e", "n", "r", "sa", "t"],
|
||||
"matchPrefix": "$__"
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,16 +1,17 @@
|
||||
var events = require('./events');
|
||||
var domInsert = require('./dom-insert');
|
||||
var EMPTY_ARRAY = [];
|
||||
|
||||
function checkAddedToDOM(result, method) {
|
||||
if (!result.$__added) {
|
||||
if (!result.$__widgets) {
|
||||
throw new Error('Cannot call ' + method + '() until after HTML fragment is added to DOM.');
|
||||
}
|
||||
}
|
||||
|
||||
function getWidgetDefs(result) {
|
||||
var widgetDefs = result.$__out.data.widgets;
|
||||
var widgetDefs = result.$__widgets;
|
||||
|
||||
if (!widgetDefs || widgetDefs.length === 0) {
|
||||
if (widgetDefs.length === 0) {
|
||||
throw new Error('No widget rendered');
|
||||
}
|
||||
return widgetDefs;
|
||||
@ -18,7 +19,7 @@ function getWidgetDefs(result) {
|
||||
|
||||
function RenderResult(out) {
|
||||
this.out = this.$__out = out;
|
||||
this.$__added = false;
|
||||
this.$__widgets = null;
|
||||
}
|
||||
|
||||
module.exports = RenderResult;
|
||||
@ -62,14 +63,9 @@ var proto = RenderResult.prototype = {
|
||||
},
|
||||
|
||||
afterInsert: function(doc) {
|
||||
this.$__added = true;
|
||||
|
||||
var data = this.$__out.data;
|
||||
|
||||
var widgetsContext = this.$__out.global.widgets;
|
||||
var widgetDefs = widgetsContext ? widgetsContext.widgets : null;
|
||||
|
||||
data.widgets = widgetDefs;
|
||||
var out = this.$__out;
|
||||
var widgetsContext = out.global.widgets;
|
||||
this.$__widgets = (widgetsContext && widgetsContext.$__widgets) || EMPTY_ARRAY;
|
||||
|
||||
events.emit('mountNode', {
|
||||
result: this,
|
||||
|
||||
@ -1,9 +1,5 @@
|
||||
{
|
||||
"browser": {
|
||||
"./loader/index.js": "./loader/index-browser.js"
|
||||
},
|
||||
"minprops": {
|
||||
"exclude": ["c", "ca", "e", "n", "sa", "t"],
|
||||
"matchPrefix": "$__"
|
||||
}
|
||||
}
|
||||
@ -5,7 +5,7 @@ module.exports = function(helpers) {
|
||||
includeWidget: false
|
||||
});
|
||||
|
||||
expect(widget).to.equal(undefined);
|
||||
expect(widget == null).to.equal(true);
|
||||
|
||||
expect(helpers.targetEl.innerHTML).contain('[app-conditional-widget]');
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ module.exports = function(helpers) {
|
||||
includeWidget: false
|
||||
});
|
||||
|
||||
expect(widget).to.equal(undefined);
|
||||
expect(widget == null).to.equal(true);
|
||||
|
||||
expect(helpers.targetEl.innerHTML).contain('[app-conditional-widget]');
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ var marko_template = module.exports = require("marko/html").t(__filename),
|
||||
marko_attr = marko_helpers.a;
|
||||
|
||||
function render(data, out, widget, state) {
|
||||
widget.type = marko_widgetTypes[data.isMobile ? "default" : "mobile"];
|
||||
widget.t(marko_widgetTypes[data.isMobile ? "default" : "mobile"]);
|
||||
|
||||
out.w("<div" +
|
||||
marko_attr("id", widget.id) +
|
||||
|
||||
@ -5,7 +5,7 @@ script marko-init
|
||||
|
||||
function componentsDataProvider(callback) {
|
||||
componentsTemplate.renderToString({}, function(err, html, out) {
|
||||
var widgetIds = markoWidgets.getRenderedWidgetIds(out);
|
||||
var widgetIds = markoWidgets.getRenderedWidgets(out);
|
||||
|
||||
// Serialize the HTML and the widget IDs to the browser
|
||||
callback(null, {
|
||||
|
||||
@ -123,12 +123,12 @@ function resetWidget(widget) {
|
||||
|
||||
function hasCompatibleWidget(widgetsContext, existingWidget) {
|
||||
var id = existingWidget.id;
|
||||
var newWidgetDef = widgetsContext.getWidget(id);
|
||||
var newWidgetDef = widgetsContext.$__widgetsById[id];
|
||||
if (!newWidgetDef) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return existingWidget.$__type === newWidgetDef.type;
|
||||
return existingWidget.$__type === newWidgetDef.$__type;
|
||||
}
|
||||
|
||||
function handleCustomEventWithMethodListener(widget, targetMethodName, args, extraArgs) {
|
||||
@ -546,7 +546,7 @@ Widget.prototype = widgetProto = {
|
||||
}
|
||||
|
||||
if (widgetsContext && id) {
|
||||
var preserved = widgetsContext.preserved[id];
|
||||
var preserved = widgetsContext.$__preserved[id];
|
||||
|
||||
if (preserved && !preserved.bodyOnly) {
|
||||
if (preserved.bodyEl) {
|
||||
@ -577,7 +577,7 @@ Widget.prototype = widgetProto = {
|
||||
function onBeforeElChildrenUpdated(el) {
|
||||
var id = el.id;
|
||||
if (widgetsContext && id) {
|
||||
var preserved = widgetsContext.preserved[id];
|
||||
var preserved = widgetsContext.$__preserved[id];
|
||||
if (preserved && preserved.bodyOnly) {
|
||||
// Don't morph the children since they are preserved
|
||||
return MORPHDOM_SKIP;
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
var repeatedId = require('./repeated-id');
|
||||
var repeatedRegExp = /\[\]$/;
|
||||
var uniqueId = require('./uniqueId');
|
||||
|
||||
/**
|
||||
* A WidgetDef is used to hold the metadata collected at runtime for
|
||||
@ -8,35 +9,45 @@ var repeatedRegExp = /\[\]$/;
|
||||
* later (after the rendered HTML has been added to the DOM)
|
||||
*/
|
||||
function WidgetDef(config, endFunc, out) {
|
||||
this.type = config.type; // The widget module type name that is passed to the factory
|
||||
this.$__type = config.$__type; // The widget module type name that is passed to the factory
|
||||
this.id = config.id; // The unique ID of the widget
|
||||
this.config = config.config; // Widget config object (may be null)
|
||||
this.state = config.state; // Widget state object (may be null)
|
||||
this.scope = config.scope; // The ID of the widget that this widget is scoped within
|
||||
this.customEvents = config.customEvents; // An array containing information about custom events
|
||||
this.$__config = config.$__config; // Widget config object (may be null)
|
||||
this.$__state = config.$__state; // Widget state object (may be null)
|
||||
this.$__scope = config.$__scope; // The ID of the widget that this widget is scoped within
|
||||
this.$__customEvents = config.$__customEvents; // An array containing information about custom events
|
||||
this.bodyElId = config.bodyElId; // The ID for the default body element (if any any)
|
||||
this.roots = config.roots;
|
||||
this.$__roots = config.$__roots;
|
||||
this.body = config.body;
|
||||
this.$__existingWidget = config.$__existingWidget;
|
||||
|
||||
this.children = null; // An array of nested WidgetDef instances
|
||||
this.end = endFunc; // A function that when called will pop this widget def off the stack
|
||||
this.domEvents = null; // An array of DOM events that need to be added (in sets of three)
|
||||
this.out = out; // The AsyncWriter that this widget is associated with
|
||||
this._nextId = 0; // The unique integer to use for the next scoped ID
|
||||
this.$__children = null; // An array of nested WidgetDef instances
|
||||
this.$__end = endFunc; // A function that when called will pop this widget def off the stack
|
||||
this.$__domEvents = null; // An array of DOM events that need to be added (in sets of three)
|
||||
this.$__out = out; // The AsyncWriter that this widget is associated with
|
||||
this.$__nextIdIndex = 0; // The unique integer to use for the next scoped ID
|
||||
this.widget = null; // This ised by RenderResult to reference the associated widget instance after creation
|
||||
}
|
||||
|
||||
WidgetDef.prototype = {
|
||||
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;
|
||||
$__addChild: function (widgetDef) {
|
||||
var children = this.$__children;
|
||||
|
||||
if (children) {
|
||||
children.push(widgetDef);
|
||||
} else {
|
||||
this.children = [widgetDef];
|
||||
this.$__children = [widgetDef];
|
||||
}
|
||||
},
|
||||
/**
|
||||
@ -52,7 +63,7 @@ WidgetDef.prototype = {
|
||||
return this.id;
|
||||
} else {
|
||||
if (typeof nestedId === 'string' && repeatedRegExp.test(nestedId)) {
|
||||
return repeatedId.$__nextId(this.out, this.id, nestedId);
|
||||
return repeatedId.$__nextId(this.$__out, this.id, nestedId);
|
||||
} else {
|
||||
return this.id + '-' + nestedId;
|
||||
}
|
||||
@ -66,47 +77,39 @@ WidgetDef.prototype = {
|
||||
* @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
|
||||
*/
|
||||
addDomEvent: function(type, targetMethod, elId, extraArgs) {
|
||||
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;
|
||||
}
|
||||
|
||||
if (!this.domEvents) {
|
||||
this.domEvents = [];
|
||||
}
|
||||
this.domEvents.push(type);
|
||||
this.domEvents.push(targetMethod);
|
||||
this.domEvents.push(elId);
|
||||
this.domEvents.push(extraArgs);
|
||||
},
|
||||
/**
|
||||
* Returns a string representation of the DOM events data.
|
||||
*/
|
||||
getDomEventsAttr: function() {
|
||||
if (this.domEvents) {
|
||||
return this.domEvents.join(',');
|
||||
if (!this.$__domEvents) {
|
||||
this.$__domEvents = [];
|
||||
}
|
||||
this.$__domEvents.push(type);
|
||||
this.$__domEvents.push(targetMethod);
|
||||
this.$__domEvents.push(elId);
|
||||
this.$__domEvents.push(extraArgs);
|
||||
},
|
||||
/**
|
||||
* Returns the next auto generated unique ID for a nested DOM element or nested DOM widget
|
||||
*/
|
||||
nextId: function() {
|
||||
return this.id + '-w' + (this._nextId++);
|
||||
$__nextId: function() {
|
||||
return this.id ? this.id + '-w' + (this.$__nextIdIndex++) : uniqueId(this.$__out);
|
||||
},
|
||||
|
||||
toJSON: function() {
|
||||
$__toJSON: function() {
|
||||
return {
|
||||
type: this.type,
|
||||
$__type: this.$__type,
|
||||
id: this.id,
|
||||
config: this.config,
|
||||
state: this.state,
|
||||
scope: this.scope,
|
||||
domEvents: this.domEvents,
|
||||
customEvents: this.customEvents,
|
||||
$__config: this.$__config,
|
||||
$__state: this.$__state,
|
||||
$__scope: this.$__scope,
|
||||
$__domEvents: this.$__domEvents,
|
||||
$__customEvents: this.$__customEvents,
|
||||
bodyElId: this.bodyElId,
|
||||
roots: this.roots
|
||||
$__roots: this.$__roots
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,95 +1,73 @@
|
||||
'use strict';
|
||||
|
||||
var WidgetDef = require('./WidgetDef');
|
||||
var uniqueId = require('./uniqueId');
|
||||
var initWidgets = require('./init-widgets');
|
||||
var EMPTY_OBJECT = {};
|
||||
|
||||
function WidgetsContext(out) {
|
||||
this.out = out;
|
||||
this.widgets = [];
|
||||
this.widgetStack = [];
|
||||
this.preserved = EMPTY_OBJECT;
|
||||
this.reusableWidgets = null;
|
||||
this.reusableWidgetsById = null;
|
||||
this.widgetsById = {};
|
||||
function WidgetsContext(out, root) {
|
||||
if (!root) {
|
||||
root = new WidgetDef(EMPTY_OBJECT, null, out);
|
||||
}
|
||||
|
||||
this.$__out = out;
|
||||
this.$__widgetStack = [root];
|
||||
this.$__preserved = EMPTY_OBJECT;
|
||||
this.$__widgetsById = {};
|
||||
}
|
||||
|
||||
WidgetsContext.prototype = {
|
||||
getWidgets: function () {
|
||||
return this.widgets;
|
||||
get $__widgets() {
|
||||
return this.$__widgetStack[0].$__children;
|
||||
},
|
||||
|
||||
getWidgetStack: function() {
|
||||
return this.widgetStack;
|
||||
},
|
||||
|
||||
getCurrentWidget: function() {
|
||||
return this.widgetStack.length ? this.widgetStack[this.widgetStack.length - 1] : undefined;
|
||||
},
|
||||
|
||||
beginWidget: function (widgetInfo, callback) {
|
||||
$__beginWidget: function (widgetInfo, callback) {
|
||||
var self = this;
|
||||
var widgetStack = self.widgetStack;
|
||||
var widgetStack = self.$__widgetStack;
|
||||
var origLength = widgetStack.length;
|
||||
var parent = origLength ? widgetStack[origLength - 1] : null;
|
||||
var parent = widgetStack[origLength - 1];
|
||||
|
||||
var widgetId = widgetInfo.id;
|
||||
|
||||
if (!widgetId) {
|
||||
widgetId = self._nextWidgetId();
|
||||
widgetInfo.id = widgetId;
|
||||
widgetInfo.id = widgetId = parent.$__nextId();
|
||||
}
|
||||
|
||||
widgetInfo.parent = parent;
|
||||
|
||||
function end() {
|
||||
widgetStack.length = origLength;
|
||||
}
|
||||
|
||||
var widgetDef = new WidgetDef(widgetInfo, end, this.out);
|
||||
|
||||
this.widgetsById[widgetId] = widgetDef;
|
||||
|
||||
if (parent) {
|
||||
//Check if it is a top-level widget
|
||||
parent.addChild(widgetDef);
|
||||
} else {
|
||||
self.widgets.push(widgetDef);
|
||||
}
|
||||
var widgetDef = new WidgetDef(widgetInfo, end, this.$__out);
|
||||
this.$__widgetsById[widgetId] = widgetDef;
|
||||
parent.$__addChild(widgetDef);
|
||||
widgetStack.push(widgetDef);
|
||||
|
||||
return widgetDef;
|
||||
},
|
||||
getWidget: function(id) {
|
||||
return this.widgetsById[id];
|
||||
$__clearWidgets: function () {
|
||||
this.$__widgetStack = [new WidgetDef(EMPTY_OBJECT, null, this.$__out)];
|
||||
},
|
||||
hasWidgets: function () {
|
||||
return this.widgets.length !== 0;
|
||||
},
|
||||
clearWidgets: function () {
|
||||
this.widgets = [];
|
||||
this.widgetStack = [];
|
||||
},
|
||||
_nextWidgetId: function () {
|
||||
return uniqueId(this.out);
|
||||
},
|
||||
initWidgets: function (document) {
|
||||
var widgetDefs = this.widgets;
|
||||
initWidgets.initClientRendered(widgetDefs, document);
|
||||
this.clearWidgets();
|
||||
},
|
||||
|
||||
preservedDOMNode: function(existingEl, bodyOnly, bodyEl) {
|
||||
var preserved = this.preserved ;
|
||||
if (preserved === EMPTY_OBJECT) {
|
||||
preserved = this.preserved = {};
|
||||
$__initWidgets: function (document) {
|
||||
var widgetDefs = this.$__widgets;
|
||||
if (widgetDefs) {
|
||||
initWidgets.initClientRendered(widgetDefs, document);
|
||||
this.$__clearWidgets();
|
||||
}
|
||||
preserved[existingEl.id] = { bodyOnly: bodyOnly, bodyEl: bodyEl};
|
||||
},
|
||||
$__nextWidgetId: function() {
|
||||
var widgetStack = this.$__widgetStack;
|
||||
var parent = widgetStack[widgetStack.length - 1];
|
||||
return parent.$__nextId();
|
||||
},
|
||||
$__preserveDOMNode: function(existingEl, bodyOnly, bodyEl) {
|
||||
var preserved = this.$__preserved ;
|
||||
if (preserved === EMPTY_OBJECT) {
|
||||
preserved = this.$__preserved = {};
|
||||
}
|
||||
preserved[existingEl.id] = { bodyOnly: bodyOnly, bodyEl: bodyEl };
|
||||
}
|
||||
};
|
||||
|
||||
WidgetsContext.getWidgetsContext = function (out) {
|
||||
WidgetsContext.$__getWidgetsContext = function (out) {
|
||||
var global = out.global;
|
||||
|
||||
return out.data.widgets ||
|
||||
@ -97,5 +75,4 @@ WidgetsContext.getWidgetsContext = function (out) {
|
||||
(global.widgets = new WidgetsContext(out));
|
||||
};
|
||||
|
||||
|
||||
module.exports = WidgetsContext;
|
||||
@ -2,9 +2,7 @@ var Widget = require('./Widget');
|
||||
var initServerRendered = require('./init-widgets').initServerRendered;
|
||||
var updateManager = require('./update-manager');
|
||||
var events = require('../runtime/events');
|
||||
var WidgetsContext = exports.WidgetsContext = require('./WidgetsContext');
|
||||
|
||||
exports.getWidgetsContext = WidgetsContext.getWidgetsContext;
|
||||
exports.Widget = Widget;
|
||||
|
||||
exports.onInitWidget = function(listener) {
|
||||
@ -76,7 +74,7 @@ events.on('dom/beforeRemove', function(eventArgs) {
|
||||
var out = eventArgs.out;
|
||||
var widgetsContext = out.global.widgets;
|
||||
if (widgetsContext) {
|
||||
widgetsContext.initWidgets(eventArgs.document);
|
||||
widgetsContext.$__initWidgets(eventArgs.document);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@ -17,30 +17,28 @@ WrappedString.prototype = {
|
||||
}
|
||||
};
|
||||
|
||||
exports.WidgetsContext = WidgetsContext;
|
||||
exports.getWidgetsContext = WidgetsContext.getWidgetsContext;
|
||||
exports.uniqueId = require('./uniqueId');
|
||||
|
||||
function flattenHelper(widgets, flattened) {
|
||||
for (var i = 0, len = widgets.length; i < len; i++) {
|
||||
var widgetDef = widgets[i];
|
||||
if (!widgetDef.type) {
|
||||
if (!widgetDef.$__type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var children = widgetDef.children;
|
||||
var children = widgetDef.$__children;
|
||||
|
||||
if (children) {
|
||||
// Depth-first search (children should be initialized before parent)
|
||||
flattenHelper(children, flattened);
|
||||
}
|
||||
|
||||
flattened.push(widgetDef.toJSON());
|
||||
flattened.push(widgetDef.$__toJSON());
|
||||
}
|
||||
}
|
||||
|
||||
function getRenderedWidgets(widgetsContext) {
|
||||
var widgets = widgetsContext.getWidgets();
|
||||
var widgets = widgetsContext.$__widgets;
|
||||
if (!widgets || !widgets.length) {
|
||||
return;
|
||||
}
|
||||
@ -65,7 +63,7 @@ function writeInitWidgetsCode(widgetsContext, out) {
|
||||
warp10.stringify(renderedWidgets).replace(escapeEndingScriptTagRegExp, '\\u003C/') +
|
||||
')})()</script>');
|
||||
|
||||
widgetsContext.clearWidgets();
|
||||
widgetsContext.$__clearWidgets();
|
||||
}
|
||||
|
||||
exports.writeInitWidgetsCode = writeInitWidgetsCode;
|
||||
@ -77,7 +75,7 @@ exports.writeInitWidgetsCode = writeInitWidgetsCode;
|
||||
* @param {WidgetsContext|AsyncWriter} widgetsContext A WidgetsContext or an AsyncWriter
|
||||
* @return {Object} An object with information about the rendered widgets that can be serialized to JSON. The object should be treated as opaque
|
||||
*/
|
||||
exports.getRenderedWidgets = exports.getRenderedWidgetIds /* deprecated */ = function(widgetsContext) {
|
||||
exports.getRenderedWidgets = function(widgetsContext) {
|
||||
if (!(widgetsContext instanceof WidgetsContext)) {
|
||||
// Assume that the provided "widgetsContext" argument is
|
||||
// actually an AsyncWriter
|
||||
@ -86,7 +84,7 @@ exports.getRenderedWidgets = exports.getRenderedWidgetIds /* deprecated */ = fun
|
||||
throw new Error('Invalid argument: ' + widgetsContext);
|
||||
}
|
||||
|
||||
widgetsContext = WidgetsContext.getWidgetsContext(out);
|
||||
widgetsContext = WidgetsContext.$__getWidgetsContext(out);
|
||||
}
|
||||
|
||||
var renderedWidgets = getRenderedWidgets(widgetsContext);
|
||||
|
||||
@ -48,15 +48,15 @@ function getNestedEl(widget, nestedId, document) {
|
||||
}
|
||||
|
||||
function initWidget(widgetDef, doc) {
|
||||
var type = widgetDef.type;
|
||||
var type = widgetDef.$__type;
|
||||
var id = widgetDef.id;
|
||||
var config = widgetDef.config;
|
||||
var state = widgetDef.state;
|
||||
var scope = widgetDef.scope;
|
||||
var domEvents = widgetDef.domEvents;
|
||||
var customEvents = widgetDef.customEvents;
|
||||
var config = widgetDef.$__config;
|
||||
var state = widgetDef.$__state;
|
||||
var scope = widgetDef.$__scope;
|
||||
var domEvents = widgetDef.$__domEvents;
|
||||
var customEvents = widgetDef.$__customEvents;
|
||||
var bodyElId = widgetDef.bodyElId;
|
||||
var existingWidget = widgetDef.existingWidget;
|
||||
var existingWidget = widgetDef.$__existingWidget;
|
||||
|
||||
var el;
|
||||
var i;
|
||||
@ -82,7 +82,7 @@ function initWidget(widgetDef, doc) {
|
||||
}
|
||||
|
||||
var els;
|
||||
var rootIds = widgetDef.roots;
|
||||
var rootIds = widgetDef.$__roots;
|
||||
var rootWidgets;
|
||||
|
||||
if (rootIds) {
|
||||
@ -218,11 +218,11 @@ function initClientRendered(widgetDefs, doc) {
|
||||
for (var i=0,len=widgetDefs.length; i<len; i++) {
|
||||
var widgetDef = widgetDefs[i];
|
||||
|
||||
if (widgetDef.children) {
|
||||
initClientRendered(widgetDef.children, doc);
|
||||
if (widgetDef.$__children) {
|
||||
initClientRendered(widgetDef.$__children, doc);
|
||||
}
|
||||
|
||||
if (!widgetDef.type) {
|
||||
if (!widgetDef.$__type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@ -4,9 +4,5 @@
|
||||
"./uniqueId.js": "./uniqueId-browser.js",
|
||||
"./init-widgets.js": "./init-widgets-browser.js",
|
||||
"./defineWidget.js": "./defineWidget-browser.js"
|
||||
},
|
||||
"minprops": {
|
||||
"exclude": ["r"],
|
||||
"matchPrefix": "$__"
|
||||
}
|
||||
}
|
||||
@ -1,26 +1,10 @@
|
||||
/*
|
||||
* Copyright 2011 eBay Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
var registered = {};
|
||||
var loaded = {};
|
||||
var widgetTypes = {};
|
||||
var defineWidget;
|
||||
var defineRenderer;
|
||||
|
||||
exports.register = function(typeName, def) {
|
||||
function register(typeName, def) {
|
||||
if (typeof def === 'function') {
|
||||
// We do this to kick off registering of nested widgets
|
||||
// but we don't use the return value just yet since there
|
||||
@ -32,7 +16,7 @@ exports.register = function(typeName, def) {
|
||||
delete loaded[typeName];
|
||||
delete widgetTypes[typeName];
|
||||
return typeName;
|
||||
};
|
||||
}
|
||||
|
||||
function load(typeName) {
|
||||
var target = loaded[typeName];
|
||||
@ -86,9 +70,7 @@ function getWidgetClass(typeName) {
|
||||
return WidgetClass;
|
||||
}
|
||||
|
||||
exports.load = load;
|
||||
|
||||
exports.createWidget = function(typeName, id, document) {
|
||||
function createWidget(typeName, id, document) {
|
||||
var WidgetClass = getWidgetClass(typeName);
|
||||
var widget;
|
||||
if (typeof WidgetClass === 'function') {
|
||||
@ -99,7 +81,11 @@ exports.createWidget = function(typeName, id, document) {
|
||||
widget.$__document = document;
|
||||
}
|
||||
return widget;
|
||||
};
|
||||
}
|
||||
|
||||
exports.register = register;
|
||||
exports.createWidget = createWidget;
|
||||
exports.load = load;
|
||||
|
||||
defineWidget = require('./defineWidget');
|
||||
defineRenderer = require('./defineRenderer');
|
||||
@ -4,6 +4,7 @@ var includeTag = require('./taglib/include-tag');
|
||||
var repeatedId = require('./repeated-id');
|
||||
var getRootEls = markoWidgets.$__roots;
|
||||
var repeatedRegExp = /\[\]$/;
|
||||
var WidgetsContext = require('./WidgetsContext');
|
||||
|
||||
var RERENDER_WIDGET_INDEX = 0;
|
||||
var RERENDER_WIDGET_STATE_INDEX = 1;
|
||||
@ -58,7 +59,7 @@ function preserveWidgetEls(existingWidget, out, widgetsContext, widgetBody) {
|
||||
|
||||
out.endElement();
|
||||
|
||||
widgetsContext.preservedDOMNode(el, false, bodyEl); // Mark the element as being preserved (for morphdom)
|
||||
widgetsContext.$__preserveDOMNode(el, false, bodyEl); // Mark the element as being preserved (for morphdom)
|
||||
}
|
||||
|
||||
existingWidget._reset(); // The widget is no longer dirty so reset internal flags
|
||||
@ -72,7 +73,7 @@ function handleBeginAsync(event) {
|
||||
var widgetsContext = asyncOut.global.widgets;
|
||||
var widgetStack;
|
||||
|
||||
if (widgetsContext && (widgetStack = widgetsContext.widgetStack).length) {
|
||||
if (widgetsContext && (widgetStack = widgetsContext.$__widgetStack)) {
|
||||
// All of the widgets in this async block should be
|
||||
// initialized after the widgets in the parent. Therefore,
|
||||
// we will create a new WidgetsContext for the nested
|
||||
@ -81,8 +82,7 @@ function handleBeginAsync(event) {
|
||||
// stack (to begin with). This will result in top-level widgets
|
||||
// of the async block being added as children of the widget in the
|
||||
// parent block.
|
||||
var nestedWidgetsContext = new markoWidgets.WidgetsContext(asyncOut);
|
||||
nestedWidgetsContext.widgetStack = [widgetStack[widgetStack.length-1]];
|
||||
var nestedWidgetsContext = new markoWidgets.WidgetsContext(asyncOut, widgetStack[widgetStack.length-1]);
|
||||
asyncOut.data.widgets = nestedWidgetsContext;
|
||||
}
|
||||
asyncOut.data.$w = parentOut.data.$w;
|
||||
@ -230,14 +230,10 @@ module.exports = function createRendererFunc(templateRenderFunc, widgetProps, re
|
||||
customEvents = widgetArgs[2];
|
||||
}
|
||||
|
||||
var widgetsContext = markoWidgets.getWidgetsContext(out);
|
||||
var widgetsContext = WidgetsContext.$__getWidgetsContext(out);
|
||||
|
||||
if (!id) {
|
||||
var parentWidget = widgetsContext.getCurrentWidget();
|
||||
|
||||
if (parentWidget) {
|
||||
id = parentWidget.nextId();
|
||||
}
|
||||
id = widgetsContext.$__nextWidgetId();
|
||||
}
|
||||
|
||||
var existingWidget;
|
||||
@ -292,16 +288,16 @@ module.exports = function createRendererFunc(templateRenderFunc, widgetProps, re
|
||||
existingWidget.$__emitLifecycleEvent('beforeUpdate');
|
||||
}
|
||||
|
||||
var widgetDef = widgetsContext.beginWidget({
|
||||
type: typeName,
|
||||
var widgetDef = widgetsContext.$__beginWidget({
|
||||
$__type: typeName,
|
||||
id: id,
|
||||
config: widgetConfig,
|
||||
state: widgetState,
|
||||
customEvents: customEvents,
|
||||
scope: scope,
|
||||
existingWidget: existingWidget,
|
||||
$__config: widgetConfig,
|
||||
$__state: widgetState,
|
||||
$__customEvents: customEvents,
|
||||
$__scope: scope,
|
||||
$__existingWidget: existingWidget,
|
||||
bodyElId: bodyElId,
|
||||
roots: roots,
|
||||
$__roots: roots,
|
||||
body: widgetBody
|
||||
});
|
||||
|
||||
@ -309,6 +305,6 @@ module.exports = function createRendererFunc(templateRenderFunc, widgetProps, re
|
||||
// data that we constructed
|
||||
templateRenderFunc(templateData, out, widgetDef, widgetState);
|
||||
|
||||
widgetDef.end();
|
||||
widgetDef.$__end();
|
||||
};
|
||||
};
|
||||
@ -1,17 +1,17 @@
|
||||
var REPEATED_ID_KEY = '$rep';
|
||||
|
||||
function RepeatedId() {
|
||||
this.nextIdLookup = {};
|
||||
this.$__nextIdLookup = {};
|
||||
}
|
||||
|
||||
RepeatedId.prototype = {
|
||||
nextId: function(parentId, id) {
|
||||
$__nextId: function(parentId, id) {
|
||||
var indexLookupKey = parentId + '-' + id;
|
||||
var currentIndex = this.nextIdLookup[indexLookupKey];
|
||||
var currentIndex = this.$__nextIdLookup[indexLookupKey];
|
||||
if (currentIndex == null) {
|
||||
currentIndex = this.nextIdLookup[indexLookupKey] = 0;
|
||||
currentIndex = this.$__nextIdLookup[indexLookupKey] = 0;
|
||||
} else {
|
||||
currentIndex = ++this.nextIdLookup[indexLookupKey];
|
||||
currentIndex = ++this.$__nextIdLookup[indexLookupKey];
|
||||
}
|
||||
|
||||
return indexLookupKey.slice(0, -2) + '[' + currentIndex + ']';
|
||||
@ -24,5 +24,5 @@ exports.$__nextId = function(out, parentId, id) {
|
||||
repeatedId = out.global[REPEATED_ID_KEY] = new RepeatedId();
|
||||
}
|
||||
|
||||
return repeatedId.nextId(parentId, id);
|
||||
return repeatedId.$__nextId(parentId, id);
|
||||
};
|
||||
|
||||
@ -110,12 +110,14 @@ module.exports = function handleWidgetBind() {
|
||||
}
|
||||
|
||||
el.insertSiblingBefore(
|
||||
builder.assignment(
|
||||
builder.memberExpression(builder.identifier('widget'), builder.identifier('type')),
|
||||
builder.memberExpression(
|
||||
builder.identifier('marko_widgetTypes'),
|
||||
bindAttrValue,
|
||||
true /* computed */)));
|
||||
builder.functionCall(
|
||||
builder.memberExpression(builder.identifier('widget'), builder.identifier('t')),
|
||||
[
|
||||
builder.memberExpression(
|
||||
builder.identifier('marko_widgetTypes'),
|
||||
bindAttrValue,
|
||||
true /* computed */)
|
||||
]));
|
||||
}
|
||||
|
||||
var renderingLogic;
|
||||
@ -165,9 +167,11 @@ module.exports = function handleWidgetBind() {
|
||||
|
||||
if (el.hasAttribute('w-config')) {
|
||||
el.insertSiblingBefore(
|
||||
builder.assignment(
|
||||
builder.memberExpression(builder.identifier('widget'), builder.identifier('config')),
|
||||
el.getAttributeValue('w-config')));
|
||||
builder.functionCall(
|
||||
builder.memberExpression(builder.identifier('widget'), builder.identifier('c')),
|
||||
[
|
||||
el.getAttributeValue('w-config')
|
||||
]));
|
||||
|
||||
el.removeAttribute('w-config');
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ function addDirectEventListener(transformHelper, eventType, targetMethod, extraA
|
||||
|
||||
var addDomEvent = builder.memberExpression(
|
||||
builder.identifier('widget'),
|
||||
builder.identifier('addDomEvent'));
|
||||
builder.identifier('e'));
|
||||
|
||||
let widgetIdInfo = transformHelper.assignWidgetId(true /* repeated */);
|
||||
let idVarNode = widgetIdInfo.idVarNode ? null : widgetIdInfo.createIdVarNode();
|
||||
|
||||
@ -1,35 +1,17 @@
|
||||
/*
|
||||
* Copyright 2011 eBay Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Helper method to return the metadata for the current widget being rendered.
|
||||
* Helper method to return the WidgetDef for the current widget being rendered.
|
||||
* This is, it returns the widget at the top of the widget stack.
|
||||
* @param {AsyncWriter} out The current rendering context that holds info about rendered widgets.
|
||||
* @return {WidgetDef} The WidgetDef instance
|
||||
*/
|
||||
module.exports = function getCurrentWidget(out) {
|
||||
var widgets = out.global.widgets;
|
||||
if (!widgets) {
|
||||
var widgetsContext = out.global.widgets;
|
||||
var widgetStack;
|
||||
var len;
|
||||
|
||||
if (!widgetsContext || (len = (widgetStack = widgetsContext.$__widgetStack).length) < 2) {
|
||||
throw new Error('No widget found');
|
||||
}
|
||||
|
||||
var widget = widgets.getCurrentWidget();
|
||||
if (!widget) {
|
||||
throw new Error('No widget found');
|
||||
}
|
||||
|
||||
return widget;
|
||||
return widgetStack[len - 1];
|
||||
};
|
||||
@ -1,6 +1,6 @@
|
||||
var isBrowser = typeof window !== 'undefined';
|
||||
var normalInclude = require('../../taglibs/core/include-tag');
|
||||
var markoWidgets = require('../');
|
||||
var WidgetsContext = require('../WidgetsContext');
|
||||
|
||||
module.exports = function include(input, out) {
|
||||
var target = input._target;
|
||||
@ -16,8 +16,8 @@ module.exports = function include(input, out) {
|
||||
// the existing body content in the DOM
|
||||
var existingEl = document.getElementById(elId);
|
||||
if (existingEl) {
|
||||
var widgetsContext = markoWidgets.getWidgetsContext(out);
|
||||
widgetsContext.preservedDOMNode(existingEl, true /* body only */);
|
||||
var widgetsContext = WidgetsContext.$__getWidgetsContext(out);
|
||||
widgetsContext.$__preserveDOMNode(existingEl, true /* body only */);
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -25,18 +25,13 @@ module.exports = function render(input, out) {
|
||||
});
|
||||
|
||||
if (out.isSync()) {
|
||||
var widgetsContext = WidgetsContext.getWidgetsContext(out);
|
||||
if (widgetsContext.hasWidgets()) {
|
||||
writeInitWidgetsCode(widgetsContext, out);
|
||||
}
|
||||
var widgetsContext = WidgetsContext.$__getWidgetsContext(out);
|
||||
writeInitWidgetsCode(widgetsContext, out);
|
||||
} else {
|
||||
var asyncOut = out.beginAsync({ last: true, timeout: -1 });
|
||||
out.onLast(function(next) {
|
||||
var widgetsContext = WidgetsContext.getWidgetsContext(out);
|
||||
if (widgetsContext.hasWidgets()) {
|
||||
writeInitWidgetsCode(widgetsContext, asyncOut);
|
||||
}
|
||||
|
||||
var widgetsContext = WidgetsContext.$__getWidgetsContext(out);
|
||||
writeInitWidgetsCode(widgetsContext, asyncOut);
|
||||
asyncOut.end();
|
||||
next();
|
||||
});
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
var widgets = require('../');
|
||||
var WidgetsContext = require('../WidgetsContext');
|
||||
|
||||
module.exports = function render(input, out) {
|
||||
|
||||
@ -15,7 +15,7 @@ module.exports = function render(input, out) {
|
||||
if (condition !== false) {
|
||||
var existingEl = document.getElementById(id);
|
||||
if (existingEl) {
|
||||
var widgetsContext = widgets.getWidgetsContext(out);
|
||||
var widgetsContext = WidgetsContext.$__getWidgetsContext(out);
|
||||
var bodyOnly = input.bodyOnly === true;
|
||||
// Don't actually render anything since the element is already in the DOM,
|
||||
// but keep track that the node is being preserved so that we can ignore
|
||||
@ -31,7 +31,7 @@ module.exports = function render(input, out) {
|
||||
out.endElement();
|
||||
}
|
||||
|
||||
widgetsContext.preservedDOMNode(existingEl, bodyOnly);
|
||||
widgetsContext.$__preserveDOMNode(existingEl, bodyOnly);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
function IdProvider(out) {
|
||||
var global = this.global = out.global;
|
||||
this.prefix = global.widgetIdPrefix || 'w';
|
||||
this.$__prefix = global.widgetIdPrefix || 'w';
|
||||
|
||||
if (global._nextWidgetId == null) {
|
||||
global._nextWidgetId = 0;
|
||||
}
|
||||
}
|
||||
|
||||
IdProvider.prototype.nextId = function() {
|
||||
return this.prefix + (this.global._nextWidgetId++);
|
||||
IdProvider.prototype.$__nextId = function() {
|
||||
return this.$__prefix + (this.global._nextWidgetId++);
|
||||
};
|
||||
|
||||
module.exports = function (out) {
|
||||
@ -16,5 +16,5 @@ module.exports = function (out) {
|
||||
var idProvider = global._widgetIdProvider ||
|
||||
(global._widgetIdProvider = new IdProvider(out));
|
||||
|
||||
return idProvider.nextId();
|
||||
return idProvider.$__nextId();
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user