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) {
|
2010-03-26 23:27:18 +00:00
|
|
|
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 03:20:05 +00:00
|
|
|
}
|
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){
|
2010-03-30 03:25:42 +00:00
|
|
|
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];
|
2010-03-30 03:25:42 +00:00
|
|
|
attrValue[attr.name] = attr.value;
|
2010-03-23 21:57:11 +00:00
|
|
|
}
|
2010-03-30 03:25:42 +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){
|
2010-03-30 03:25:42 +00:00
|
|
|
parentScope = parentScope || {};
|
2010-03-26 23:27:18 +00:00
|
|
|
var scope = createScope(parentScope);
|
2010-03-30 03:25:42 +00:00
|
|
|
parentScope.$root = parentScope.$root || scope;
|
2010-03-26 23:27:18 +00:00
|
|
|
return extend(scope, {
|
2010-03-26 05:03:11 +00:00
|
|
|
$element:element,
|
2010-03-26 23:27:18 +00:00
|
|
|
$init: function() {
|
|
|
|
|
template.init(element, scope);
|
|
|
|
|
scope.$eval();
|
2010-03-30 03:25:42 +00:00
|
|
|
return scope;
|
2010-03-26 23:27:18 +00:00
|
|
|
}
|
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,
|
2010-03-24 23:13:42 +00:00
|
|
|
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
|
2010-03-30 03:25:42 +00:00
|
|
|
eachAttribute(element, function(value, name){
|
2010-03-23 21:57:11 +00:00
|
|
|
foreach(self.attrMarkup, function(markup){
|
|
|
|
|
markup.call(selfApi, value, name, element);
|
|
|
|
|
});
|
|
|
|
|
});
|
2010-03-30 03:25:42 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
};
|