mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-13 17:23:11 +00:00
all angular.js directives now work
This commit is contained in:
parent
a822708674
commit
6ff550cfa9
5 changed files with 121 additions and 45 deletions
|
|
@ -53,17 +53,11 @@ var isVisible = isVisible || function (element) {
|
||||||
return jQuery(element).is(":visible");
|
return jQuery(element).is(":visible");
|
||||||
};
|
};
|
||||||
|
|
||||||
function isDefined(value){
|
function isDefined(value){ return typeof value != 'undefined'; }
|
||||||
return typeof value != 'undefined';
|
function isObject(value){ return typeof value == 'object';}
|
||||||
}
|
function isString(value){ return typeof value == 'string';}
|
||||||
|
function isArray(value) { return value instanceof Array; }
|
||||||
function isObject(value){
|
function isFunction(value){ return typeof value == 'function';}
|
||||||
return typeof value == 'object';
|
|
||||||
}
|
|
||||||
|
|
||||||
function isFunction(value){
|
|
||||||
return typeof value == 'function';
|
|
||||||
}
|
|
||||||
|
|
||||||
function log(a, b, c){
|
function log(a, b, c){
|
||||||
var console = window['console'];
|
var console = window['console'];
|
||||||
|
|
|
||||||
|
|
@ -12,12 +12,13 @@ function Template() {
|
||||||
|
|
||||||
Template.prototype = {
|
Template.prototype = {
|
||||||
init: function(element, scope) {
|
init: function(element, scope) {
|
||||||
|
element = jqLite(element);
|
||||||
foreach(this.inits, function(fn) {
|
foreach(this.inits, function(fn) {
|
||||||
scope.apply(fn, jqLite(element));
|
scope.apply(fn, element);
|
||||||
});
|
});
|
||||||
|
|
||||||
var i,
|
var i,
|
||||||
childNodes = element.childNodes,
|
childNodes = element[0].childNodes,
|
||||||
children = this.children,
|
children = this.children,
|
||||||
paths = this.paths,
|
paths = this.paths,
|
||||||
length = paths.length;
|
length = paths.length;
|
||||||
|
|
@ -82,8 +83,7 @@ function jqClearData(element) {
|
||||||
};
|
};
|
||||||
|
|
||||||
function JQLite(element) {
|
function JQLite(element) {
|
||||||
//todo: change to this[0];
|
this[0] = element;
|
||||||
this.element = element;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function jqLite(element) {
|
function jqLite(element) {
|
||||||
|
|
@ -97,7 +97,7 @@ function jqLite(element) {
|
||||||
|
|
||||||
JQLite.prototype = {
|
JQLite.prototype = {
|
||||||
data: function(key, value) {
|
data: function(key, value) {
|
||||||
var element = this.element,
|
var element = this[0],
|
||||||
cacheId = element[jqName],
|
cacheId = element[jqName],
|
||||||
cache = jqCache[cacheId || -1];
|
cache = jqCache[cacheId || -1];
|
||||||
if (isDefined(value)) {
|
if (isDefined(value)) {
|
||||||
|
|
@ -112,7 +112,7 @@ JQLite.prototype = {
|
||||||
},
|
},
|
||||||
|
|
||||||
removeData: function(){
|
removeData: function(){
|
||||||
jqClearData(this.element);
|
jqClearData(this[0]);
|
||||||
},
|
},
|
||||||
|
|
||||||
dealoc: function(){
|
dealoc: function(){
|
||||||
|
|
@ -121,11 +121,11 @@ JQLite.prototype = {
|
||||||
for ( var i = 0, children = element.childNodes; i < children.length; i++) {
|
for ( var i = 0, children = element.childNodes; i < children.length; i++) {
|
||||||
dealoc(children[0]);
|
dealoc(children[0]);
|
||||||
}
|
}
|
||||||
})(this.element);
|
})(this[0]);
|
||||||
},
|
},
|
||||||
|
|
||||||
bind: function(type, fn){
|
bind: function(type, fn){
|
||||||
var element = this.element,
|
var element = this[0],
|
||||||
bind = this.data('bind'),
|
bind = this.data('bind'),
|
||||||
eventHandler;
|
eventHandler;
|
||||||
if (!bind) this.data('bind', bind = {});
|
if (!bind) this.data('bind', bind = {});
|
||||||
|
|
@ -158,7 +158,7 @@ JQLite.prototype = {
|
||||||
},
|
},
|
||||||
|
|
||||||
eachTextNode: function(fn){
|
eachTextNode: function(fn){
|
||||||
var i, chldNodes = this.element.childNodes || [], size = chldNodes.length, chld;
|
var i, chldNodes = this[0].childNodes || [], size = chldNodes.length, chld;
|
||||||
for (i = 0; i < size; i++) {
|
for (i = 0; i < size; i++) {
|
||||||
if((chld = new JQLite(chldNodes[i])).isText()) {
|
if((chld = new JQLite(chldNodes[i])).isText()) {
|
||||||
fn(chld, i);
|
fn(chld, i);
|
||||||
|
|
@ -168,7 +168,7 @@ JQLite.prototype = {
|
||||||
|
|
||||||
|
|
||||||
eachNode: function(fn){
|
eachNode: function(fn){
|
||||||
var i, chldNodes = this.element.childNodes || [], size = chldNodes.length, chld;
|
var i, chldNodes = this[0].childNodes || [], size = chldNodes.length, chld;
|
||||||
for (i = 0; i < size; i++) {
|
for (i = 0; i < size; i++) {
|
||||||
if(!(chld = new JQLite(chldNodes[i])).isText()) {
|
if(!(chld = new JQLite(chldNodes[i])).isText()) {
|
||||||
fn(chld, i);
|
fn(chld, i);
|
||||||
|
|
@ -177,7 +177,7 @@ JQLite.prototype = {
|
||||||
},
|
},
|
||||||
|
|
||||||
eachAttribute: function(fn){
|
eachAttribute: function(fn){
|
||||||
var i, attrs = this.element.attributes || [], size = attrs.length, chld, attr;
|
var i, attrs = this[0].attributes || [], size = attrs.length, chld, attr;
|
||||||
for (i = 0; i < size; i++) {
|
for (i = 0; i < size; i++) {
|
||||||
var attr = attrs[i];
|
var attr = attrs[i];
|
||||||
fn(attr.name, attr.value);
|
fn(attr.name, attr.value);
|
||||||
|
|
@ -185,25 +185,25 @@ JQLite.prototype = {
|
||||||
},
|
},
|
||||||
|
|
||||||
replaceWith: function(replaceNode) {
|
replaceWith: function(replaceNode) {
|
||||||
this.element.parentNode.replaceChild(jqLite(replaceNode).element, this.element);
|
this[0].parentNode.replaceChild(jqLite(replaceNode)[0], this[0]);
|
||||||
},
|
},
|
||||||
|
|
||||||
remove: function() {
|
remove: function() {
|
||||||
this.dealoc();
|
this.dealoc();
|
||||||
this.element.parentNode.removeChild(this.element);
|
this[0].parentNode.removeChild(this[0]);
|
||||||
},
|
},
|
||||||
|
|
||||||
removeAttr: function(name) {
|
removeAttr: function(name) {
|
||||||
this.element.removeAttribute(name);
|
this[0].removeAttribute(name);
|
||||||
},
|
},
|
||||||
|
|
||||||
after: function(element) {
|
after: function(element) {
|
||||||
this.element.parentNode.insertBefore(jqLite(element).element, this.element.nextSibling);
|
this[0].parentNode.insertBefore(jqLite(element)[0], this[0].nextSibling);
|
||||||
},
|
},
|
||||||
|
|
||||||
hasClass: function(selector) {
|
hasClass: function(selector) {
|
||||||
var className = " " + selector + " ";
|
var className = " " + selector + " ";
|
||||||
if ( (" " + this.element.className + " ").replace(/[\n\t]/g, " ").indexOf( className ) > -1 ) {
|
if ( (" " + this[0].className + " ").replace(/[\n\t]/g, " ").indexOf( className ) > -1 ) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -211,12 +211,25 @@ JQLite.prototype = {
|
||||||
|
|
||||||
addClass: function( selector ) {
|
addClass: function( selector ) {
|
||||||
if (!this.hasClass(selector)) {
|
if (!this.hasClass(selector)) {
|
||||||
this.element.className += ' ' + selector;
|
this[0].className += ' ' + selector;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
css: function(name, value) {
|
||||||
|
var style = this[0].style;
|
||||||
|
if (isString(name)) {
|
||||||
|
if (isDefined(value)) {
|
||||||
|
style[name] = value;
|
||||||
|
} else {
|
||||||
|
return style[name];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
extend(style, name);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
attr: function(name, value){
|
attr: function(name, value){
|
||||||
var e = this.element;
|
var e = this[0];
|
||||||
if (isObject(name)) {
|
if (isObject(name)) {
|
||||||
foreach(name, function(value, name){
|
foreach(name, function(value, name){
|
||||||
e.setAttribute(name, value);
|
e.setAttribute(name, value);
|
||||||
|
|
@ -230,21 +243,21 @@ JQLite.prototype = {
|
||||||
|
|
||||||
text: function(value) {
|
text: function(value) {
|
||||||
if (isDefined(value)) {
|
if (isDefined(value)) {
|
||||||
this.element.textContent = value;
|
this[0].textContent = value;
|
||||||
}
|
}
|
||||||
return this.element.textContent;
|
return this[0].textContent;
|
||||||
},
|
},
|
||||||
|
|
||||||
html: function(value) {
|
html: function(value) {
|
||||||
if (isDefined(value)) {
|
if (isDefined(value)) {
|
||||||
this.element.innerHTML = value;
|
this[0].innerHTML = value;
|
||||||
}
|
}
|
||||||
return this.element.innerHTML;
|
return this[0].innerHTML;
|
||||||
},
|
},
|
||||||
|
|
||||||
parent: function() { return jqLite(this.element.parentNode);},
|
parent: function() { return jqLite(this[0].parentNode);},
|
||||||
isText: function() { return this.element.nodeType == Node.TEXT_NODE; },
|
isText: function() { return this[0].nodeType == Node.TEXT_NODE; },
|
||||||
clone: function() { return jqLite(this.element.cloneNode(true)); }
|
clone: function() { return jqLite(this[0].cloneNode(true)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
///////////////////////////////////
|
///////////////////////////////////
|
||||||
|
|
@ -275,7 +288,7 @@ Compiler.prototype = {
|
||||||
|
|
||||||
templatize: function(element){
|
templatize: function(element){
|
||||||
var self = this,
|
var self = this,
|
||||||
elementName = element.element.nodeName,
|
elementName = element[0].nodeName,
|
||||||
widgets = self.widgets,
|
widgets = self.widgets,
|
||||||
widget = widgets[elementName],
|
widget = widgets[elementName],
|
||||||
markup = self.markup,
|
markup = self.markup,
|
||||||
|
|
|
||||||
|
|
@ -102,13 +102,39 @@ angularDirective("ng-watch", function(expression, element){
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
//Styling
|
function ngClass(selector) {
|
||||||
//
|
return function(expression, element){
|
||||||
//ng-class
|
var existing = element[0].className + ' ';
|
||||||
//ng-class-odd, ng-class-even
|
return function(element){
|
||||||
//ng-style
|
this.$addEval(expression, function(value){
|
||||||
//ng-show, ng-hide
|
if (selector(this.$index)) {
|
||||||
|
if (isArray(value)) value = value.join(' ');
|
||||||
|
element[0].className = (existing + value).replace(/\s\s+/g, ' ');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
angularDirective("ng-class", ngClass(function(){return true;}));
|
||||||
|
angularDirective("ng-class-odd", ngClass(function(i){return i % 2 == 1;}));
|
||||||
|
angularDirective("ng-class-even", ngClass(function(i){return i % 2 == 0;}));
|
||||||
|
|
||||||
|
angularDirective("ng-show", function(expression, element){
|
||||||
|
return function(element){
|
||||||
|
this.$addEval(expression, function(value){
|
||||||
|
element.css('display', toBoolean(value) ? '' : 'none');
|
||||||
|
});
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
angularDirective("ng-hide", function(expression, element){
|
||||||
|
return function(element){
|
||||||
|
this.$addEval(expression, function(value){
|
||||||
|
element.css('display', toBoolean(value) ? 'none' : '');
|
||||||
|
});
|
||||||
|
};
|
||||||
|
});
|
||||||
/////////////////////////////////////////
|
/////////////////////////////////////////
|
||||||
/////////////////////////////////////////
|
/////////////////////////////////////////
|
||||||
/////////////////////////////////////////
|
/////////////////////////////////////////
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ describe('compiler', function(){
|
||||||
directives.directive = function(expression, element){
|
directives.directive = function(expression, element){
|
||||||
log += "found";
|
log += "found";
|
||||||
expect(expression).toEqual("expr");
|
expect(expression).toEqual("expr");
|
||||||
expect(element.element).toEqual(e);
|
expect(element[0]).toEqual(e);
|
||||||
return function initFn() {
|
return function initFn() {
|
||||||
log += ":init";
|
log += ":init";
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ describe("directives", function(){
|
||||||
var compiler = new Compiler(angularMarkup, angularDirective, angularWidget);
|
var compiler = new Compiler(angularMarkup, angularDirective, angularWidget);
|
||||||
compile = function(html) {
|
compile = function(html) {
|
||||||
element = jqLite(html);
|
element = jqLite(html);
|
||||||
var view = compiler.compile(element.element)(element.element);
|
var view = compiler.compile(element)(element);
|
||||||
view.init();
|
view.init();
|
||||||
return view.scope;
|
return view.scope;
|
||||||
};
|
};
|
||||||
|
|
@ -108,4 +108,47 @@ describe("directives", function(){
|
||||||
element.click();
|
element.click();
|
||||||
expect(scope.get('clicked')).toEqual(true);
|
expect(scope.get('clicked')).toEqual(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should ng-class', function(){
|
||||||
|
var scope = compile('<div class="existing" ng-class="[\'A\', \'B\']"></div>');
|
||||||
|
scope.updateView();
|
||||||
|
expect(element.hasClass('existing')).toBeTruthy();
|
||||||
|
expect(element.hasClass('A')).toBeTruthy();
|
||||||
|
expect(element.hasClass('B')).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should ng-class odd/even', function(){
|
||||||
|
var scope = compile('<ul><li ng-repeat="i in [0,1]" class="existing" ng-class-odd="\'odd\'" ng-class-even="\'even\'"></li><ul>');
|
||||||
|
scope.updateView();
|
||||||
|
var e1 = jQuery(element.parent()[0]).find('li:first');
|
||||||
|
var e2 = jQuery(element.parent()[0]).find('li:last');
|
||||||
|
expect(e1.hasClass('existing')).toBeTruthy();
|
||||||
|
expect(e1.hasClass('even')).toBeTruthy();
|
||||||
|
expect(e2.hasClass('existing')).toBeTruthy();
|
||||||
|
expect(e2.hasClass('odd')).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should ng-style', function(){
|
||||||
|
var scope = compile('<div ng-style="{color:\'red\'}"></div>');
|
||||||
|
scope.updateView();
|
||||||
|
expect(element.css('color')).toEqual('red');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should ng-show', function(){
|
||||||
|
var scope = compile('<div ng-hide="hide"></div>');
|
||||||
|
scope.updateView();
|
||||||
|
expect(element.css('display')).toEqual('');
|
||||||
|
scope.set('hide', true);
|
||||||
|
scope.updateView();
|
||||||
|
expect(element.css('display')).toEqual('none');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should ng-hide', function(){
|
||||||
|
var scope = compile('<div ng-show="show"></div>');
|
||||||
|
scope.updateView();
|
||||||
|
expect(element.css('display')).toEqual('none');
|
||||||
|
scope.set('show', true);
|
||||||
|
scope.updateView();
|
||||||
|
expect(element.css('display')).toEqual('');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue