2022-05-24 21:11:12 +00:00
|
|
|
const VnObject = require('./object');
|
2022-06-06 12:49:18 +00:00
|
|
|
const Component = require('./component');
|
2022-05-28 19:27:36 +00:00
|
|
|
const VnNode = require('./node');
|
2022-05-24 21:11:12 +00:00
|
|
|
const Scope = require('./scope');
|
2022-05-30 01:30:33 +00:00
|
|
|
const Type = require('./type');
|
2022-05-24 21:11:12 +00:00
|
|
|
const kebabToCamel = require('./string-util').kebabToCamel;
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
2015-03-06 23:33:54 +00:00
|
|
|
* Creates a object from a XML specification.
|
2022-05-24 10:18:44 +00:00
|
|
|
*/
|
|
|
|
module.exports = new Class({
|
2022-05-24 21:11:12 +00:00
|
|
|
Extends: VnObject
|
2015-11-09 17:11:51 +00:00
|
|
|
,_contexts: null
|
|
|
|
|
2015-11-17 10:34:33 +00:00
|
|
|
/**
|
|
|
|
* Compiles an XML file.
|
|
|
|
*
|
2022-05-24 21:11:12 +00:00
|
|
|
* @param {String} path The XML path
|
|
|
|
* @return {Boolean} %true on success, %false othersise
|
2022-05-24 10:18:44 +00:00
|
|
|
*/
|
2022-05-24 21:11:12 +00:00
|
|
|
,compileFile: function(path) {
|
2016-09-26 09:28:47 +00:00
|
|
|
this._path = path;
|
2022-05-24 21:11:12 +00:00
|
|
|
return this.compileDocument(Vn.getXml(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compiles an XML string.
|
|
|
|
*
|
|
|
|
* @param {String} xmlString The XML string
|
|
|
|
* @return {Boolean} %true on success, %false othersise
|
|
|
|
*/
|
|
|
|
,compileString: function(xmlString) {
|
2022-05-24 10:18:44 +00:00
|
|
|
var parser = new DOMParser();
|
2022-05-24 21:11:12 +00:00
|
|
|
var doc = parser.parseFromString(xmlString, 'text/xml');
|
|
|
|
return this.compileDocument(doc);
|
|
|
|
}
|
2015-03-06 23:33:54 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
/**
|
|
|
|
* Compiles a XML document.
|
|
|
|
*
|
|
|
|
* @param {Document} doc The DOM document
|
|
|
|
* @return {Boolean} %true on success, %false othersise
|
|
|
|
*/
|
|
|
|
,compileDocument: function(doc, exprArgs) {
|
|
|
|
if (!doc)
|
|
|
|
return false;
|
2015-11-09 17:11:51 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
this._preCompile(exprArgs);
|
|
|
|
var docElement = doc.documentElement;
|
2015-03-06 23:33:54 +00:00
|
|
|
|
2022-05-24 10:18:44 +00:00
|
|
|
if (docElement.tagName !== 'vn') {
|
2022-05-24 21:11:12 +00:00
|
|
|
this.showError('The toplevel tag should be named \'vn\'');
|
2015-11-09 17:11:51 +00:00
|
|
|
this._contexts = null;
|
2015-03-06 23:33:54 +00:00
|
|
|
return false;
|
2015-11-09 17:11:51 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
var childs = docElement.childNodes;
|
|
|
|
if (childs)
|
|
|
|
for (var i = 0; i < childs.length; i++)
|
2022-05-24 21:11:12 +00:00
|
|
|
this._compile(childs[i]);
|
2015-03-06 23:33:54 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
this._postCompile();
|
2015-03-06 23:33:54 +00:00
|
|
|
return true;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-03-09 08:36:54 +00:00
|
|
|
|
2015-11-17 10:34:33 +00:00
|
|
|
/**
|
|
|
|
* Compiles a single DOM node.
|
|
|
|
*
|
|
|
|
* @path Node The DOM node
|
|
|
|
* @return %true on success, %false othersise
|
2022-05-24 10:18:44 +00:00
|
|
|
*/
|
2022-05-24 21:11:12 +00:00
|
|
|
,compileNode: function(node, exprArgs) {
|
|
|
|
this._preCompile(exprArgs);
|
|
|
|
this._mainContext = this._compile(node).id;
|
|
|
|
this._postCompile();
|
2015-11-17 10:34:33 +00:00
|
|
|
return true;
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
2022-05-24 21:11:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called before starting to compile nodes.
|
|
|
|
*/
|
|
|
|
,_preCompile: function(exprArgs) {
|
|
|
|
this._path = null;
|
|
|
|
this._tags = {};
|
|
|
|
this._contexts = [];
|
|
|
|
this._contextMap = {};
|
|
|
|
this._links = [];
|
|
|
|
this._mainContext = null;
|
|
|
|
|
|
|
|
this._baseExprArgs = ['_', '$'];
|
|
|
|
if (exprArgs)
|
|
|
|
this._baseExprArgs = this._baseExprArgs.concat(exprArgs);
|
|
|
|
|
|
|
|
this._baseEventArgs = this._baseExprArgs.concat(['$event']);
|
|
|
|
|
|
|
|
this._exprArgs = this._baseExprArgs.join(',');
|
|
|
|
this._eventArgs = this._baseEventArgs.join(',');
|
|
|
|
}
|
2015-03-09 08:36:54 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
/**
|
|
|
|
* Called after all nodes have been compiled.
|
|
|
|
*/
|
|
|
|
,_postCompile: function() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compiles a node.
|
|
|
|
*/
|
|
|
|
,_compile: function(node) {
|
|
|
|
let context = null;
|
|
|
|
let tagName = null;
|
|
|
|
const isElement = node.nodeType === Node.ELEMENT_NODE;
|
|
|
|
|
|
|
|
if (isElement)
|
|
|
|
tagName = node.tagName.toLowerCase();
|
|
|
|
else if (node.nodeType !== Node.TEXT_NODE
|
|
|
|
|| /^[\n\r\t]*$/.test(node.textContent))
|
2015-11-09 17:11:51 +00:00
|
|
|
return null;
|
2022-05-24 21:11:12 +00:00
|
|
|
|
|
|
|
context =
|
|
|
|
this.textCompile(node, tagName)
|
|
|
|
|| this.objectCompile(node, tagName)
|
|
|
|
|| this.elementCompile(node, tagName);
|
|
|
|
|
|
|
|
context.id = this._contexts.length;
|
|
|
|
|
|
|
|
if (isElement) {
|
|
|
|
var nodeId = node.getAttribute('id');
|
2015-11-09 17:11:51 +00:00
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
if (nodeId) {
|
2022-05-24 21:11:12 +00:00
|
|
|
this._contextMap[kebabToCamel(nodeId)] = context.id;
|
2022-05-28 19:27:36 +00:00
|
|
|
context.nodeId = nodeId;
|
|
|
|
}
|
2022-05-24 21:11:12 +00:00
|
|
|
|
|
|
|
var tags = this._tags[tagName];
|
|
|
|
|
|
|
|
if (!tags)
|
|
|
|
this._tags[tagName] = tags = [];
|
|
|
|
|
|
|
|
tags.push(context.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._contexts.push(context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
,getMain: function(scope) {
|
|
|
|
return scope.objects[this._mainContext];
|
|
|
|
}
|
|
|
|
|
|
|
|
,getByTagName: function(scope, tagName) {
|
|
|
|
var tags = this._tags[tagName];
|
|
|
|
|
|
|
|
if (tags) {
|
|
|
|
var arr = new Array(tags.length);
|
|
|
|
|
|
|
|
for (var i = 0; i < tags.length; i++)
|
|
|
|
arr[i] = scope.objects[tags[i]];
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
,load: function(dstDocument, thisArg, parentScope) {
|
|
|
|
if (this._contexts === null)
|
|
|
|
return null;
|
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
const doc = dstDocument ? dstDocument : document;
|
2022-05-24 21:11:12 +00:00
|
|
|
const contexts = this._contexts;
|
|
|
|
const len = contexts.length;
|
|
|
|
const objects = new Array(len);
|
2022-05-28 19:27:36 +00:00
|
|
|
const scope = new Scope(this, objects, thisArg, parentScope);
|
|
|
|
|
2022-05-24 10:18:44 +00:00
|
|
|
for (var i = 0; i < len; i++) {
|
2015-11-09 17:11:51 +00:00
|
|
|
var context = contexts[i];
|
|
|
|
|
|
|
|
if (context.tagName)
|
2022-05-28 19:27:36 +00:00
|
|
|
objects[i] = this.elementInstantiate(doc, context, scope);
|
2015-11-09 17:11:51 +00:00
|
|
|
else if (context.klass)
|
2022-05-28 19:27:36 +00:00
|
|
|
objects[i] = this.objectInstantiate(doc, context, scope);
|
2015-11-09 17:11:51 +00:00
|
|
|
else
|
2022-05-28 19:27:36 +00:00
|
|
|
objects[i] = this.textInstantiate(doc, context, scope);
|
2015-03-06 23:33:54 +00:00
|
|
|
}
|
2022-05-28 19:27:36 +00:00
|
|
|
|
|
|
|
return scope;
|
2015-11-17 10:34:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
,link: function(scope, exprScope) {
|
|
|
|
const objects = scope.objects;
|
|
|
|
const links = this._links;
|
2016-10-04 15:27:49 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
// Pre-link
|
|
|
|
|
|
|
|
for (var i = links.length - 1; i >= 0; i--) {
|
|
|
|
const link = links[i];
|
|
|
|
const object = objects[link.context.id];
|
2022-05-28 01:18:06 +00:00
|
|
|
const objectRef = scope.$[link.objectId];
|
2022-05-24 21:11:12 +00:00
|
|
|
|
|
|
|
if (objectRef === undefined) {
|
|
|
|
this.showError('Referenced unexistent object with id \'%s\'',
|
|
|
|
link.objectId);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (link.prop)
|
|
|
|
object[link.prop] = objectRef;
|
|
|
|
else
|
|
|
|
object.appendChild(objectRef);
|
2015-03-06 23:33:54 +00:00
|
|
|
}
|
2015-11-09 17:11:51 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
// Post-link
|
|
|
|
|
|
|
|
const baseExprScope = [
|
|
|
|
_,
|
2022-05-28 01:18:06 +00:00
|
|
|
scope.$
|
2022-05-24 21:11:12 +00:00
|
|
|
].concat(exprScope);
|
2022-05-24 10:18:44 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
this.linkExpr(scope, baseExprScope);
|
|
|
|
|
|
|
|
const contexts = this._contexts;
|
2022-05-24 10:18:44 +00:00
|
|
|
for (var i = 0; i < contexts.length; i++) {
|
2022-05-24 21:11:12 +00:00
|
|
|
const context = contexts[i];
|
|
|
|
const object = objects[i];
|
2015-11-09 17:11:51 +00:00
|
|
|
|
|
|
|
if (context.tagName)
|
2022-05-24 21:11:12 +00:00
|
|
|
this.elementLink(context, object, objects, scope, baseExprScope);
|
2015-11-09 17:11:51 +00:00
|
|
|
else if (context.klass)
|
2022-05-24 21:11:12 +00:00
|
|
|
this.objectLink(context, object, objects, scope, baseExprScope);
|
2015-03-19 19:36:11 +00:00
|
|
|
}
|
2015-11-09 17:11:51 +00:00
|
|
|
}
|
2022-05-24 10:18:44 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
,linkExpr(scope, baseScope, exprScope) {
|
2022-05-24 10:18:44 +00:00
|
|
|
const contexts = this._contexts;
|
2022-05-24 21:11:12 +00:00
|
|
|
const objects = scope.objects;
|
2022-05-24 10:18:44 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
exprScope = baseScope.concat(exprScope);
|
2022-05-24 10:18:44 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < contexts.length; i++) {
|
|
|
|
const context = contexts[i];
|
|
|
|
const object = objects[i];
|
|
|
|
|
|
|
|
if (context.exprs) {
|
|
|
|
const values = [];
|
|
|
|
for (expr of context.exprs) {
|
|
|
|
let value = undefined;
|
|
|
|
try {
|
2022-05-24 21:11:12 +00:00
|
|
|
value = expr.apply(scope.thisArg, exprScope);
|
2022-05-24 10:18:44 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.warn('Expression error:', e.message);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
values.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
let k = 0;
|
|
|
|
const text = context.text.replace(/{{\d+}}/g, function() {
|
|
|
|
return values[k++];
|
|
|
|
});
|
|
|
|
object.textContent = text;
|
|
|
|
} else {
|
|
|
|
const dynProps = context.dynProps;
|
|
|
|
|
|
|
|
for (const prop in dynProps) {
|
|
|
|
let value = undefined;
|
|
|
|
try {
|
2022-05-24 21:11:12 +00:00
|
|
|
value = dynProps[prop].apply(scope.thisArg, exprScope);
|
2022-05-24 10:18:44 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.warn('Expression error:', e.message);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context.tagName)
|
|
|
|
object.setAttribute(prop, value);
|
|
|
|
else
|
|
|
|
object[prop] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
,showError: function(error) {
|
|
|
|
var path = this._path ? this._path : 'Node';
|
|
|
|
var logArgs = ['Vn.Builder: %s: '+ error, path];
|
|
|
|
|
|
|
|
for (var i = 1; i < arguments.length; i++)
|
|
|
|
logArgs.push(arguments[i]);
|
|
|
|
|
|
|
|
console.warn.apply(null, logArgs);
|
2015-11-09 17:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
,_addLink: function(context, prop, objectId) {
|
|
|
|
this._links.push({
|
|
|
|
context
|
|
|
|
,prop
|
|
|
|
,objectId: kebabToCamel(objectId)
|
|
|
|
});
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-11-09 17:11:51 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
,fnExpr(expr) {
|
|
|
|
return new Function(this._exprArgs,
|
|
|
|
'"use strict"; return ' + expr + ';'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
,matchExpr(value) {
|
|
|
|
const match = /^{{(.*)}}$/.exec(value);
|
|
|
|
if (!match) return null;
|
|
|
|
return this.fnExpr(match[1]);
|
|
|
|
}
|
2015-11-09 08:14:33 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
,_translateValue: function(value) {
|
|
|
|
var chr = value.charAt(0);
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
if (chr === '_')
|
|
|
|
return _(value.substr(1));
|
|
|
|
else if (chr === '\\' && value.charAt(1) === '_')
|
|
|
|
return value.substr(1);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
,_getMethod: function(value) {
|
|
|
|
let method;
|
2015-11-09 08:14:33 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
if (this.isIdentifier(value)) {
|
|
|
|
// XXX: Compatibility with old events
|
|
|
|
method = value;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
method = new Function(this._eventArgs,
|
|
|
|
'"use strict"; return ' + value + ';'
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
this.showError(`Method: ${err.message}: ${value}`);
|
|
|
|
}
|
2015-11-09 08:14:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
|
|
|
,_isEvent: function(attribute) {
|
|
|
|
return /^on-\w+/.test(attribute);
|
2015-11-09 17:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
,isIdentifier: function(value) {
|
|
|
|
return /^[a-zA-Z_$][\w$]*$/.test(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
,_replaceFunc: function(token) {
|
|
|
|
return token.charAt(1).toUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ TextNode
|
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
/**
|
|
|
|
* Creates a text node context.
|
2022-05-24 10:18:44 +00:00
|
|
|
*/
|
|
|
|
,textCompile: function(node, tagName) {
|
|
|
|
if (!tagName) {
|
|
|
|
let text = node.textContent;
|
|
|
|
|
|
|
|
if (/{{.*}}/.test(text)) {
|
|
|
|
let i = 0;
|
|
|
|
const self = this;
|
|
|
|
const exprs = [];
|
|
|
|
text = text.replace(/{{((?:(?!}}).)*)}}/g, function(match, capture) {
|
|
|
|
exprs.push(self.fnExpr(capture));
|
|
|
|
return `{{${i++}}}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
return {text, exprs};
|
|
|
|
} else
|
|
|
|
return {text};
|
|
|
|
} else if (tagName === 't')
|
|
|
|
return {text: _(node.firstChild.textContent)};
|
2015-11-09 17:11:51 +00:00
|
|
|
else
|
|
|
|
return null;
|
2015-03-19 19:36:11 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
,textInstantiate: function(doc, context) {
|
|
|
|
return doc.createTextNode(context.exprs ? '' : context.text);
|
2015-11-09 17:11:51 +00:00
|
|
|
}
|
2022-05-24 21:11:12 +00:00
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Vn.Object
|
2015-11-09 17:11:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a object context.
|
2022-05-24 10:18:44 +00:00
|
|
|
*/
|
|
|
|
,objectCompile: function(node, tagName) {
|
2016-09-26 09:28:47 +00:00
|
|
|
var klass = vnCustomTags[tagName];
|
2015-11-09 17:11:51 +00:00
|
|
|
|
|
|
|
if (!klass)
|
|
|
|
return null;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
var props = {};
|
|
|
|
var objectProps = {};
|
|
|
|
var childs = [];
|
|
|
|
var events = {};
|
|
|
|
|
|
|
|
var context = {
|
|
|
|
klass: klass,
|
|
|
|
props: props,
|
2022-05-24 21:11:12 +00:00
|
|
|
dynProps: {},
|
|
|
|
funcProps: {},
|
2015-11-09 17:11:51 +00:00
|
|
|
objectProps: objectProps,
|
|
|
|
childs: childs,
|
|
|
|
events: events,
|
|
|
|
custom: null
|
|
|
|
};
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
var a = node.attributes;
|
2015-11-09 08:14:33 +00:00
|
|
|
|
2022-05-24 10:18:44 +00:00
|
|
|
for (var i = 0; i < a.length; i++) {
|
2015-11-09 17:11:51 +00:00
|
|
|
var attribute = a[i].nodeName;
|
|
|
|
var value = a[i].nodeValue;
|
2015-11-09 08:14:33 +00:00
|
|
|
|
2022-05-24 10:18:44 +00:00
|
|
|
if (this._isEvent(attribute)) {
|
|
|
|
var handler = this._getMethod(value)
|
2015-11-19 13:57:23 +00:00
|
|
|
|
|
|
|
if (handler)
|
2022-05-24 10:18:44 +00:00
|
|
|
events[attribute.substr(3)] = handler;
|
|
|
|
} else if (!/^(id|property)$/.test(attribute)) {
|
2022-05-24 21:11:12 +00:00
|
|
|
this.propCompile(context, klass, props,
|
2015-11-09 17:11:51 +00:00
|
|
|
node, attribute, value);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-09 17:11:51 +00:00
|
|
|
|
|
|
|
var childNodes = node.childNodes;
|
|
|
|
|
|
|
|
if (childNodes)
|
2022-05-24 10:18:44 +00:00
|
|
|
for (var i = 0; i < childNodes.length; i++) {
|
2015-11-09 17:11:51 +00:00
|
|
|
var child = childNodes[i];
|
2018-03-26 16:35:02 +00:00
|
|
|
var isElement = child.nodeType === Node.ELEMENT_NODE;
|
2022-05-24 10:18:44 +00:00
|
|
|
var childTagName = isElement ? child.tagName.toLowerCase() : null;
|
2015-11-09 17:11:51 +00:00
|
|
|
var childContext;
|
|
|
|
|
2022-05-24 10:18:44 +00:00
|
|
|
if (childTagName === 'pointer') {
|
|
|
|
this._addLink(context, null, child.getAttribute('object'));
|
|
|
|
} else if (childTagName === 'custom') {
|
2015-11-09 17:11:51 +00:00
|
|
|
context.custom = child;
|
2022-05-24 21:11:12 +00:00
|
|
|
} else if (childContext = this._compile(child)) {
|
2022-05-24 10:18:44 +00:00
|
|
|
var prop = isElement ? child.getAttribute('property') : null;
|
2015-11-09 17:11:51 +00:00
|
|
|
|
2022-05-24 10:18:44 +00:00
|
|
|
if (prop) {
|
|
|
|
prop = prop.replace(/-./g, this._replaceFunc);
|
2015-11-09 17:11:51 +00:00
|
|
|
objectProps[prop] = childContext.id;
|
2022-05-24 10:18:44 +00:00
|
|
|
} else
|
|
|
|
childs.push(childContext.id);
|
2015-11-09 17:11:51 +00:00
|
|
|
}
|
2015-07-07 15:27:47 +00:00
|
|
|
}
|
2015-11-09 17:11:51 +00:00
|
|
|
|
|
|
|
return context;
|
2015-07-07 15:27:47 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
,propCompile: function(context, klass, props, node, attribute, value) {
|
|
|
|
let isLink = false;
|
|
|
|
let propError = false;
|
|
|
|
let newValue = null;
|
|
|
|
const propName = attribute.replace(/-./g, this._replaceFunc);
|
|
|
|
const propInfo = klass.Properties[propName];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-24 10:18:44 +00:00
|
|
|
if (!propInfo) {
|
2022-05-24 21:11:12 +00:00
|
|
|
this.showError('Attribute \'%s\' not valid for tag \'%s\'',
|
2015-11-09 17:11:51 +00:00
|
|
|
attribute, node.tagName);
|
2015-07-03 05:49:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-05-24 10:18:44 +00:00
|
|
|
if (!value) {
|
2022-05-24 21:11:12 +00:00
|
|
|
this.showError('Attribute \'%s\' empty on tag \'%s\'',
|
2015-11-09 17:11:51 +00:00
|
|
|
attribute, node.tagName);
|
2015-01-23 13:09:30 +00:00
|
|
|
return;
|
2015-11-09 17:11:51 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-24 10:18:44 +00:00
|
|
|
const expr = this.matchExpr(value);
|
|
|
|
|
|
|
|
if (expr) {
|
2022-05-24 21:11:12 +00:00
|
|
|
context.dynProps[propName] = expr;
|
2022-05-24 10:18:44 +00:00
|
|
|
} else {
|
|
|
|
switch (propInfo.type) {
|
2015-01-23 13:09:30 +00:00
|
|
|
case Boolean:
|
2022-05-24 10:18:44 +00:00
|
|
|
newValue = (/^(true|1)$/i).test(value);
|
2015-01-23 13:09:30 +00:00
|
|
|
break;
|
|
|
|
case Number:
|
2022-05-24 10:18:44 +00:00
|
|
|
newValue = 0 + new Number(value);
|
2015-01-23 13:09:30 +00:00
|
|
|
break;
|
|
|
|
case String:
|
2022-05-24 10:18:44 +00:00
|
|
|
newValue = this._translateValue(value);
|
2015-03-06 23:33:54 +00:00
|
|
|
break;
|
2015-01-23 13:09:30 +00:00
|
|
|
case Function:
|
2022-05-24 21:11:12 +00:00
|
|
|
context.funcProps[propName] = this._getMethod(value);
|
2015-01-23 13:09:30 +00:00
|
|
|
break;
|
2022-05-30 01:30:33 +00:00
|
|
|
case Type:
|
|
|
|
newValue = window[value];
|
|
|
|
break;
|
2015-01-23 13:09:30 +00:00
|
|
|
default:
|
2015-11-09 17:11:51 +00:00
|
|
|
if (propInfo.enumType)
|
|
|
|
newValue = propInfo.enumType[value];
|
2015-12-02 17:26:58 +00:00
|
|
|
else if (propInfo.type instanceof Function)
|
|
|
|
isLink = true;
|
2022-05-24 21:11:12 +00:00
|
|
|
else
|
|
|
|
propError = true;
|
2022-05-24 10:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isLink)
|
|
|
|
this._addLink(context, propName, value);
|
|
|
|
else if (newValue !== null && newValue !== undefined)
|
|
|
|
props[propName] = newValue;
|
2022-05-24 21:11:12 +00:00
|
|
|
else if (propError)
|
|
|
|
this.showError('Attribute \'%s\' invalid for tag \'%s\'',
|
2022-05-24 10:18:44 +00:00
|
|
|
attribute, node.tagName);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-11-09 17:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
,objectInstantiate: function(doc, context, scope) {
|
|
|
|
const object = new context.klass();
|
|
|
|
object.setProperties(context.props);
|
|
|
|
|
2022-06-06 12:49:18 +00:00
|
|
|
if (context.nodeId && object instanceof Component) {
|
2022-05-28 19:27:36 +00:00
|
|
|
var id = context.nodeId;
|
|
|
|
object.htmlId = scope.getHtmlId(id);
|
2022-06-06 17:13:57 +00:00
|
|
|
object.className = '_'+ id +' '+ (object.className || '');
|
2022-05-28 19:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
2015-11-09 17:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
,objectLink: function(context, object, objects, scope, exprScope) {
|
|
|
|
const objectProps = context.objectProps;
|
|
|
|
for (const prop in objectProps)
|
2015-11-09 17:11:51 +00:00
|
|
|
object[prop] = objects[objectProps[prop]];
|
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
const childs = context.childs;
|
|
|
|
for (let i = 0; i < childs.length; i++)
|
2022-05-24 10:18:44 +00:00
|
|
|
object.appendChild(objects[childs[i]]);
|
2022-05-24 21:11:12 +00:00
|
|
|
|
|
|
|
const funcProps = context.funcProps;
|
|
|
|
for (const prop in funcProps) {
|
|
|
|
let method;
|
|
|
|
const handler = funcProps[prop];
|
|
|
|
|
|
|
|
if (typeof handler === 'string') {
|
|
|
|
// XXX: Compatibility with old expressions
|
|
|
|
method = scope.thisArg[handler];
|
|
|
|
if (!method)
|
|
|
|
this.showError(`Function '${handler}' not found`);
|
|
|
|
method = method.bind(scope.thisArg);
|
|
|
|
} else {
|
|
|
|
method = function() {
|
|
|
|
handler.apply(scope.thisArg, exprScope);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method)
|
|
|
|
object[prop] = method;
|
|
|
|
}
|
|
|
|
|
|
|
|
const events = context.events;
|
|
|
|
for (const event in events) {
|
|
|
|
let listener;
|
|
|
|
const handler = events[event];
|
|
|
|
|
|
|
|
if (typeof handler === 'string') {
|
|
|
|
// XXX: Compatibility with old expressions
|
|
|
|
listener = scope.thisArg[handler];
|
|
|
|
if (!listener)
|
|
|
|
this.showError(`Function '${handler}' not found`);
|
|
|
|
} else {
|
|
|
|
listener = function() {
|
|
|
|
handler.apply(scope.thisArg, exprScope.concat(arguments));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listener)
|
|
|
|
object.on(event, listener, scope.thisArg);
|
|
|
|
}
|
2015-11-09 17:11:51 +00:00
|
|
|
|
|
|
|
if (context.custom)
|
2022-05-24 21:11:12 +00:00
|
|
|
object.loadXml(scope, context.custom);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2022-05-24 21:11:12 +00:00
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Element
|
2015-11-09 17:11:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a HTML node context.
|
2022-05-24 10:18:44 +00:00
|
|
|
*/
|
|
|
|
,elementCompile: function(node, tagName) {
|
2015-11-09 17:11:51 +00:00
|
|
|
var attributes = {};
|
2022-05-24 10:18:44 +00:00
|
|
|
var dynProps = {};
|
2015-11-09 17:11:51 +00:00
|
|
|
var childs = [];
|
|
|
|
var events = {};
|
|
|
|
var handler;
|
|
|
|
|
|
|
|
var a = node.attributes;
|
|
|
|
|
2022-05-24 10:18:44 +00:00
|
|
|
for (var i = 0; i < a.length; i++) {
|
2015-11-09 17:11:51 +00:00
|
|
|
var attribute = a[i].nodeName;
|
|
|
|
var value = a[i].nodeValue;
|
2022-05-24 10:18:44 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
if (this._isEvent(attribute)) {
|
2022-05-24 10:18:44 +00:00
|
|
|
var handler = this._getMethod(value);
|
2022-05-24 21:11:12 +00:00
|
|
|
if (handler) events[attribute.substr(3)] = handler;
|
|
|
|
} else if (attribute !== 'id') {
|
|
|
|
const expr = this.matchExpr(value);
|
|
|
|
if (expr)
|
|
|
|
dynProps[attribute] = expr;
|
|
|
|
else
|
|
|
|
attributes[attribute] = this._translateValue(value);
|
|
|
|
}
|
2015-11-09 17:11:51 +00:00
|
|
|
}
|
2015-10-14 11:51:43 +00:00
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
var childContext;
|
|
|
|
var childNodes = node.childNodes;
|
|
|
|
|
|
|
|
if (childNodes)
|
|
|
|
for (var i = 0; i < childNodes.length; i++)
|
2022-05-24 21:11:12 +00:00
|
|
|
if (childContext = this._compile(childNodes[i]))
|
2022-05-24 10:18:44 +00:00
|
|
|
childs.push(childContext.id);
|
2015-11-09 17:11:51 +00:00
|
|
|
|
|
|
|
return {
|
2022-05-24 10:18:44 +00:00
|
|
|
tagName,
|
|
|
|
attributes,
|
|
|
|
dynProps,
|
|
|
|
childs,
|
|
|
|
events
|
2015-11-09 17:11:51 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
,elementInstantiate: function(doc, context, scope) {
|
|
|
|
var object = doc.createElement(context.tagName);
|
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
const attributes = context.attributes;
|
|
|
|
for (const attribute in attributes)
|
2022-05-24 10:18:44 +00:00
|
|
|
object.setAttribute(attribute, attributes[attribute]);
|
2015-10-14 11:51:43 +00:00
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
if (context.nodeId) {
|
|
|
|
const id = context.nodeId;
|
|
|
|
object.setAttribute('id', scope.getHtmlId(id));
|
|
|
|
VnNode.addClass(object, '_'+ id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
,elementLink: function(context, object, objects, scope, exprScope) {
|
2022-05-24 21:11:12 +00:00
|
|
|
const childs = context.childs;
|
2022-05-24 10:18:44 +00:00
|
|
|
for (var i = 0; i < childs.length; i++) {
|
2022-05-24 21:11:12 +00:00
|
|
|
let child = objects[childs[i]];
|
2015-11-09 17:11:51 +00:00
|
|
|
|
2022-06-06 12:49:18 +00:00
|
|
|
if (child instanceof Component)
|
2016-10-16 14:16:08 +00:00
|
|
|
child = child.node;
|
2015-11-17 10:34:33 +00:00
|
|
|
if (child instanceof Node)
|
2022-05-24 10:18:44 +00:00
|
|
|
object.appendChild(child);
|
2015-11-09 17:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
const events = context.events;
|
|
|
|
for (const event in events) {
|
|
|
|
let listener;
|
|
|
|
const handler = events[event];
|
|
|
|
if (typeof handler === 'string') {
|
|
|
|
// XXX: Compatibility with old expressions
|
|
|
|
listener = scope.thisArg[handler];
|
|
|
|
if (!listener)
|
|
|
|
this.showError(`Function '${handler}' not found`);
|
|
|
|
listener = listener.bind(scope.thisArg);
|
|
|
|
} else {
|
|
|
|
listener = function(e) {
|
|
|
|
handler.apply(scope.thisArg, exprScope.concat(e));
|
|
|
|
};
|
|
|
|
}
|
2015-08-17 18:02:14 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
if (listener)
|
|
|
|
object.addEventListener(event, listener);
|
|
|
|
}
|
2015-07-28 19:14:26 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
});
|