Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Unified Diff: third_party/pkg/angular/lib/directive/ng_if.dart

Issue 124053002: Adding Angular and dependent packages for testing (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/pkg/angular/lib/directive/ng_if.dart
diff --git a/third_party/pkg/angular/lib/directive/ng_if.dart b/third_party/pkg/angular/lib/directive/ng_if.dart
new file mode 100644
index 0000000000000000000000000000000000000000..5c026443aa0e057996f8fbfcc4094a36493b2822
--- /dev/null
+++ b/third_party/pkg/angular/lib/directive/ng_if.dart
@@ -0,0 +1,161 @@
+part of angular.directive;
+
+/**
+ * Base class for NgIfAttrDirective and NgUnlessAttrDirective.
+ */
+abstract class _NgUnlessIfAttrDirectiveBase {
+ final BoundBlockFactory _boundBlockFactory;
+ final BlockHole _blockHole;
+ final Scope _scope;
+
+ Block _block;
+
+ /**
+ * The new child scope. This child scope is recreated whenever the `ng-if`
+ * subtree is inserted into the DOM and destroyed when it's removed from the
+ * DOM. Refer
+ * https://github.com/angular/angular.js/wiki/The-Nuances-of-Scope-Prototypal-Inheritance prototypal inheritance
+ */
+ Scope _childScope;
+
+ _NgUnlessIfAttrDirectiveBase(this._boundBlockFactory, this._blockHole, this._scope);
+
+ // Override in subclass.
+ set condition(value);
+
+ void _ensureBlockExists() {
+ if (_block == null) {
+ _childScope = _scope.$new();
+ _block = _boundBlockFactory(_childScope);
+ _block.insertAfter(_blockHole);
+ }
+ }
+
+ void _ensureBlockDestroyed() {
+ if (_block != null) {
+ _block.remove();
+ _childScope.$destroy();
+ _block = null;
+ _childScope = null;
+ }
+ }
+}
+
+
+/**
+ * The `ng-if` directive compliments the `ng-unless` (provided by
+ * [NgUnlessAttrDirective]) directive.
+ *
+ * directive based on the **truthy/falsy** value of the provided expression.
+ * Specifically, if the expression assigned to `ng-if` evaluates to a `false`
+ * value, then the subtree is removed from the DOM. Otherwise, *a clone of the
+ * subtree* is reinserted into the DOM. This clone is created from the compiled
+ * state. As such, modifications made to the element after compilation (e.g.
+ * changing the `class`) are lost when the element is destroyed.
+ *
+ * Whenever the subtree is inserted into the DOM, it always gets a new child
+ * scope. This child scope is destroyed when the subtree is removed from the
+ * DOM. Refer
+ * https://github.com/angular/angular.js/wiki/The-Nuances-of-Scope-Prototypal-Inheritance prototypal inheritance
+ *
+ * This has an important implication when `ng-model` is used inside an `ng-if`
+ * to bind to a javascript primitive defined in the parent scope. In such a
+ * situation, any modifications made to the variable in the `ng-if` subtree will
+ * be made on the child scope and override (hide) the value in the parent scope.
+ * The parent scope will remain unchanged by changes affected by this subtree.
+ *
+ * Note: `ng-if` differs from `ng-show` and `ng-hide` in that `ng-if` completely
+ * removes and recreates the element in the DOM rather than changing its
+ * visibility via the `display` css property. A common case when this
+ * difference is significant is when using css selectors that rely on an
+ * element's position within the DOM (HTML), such as the `:first-child` or
+ * `:last-child` pseudo-classes.
+ *
+ * Example:
+ *
+ * <!-- By using ng-if instead of ng-show, we avoid the cost of the showdown
+ * filter, the repeater, etc. -->
+ * <div ng-if="showDetails">
+ * {{obj.details.markdownText | showdown}}
+ * <div ng-repeat="item in obj.details.items">
+ * ...
+ * </div>
+ * </div>
+ */
+@NgDirective(
+ children: NgAnnotation.TRANSCLUDE_CHILDREN,
+ selector:'[ng-if]',
+ map: const {'.': '=>condition'})
+class NgIfDirective extends _NgUnlessIfAttrDirectiveBase {
+ NgIfDirective(BoundBlockFactory boundBlockFactory,
+ BlockHole blockHole,
+ Scope scope): super(boundBlockFactory, blockHole, scope);
+
+ set condition(value) {
+ if (toBool(value))
+ _ensureBlockExists();
+ else
+ _ensureBlockDestroyed();
+ }
+}
+
+
+/**
+ * The `ng-unless` directive compliments the `ng-if` (provided by
+ * [NgIfAttrDirective]) directive.
+ *
+ * The `ng-unless` directive recreates/destroys the DOM subtree containing the
+ * directive based on the **falsy/truthy** value of the provided expression.
+ * Specifically, if the expression assigned to `ng-unless` evaluates to a `true`
+ * value, then the subtree is removed from the DOM. Otherwise, *a clone of the
+ * subtree* is reinserted into the DOM. This clone is created from the compiled
+ * state. As such, modifications made to the element after compilation (e.g.
+ * changing the `class`) are lost when the element is destroyed.
+ *
+ * Whenever the subtree is inserted into the DOM, it always gets a new child
+ * scope. This child scope is destroyed when the subtree is removed from the
+ * DOM. Refer
+ * https://github.com/angular/angular.js/wiki/The-Nuances-of-Scope-Prototypal-Inheritance prototypal inheritance
+ *
+ * This has an important implication when `ng-model` is used inside an
+ * `ng-unless` to bind to a javascript primitive defined in the parent scope.
+ * In such a situation, any modifications made to the variable in the
+ * `ng-unless` subtree will be made on the child scope and override (hide) the
+ * value in the parent scope. The parent scope will remain unchanged by changes
+ * affected by this subtree.
+ *
+ * Note: `ng-unless` differs from `ng-show` and `ng-hide` in that `ng-unless`
+ * completely removes and recreates the element in the DOM rather than changing
+ * its visibility via the `display` css property. A common case when this
+ * difference is significant is when using css selectors that rely on an
+ * element's position within the DOM (HTML), such as the `:first-child` or
+ * `:last-child` pseudo-classes.
+ *
+ * Example:
+ *
+ * <!-- By using ng-unless instead of ng-show, we avoid the cost of the showdown
+ * filter, the repeater, etc. -->
+ * <div ng-unless="terseView">
+ * {{obj.details.markdownText | showdown}}
+ * <div ng-repeat="item in obj.details.items">
+ * ...
+ * </div>
+ * </div>
+ */
+@NgDirective(
+ children: NgAnnotation.TRANSCLUDE_CHILDREN,
+ selector:'[ng-unless]',
+ map: const {'.': '=>condition'})
+class NgUnlessDirective extends _NgUnlessIfAttrDirectiveBase {
+
+ NgUnlessDirective(BoundBlockFactory boundBlockFactory,
+ BlockHole blockHole,
+ Scope scope): super(boundBlockFactory, blockHole, scope);
+
+ set condition(value) {
+ if (!toBool(value))
+ _ensureBlockExists();
+ else
+ _ensureBlockDestroyed();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698