angular.js/src/Compiler.js

170 lines
4.5 KiB
JavaScript
Raw Normal View History

2010-03-19 23:41:02 +00:00
/**
* Template provides directions an how to bind to a given element.
* It contains a list of init functions which need to be called to
* bind to a new instance of elements. It also provides a list
* of child paths which contain child templates
*/
function Template() {
this.paths = [];
this.children = [];
this.inits = [];
}
Template.prototype = {
init: function(element, scope) {
2010-03-23 04:29:57 +00:00
element = jqLite(element);
2010-03-19 23:41:02 +00:00
foreach(this.inits, function(fn) {
2010-03-26 05:03:11 +00:00
scope.$tryEval(fn, element, element);
2010-03-19 23:41:02 +00:00
});
var i,
2010-03-23 04:29:57 +00:00
childNodes = element[0].childNodes,
2010-03-19 23:41:02 +00:00
children = this.children,
paths = this.paths,
length = paths.length;
for (i = 0; i < length; i++) {
children[i].init(childNodes[paths[i]], scope);
}
},
2010-03-20 05:18:39 +00:00
2010-03-19 23:41:02 +00:00
addInit:function(init) {
if (init) {
this.inits.push(init);
}
},
addChild: function(index, template) {
2010-03-22 20:58:04 +00:00
if (template) {
this.paths.push(index);
this.children.push(template);
}
2010-03-20 05:18:39 +00:00
},
empty: function() {
return this.inits.length == 0 && this.paths.length == 0;
2010-03-19 23:41:02 +00:00
}
};
///////////////////////////////////
2010-03-23 21:57:11 +00:00
//Compiler
2010-03-19 23:41:02 +00:00
//////////////////////////////////
2010-03-23 21:57:11 +00:00
function isTextNode(node) {
return node.nodeName == '#text';
2010-03-19 23:41:02 +00:00
}
2010-03-23 21:57:11 +00:00
function eachTextNode(element, fn){
var i, chldNodes = element[0].childNodes || [], size = chldNodes.length, chld;
for (i = 0; i < size; i++) {
if(isTextNode(chld = chldNodes[i])) {
fn(jqLite(chld), i);
}
2010-03-22 20:58:04 +00:00
}
2010-03-19 23:41:02 +00:00
}
2010-03-23 21:57:11 +00:00
function eachNode(element, fn){
var i, chldNodes = element[0].childNodes || [], size = chldNodes.length, chld;
for (i = 0; i < size; i++) {
if(!isTextNode(chld = chldNodes[i])) {
fn(jqLite(chld), i);
}
2010-03-23 21:57:11 +00:00
}
}
2010-03-19 23:41:02 +00:00
2010-03-23 21:57:11 +00:00
function eachAttribute(element, fn){
var i, attrs = element[0].attributes || [], size = attrs.length, chld, attr, attrValue = {};
2010-03-23 21:57:11 +00:00
for (i = 0; i < size; i++) {
var attr = attrs[i];
attrValue[attr.name] = attr.value;
2010-03-23 21:57:11 +00:00
}
foreach(attrValue, fn);
2010-03-23 21:57:11 +00:00
}
2010-03-19 23:41:02 +00:00
2010-03-23 21:57:11 +00:00
function Compiler(textMarkup, attrMarkup, directives, widgets){
this.textMarkup = textMarkup;
this.attrMarkup = attrMarkup;
2010-03-19 23:41:02 +00:00
this.directives = directives;
this.widgets = widgets;
}
Compiler.prototype = {
2010-03-22 20:58:04 +00:00
compile: function(rawElement) {
rawElement = jqLite(rawElement);
var template = this.templatize(rawElement) || new Template();
return function(element, parentScope){
parentScope = parentScope || {};
var scope = createScope(parentScope);
parentScope.$root = parentScope.$root || scope;
return extend(scope, {
2010-03-26 05:03:11 +00:00
$element:element,
$init: function() {
template.init(element, scope);
scope.$eval();
return scope;
}
2010-03-26 05:03:11 +00:00
});
2010-03-19 23:41:02 +00:00
};
},
2010-03-22 20:58:04 +00:00
templatize: function(element){
2010-03-19 23:41:02 +00:00
var self = this,
widget = self.widgets[element[0].nodeName],
2010-03-19 23:41:02 +00:00
directives = self.directives,
2010-03-22 20:58:04 +00:00
descend = true,
2010-03-19 23:41:02 +00:00
exclusive = false,
2010-03-20 05:18:39 +00:00
directiveQueue = [],
2010-03-22 20:58:04 +00:00
template = new Template(),
selfApi = {
compile: bind(self, self.compile),
2010-03-23 21:57:11 +00:00
comment:function(text) {return jqLite(document.createComment(text));},
element:function(type) {return jqLite(document.createElement(type));},
text:function(text) {return jqLite(document.createTextNode(text));},
2010-03-22 20:58:04 +00:00
descend: function(value){ if(isDefined(value)) descend = value; return descend;}
};
if (widget) {
2010-03-25 21:43:05 +00:00
descend = false;
2010-03-22 20:58:04 +00:00
template.addInit(widget.call(selfApi, element));
} else {
// process markup for text nodes only
2010-03-23 21:57:11 +00:00
eachTextNode(element, function(textNode){
var text = textNode.text();
foreach(self.textMarkup, function(markup){
markup.call(selfApi, text, textNode, element);
});
2010-03-22 20:58:04 +00:00
});
2010-03-19 23:41:02 +00:00
2010-03-22 20:58:04 +00:00
// Process attributes/directives
eachAttribute(element, function(value, name){
2010-03-23 21:57:11 +00:00
foreach(self.attrMarkup, function(markup){
markup.call(selfApi, value, name, element);
});
});
eachAttribute(element, function(value, name){
2010-03-22 20:58:04 +00:00
var directive = directives[name];
if (!exclusive && directive) {
2010-03-19 23:41:02 +00:00
if (directive.exclusive) {
exclusive = true;
2010-03-20 05:18:39 +00:00
directiveQueue = [];
2010-03-19 23:41:02 +00:00
}
2010-03-26 05:03:11 +00:00
directiveQueue.push(bind(selfApi, directive, value, element));
2010-03-19 23:41:02 +00:00
}
2010-03-22 20:58:04 +00:00
});
2010-03-20 05:18:39 +00:00
2010-03-22 20:58:04 +00:00
// Execute directives
foreach(directiveQueue, function(directive){
template.addInit(directive());
2010-03-19 23:41:02 +00:00
});
2010-03-22 20:58:04 +00:00
2010-03-25 21:43:05 +00:00
}
// Process non text child nodes
if (descend) {
eachNode(element, function(child, i){
template.addChild(i, self.templatize(child));
});
2010-03-19 23:41:02 +00:00
}
2010-03-20 05:18:39 +00:00
return template.empty() ? null : template;
2010-03-19 23:41:02 +00:00
}
};