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

Side by Side Diff: third_party/pkg/angular/lib/directive/ng_class.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, 11 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 part of angular.directive;
2
3 /**
4 * The `ngClass` allows you to set CSS classes on HTML an element, dynamically,
5 * by databinding an expression that represents all classes to be added.
6 *
7 * The directive won't add duplicate classes if a particular class was
8 * already set.
9 *
10 * When the expression changes, the previously added classes are removed and
11 * only then the new classes are added.
12 *
13 * The result of the expression evaluation can be a string representing space
14 * delimited class names, an array, or a map of class names to boolean values.
15 * In the case of a map, the names of the properties whose values are truthy
16 * will be added as css classes to the element.
17 *
18 * ##Examples
19 *
20 * index.html:
21 *
22 * <p ng-class="{strike: strike, bold: bold, red: red}">Map Syntax Example</ p>
23 * <input type="checkbox" ng-model="bold"> bold
24 * <input type="checkbox" ng-model="strike"> strike
25 * <input type="checkbox" ng-model="red"> red
26 * <hr>
27 * <p ng-class="style">Using String Syntax</p>
28 * <input type="text" ng-model="style" placeholder="Type: bold strike red">
29 * <hr>
30 * <p ng-class="[style1, style2, style3]">Using Array Syntax</p>
31 * <input ng-model="style1" placeholder="Type: bold"><br>
32 * <input ng-model="style2" placeholder="Type: strike"><br>
33 * <input ng-model="style3" placeholder="Type: red"><br>
34 *
35 * style.css:
36 *
37 * .strike {
38 * text-decoration: line-through;
39 * }
40 * .bold {
41 * font-weight: bold;
42 * }
43 * .red {
44 * color: red;
45 * }
46 *
47 */
48 @NgDirective(
49 selector: '[ng-class]',
50 map: const {'ng-class': '@valueExpression'},
51 exportExpressionAttrs: const ['ng-class'])
52 class NgClassDirective extends _NgClassBase {
53 NgClassDirective(dom.Element element, Scope scope, NodeAttrs attrs)
54 : super(element, scope, null, attrs);
55 }
56
57 /**
58 * The `ngClassOdd` and `ngClassEven` directives work exactly as
59 * {@link ng.directive:ngClass ngClass}, except it works in
60 * conjunction with `ngRepeat` and takes affect only on odd (even) rows.
61 *
62 * This directive can be applied only within a scope of an `ngRepeat`.
63 *
64 * ##Examples
65 *
66 * index.html:
67 *
68 * <li ng-repeat="name in ['John', 'Mary', 'Cate', 'Suz']">
69 * <span ng-class-odd="'odd'" ng-class-even="'even'">
70 * {{name}}
71 * </span>
72 * </li>
73 *
74 * style.css:
75 *
76 * .odd {
77 * color: red;
78 * }
79 * .even {
80 * color: blue;
81 * }
82 */
83 @NgDirective(
84 selector: '[ng-class-odd]',
85 map: const {'ng-class-odd': '@valueExpression'},
86 exportExpressionAttrs: const ['ng-class-odd'])
87 class NgClassOddDirective extends _NgClassBase {
88 NgClassOddDirective(dom.Element element, Scope scope, NodeAttrs attrs)
89 : super(element, scope, 0, attrs);
90 }
91
92 /**
93 * The `ngClassOdd` and `ngClassEven` directives work exactly as
94 * {@link ng.directive:ngClass ngClass}, except it works in
95 * conjunction with `ngRepeat` and takes affect only on odd (even) rows.
96 *
97 * This directive can be applied only within a scope of an `ngRepeat`.
98 *
99 * ##Examples
100 *
101 * index.html:
102 *
103 * <li ng-repeat="name in ['John', 'Mary', 'Cate', 'Suz']">
104 * <span ng-class-odd="'odd'" ng-class-even="'even'">
105 * {{name}}
106 * </span>
107 * </li>
108 *
109 * style.css:
110 *
111 * .odd {
112 * color: red;
113 * }
114 * .even {
115 * color: blue;
116 * }
117 */
118 @NgDirective(
119 selector: '[ng-class-even]',
120 map: const {'ng-class-even': '@valueExpression'},
121 exportExpressionAttrs: const ['ng-class-even'])
122 class NgClassEvenDirective extends _NgClassBase {
123 NgClassEvenDirective(dom.Element element, Scope scope, NodeAttrs attrs)
124 : super(element, scope, 1, attrs);
125 }
126
127 abstract class _NgClassBase {
128 final dom.Element element;
129 final Scope scope;
130 final int mode;
131 final NodeAttrs nodeAttrs;
132 var previousSet = [];
133 var currentSet = [];
134
135 _NgClassBase(this.element, this.scope, this.mode, this.nodeAttrs) {
136 var prevClass;
137
138 nodeAttrs.observe('class', (String newValue) {
139 if (prevClass != newValue) {
140 prevClass = newValue;
141 _handleChange(scope[r'$index']);
142 }
143 });
144 }
145
146 set valueExpression(currentExpression) {
147 // this should be called only once, so we don't worry about cleaning up
148 // watcher registrations.
149 scope.$watchCollection(currentExpression, (current) {
150 currentSet = _flatten(current);
151 _handleChange(scope[r'$index']);
152 });
153 if (mode != null) {
154 scope.$watch(r'$index', (index, oldIndex) {
155 var mod = index % 2;
156 if (oldIndex == null || mod != oldIndex % 2) {
157 if (mod == mode) {
158 element.classes.addAll(currentSet);
159 } else {
160 element.classes.removeAll(previousSet);
161 }
162 }
163 });
164 }
165 }
166
167 _handleChange(index) {
168 if (mode == null || (index != null && index % 2 == mode)) {
169 element.classes.removeAll(previousSet);
170 element.classes.addAll(currentSet);
171 }
172
173 previousSet = currentSet;
174 }
175
176 static List<String> _flatten(classes) {
177 if (classes == null) return [];
178 if (classes is List) {
179 return classes;
180 }
181 if (classes is Map) {
182 return classes.keys.where((key) => toBool(classes[key])).toList();
183 }
184 if (classes is String) {
185 return classes.split(' ');
186 }
187 throw 'ng-class expects expression value to be List, Map or String.';
188 }
189 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698