angular.js/src/Compiler.js

220 lines
6 KiB
JavaScript
Raw Normal View History

2010-03-19 23:41:02 +00:00
/**
2010-04-07 17:17:15 +00:00
* Template provides directions an how to bind to a given element.
2010-03-19 23:41:02 +00:00
* 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
*/
2010-04-12 23:24:28 +00:00
function Template(priority) {
2010-03-19 23:41:02 +00:00
this.paths = [];
this.children = [];
this.inits = [];
2010-04-12 23:24:28 +00:00
this.priority = priority || 0;
2010-03-19 23:41:02 +00:00
}
Template.prototype = {
init: function(element, scope) {
2010-04-12 23:24:28 +00:00
var inits = {};
this.collectInits(element, inits);
foreachSorted(inits, function(queue){
foreach(queue, function(fn){
fn(scope);
});
});
},
collectInits: function(element, inits) {
var queue = inits[this.priority];
if (!queue) {
inits[this.priority] = queue = [];
}
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-04-12 23:24:28 +00:00
queue.push(function(scope) {
2010-07-26 22:32:08 +00:00
scope.$tryEval(function(){
return fn.call(scope, element);
}, element);
2010-04-12 23:24:28 +00:00
});
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++) {
2010-04-12 23:24:28 +00:00
children[i].collectInits(childNodes[paths[i]], inits);
2010-03-19 23:41:02 +00:00
}
},
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() {
2010-04-04 00:04:36 +00:00
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 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);
2010-04-30 19:22:07 +00:00
var index = 0,
template,
parent = rawElement.parent();
if (parent && parent[0]) {
parent = parent[0];
for(var i = 0; i < parent.childNodes.length; i++) {
if (parent.childNodes[i] == rawElement[0]) {
index = i;
}
}
}
template = this.templatize(rawElement, index, 0) || new Template();
2010-03-22 20:58:04 +00:00
return function(element, parentScope){
2010-03-30 04:36:34 +00:00
element = jqLite(element);
2010-04-04 00:04:36 +00:00
var scope = parentScope && parentScope.$eval ?
parentScope :
createScope(parentScope || {}, angularService);
return extend(scope, {
2010-03-26 05:03:11 +00:00
$element:element,
$init: function() {
template.init(element, scope);
scope.$eval();
2010-04-05 18:46:53 +00:00
delete scope.$init;
return scope;
}
2010-03-26 05:03:11 +00:00
});
2010-03-19 23:41:02 +00:00
};
},
templatize: function(element, elementIndex, priority){
2010-03-19 23:41:02 +00:00
var self = this,
2010-03-30 21:55:04 +00:00
widget,
directiveFns = self.directives,
2010-03-22 20:58:04 +00:00
descend = true,
2010-03-30 21:55:04 +00:00
directives = true,
2010-04-12 23:24:28 +00:00
template,
2010-03-22 20:58:04 +00:00
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-30 21:55:04 +00:00
descend: function(value){ if(isDefined(value)) descend = value; return descend;},
directives: function(value){ if(isDefined(value)) directives = value; return directives;}
2010-03-22 20:58:04 +00:00
};
try {
priority = element.attr('ng:eval-order') || priority || 0;
} catch (e) {
// for some reason IE throws error under some weird circumstances. so just assume nothing
priority = priority || 0;
}
2010-04-12 23:24:28 +00:00
if (isString(priority)) {
priority = PRIORITY[uppercase(priority)] || 0;
}
template = new Template(priority);
2010-03-30 21:55:04 +00:00
eachAttribute(element, function(value, name){
if (!widget) {
if (widget = self.widgets('@' + name)) {
2010-03-30 21:55:04 +00:00
widget = bind(selfApi, widget, value, element);
}
}
});
if (!widget) {
if (widget = self.widgets(nodeName(element))) {
2010-03-30 21:55:04 +00:00
widget = bind(selfApi, widget, element);
}
}
2010-03-22 20:58:04 +00:00
if (widget) {
2010-03-25 21:43:05 +00:00
descend = false;
2010-03-30 21:55:04 +00:00
directives = false;
var parent = element.parent();
2010-03-22 20:58:04 +00:00
template.addInit(widget.call(selfApi, element));
if (parent && parent[0]) {
element = jqLite(parent[0].childNodes[elementIndex]);
}
2010-03-30 21:55:04 +00:00
}
if (descend){
2010-03-22 20:58:04 +00:00
// 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){
2010-03-23 21:57:11 +00:00
markup.call(selfApi, text, textNode, element);
});
2010-03-22 20:58:04 +00:00
});
2010-03-30 21:55:04 +00:00
}
2010-03-19 23:41:02 +00:00
2010-03-30 21:55:04 +00:00
if (directives) {
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-30 21:55:04 +00:00
template.addInit((directiveFns[name]||noop).call(selfApi, value, element));
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, i, priority));
2010-03-25 21:43:05 +00:00
});
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
}
};
2010-03-30 21:55:04 +00:00
function eachTextNode(element, fn){
2010-04-09 23:20:15 +00:00
var i, chldNodes = element[0].childNodes || [], chld;
for (i = 0; i < chldNodes.length; i++) {
2010-03-30 21:55:04 +00:00
if(isTextNode(chld = chldNodes[i])) {
fn(jqLite(chld), i);
}
}
}
function eachNode(element, fn){
2010-04-09 23:20:15 +00:00
var i, chldNodes = element[0].childNodes || [], chld;
for (i = 0; i < chldNodes.length; i++) {
2010-03-30 21:55:04 +00:00
if(!isTextNode(chld = chldNodes[i])) {
fn(jqLite(chld), i);
}
}
}
function eachAttribute(element, fn){
2010-04-20 00:02:46 +00:00
var i, attrs = element[0].attributes || [], chld, attr, name, value, attrValue = {};
2010-04-09 23:20:15 +00:00
for (i = 0; i < attrs.length; i++) {
2010-04-04 00:04:36 +00:00
attr = attrs[i];
name = attr.name;
2010-04-20 00:02:46 +00:00
value = attr.value;
if (msie && name == 'href') {
value = decodeURIComponent(element[0].getAttribute(name, 2));
}
attrValue[name] = value;
2010-03-30 21:55:04 +00:00
}
2010-04-09 23:20:15 +00:00
foreachSorted(attrValue, fn);
2010-03-30 21:55:04 +00:00
}