@angular-wave/angular.ts 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/angular-ts.esm.js +1 -1
- package/dist/angular-ts.umd.js +1 -1
- package/package.json +1 -1
- package/rollup.config.js +2 -5
- package/src/core/compile.js +69 -104
- package/src/directive/if.js +2 -7
- package/src/directive/repeat.js +5 -346
- package/src/directive/repeat.md +358 -0
- package/src/directive/switch.js +2 -4
- package/src/exts/messages.js +2 -494
- package/src/exts/messages.md +550 -0
- package/src/injector.md +1 -1
- package/src/jqLite.js +11 -17
- package/src/loader.js +0 -4
- package/test/binding.spec.js +24 -24
- package/test/jqlite.spec.js +0 -56
- package/test/messages/messages.spec.js +2 -4
- package/test/module-test.html +5 -1
- package/test/ng/compile.spec.js +3 -82
- package/test/ng/directive/repeat.spec.js +0 -37
- package/test/ng/directive/switch.spec.js +2 -9
- package/types/jqlite.d.ts +0 -78
- package/dist/angular-ts.cjs.js +0 -1
package/src/directive/repeat.js
CHANGED
|
@@ -7,347 +7,12 @@ import {
|
|
|
7
7
|
} from "../core/utils";
|
|
8
8
|
import { getBlockNodes } from "../jqLite";
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
* @ngdoc directive
|
|
12
|
-
* @name ngRepeat
|
|
13
|
-
* @multiElement
|
|
14
|
-
* @restrict A
|
|
15
|
-
*
|
|
16
|
-
* @description
|
|
17
|
-
* The `ngRepeat` directive instantiates a template once per item from a collection. Each template
|
|
18
|
-
* instance gets its own scope, where the given loop variable is set to the current collection item,
|
|
19
|
-
* and `$index` is set to the item index or key.
|
|
20
|
-
*
|
|
21
|
-
* Special properties are exposed on the local scope of each template instance, including:
|
|
22
|
-
*
|
|
23
|
-
* | Variable | Type | Details |
|
|
24
|
-
* |-----------|-----------------|-----------------------------------------------------------------------------|
|
|
25
|
-
* | `$index` | {@type number} | iterator offset of the repeated element (0..length-1) |
|
|
26
|
-
* | `$first` | {@type boolean} | true if the repeated element is first in the iterator. |
|
|
27
|
-
* | `$middle` | {@type boolean} | true if the repeated element is between the first and last in the iterator. |
|
|
28
|
-
* | `$last` | {@type boolean} | true if the repeated element is last in the iterator. |
|
|
29
|
-
* | `$even` | {@type boolean} | true if the iterator position `$index` is even (otherwise false). |
|
|
30
|
-
* | `$odd` | {@type boolean} | true if the iterator position `$index` is odd (otherwise false). |
|
|
31
|
-
*
|
|
32
|
-
* <div class="alert alert-info">
|
|
33
|
-
* Creating aliases for these properties is possible with {@link ng.directive:ngInit `ngInit`}.
|
|
34
|
-
* This may be useful when, for instance, nesting ngRepeats.
|
|
35
|
-
* </div>
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* ## Iterating over object properties
|
|
39
|
-
*
|
|
40
|
-
* It is possible to get `ngRepeat` to iterate over the properties of an object using the following
|
|
41
|
-
* syntax:
|
|
42
|
-
*
|
|
43
|
-
* ```js
|
|
44
|
-
* <div ng-repeat="(key, value) in myObj"> ... </div>
|
|
45
|
-
* ```
|
|
46
|
-
*
|
|
47
|
-
* However, there are a few limitations compared to array iteration:
|
|
48
|
-
*
|
|
49
|
-
* - The JavaScript specification does not define the order of keys
|
|
50
|
-
* returned for an object, so AngularJS relies on the order returned by the browser
|
|
51
|
-
* when running `for key in myObj`. Browsers generally follow the strategy of providing
|
|
52
|
-
* keys in the order in which they were defined, although there are exceptions when keys are deleted
|
|
53
|
-
* and reinstated. See the
|
|
54
|
-
* [MDN page on `delete` for more info](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Cross-browser_notes).
|
|
55
|
-
*
|
|
56
|
-
* - `ngRepeat` will silently *ignore* object keys starting with `$`, because
|
|
57
|
-
* it's a prefix used by AngularJS for public (`$`) and private (`$$`) properties.
|
|
58
|
-
*
|
|
59
|
-
* - The built-in filters {@link ng.orderBy orderBy} and {@link ng.filter filter} do not work with
|
|
60
|
-
* objects, and will throw an error if used with one.
|
|
61
|
-
*
|
|
62
|
-
* If you are hitting any of these limitations, the recommended workaround is to convert your object into an array
|
|
63
|
-
* that is sorted into the order that you prefer before providing it to `ngRepeat`. You could
|
|
64
|
-
* do this with a filter such as [toArrayFilter](http://ngmodules.org/modules/angular-toArrayFilter)
|
|
65
|
-
* or implement a `$watch` on the object yourself.
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* ## Tracking and Duplicates
|
|
69
|
-
*
|
|
70
|
-
* `ngRepeat` uses {@link $rootScope.Scope#$watchCollection $watchCollection} to detect changes in
|
|
71
|
-
* the collection. When a change happens, `ngRepeat` then makes the corresponding changes to the DOM:
|
|
72
|
-
*
|
|
73
|
-
* * When an item is added, a new instance of the template is added to the DOM.
|
|
74
|
-
* * When an item is removed, its template instance is removed from the DOM.
|
|
75
|
-
* * When items are reordered, their respective templates are reordered in the DOM.
|
|
76
|
-
*
|
|
77
|
-
* To minimize creation of DOM elements, `ngRepeat` uses a function
|
|
78
|
-
* to "keep track" of all items in the collection and their corresponding DOM elements.
|
|
79
|
-
* For example, if an item is added to the collection, `ngRepeat` will know that all other items
|
|
80
|
-
* already have DOM elements, and will not re-render them.
|
|
81
|
-
*
|
|
82
|
-
* All different types of tracking functions, their syntax, and their support for duplicate
|
|
83
|
-
* items in collections can be found in the
|
|
84
|
-
* {@link ngRepeat#ngRepeat-arguments ngRepeat expression description}.
|
|
85
|
-
*
|
|
86
|
-
* <div class="alert alert-success">
|
|
87
|
-
* **Best Practice:** If you are working with objects that have a unique identifier property, you
|
|
88
|
-
* should track by this identifier instead of the object instance,
|
|
89
|
-
* e.g. `item in items track by item.id`.
|
|
90
|
-
* Should you reload your data later, `ngRepeat` will not have to rebuild the DOM elements for items
|
|
91
|
-
* it has already rendered, even if the JavaScript objects in the collection have been substituted
|
|
92
|
-
* for new ones. For large collections, this significantly improves rendering performance.
|
|
93
|
-
* </div>
|
|
94
|
-
*
|
|
95
|
-
* ### Effects of DOM Element re-use
|
|
96
|
-
*
|
|
97
|
-
* When DOM elements are re-used, ngRepeat updates the scope for the element, which will
|
|
98
|
-
* automatically update any active bindings on the template. However, other
|
|
99
|
-
* functionality will not be updated, because the element is not re-created:
|
|
100
|
-
*
|
|
101
|
-
* - Directives are not re-compiled
|
|
102
|
-
* - {@link guide/expression#one-time-binding one-time expressions} on the repeated template are not
|
|
103
|
-
* updated if they have stabilized.
|
|
104
|
-
*
|
|
105
|
-
* The above affects all kinds of element re-use due to tracking, but may be especially visible
|
|
106
|
-
* when tracking by `$index` due to the way ngRepeat re-uses elements.
|
|
107
|
-
*
|
|
108
|
-
* The following example shows the effects of different actions with tracking:
|
|
10
|
+
const ngRepeatEndComment = document.createComment("");
|
|
109
11
|
|
|
110
|
-
<example module="ngRepeat" name="ngRepeat-tracking" deps="angular-animate.js" animations="true">
|
|
111
|
-
<file name="script.js">
|
|
112
|
-
angular.module('ngRepeat', ['ngAnimate']).controller('repeatController', function($scope) {
|
|
113
|
-
let friends = [
|
|
114
|
-
{name:'John', age:25},
|
|
115
|
-
{name:'Mary', age:40},
|
|
116
|
-
{name:'Peter', age:85}
|
|
117
|
-
];
|
|
118
|
-
|
|
119
|
-
$scope.removeFirst = function() {
|
|
120
|
-
$scope.friends.shift();
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
$scope.updateAge = function() {
|
|
124
|
-
$scope.friends.forEach(function(el) {
|
|
125
|
-
el.age = el.age + 5;
|
|
126
|
-
});
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
$scope.copy = function() {
|
|
130
|
-
$scope.friends = structuredClone($scope.friends);
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
$scope.reset = function() {
|
|
134
|
-
$scope.friends = structuredClone(friends);
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
$scope.reset();
|
|
138
|
-
});
|
|
139
|
-
</file>
|
|
140
|
-
<file name="index.html">
|
|
141
|
-
<div ng-controller="repeatController">
|
|
142
|
-
<ol>
|
|
143
|
-
<li>When you click "Update Age", only the first list updates the age, because all others have
|
|
144
|
-
a one-time binding on the age property. If you then click "Copy", the current friend list
|
|
145
|
-
is copied, and now the second list updates the age, because the identity of the collection items
|
|
146
|
-
has changed and the list must be re-rendered. The 3rd and 4th list stay the same, because all the
|
|
147
|
-
items are already known according to their tracking functions.
|
|
148
|
-
</li>
|
|
149
|
-
<li>When you click "Remove First", the 4th list has the wrong age on both remaining items. This is
|
|
150
|
-
due to tracking by $index: when the first collection item is removed, ngRepeat reuses the first
|
|
151
|
-
DOM element for the new first collection item, and so on. Since the age property is one-time
|
|
152
|
-
bound, the value remains from the collection item which was previously at this index.
|
|
153
|
-
</li>
|
|
154
|
-
</ol>
|
|
155
|
-
|
|
156
|
-
<button ng-click="removeFirst()">Remove First</button>
|
|
157
|
-
<button ng-click="updateAge()">Update Age</button>
|
|
158
|
-
<button ng-click="copy()">Copy</button>
|
|
159
|
-
<br><button ng-click="reset()">Reset List</button>
|
|
160
|
-
<br>
|
|
161
|
-
<code>track by $id(friend)</code> (default):
|
|
162
|
-
<ul class="example-animate-container">
|
|
163
|
-
<li class="animate-repeat" ng-repeat="friend in friends">
|
|
164
|
-
{{friend.name}} is {{friend.age}} years old.
|
|
165
|
-
</li>
|
|
166
|
-
</ul>
|
|
167
|
-
<code>track by $id(friend)</code> (default), with age one-time binding:
|
|
168
|
-
<ul class="example-animate-container">
|
|
169
|
-
<li class="animate-repeat" ng-repeat="friend in friends">
|
|
170
|
-
{{friend.name}} is {{::friend.age}} years old.
|
|
171
|
-
</li>
|
|
172
|
-
</ul>
|
|
173
|
-
<code>track by friend.name</code>, with age one-time binding:
|
|
174
|
-
<ul class="example-animate-container">
|
|
175
|
-
<li class="animate-repeat" ng-repeat="friend in friends track by friend.name">
|
|
176
|
-
{{friend.name}} is {{::friend.age}} years old.
|
|
177
|
-
</li>
|
|
178
|
-
</ul>
|
|
179
|
-
<code>track by $index</code>, with age one-time binding:
|
|
180
|
-
<ul class="example-animate-container">
|
|
181
|
-
<li class="animate-repeat" ng-repeat="friend in friends track by $index">
|
|
182
|
-
{{friend.name}} is {{::friend.age}} years old.
|
|
183
|
-
</li>
|
|
184
|
-
</ul>
|
|
185
|
-
</div>
|
|
186
|
-
</file>
|
|
187
|
-
<file name="animations.css">
|
|
188
|
-
.example-animate-container {
|
|
189
|
-
background:white;
|
|
190
|
-
border:1px solid black;
|
|
191
|
-
list-style:none;
|
|
192
|
-
margin:0;
|
|
193
|
-
padding:0 10px;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
.animate-repeat {
|
|
197
|
-
line-height:30px;
|
|
198
|
-
list-style:none;
|
|
199
|
-
box-sizing:border-box;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
.animate-repeat.ng-move,
|
|
203
|
-
.animate-repeat.ng-enter,
|
|
204
|
-
.animate-repeat.ng-leave {
|
|
205
|
-
transition:all linear 0.5s;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
.animate-repeat.ng-leave.ng-leave-active,
|
|
209
|
-
.animate-repeat.ng-move,
|
|
210
|
-
.animate-repeat.ng-enter {
|
|
211
|
-
opacity:0;
|
|
212
|
-
max-height:0;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
.animate-repeat.ng-leave,
|
|
216
|
-
.animate-repeat.ng-move.ng-move-active,
|
|
217
|
-
.animate-repeat.ng-enter.ng-enter-active {
|
|
218
|
-
opacity:1;
|
|
219
|
-
max-height:30px;
|
|
220
|
-
}
|
|
221
|
-
</file>
|
|
222
|
-
</example>
|
|
223
|
-
|
|
224
|
-
*
|
|
225
|
-
* ## Special repeat start and end points
|
|
226
|
-
* To repeat a series of elements instead of just one parent element, ngRepeat (as well as other ng directives) supports extending
|
|
227
|
-
* the range of the repeater by defining explicit start and end points by using **ng-repeat-start** and **ng-repeat-end** respectively.
|
|
228
|
-
* The **ng-repeat-start** directive works the same as **ng-repeat**, but will repeat all the HTML code (including the tag it's defined on)
|
|
229
|
-
* up to and including the ending HTML tag where **ng-repeat-end** is placed.
|
|
230
|
-
*
|
|
231
|
-
* The example below makes use of this feature:
|
|
232
|
-
* ```html
|
|
233
|
-
* <header ng-repeat-start="item in items">
|
|
234
|
-
* Header {{ item }}
|
|
235
|
-
* </header>
|
|
236
|
-
* <div class="body">
|
|
237
|
-
* Body {{ item }}
|
|
238
|
-
* </div>
|
|
239
|
-
* <footer ng-repeat-end>
|
|
240
|
-
* Footer {{ item }}
|
|
241
|
-
* </footer>
|
|
242
|
-
* ```
|
|
243
|
-
*
|
|
244
|
-
* And with an input of {@type ['A','B']} for the items variable in the example above, the output will evaluate to:
|
|
245
|
-
* ```html
|
|
246
|
-
* <header>
|
|
247
|
-
* Header A
|
|
248
|
-
* </header>
|
|
249
|
-
* <div class="body">
|
|
250
|
-
* Body A
|
|
251
|
-
* </div>
|
|
252
|
-
* <footer>
|
|
253
|
-
* Footer A
|
|
254
|
-
* </footer>
|
|
255
|
-
* <header>
|
|
256
|
-
* Header B
|
|
257
|
-
* </header>
|
|
258
|
-
* <div class="body">
|
|
259
|
-
* Body B
|
|
260
|
-
* </div>
|
|
261
|
-
* <footer>
|
|
262
|
-
* Footer B
|
|
263
|
-
* </footer>
|
|
264
|
-
* ```
|
|
265
|
-
*
|
|
266
|
-
* The custom start and end points for ngRepeat also support all other HTML directive syntax flavors provided in AngularJS (such
|
|
267
|
-
* as **data-ng-repeat-start**, **x-ng-repeat-start** and **ng:repeat-start**).
|
|
268
|
-
*
|
|
269
|
-
* @animations
|
|
270
|
-
* | Animation | Occurs |
|
|
271
|
-
* |----------------------------------|-------------------------------------|
|
|
272
|
-
* | {@link ng.$animate#enter enter} | when a new item is added to the list or when an item is revealed after a filter |
|
|
273
|
-
* | {@link ng.$animate#leave leave} | when an item is removed from the list or when an item is filtered out |
|
|
274
|
-
* | {@link ng.$animate#move move } | when an adjacent item is filtered out causing a reorder or when the item contents are reordered |
|
|
275
|
-
*
|
|
276
|
-
* See the example below for defining CSS animations with ngRepeat.
|
|
277
|
-
*
|
|
278
|
-
* @element ANY
|
|
279
|
-
* @scope
|
|
280
|
-
* @priority 1000
|
|
281
|
-
* @param {repeat_expression} ngRepeat The expression indicating how to enumerate a collection. These
|
|
282
|
-
* formats are currently supported:
|
|
283
|
-
*
|
|
284
|
-
* * `variable in expression` – where variable is the user defined loop variable and `expression`
|
|
285
|
-
* is a scope expression giving the collection to enumerate.
|
|
286
|
-
*
|
|
287
|
-
* For example: `album in artist.albums`.
|
|
288
|
-
*
|
|
289
|
-
* * `(key, value) in expression` – where `key` and `value` can be any user defined identifiers,
|
|
290
|
-
* and `expression` is the scope expression giving the collection to enumerate.
|
|
291
|
-
*
|
|
292
|
-
* For example: `(name, age) in {'adam':10, 'amalie':12}`.
|
|
293
|
-
*
|
|
294
|
-
* * `variable in expression track by tracking_expression` – You can also provide an optional tracking expression
|
|
295
|
-
* which can be used to associate the objects in the collection with the DOM elements. If no tracking expression
|
|
296
|
-
* is specified, ng-repeat associates elements by identity. It is an error to have
|
|
297
|
-
* more than one tracking expression value resolve to the same key. (This would mean that two distinct objects are
|
|
298
|
-
* mapped to the same DOM element, which is not possible.)
|
|
299
|
-
*
|
|
300
|
-
* *Default tracking: $id()*: `item in items` is equivalent to `item in items track by $id(item)`.
|
|
301
|
-
* This implies that the DOM elements will be associated by item identity in the collection.
|
|
302
|
-
*
|
|
303
|
-
* The built-in `$id()` function can be used to assign a unique
|
|
304
|
-
* `$$hashKey` property to each item in the collection. This property is then used as a key to associated DOM elements
|
|
305
|
-
* with the corresponding item in the collection by identity. Moving the same object would move
|
|
306
|
-
* the DOM element in the same way in the DOM.
|
|
307
|
-
* Note that the default id function does not support duplicate primitive values (`number`, `string`),
|
|
308
|
-
* but supports duplictae non-primitive values (`object`) that are *equal* in shape.
|
|
309
|
-
*
|
|
310
|
-
* *Custom Expression*: It is possible to use any AngularJS expression to compute the tracking
|
|
311
|
-
* id, for example with a function, or using a property on the collection items.
|
|
312
|
-
* `item in items track by item.id` is a typical pattern when the items have a unique identifier,
|
|
313
|
-
* e.g. database id. In this case the object identity does not matter. Two objects are considered
|
|
314
|
-
* equivalent as long as their `id` property is same.
|
|
315
|
-
* Tracking by unique identifier is the most performant way and should be used whenever possible.
|
|
316
|
-
*
|
|
317
|
-
* *$index*: This special property tracks the collection items by their index, and
|
|
318
|
-
* re-uses the DOM elements that match that index, e.g. `item in items track by $index`. This can
|
|
319
|
-
* be used for a performance improvement if no unique identfier is available and the identity of
|
|
320
|
-
* the collection items cannot be easily computed. It also allows duplicates.
|
|
321
|
-
*
|
|
322
|
-
* <div class="alert alert-warning">
|
|
323
|
-
* <strong>Note:</strong> Re-using DOM elements can have unforeseen effects. Read the
|
|
324
|
-
* {@link ngRepeat#tracking-and-duplicates section on tracking and duplicates} for
|
|
325
|
-
* more info.
|
|
326
|
-
* </div>
|
|
327
|
-
*
|
|
328
|
-
* <div class="alert alert-warning">
|
|
329
|
-
* <strong>Note:</strong> the `track by` expression must come last - after any filters, and the alias expression:
|
|
330
|
-
* `item in items | filter:searchText as results track by item.id`
|
|
331
|
-
* </div>
|
|
332
|
-
*
|
|
333
|
-
* * `variable in expression as alias_expression` – You can also provide an optional alias expression which will then store the
|
|
334
|
-
* intermediate results of the repeater after the filters have been applied. Typically this is used to render a special message
|
|
335
|
-
* when a filter is active on the repeater, but the filtered result set is empty.
|
|
336
|
-
*
|
|
337
|
-
* For example: `item in items | filter:x as results` will store the fragment of the repeated items as `results`, but only after
|
|
338
|
-
* the items have been processed through the filter.
|
|
339
|
-
*
|
|
340
|
-
* Please note that `as [variable name]` is not an operator but rather a part of ngRepeat
|
|
341
|
-
* micro-syntax so it can be used only after all filters (and not as operator, inside an expression).
|
|
342
|
-
*
|
|
343
|
-
* For example: `item in items | filter : x | orderBy : order | limitTo : limit as results track by item.id` .
|
|
344
|
-
*
|
|
345
|
-
*/
|
|
346
12
|
export const ngRepeatDirective = [
|
|
347
13
|
"$parse",
|
|
348
14
|
"$animate",
|
|
349
|
-
|
|
350
|
-
($parse, $animate, $compile) => {
|
|
15
|
+
($parse, $animate) => {
|
|
351
16
|
const NG_REMOVED = "$$NG_REMOVED";
|
|
352
17
|
const ngRepeatMinErr = minErr("ngRepeat");
|
|
353
18
|
|
|
@@ -395,10 +60,6 @@ export const ngRepeatDirective = [
|
|
|
395
60
|
$$tlb: true,
|
|
396
61
|
compile: function ngRepeatCompile($element, $attr) {
|
|
397
62
|
const expression = $attr.ngRepeat;
|
|
398
|
-
const ngRepeatEndComment = $compile.$$createComment(
|
|
399
|
-
"end ngRepeat",
|
|
400
|
-
expression,
|
|
401
|
-
);
|
|
402
63
|
|
|
403
64
|
let match = expression.match(
|
|
404
65
|
/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/,
|
|
@@ -479,7 +140,7 @@ export const ngRepeatDirective = [
|
|
|
479
140
|
|
|
480
141
|
// watch props
|
|
481
142
|
//watch props
|
|
482
|
-
$scope.$watchCollection(rhs,
|
|
143
|
+
$scope.$watchCollection(rhs, (collection) => {
|
|
483
144
|
var index,
|
|
484
145
|
length,
|
|
485
146
|
previousNode = $element[0], // node that cloned nodes should be inserted after
|
|
@@ -616,12 +277,10 @@ export const ngRepeatDirective = [
|
|
|
616
277
|
);
|
|
617
278
|
} else {
|
|
618
279
|
// new item which we don't know about
|
|
619
|
-
$transclude(
|
|
280
|
+
$transclude((clone, scope) => {
|
|
620
281
|
block.scope = scope;
|
|
621
|
-
|
|
622
|
-
var endNode = ngRepeatEndComment.cloneNode(false);
|
|
282
|
+
const endNode = ngRepeatEndComment.cloneNode(false);
|
|
623
283
|
clone[clone.length++] = endNode;
|
|
624
|
-
|
|
625
284
|
$animate.enter(clone, null, previousNode);
|
|
626
285
|
previousNode = endNode;
|
|
627
286
|
// Note: We only need the first/last node of the cloned nodes.
|