This repository has been archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
part of angular.core.dom; | ||
|
||
class NgElement { | ||
|
||
final dom.Element node; | ||
final Scope _scope; | ||
final NgAnimate _animate; | ||
final _classes = new Map<String, bool>(); | ||
|
||
NgElement(this.node, this._scope, NgAnimate this._animate); | ||
|
||
addClass(String className) { | ||
if(_classes.isEmpty) { | ||
_listenOnWrite(); | ||
} | ||
_classes[className] = true; | ||
} | ||
|
||
removeClass(String className) { | ||
if(_classes.isEmpty) { | ||
_listenOnWrite(); | ||
} | ||
_classes[className] = false; | ||
} | ||
|
||
_listenOnWrite() { | ||
_scope.rootScope.domWrite(() => flush()); | ||
} | ||
|
||
flush() { | ||
_classes.forEach((className, status) { | ||
status == true | ||
? _animate.addClass(node, className) | ||
: _animate.removeClass(node, className); | ||
}); | ||
_classes.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
library ng_element_spec; | ||
|
||
import '../_specs.dart'; | ||
import 'dart:html' as dom; | ||
|
||
void main() { | ||
describe('ngElement', () { | ||
|
||
it('should add classes on domWrite to the element', | ||
inject((TestBed _, NgAnimate animate) { | ||
|
||
var scope = _.rootScope; | ||
var element = _.compile('<div></div>'); | ||
var ngElement = new NgElement(element, scope, animate); | ||
|
||
ngElement.addClass('one'); | ||
ngElement.addClass('two three'); | ||
|
||
['one','two','three'].forEach((className) { | ||
expect(element.classes.contains(className)).toBe(false); | ||
}); | ||
|
||
scope.apply(); | ||
|
||
['one','two','three'].forEach((className) { | ||
expect(element.classes.contains(className)).toBe(true); | ||
}); | ||
})); | ||
|
||
it('should remove classes on domWrite to the element', | ||
inject((TestBed _, NgAnimate animate) { | ||
|
||
var scope = _.rootScope; | ||
var element = _.compile('<div class="one two three four"></div>'); | ||
var ngElement = new NgElement(element, scope, animate); | ||
|
||
ngElement.removeClass('one'); | ||
ngElement.removeClass('two'); | ||
ngElement.removeClass('three'); | ||
|
||
['one','two','three'].forEach((className) { | ||
expect(element.classes.contains(className)).toBe(true); | ||
}); | ||
expect(element.classes.contains('four')).toBe(true); | ||
|
||
scope.apply(); | ||
|
||
['one','two','three'].forEach((className) { | ||
expect(element.classes.contains(className)).toBe(false); | ||
}); | ||
expect(element.classes.contains('four')).toBe(true); | ||
})); | ||
|
||
it('should always apply the last dom operation on the given className', | ||
inject((TestBed _, NgAnimate animate) { | ||
|
||
var scope = _.rootScope; | ||
var element = _.compile('<div></div>'); | ||
var ngElement = new NgElement(element, scope, animate); | ||
|
||
ngElement.addClass('one'); | ||
ngElement.addClass('one'); | ||
ngElement.removeClass('one'); | ||
|
||
expect(element.classes.contains('one')).toBe(false); | ||
|
||
scope.apply(); | ||
|
||
expect(element.classes.contains('one')).toBe(false); | ||
|
||
element.classes.add('one'); | ||
|
||
ngElement.removeClass('one'); | ||
ngElement.removeClass('one'); | ||
ngElement.addClass('one'); | ||
|
||
expect(element.classes.contains('one')).toBe(true); | ||
})); | ||
}); | ||
} |