@nativescript/compiler-angular 0.0.0-stage → 9.2.0-alpha.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-libraries.js +173 -0
- package/dist/angular.js +2895 -0
- package/dist/component-bases.js +746 -0
- package/dist/core-api.js +8 -0
- package/dist/entry.js +118 -0
- package/dist/index.js +157 -0
- package/dist/kotlin.js +280 -0
- package/dist/modules.js +698 -0
- package/dist/ng-modules.js +66 -0
- package/dist/package.js +15 -0
- package/dist/refusals.js +5 -0
- package/dist/semantics.js +213 -0
- package/dist/swift.js +340 -0
- package/kit-android/fromcore/AngularAPI.kt +172 -0
- package/kit-android/fromcore/AngularRouter.kt +587 -0
- package/kit-android/handport/Router.kt +151 -0
- package/kit-android/main/AngularChangeDetection.kt +250 -0
- package/kit-android/main/AngularCore.kt +685 -0
- package/kit-android/main/AngularHttp.kt +431 -0
- package/kit-android/main/AngularRouterValues.kt +154 -0
- package/kit-android/main/AngularRx.kt +224 -0
- package/kit-apple/AngularAPI.swift +224 -0
- package/kit-apple/AngularChangeDetection.swift +248 -0
- package/kit-apple/AngularCore.swift +764 -0
- package/kit-apple/AngularHttp.swift +467 -0
- package/kit-apple/AngularRouter.swift +626 -0
- package/kit-apple/AngularRouterValues.swift +285 -0
- package/kit-apple/AngularRx.swift +246 -0
- package/kit-apple/kit-index.json +739 -0
- package/package.json +53 -4
- package/runtime/angular-forms.ts +3099 -0
- package/runtime/angular-rx.ts +550 -0
- package/runtime/angular.ts +826 -0
- package/README.md +0 -3
|
@@ -0,0 +1,826 @@
|
|
|
1
|
+
// The Angular runtime a compiled template uses, written once for both backends: @angular/common's pipes, which the
|
|
2
|
+
// template compiler creates one of per pipe in a template, as Angular's does, and calls as Angular calls them (a pure
|
|
3
|
+
// pipe only when an argument changed), NgClass and NgStyle, and @angular/core's QueryList. The pipes' output is
|
|
4
|
+
// Angular's for the `en-US` locale, the only one a compiled release has: the formatting follows @angular/common's
|
|
5
|
+
// formatDate and formatNumber, with en-US's locale data.
|
|
6
|
+
// Those parts are derived from @angular/common:
|
|
7
|
+
//
|
|
8
|
+
// @license
|
|
9
|
+
// Copyright Google LLC All Rights Reserved.
|
|
10
|
+
// Use of this source code is governed by an MIT-style license that can be
|
|
11
|
+
// found in the LICENSE file at https://angular.dev/license
|
|
12
|
+
import { ChangeDetectorRef, ElementRef, inject } from '@angular/core';
|
|
13
|
+
|
|
14
|
+
/** Angular's NG02100 error, as `invalidPipeArgumentError` words it. */
|
|
15
|
+
function invalidPipeArgument(pipe: string, value: unknown): Error {
|
|
16
|
+
return new Error(`NG02100: InvalidPipeArgument: '${value}' for pipe '${pipe}'`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function checkLocale(locale: string | undefined): void {
|
|
20
|
+
// A compiled release has en-US's locale data only.
|
|
21
|
+
if (locale && !/^en(?:[-_]US)?$/i.test(locale)) throw new Error(`NG0701: Missing locale data for the locale "${locale}".`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const MONTHS = [
|
|
25
|
+
['J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D'],
|
|
26
|
+
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
|
|
27
|
+
['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
|
|
28
|
+
];
|
|
29
|
+
const DAYS = [
|
|
30
|
+
['S', 'M', 'T', 'W', 'T', 'F', 'S'],
|
|
31
|
+
['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
|
|
32
|
+
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
|
|
33
|
+
['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
|
|
34
|
+
];
|
|
35
|
+
const ERAS = [['B', 'A'], ['BC', 'AD'], ['Before Christ', 'Anno Domini']];
|
|
36
|
+
/** en-US's day periods: the format style has narrow and abbreviated names, the standalone style one name for all widths. */
|
|
37
|
+
const DAY_PERIODS = [['a', 'p'], ['AM', 'PM']];
|
|
38
|
+
const STANDALONE_DAY_PERIODS = ['AM', 'PM'];
|
|
39
|
+
|
|
40
|
+
/** en-US's named formats: CLDR puts a narrow no-break space (U+202F) before the day period. */
|
|
41
|
+
const NAMED_FORMATS: Record<string, string> = {
|
|
42
|
+
shortDate: 'M/d/yy', mediumDate: 'MMM d, y', longDate: 'MMMM d, y', fullDate: 'EEEE, MMMM d, y',
|
|
43
|
+
shortTime: 'h:mm\u202Fa', mediumTime: 'h:mm:ss\u202Fa', longTime: 'h:mm:ss\u202Fa z', fullTime: 'h:mm:ss\u202Fa zzzz',
|
|
44
|
+
short: 'M/d/yy, h:mm\u202Fa', medium: 'MMM d, y, h:mm:ss\u202Fa', long: 'MMMM d, y, h:mm:ss\u202Fa z', full: 'EEEE, MMMM d, y, h:mm:ss\u202Fa zzzz',
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const ISO8601_DATE = /^(\d{4,})-?(\d\d)-?(\d\d)(?:T(\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d+))?)?)?(Z|([+-])(\d\d):?(\d\d))?)?$/;
|
|
48
|
+
const DATE_FORMATS_SPLIT = /((?:[^BEGHLMOSWYZabcdhmswyz']+)|(?:'(?:[^']|'')*')|(?:G{1,5}|y{1,4}|Y{1,4}|M{1,5}|L{1,5}|w{1,2}|W{1}|d{1,2}|E{1,6}|c{1,6}|a{1,5}|b{1,5}|B{1,5}|h{1,2}|H{1,2}|m{1,2}|s{1,2}|S{1,3}|z{1,4}|Z{1,5}|O{1,4}))([\s\S]*)/;
|
|
49
|
+
|
|
50
|
+
function isDate(value: unknown): value is Date {
|
|
51
|
+
if (!(value instanceof Date)) return false;
|
|
52
|
+
const date: Date = value;
|
|
53
|
+
return !isNaN(date.getTime());
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function createDate(year: number, month: number, day: number): Date {
|
|
57
|
+
const date = new Date(0);
|
|
58
|
+
date.setFullYear(year, month, day);
|
|
59
|
+
date.setHours(0, 0, 0);
|
|
60
|
+
return date;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function isoStringToDate(match: RegExpMatchArray): Date {
|
|
64
|
+
const date = new Date(0);
|
|
65
|
+
let tzHour = 0;
|
|
66
|
+
let tzMin = 0;
|
|
67
|
+
const utc = !!match[8];
|
|
68
|
+
if (match[9]) {
|
|
69
|
+
tzHour = Number(match[9] + match[10]);
|
|
70
|
+
tzMin = Number(match[9] + match[11]);
|
|
71
|
+
}
|
|
72
|
+
if (utc) date.setUTCFullYear(Number(match[1]), Number(match[2]) - 1, Number(match[3]));
|
|
73
|
+
else date.setFullYear(Number(match[1]), Number(match[2]) - 1, Number(match[3]));
|
|
74
|
+
const h = Number(match[4] || 0) - tzHour;
|
|
75
|
+
const m = Number(match[5] || 0) - tzMin;
|
|
76
|
+
const s = Number(match[6] || 0);
|
|
77
|
+
const ms = Math.floor(parseFloat('0.' + (match[7] || 0)) * 1000);
|
|
78
|
+
if (utc) date.setUTCHours(h, m, s, ms);
|
|
79
|
+
else date.setHours(h, m, s, ms);
|
|
80
|
+
return date;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A Date, a timestamp, `yyyy-MM-dd` (a local date), an ISO 8601 string or anything `new Date` reads. */
|
|
84
|
+
function toDate(value: Date | string | number): Date {
|
|
85
|
+
if (isDate(value)) return value;
|
|
86
|
+
if (typeof value === 'number' && !isNaN(value)) return new Date(value);
|
|
87
|
+
if (typeof value === 'string') {
|
|
88
|
+
const text = value.trim();
|
|
89
|
+
if (/^(\d{4}(-\d{1,2}(-\d{1,2})?)?)$/.test(text)) {
|
|
90
|
+
const parts = text.split('-').map((p) => +p);
|
|
91
|
+
return createDate(parts[0], (parts.length > 1 ? parts[1] : 1) - 1, parts.length > 2 ? parts[2] : 1);
|
|
92
|
+
}
|
|
93
|
+
const parsed = parseFloat(text);
|
|
94
|
+
if (!isNaN(Number(text) - parsed)) return new Date(parsed);
|
|
95
|
+
const match = text.match(ISO8601_DATE);
|
|
96
|
+
if (match) return isoStringToDate(match);
|
|
97
|
+
const date = new Date(text);
|
|
98
|
+
if (!isDate(date)) throw new Error(`NG02311: Unable to convert "${value}" into a date`);
|
|
99
|
+
return date;
|
|
100
|
+
}
|
|
101
|
+
const date = new Date(value as any);
|
|
102
|
+
if (!isDate(date)) throw new Error(`NG02311: Unable to convert "${value}" into a date`);
|
|
103
|
+
return date;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function padNumber(num: number, digits: number, trim = false, negWrap = false): string {
|
|
107
|
+
let neg = '';
|
|
108
|
+
if (num < 0 || (negWrap && num <= 0)) {
|
|
109
|
+
if (negWrap) num = -num + 1;
|
|
110
|
+
else {
|
|
111
|
+
num = -num;
|
|
112
|
+
neg = '-';
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
let text = String(num);
|
|
116
|
+
while (text.length < digits) text = '0' + text;
|
|
117
|
+
if (trim) text = text.slice(text.length - digits);
|
|
118
|
+
return neg + text;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The minutes a timezone (`UTC`, `+0430`, `-08:00`, `GMT+2`, a US zone abbreviation) is behind UTC, as Date.parse reads it; else `fallback`. */
|
|
122
|
+
function timezoneToOffset(timezone: string, fallback: number): number {
|
|
123
|
+
const zone = timezone.replace(/:/g, '').trim().toUpperCase();
|
|
124
|
+
const named: Record<string, number> = { UTC: 0, UT: 0, GMT: 0, Z: 0, EST: 300, EDT: 240, CST: 360, CDT: 300, MST: 420, MDT: 360, PST: 480, PDT: 420 };
|
|
125
|
+
if (zone in named) return named[zone];
|
|
126
|
+
const m = /^(?:UTC|UT|GMT)?([+-])(\d{1,2}|\d{4})$/.exec(zone);
|
|
127
|
+
if (!m) return fallback;
|
|
128
|
+
const digits = m[2];
|
|
129
|
+
const hours = digits.length === 4 ? Number(digits.slice(0, 2)) : Number(digits);
|
|
130
|
+
const minutes = digits.length === 4 ? Number(digits.slice(2)) : 0;
|
|
131
|
+
const offset = hours * 60 + minutes;
|
|
132
|
+
return m[1] === '+' ? -offset : offset;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function inTimezone(date: Date, timezone: string): Date {
|
|
136
|
+
const local = date.getTimezoneOffset();
|
|
137
|
+
const shifted = new Date(date.getTime());
|
|
138
|
+
shifted.setMinutes(shifted.getMinutes() - (timezoneToOffset(timezone, local) - local));
|
|
139
|
+
return shifted;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function weekThursday(date: Date): Date {
|
|
143
|
+
const day = date.getDay();
|
|
144
|
+
return createDate(date.getFullYear(), date.getMonth(), date.getDate() + (day === 0 ? -3 : 4 - day));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function firstThursdayOfYear(year: number): Date {
|
|
148
|
+
const first = createDate(year, 0, 1).getDay();
|
|
149
|
+
return createDate(year, 0, 1 + (first <= 4 ? 4 : 11) - first);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function zoneText(width: number, offset: number): string {
|
|
153
|
+
const zone = -1 * offset;
|
|
154
|
+
const hours = zone > 0 ? Math.floor(zone / 60) : Math.ceil(zone / 60);
|
|
155
|
+
const sign = zone >= 0 ? '+' : '';
|
|
156
|
+
const minutes = padNumber(Math.abs(zone % 60), 2);
|
|
157
|
+
if (width === 0) return sign + padNumber(hours, 2) + minutes;
|
|
158
|
+
if (width === 1) return 'GMT' + sign + padNumber(hours, 1);
|
|
159
|
+
if (width === 2) return 'GMT' + sign + padNumber(hours, 2) + ':' + minutes;
|
|
160
|
+
return offset === 0 ? 'Z' : sign + padNumber(hours, 2) + ':' + minutes;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** One field of a date pattern (`yyyy`, `MMM`, `h`), or null for literal text. */
|
|
164
|
+
function formatField(field: string, date: Date, offset: number): string | null {
|
|
165
|
+
const letter = field[0];
|
|
166
|
+
const n = field.length;
|
|
167
|
+
switch (letter) {
|
|
168
|
+
case 'G': return ERAS[n <= 3 ? 1 : n === 4 ? 2 : 0][date.getFullYear() <= 0 ? 0 : 1];
|
|
169
|
+
case 'y': return n === 2 ? padNumber(date.getFullYear(), 2, true, true) : padNumber(date.getFullYear(), n === 1 ? 1 : n, false, true);
|
|
170
|
+
case 'Y': {
|
|
171
|
+
const year = weekThursday(date).getFullYear();
|
|
172
|
+
return n === 2 ? padNumber(year, 2, true) : padNumber(year, n);
|
|
173
|
+
}
|
|
174
|
+
case 'M': case 'L':
|
|
175
|
+
if (n <= 2) return padNumber(date.getMonth() + 1, n);
|
|
176
|
+
return MONTHS[n === 3 ? 1 : n === 4 ? 2 : 0][date.getMonth()];
|
|
177
|
+
case 'w': {
|
|
178
|
+
const thursday = weekThursday(date);
|
|
179
|
+
const week = 1 + Math.round((thursday.getTime() - firstThursdayOfYear(thursday.getFullYear()).getTime()) / 6.048e8);
|
|
180
|
+
return padNumber(week, n);
|
|
181
|
+
}
|
|
182
|
+
case 'W': {
|
|
183
|
+
const before = new Date(date.getFullYear(), date.getMonth(), 1).getDay() - 1;
|
|
184
|
+
return padNumber(1 + Math.floor((date.getDate() + before) / 7), 1);
|
|
185
|
+
}
|
|
186
|
+
case 'd': return padNumber(date.getDate(), n);
|
|
187
|
+
case 'c':
|
|
188
|
+
if (n <= 2) return padNumber(date.getDay(), 1);
|
|
189
|
+
return DAYS[n === 3 ? 1 : n === 4 ? 2 : n === 5 ? 0 : 3][date.getDay()];
|
|
190
|
+
case 'E': return DAYS[n <= 3 ? 1 : n === 4 ? 2 : n === 5 ? 0 : 3][date.getDay()];
|
|
191
|
+
case 'a': return n === 5 ? DAY_PERIODS[0][date.getHours() < 12 ? 0 : 1] : DAY_PERIODS[1][date.getHours() < 12 ? 0 : 1];
|
|
192
|
+
case 'b': case 'B':
|
|
193
|
+
// Angular's flexible day periods need the locale's extra data, which en-US's built-in data leaves out.
|
|
194
|
+
throw new Error('NG02303: Missing extra locale data for the locale "en". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.');
|
|
195
|
+
case 'h': {
|
|
196
|
+
let hours = date.getHours();
|
|
197
|
+
if (hours > 12) hours -= 12;
|
|
198
|
+
if (hours === 0) hours = 12;
|
|
199
|
+
return padNumber(hours, n);
|
|
200
|
+
}
|
|
201
|
+
case 'H': return padNumber(date.getHours(), n);
|
|
202
|
+
case 'm': return padNumber(date.getMinutes(), n);
|
|
203
|
+
case 's': return padNumber(date.getSeconds(), n);
|
|
204
|
+
case 'S': return padNumber(date.getMilliseconds(), 3).substring(0, n);
|
|
205
|
+
case 'Z': return zoneText(n <= 3 ? 0 : n === 4 ? 2 : 3, offset);
|
|
206
|
+
case 'O': case 'z': return zoneText(n <= 3 ? 1 : 2, offset);
|
|
207
|
+
}
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Angular's `formatDate` for en-US. */
|
|
212
|
+
export function formatDate(value: Date | string | number, format: string, locale?: string, timezone?: string): string {
|
|
213
|
+
checkLocale(locale);
|
|
214
|
+
let date = toDate(value);
|
|
215
|
+
if (format.length > 256) throw new Error('NG02300: Date format is too long. Exceeded maximum length of 256 characters.');
|
|
216
|
+
let rest = NAMED_FORMATS[format] ?? format;
|
|
217
|
+
const parts: string[] = [];
|
|
218
|
+
while (rest) {
|
|
219
|
+
const match = DATE_FORMATS_SPLIT.exec(rest);
|
|
220
|
+
if (!match) {
|
|
221
|
+
parts.push(rest);
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
parts.push(match[1]);
|
|
225
|
+
if (!match[2]) break;
|
|
226
|
+
rest = match[2];
|
|
227
|
+
}
|
|
228
|
+
if (parts.some((p) => /^Y+$/.test(p)) && !parts.some((p) => /^w+$/.test(p))) {
|
|
229
|
+
const message = `NG02300: Suspicious use of week-based year "Y" in date pattern "${parts.join('')}". Did you mean to use calendar year "y" instead?`;
|
|
230
|
+
if (parts.length === 1) console.error(message);
|
|
231
|
+
else throw new Error(message);
|
|
232
|
+
}
|
|
233
|
+
let offset = date.getTimezoneOffset();
|
|
234
|
+
if (timezone) {
|
|
235
|
+
offset = timezoneToOffset(timezone, offset);
|
|
236
|
+
date = inTimezone(date, timezone);
|
|
237
|
+
}
|
|
238
|
+
let text = '';
|
|
239
|
+
for (const part of parts) {
|
|
240
|
+
const field = /^[BEGHLMOSWYZabcdhmswyz]+$/.test(part) ? formatField(part, date, offset) : null;
|
|
241
|
+
text += field !== null ? field : part === "''" ? "'" : part.replace(/(^'|'$)/g, '').replace(/''/g, "'");
|
|
242
|
+
}
|
|
243
|
+
return text;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
interface ParsedNumber {
|
|
247
|
+
digits: number[];
|
|
248
|
+
exponent: number;
|
|
249
|
+
integerLen: number;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function parseNumber(num: number): ParsedNumber {
|
|
253
|
+
let text = Math.abs(num) + '';
|
|
254
|
+
let exponent = 0;
|
|
255
|
+
let integerLen = text.indexOf('.');
|
|
256
|
+
if (integerLen > -1) text = text.replace('.', '');
|
|
257
|
+
const e = text.search(/e/i);
|
|
258
|
+
if (e > 0) {
|
|
259
|
+
if (integerLen < 0) integerLen = e;
|
|
260
|
+
integerLen += +text.slice(e + 1);
|
|
261
|
+
text = text.substring(0, e);
|
|
262
|
+
} else if (integerLen < 0) {
|
|
263
|
+
integerLen = text.length;
|
|
264
|
+
}
|
|
265
|
+
let i = 0;
|
|
266
|
+
while (text.charAt(i) === '0') i++;
|
|
267
|
+
let digits: number[] = [];
|
|
268
|
+
let last = text.length;
|
|
269
|
+
if (i === last) {
|
|
270
|
+
digits = [0];
|
|
271
|
+
integerLen = 1;
|
|
272
|
+
} else {
|
|
273
|
+
last--;
|
|
274
|
+
while (text.charAt(last) === '0') last--;
|
|
275
|
+
integerLen -= i;
|
|
276
|
+
for (; i <= last; i++) digits.push(Number(text.charAt(i)));
|
|
277
|
+
}
|
|
278
|
+
if (integerLen > 22) {
|
|
279
|
+
digits = digits.slice(0, 21);
|
|
280
|
+
exponent = integerLen - 1;
|
|
281
|
+
integerLen = 1;
|
|
282
|
+
}
|
|
283
|
+
return { digits, exponent, integerLen };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Rounds the digits to between `minFrac` and `maxFrac` fraction digits, half up, as Angular's `roundNumber` does. */
|
|
287
|
+
function roundNumber(parsed: ParsedNumber, minFrac: number, maxFrac: number): void {
|
|
288
|
+
if (minFrac > maxFrac) throw new Error(`NG02307: The minimum number of digits after fraction (${minFrac}) is higher than the maximum (${maxFrac}).`);
|
|
289
|
+
const digits = parsed.digits;
|
|
290
|
+
let fractionLen = digits.length - parsed.integerLen;
|
|
291
|
+
const fractionSize = Math.min(Math.max(minFrac, fractionLen), maxFrac);
|
|
292
|
+
let roundAt = fractionSize + parsed.integerLen;
|
|
293
|
+
const digit = roundAt < digits.length ? digits[roundAt] : NaN;
|
|
294
|
+
if (roundAt > 0) {
|
|
295
|
+
digits.length = Math.min(digits.length, Math.max(parsed.integerLen, roundAt));
|
|
296
|
+
for (let j = roundAt; j < digits.length; j++) digits[j] = 0;
|
|
297
|
+
} else {
|
|
298
|
+
fractionLen = Math.max(0, fractionLen);
|
|
299
|
+
parsed.integerLen = 1;
|
|
300
|
+
roundAt = fractionSize + 1;
|
|
301
|
+
digits.length = Math.max(1, roundAt);
|
|
302
|
+
digits[0] = 0;
|
|
303
|
+
for (let k = 1; k < roundAt; k++) digits[k] = 0;
|
|
304
|
+
}
|
|
305
|
+
if (digit >= 5) {
|
|
306
|
+
if (roundAt - 1 < 0) {
|
|
307
|
+
for (let k = 0; k > roundAt; k--) {
|
|
308
|
+
digits.unshift(0);
|
|
309
|
+
parsed.integerLen++;
|
|
310
|
+
}
|
|
311
|
+
digits.unshift(1);
|
|
312
|
+
parsed.integerLen++;
|
|
313
|
+
} else {
|
|
314
|
+
digits[roundAt - 1]++;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
for (; fractionLen < Math.max(0, fractionSize); fractionLen++) digits.push(0);
|
|
318
|
+
let dropTrailingZeros = fractionSize !== 0;
|
|
319
|
+
const minLen = minFrac + parsed.integerLen;
|
|
320
|
+
let carry = 0;
|
|
321
|
+
for (let k = digits.length - 1; k >= 0; k--) {
|
|
322
|
+
const d = digits[k] + carry;
|
|
323
|
+
digits[k] = d < 10 ? d : d - 10;
|
|
324
|
+
carry = d >= 10 ? 1 : 0;
|
|
325
|
+
if (dropTrailingZeros) {
|
|
326
|
+
if (digits[k] === 0 && k >= minLen) digits.pop();
|
|
327
|
+
else dropTrailingZeros = false;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (carry) {
|
|
331
|
+
digits.unshift(carry);
|
|
332
|
+
parsed.integerLen++;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function parseDigitsInfo(digitsInfo: string, defaults: { minInt: number; minFrac: number; maxFrac: number }): { minInt: number; minFrac: number; maxFrac: number } {
|
|
337
|
+
const parts = /^(\d+)?\.((\d+)(-(\d+))?)?$/.exec(digitsInfo);
|
|
338
|
+
if (!parts) throw new Error(`NG02306: ${digitsInfo} is not a valid digit info`);
|
|
339
|
+
const out = { ...defaults };
|
|
340
|
+
// A group that did not take part is undefined, and one of digits is never empty.
|
|
341
|
+
if (parts[1]) out.minInt = parseInt(parts[1]);
|
|
342
|
+
if (parts[3]) out.minFrac = parseInt(parts[3]);
|
|
343
|
+
if (parts[5]) out.maxFrac = parseInt(parts[5]);
|
|
344
|
+
else if (parts[3] && out.minFrac > out.maxFrac) out.maxFrac = out.minFrac;
|
|
345
|
+
if (out.minInt > 100 || out.minFrac > 100 || out.maxFrac > 100) throw new Error(`NG02306: ${digitsInfo} is not a valid digit info. Exceeded maximum limits of 100 digits.`);
|
|
346
|
+
return out;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** en-US's `#,##0.###` (and `#,##0%` for percents): grouped by threes, `minInt.minFrac-maxFrac` digits. */
|
|
350
|
+
function formatDigits(value: number, digitsInfo: string | undefined, percent: boolean, defaults: { minInt: number; minFrac: number; maxFrac: number }): string {
|
|
351
|
+
let text: string;
|
|
352
|
+
let isZero = false;
|
|
353
|
+
if (!isFinite(value)) {
|
|
354
|
+
text = '∞';
|
|
355
|
+
} else {
|
|
356
|
+
const parsed = parseNumber(value);
|
|
357
|
+
if (percent && parsed.digits[0] !== 0) {
|
|
358
|
+
const fractionLen = parsed.digits.length - parsed.integerLen;
|
|
359
|
+
if (parsed.exponent) parsed.exponent += 2;
|
|
360
|
+
else {
|
|
361
|
+
if (fractionLen === 0) parsed.digits.push(0, 0);
|
|
362
|
+
else if (fractionLen === 1) parsed.digits.push(0);
|
|
363
|
+
parsed.integerLen += 2;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
const { minInt, minFrac, maxFrac } = digitsInfo ? parseDigitsInfo(digitsInfo, defaults) : defaults;
|
|
367
|
+
roundNumber(parsed, minFrac, maxFrac);
|
|
368
|
+
let digits = parsed.digits;
|
|
369
|
+
let integerLen = parsed.integerLen;
|
|
370
|
+
isZero = digits.every((d) => !d);
|
|
371
|
+
for (; integerLen < minInt; integerLen++) digits.unshift(0);
|
|
372
|
+
for (; integerLen < 0; integerLen++) digits.unshift(0);
|
|
373
|
+
let decimals: number[];
|
|
374
|
+
if (integerLen > 0) {
|
|
375
|
+
decimals = digits.slice(integerLen);
|
|
376
|
+
digits = digits.slice(0, integerLen);
|
|
377
|
+
} else {
|
|
378
|
+
decimals = digits;
|
|
379
|
+
digits = [0];
|
|
380
|
+
}
|
|
381
|
+
const groups: string[] = [];
|
|
382
|
+
if (digits.length >= 3) groups.unshift(digits.splice(digits.length - 3).join(''));
|
|
383
|
+
while (digits.length > 3) groups.unshift(digits.splice(digits.length - 3).join(''));
|
|
384
|
+
if (digits.length) groups.unshift(digits.join(''));
|
|
385
|
+
text = groups.join(',');
|
|
386
|
+
if (decimals.length) text += '.' + decimals.join('');
|
|
387
|
+
if (parsed.exponent) text += 'E+' + parsed.exponent;
|
|
388
|
+
}
|
|
389
|
+
if (percent) text += '%';
|
|
390
|
+
return value < 0 && !isZero ? '-' + text : text;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** Angular's `formatNumber` for en-US (`1.0-3` unless `digitsInfo` says otherwise). */
|
|
394
|
+
export function formatNumber(value: number, locale?: string, digitsInfo?: string): string {
|
|
395
|
+
checkLocale(locale);
|
|
396
|
+
return formatDigits(value, digitsInfo, false, { minInt: 1, minFrac: 0, maxFrac: 3 });
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** Angular's `formatPercent` for en-US. */
|
|
400
|
+
export function formatPercent(value: number, locale?: string, digitsInfo?: string): string {
|
|
401
|
+
checkLocale(locale);
|
|
402
|
+
return formatDigits(value, digitsInfo, true, { minInt: 1, minFrac: 0, maxFrac: 0 });
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function isValue(value: unknown): boolean {
|
|
406
|
+
return !(value == null || value === '' || value !== value);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function strToNumber(value: number | string): number {
|
|
410
|
+
if (typeof value === 'string' && !isNaN(Number(value) - parseFloat(value))) return Number(value);
|
|
411
|
+
if (typeof value !== 'number') throw new Error(`NG02309: ${value} is not a number`);
|
|
412
|
+
return value;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export class DatePipe {
|
|
416
|
+
transform(value: Date | string | number | null | undefined, format?: string, timezone?: string, locale?: string): string | null {
|
|
417
|
+
if (value == null || value === '' || value !== value) return null;
|
|
418
|
+
try {
|
|
419
|
+
return formatDate(value, format ?? 'mediumDate', locale, timezone);
|
|
420
|
+
} catch (error) {
|
|
421
|
+
throw invalidPipeArgument('DatePipe', (error as Error).message);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export class DecimalPipe {
|
|
427
|
+
transform(value: number | string | null | undefined, digitsInfo?: string, locale?: string): string | null {
|
|
428
|
+
if (value == null || !isValue(value)) return null;
|
|
429
|
+
try {
|
|
430
|
+
return formatNumber(strToNumber(value), locale, digitsInfo);
|
|
431
|
+
} catch (error) {
|
|
432
|
+
throw invalidPipeArgument('DecimalPipe', (error as Error).message);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
export class PercentPipe {
|
|
438
|
+
transform(value: number | string | null | undefined, digitsInfo?: string, locale?: string): string | null {
|
|
439
|
+
if (value == null || !isValue(value)) return null;
|
|
440
|
+
try {
|
|
441
|
+
return formatPercent(strToNumber(value), locale, digitsInfo);
|
|
442
|
+
} catch (error) {
|
|
443
|
+
throw invalidPipeArgument('PercentPipe', (error as Error).message);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export class UpperCasePipe {
|
|
449
|
+
transform(value: any): string | null {
|
|
450
|
+
if (value == null) return null;
|
|
451
|
+
if (typeof value !== 'string') throw invalidPipeArgument('UpperCasePipe', value);
|
|
452
|
+
return value.toUpperCase();
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
export class LowerCasePipe {
|
|
457
|
+
transform(value: any): string | null {
|
|
458
|
+
if (value == null) return null;
|
|
459
|
+
if (typeof value !== 'string') throw invalidPipeArgument('LowerCasePipe', value);
|
|
460
|
+
return value.toLowerCase();
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
export class TitleCasePipe {
|
|
465
|
+
transform(value: any): string | null {
|
|
466
|
+
if (value == null) return null;
|
|
467
|
+
if (typeof value !== 'string') throw invalidPipeArgument('TitleCasePipe', value);
|
|
468
|
+
// Angular's word pattern is every Unicode letter and digit; a word here starts at a letter or digit and runs to the next space.
|
|
469
|
+
return value.replace(/[0-9A-Za-zªµºÀ-ÖØ-öø-ˁͰ-ϿЀ-ԯԱ-ևא-תؠ-يḀ--ヿ一-鿿가-힣]\S*/g, (word) => word[0].toUpperCase() + word.slice(1).toLowerCase());
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
export class JsonPipe {
|
|
474
|
+
transform(value: any): string {
|
|
475
|
+
return JSON.stringify(value, null, 2);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
export class SlicePipe {
|
|
480
|
+
transform(value: any, start: number, end?: number): any {
|
|
481
|
+
if (value == null) return null;
|
|
482
|
+
if (typeof value !== 'string' && !Array.isArray(value)) throw invalidPipeArgument('SlicePipe', value);
|
|
483
|
+
return value.slice(start, end);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export interface KeyValue<K, V> {
|
|
488
|
+
key: K;
|
|
489
|
+
value: V;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function defaultComparator(a: KeyValue<any, any>, b: KeyValue<any, any>): number {
|
|
493
|
+
const x = a.key;
|
|
494
|
+
const y = b.key;
|
|
495
|
+
if (x === y) return 0;
|
|
496
|
+
if (x == null) return 1;
|
|
497
|
+
if (y == null) return -1;
|
|
498
|
+
if (typeof x == 'string' && typeof y == 'string') return x < y ? -1 : 1;
|
|
499
|
+
if (typeof x == 'number' && typeof y == 'number') return x - y;
|
|
500
|
+
if (typeof x == 'boolean' && typeof y == 'boolean') return x < y ? -1 : 1;
|
|
501
|
+
const xs = String(x);
|
|
502
|
+
const ys = String(y);
|
|
503
|
+
return xs == ys ? 0 : xs < ys ? -1 : 1;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Angular's `keyvalue`: an object's or Map's entries, sorted by key, as one array that stays the same array while
|
|
508
|
+
* no key or value changes, as its KeyValueDiffer keeps it.
|
|
509
|
+
*/
|
|
510
|
+
export class KeyValuePipe {
|
|
511
|
+
private keys: any[] = [];
|
|
512
|
+
private values: any[] = [];
|
|
513
|
+
private keyValues: KeyValue<any, any>[] = [];
|
|
514
|
+
private compareFn: ((a: KeyValue<any, any>, b: KeyValue<any, any>) => number) | null = defaultComparator;
|
|
515
|
+
private seen = false;
|
|
516
|
+
|
|
517
|
+
transform(input: any, compareFn: ((a: KeyValue<any, any>, b: KeyValue<any, any>) => number) | null = defaultComparator): KeyValue<any, any>[] | null {
|
|
518
|
+
if (!input || (!(input instanceof Map) && typeof input !== 'object')) return null;
|
|
519
|
+
const keys: any[] = [];
|
|
520
|
+
const values: any[] = [];
|
|
521
|
+
// Iterated untyped: a Map of any key and value types.
|
|
522
|
+
const entries: any = input;
|
|
523
|
+
if (input instanceof Map) {
|
|
524
|
+
for (const entry of entries) {
|
|
525
|
+
keys.push(entry[0]);
|
|
526
|
+
values.push(entry[1]);
|
|
527
|
+
}
|
|
528
|
+
} else {
|
|
529
|
+
for (const k of Object.keys(input)) {
|
|
530
|
+
keys.push(k);
|
|
531
|
+
values.push(input[k]);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
let changed = !this.seen || keys.length !== this.keys.length;
|
|
535
|
+
for (let i = 0; !changed && i < keys.length; i++) changed = !Object.is(keys[i], this.keys[i]) || !Object.is(values[i], this.values[i]);
|
|
536
|
+
this.seen = true;
|
|
537
|
+
if (changed) {
|
|
538
|
+
this.keys = keys;
|
|
539
|
+
this.values = values;
|
|
540
|
+
this.keyValues = keys.map((k, i): KeyValue<any, any> => ({ key: k, value: values[i] }));
|
|
541
|
+
}
|
|
542
|
+
if (changed || compareFn !== this.compareFn) {
|
|
543
|
+
if (compareFn) this.keyValues.sort(compareFn);
|
|
544
|
+
this.compareFn = compareFn;
|
|
545
|
+
}
|
|
546
|
+
return this.keyValues;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** The classes @nativescript/angular's renderer keeps for a view: an ordered set on the view, shared with the forms runtime's ng-* classes. */
|
|
551
|
+
function viewClasses(view: any): Map<string, boolean> {
|
|
552
|
+
if (!view.ngCssClasses) {
|
|
553
|
+
const classes = new Map<string, boolean>();
|
|
554
|
+
for (const name of String(view.className ?? '').split(/\s+/)) if (name) classes.set(name, true);
|
|
555
|
+
view.ngCssClasses = classes;
|
|
556
|
+
}
|
|
557
|
+
return view.ngCssClasses;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* The className NgClass gives a component's root view, which has no renderer of its own here: its static classes, then
|
|
562
|
+
* each class NgClass's value names (a string or array of names, a Set, or an object mapping names to conditions), then
|
|
563
|
+
* each `[class.x]` whose value is not undefined. A later entry decides a class an earlier one named. `toggles`
|
|
564
|
+
* alternates a `[class.x]` binding's class name and its value.
|
|
565
|
+
*/
|
|
566
|
+
export function ngClassNames(base: string, value: any, toggles: any[]): string {
|
|
567
|
+
const names: string[] = [];
|
|
568
|
+
const enabled: boolean[] = [];
|
|
569
|
+
const decide = (written: string, on: boolean) => {
|
|
570
|
+
for (const name of written.trim().split(/\s+/)) {
|
|
571
|
+
if (!name) continue;
|
|
572
|
+
const at = names.indexOf(name);
|
|
573
|
+
if (at < 0) {
|
|
574
|
+
names.push(name);
|
|
575
|
+
enabled.push(on);
|
|
576
|
+
} else enabled[at] = on;
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
decide(base, true);
|
|
580
|
+
if (typeof value === 'string') decide(value, true);
|
|
581
|
+
else if (Array.isArray(value) || value instanceof Set) {
|
|
582
|
+
for (const name of value) {
|
|
583
|
+
if (typeof name !== 'string') throw new Error(`NgClass can only toggle CSS classes expressed as strings, got ${name}`);
|
|
584
|
+
decide(name, true);
|
|
585
|
+
}
|
|
586
|
+
} else if (value != null && typeof value === 'object') {
|
|
587
|
+
for (const key of Object.keys(value)) decide(key, !!value[key]);
|
|
588
|
+
}
|
|
589
|
+
for (let k = 0; k + 1 < toggles.length; k += 2) if (toggles[k + 1] !== undefined) decide(String(toggles[k]), !!toggles[k + 1]);
|
|
590
|
+
return names.filter((_, k) => enabled[k]).join(' ');
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* @angular/common's NgClass on an element's view, beside its static classes and `[class.x]` bindings, changing the view's
|
|
595
|
+
* classes one at a time as @nativescript/angular's renderer does (each add or remove sets className): one className
|
|
596
|
+
* swapping several classes at once can leave a removed class's background behind. Each apply takes the static classes,
|
|
597
|
+
* NgClass's value (a string or array of names, a Set, or an object mapping names to conditions) and the `[class.x]`
|
|
598
|
+
* bindings (a class name and its value, alternating). NgClass removes only a class it named; a `[class.x]` changes its
|
|
599
|
+
* class when its value changes, an undefined one deferring to the static classes.
|
|
600
|
+
*/
|
|
601
|
+
export class NgClass {
|
|
602
|
+
private readonly view: any = inject(ElementRef).nativeElement;
|
|
603
|
+
private started = false;
|
|
604
|
+
private names: string[] = [];
|
|
605
|
+
private enabled: boolean[] = [];
|
|
606
|
+
private changed: boolean[] = [];
|
|
607
|
+
private touched: boolean[] = [];
|
|
608
|
+
private toggled: any[] = [];
|
|
609
|
+
|
|
610
|
+
apply(input: any[]): void {
|
|
611
|
+
const base: string = input[0];
|
|
612
|
+
const value: any = input[1];
|
|
613
|
+
const toggles: any = input[2];
|
|
614
|
+
const statics = base.trim().split(/\s+/).filter((c) => !!c);
|
|
615
|
+
if (!this.started) {
|
|
616
|
+
this.started = true;
|
|
617
|
+
const classes = new Map<string, boolean>();
|
|
618
|
+
for (const name of statics) classes.set(name, true);
|
|
619
|
+
this.view.ngCssClasses = classes;
|
|
620
|
+
this.view.className = Array.from(classes.keys()).join(' ');
|
|
621
|
+
}
|
|
622
|
+
for (const name of statics) this.update(name, true);
|
|
623
|
+
if (typeof value === 'string') {
|
|
624
|
+
for (const name of value.trim().split(/\s+/)) this.update(name, true);
|
|
625
|
+
} else if (Array.isArray(value) || value instanceof Set) {
|
|
626
|
+
for (const name of value) {
|
|
627
|
+
if (typeof name !== 'string') throw new Error(`NgClass can only toggle CSS classes expressed as strings, got ${name}`);
|
|
628
|
+
this.update(name, true);
|
|
629
|
+
}
|
|
630
|
+
} else if (value != null && typeof value === 'object') {
|
|
631
|
+
for (const key of Object.keys(value)) this.update(key, !!value[key]);
|
|
632
|
+
}
|
|
633
|
+
const names: string[] = [];
|
|
634
|
+
const enabled: boolean[] = [];
|
|
635
|
+
for (let k = 0; k < this.names.length; k++) {
|
|
636
|
+
if (this.changed[k]) this.toggle(this.names[k], this.enabled[k]);
|
|
637
|
+
else if (!this.touched[k]) {
|
|
638
|
+
if (this.enabled[k]) this.toggle(this.names[k], false);
|
|
639
|
+
continue;
|
|
640
|
+
}
|
|
641
|
+
names.push(this.names[k]);
|
|
642
|
+
enabled.push(this.enabled[k]);
|
|
643
|
+
}
|
|
644
|
+
this.names = names;
|
|
645
|
+
this.enabled = enabled;
|
|
646
|
+
this.changed = names.map(() => false);
|
|
647
|
+
this.touched = names.map(() => false);
|
|
648
|
+
for (let k = 0, t = 0; k + 1 < toggles.length; k += 2, t++) {
|
|
649
|
+
const name: string = toggles[k];
|
|
650
|
+
const on = toggles[k + 1] === undefined ? statics.indexOf(name) >= 0 : !!toggles[k + 1];
|
|
651
|
+
if (this.toggled[t] === on) continue;
|
|
652
|
+
this.toggled[t] = on;
|
|
653
|
+
this.toggle(name, on);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
private update(name: string, on: boolean): void {
|
|
658
|
+
const at = this.names.indexOf(name);
|
|
659
|
+
if (at < 0) {
|
|
660
|
+
this.names.push(name);
|
|
661
|
+
this.enabled.push(on);
|
|
662
|
+
this.changed.push(true);
|
|
663
|
+
this.touched.push(true);
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
if (this.enabled[at] !== on) {
|
|
667
|
+
this.enabled[at] = on;
|
|
668
|
+
this.changed[at] = true;
|
|
669
|
+
}
|
|
670
|
+
this.touched[at] = true;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
private toggle(written: string, on: boolean): void {
|
|
674
|
+
for (const name of written.trim().split(/\s+/)) {
|
|
675
|
+
if (!name) continue;
|
|
676
|
+
const classes = viewClasses(this.view);
|
|
677
|
+
if (on === classes.has(name)) continue;
|
|
678
|
+
if (on) classes.set(name, true);
|
|
679
|
+
else classes.delete(name);
|
|
680
|
+
this.view.className = Array.from(classes.keys()).join(' ');
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/** One style NgStyle sets: its key as written (`'width.px'`), the style it names, and the value with the unit appended. */
|
|
686
|
+
export interface NgStyleEntry {
|
|
687
|
+
key: string;
|
|
688
|
+
name: string;
|
|
689
|
+
value: any;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/** NgStyle's value (an object or a Map of styles) as the styles it sets; a null or undefined style value removes the style. */
|
|
693
|
+
export function ngStyleEntries(value: any): NgStyleEntry[] {
|
|
694
|
+
const entries: NgStyleEntry[] = [];
|
|
695
|
+
const add = (key: string, given: any) => {
|
|
696
|
+
const parts = key.split('.');
|
|
697
|
+
entries.push({ key, name: parts[0], value: given == null ? null : parts.length > 1 ? `${given}${parts[1]}` : given });
|
|
698
|
+
};
|
|
699
|
+
if (value instanceof Map) {
|
|
700
|
+
for (const [key, given] of value) add(String(key), given);
|
|
701
|
+
} else if (typeof value === 'object') {
|
|
702
|
+
for (const key of Object.keys(value)) add(key, value[key]);
|
|
703
|
+
} else throw new Error(`NgStyle takes an object or a Map of styles, got ${value}`);
|
|
704
|
+
return entries;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* @angular/common's NgStyle on an element's view, set as @nativescript/angular's renderer sets a style: `view.style[name]`,
|
|
709
|
+
* a CSS variable (`--x`) through the style's unscoped variables. A key no longer given is removed, a style is set
|
|
710
|
+
* again only when its value changed, and a null or undefined value leaves the styles as they are.
|
|
711
|
+
*/
|
|
712
|
+
export class NgStyle {
|
|
713
|
+
private readonly view: any = inject(ElementRef).nativeElement;
|
|
714
|
+
private readonly unset: any;
|
|
715
|
+
private applied: NgStyleEntry[] = [];
|
|
716
|
+
|
|
717
|
+
/** `unset` is core's unsetValue, which the component passes: this module also runs under Node, where core doesn't load. */
|
|
718
|
+
constructor(unset: any) {
|
|
719
|
+
this.unset = unset;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
/** `input` holds NgStyle's value, a new array on each read so that a value changed in place is applied again. */
|
|
723
|
+
apply(input: any[]): void {
|
|
724
|
+
const value: any = input[0];
|
|
725
|
+
if (value == null) return;
|
|
726
|
+
const entries = ngStyleEntries(value);
|
|
727
|
+
for (const old of this.applied) if (!entries.some((e) => e.key === old.key)) this.set(old.name, null);
|
|
728
|
+
for (const entry of entries) {
|
|
729
|
+
const old = this.applied.find((e) => e.key === entry.key);
|
|
730
|
+
if (!old || !Object.is(old.value, entry.value)) this.set(entry.name, entry.value);
|
|
731
|
+
}
|
|
732
|
+
this.applied = entries;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
private set(name: string, value: any): void {
|
|
736
|
+
const style = this.view.style;
|
|
737
|
+
if (name.startsWith('--')) {
|
|
738
|
+
if (value == null) style.unscopedCssVariables.delete(name);
|
|
739
|
+
else style.setUnscopedCssVariable(name, value);
|
|
740
|
+
this.view._onCssStateChange();
|
|
741
|
+
} else style[name] = value == null ? this.unset : value;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/** A QueryList's `changes`: what `subscribe` of an Observable gives, without rxjs, which this module does not depend on. */
|
|
746
|
+
export class QueryListChanges {
|
|
747
|
+
private nextId = 0;
|
|
748
|
+
private observers: { id: number; fn: (list: QueryList) => void }[] = [];
|
|
749
|
+
subscribe(observerOrNext?: ((list: QueryList) => void) | { next?: (list: QueryList) => void; error?: (e: any) => void; complete?: () => void } | null): { unsubscribe(): void } {
|
|
750
|
+
const id = ++this.nextId;
|
|
751
|
+
const fn: (list: QueryList) => void = typeof observerOrNext === 'function'
|
|
752
|
+
? observerOrNext
|
|
753
|
+
: observerOrNext && typeof observerOrNext === 'object' && typeof observerOrNext.next === 'function'
|
|
754
|
+
? (list: QueryList) => observerOrNext.next!(list)
|
|
755
|
+
: () => {};
|
|
756
|
+
this.observers.push({ id, fn });
|
|
757
|
+
return { unsubscribe: () => { this.observers = this.observers.filter((o) => o.id !== id); } };
|
|
758
|
+
}
|
|
759
|
+
next(list: QueryList): void {
|
|
760
|
+
for (const o of this.observers.slice()) o.fn(list);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* @angular/core's QueryList as a compiled template fills it (`@ViewChildren`): the matches in the order their views
|
|
766
|
+
* were made, and `changes`, after a change to them following the first results, once per task. Its items are read
|
|
767
|
+
* untyped: the type its field declares is the app's claim.
|
|
768
|
+
*/
|
|
769
|
+
export class QueryList {
|
|
770
|
+
private keys: unknown[] = [];
|
|
771
|
+
private items: any[] = [];
|
|
772
|
+
private settled = false;
|
|
773
|
+
private scheduled = false;
|
|
774
|
+
readonly changes = new QueryListChanges();
|
|
775
|
+
|
|
776
|
+
get length(): number { return this.items.length; }
|
|
777
|
+
get first(): any { return this.items[0]; }
|
|
778
|
+
get last(): any { return this.items[this.items.length - 1]; }
|
|
779
|
+
get dirty(): boolean { return this.scheduled; }
|
|
780
|
+
|
|
781
|
+
toArray(): any[] { return this.items.slice(); }
|
|
782
|
+
get(index: number): any { return this.items[index]; }
|
|
783
|
+
forEach(fn: (item: any, index: number) => void): void { this.items.forEach((item, index) => fn(item, index)); }
|
|
784
|
+
map(fn: (item: any, index: number) => any): any[] { return this.items.map((item, index) => fn(item, index)); }
|
|
785
|
+
filter(fn: (item: any, index: number) => boolean): any[] { return this.items.filter((item, index) => fn(item, index)); }
|
|
786
|
+
find(fn: (item: any, index: number) => boolean): any { return this.items.find((item, index) => fn(item, index)); }
|
|
787
|
+
some(fn: (item: any, index: number) => boolean): boolean { return this.items.some((item, index) => fn(item, index)); }
|
|
788
|
+
toString(): string { return this.items.toString(); }
|
|
789
|
+
notifyOnChanges(): void { this.changes.next(this); }
|
|
790
|
+
|
|
791
|
+
/** A match the template made: `key` is its view, which `$remove` names when the view goes. */
|
|
792
|
+
$add(key: unknown, value: any): void {
|
|
793
|
+
this.keys.push(key);
|
|
794
|
+
this.items.push(value);
|
|
795
|
+
this.update();
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
$remove(key: unknown): void {
|
|
799
|
+
const at = this.keys.indexOf(key);
|
|
800
|
+
if (at < 0) return;
|
|
801
|
+
this.keys.splice(at, 1);
|
|
802
|
+
this.items.splice(at, 1);
|
|
803
|
+
this.update();
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
private update(): void {
|
|
807
|
+
if (this.scheduled) return;
|
|
808
|
+
this.scheduled = true;
|
|
809
|
+
queueMicrotask(() => {
|
|
810
|
+
this.scheduled = false;
|
|
811
|
+
if (this.settled) this.changes.next(this);
|
|
812
|
+
this.settled = true;
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
/** The view a directive is being made on (its ElementRef), for the forms runtime, which a compiled template gives it. */
|
|
818
|
+
export function hostView(): any {
|
|
819
|
+
return inject(ElementRef).nativeElement;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
/** The markForCheck of the view a directive is being made in, for the forms runtime, which a compiled template gives it. */
|
|
823
|
+
export function viewChecker(): () => void {
|
|
824
|
+
const cd = inject(ChangeDetectorRef);
|
|
825
|
+
return () => cd.markForCheck();
|
|
826
|
+
}
|