@angular/upgrade 16.0.0-next.4 → 16.0.0-next.5

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.
Files changed (45) hide show
  1. package/{esm2020 → esm2022}/src/common/src/version.mjs +1 -1
  2. package/esm2022/src/dynamic/src/upgrade_adapter.mjs +648 -0
  3. package/{esm2020 → esm2022}/src/dynamic/src/upgrade_ng1_adapter.mjs +4 -4
  4. package/{esm2020 → esm2022}/static/src/upgrade_component.mjs +4 -4
  5. package/{esm2020 → esm2022}/static/src/upgrade_module.mjs +5 -5
  6. package/{esm2020 → esm2022}/static/testing/src/create_angular_testing_module.mjs +5 -5
  7. package/{fesm2020 → fesm2022}/static/testing.mjs +5 -5
  8. package/{fesm2020 → fesm2022}/static/testing.mjs.map +1 -1
  9. package/{fesm2020 → fesm2022}/static.mjs +9 -9
  10. package/{fesm2020 → fesm2022}/static.mjs.map +1 -1
  11. package/{fesm2020 → fesm2022}/upgrade.mjs +6 -7
  12. package/{fesm2015 → fesm2022}/upgrade.mjs.map +1 -1
  13. package/index.d.ts +1 -1
  14. package/package.json +15 -25
  15. package/static/index.d.ts +1 -1
  16. package/static/testing/index.d.ts +1 -1
  17. package/esm2020/src/dynamic/src/upgrade_adapter.mjs +0 -649
  18. package/fesm2015/static/testing.mjs +0 -281
  19. package/fesm2015/static/testing.mjs.map +0 -1
  20. package/fesm2015/static.mjs +0 -1841
  21. package/fesm2015/static.mjs.map +0 -1
  22. package/fesm2015/upgrade.mjs +0 -1943
  23. package/fesm2020/upgrade.mjs.map +0 -1
  24. /package/{esm2020 → esm2022}/index.mjs +0 -0
  25. /package/{esm2020 → esm2022}/public_api.mjs +0 -0
  26. /package/{esm2020 → esm2022}/src/common/src/angular1.mjs +0 -0
  27. /package/{esm2020 → esm2022}/src/common/src/component_info.mjs +0 -0
  28. /package/{esm2020 → esm2022}/src/common/src/constants.mjs +0 -0
  29. /package/{esm2020 → esm2022}/src/common/src/downgrade_component.mjs +0 -0
  30. /package/{esm2020 → esm2022}/src/common/src/downgrade_component_adapter.mjs +0 -0
  31. /package/{esm2020 → esm2022}/src/common/src/downgrade_injectable.mjs +0 -0
  32. /package/{esm2020 → esm2022}/src/common/src/promise_util.mjs +0 -0
  33. /package/{esm2020 → esm2022}/src/common/src/upgrade_helper.mjs +0 -0
  34. /package/{esm2020 → esm2022}/src/common/src/util.mjs +0 -0
  35. /package/{esm2020 → esm2022}/static/index.mjs +0 -0
  36. /package/{esm2020 → esm2022}/static/public_api.mjs +0 -0
  37. /package/{esm2020 → esm2022}/static/src/angular1_providers.mjs +0 -0
  38. /package/{esm2020 → esm2022}/static/src/downgrade_module.mjs +0 -0
  39. /package/{esm2020 → esm2022}/static/src/util.mjs +0 -0
  40. /package/{esm2020 → esm2022}/static/static.mjs +0 -0
  41. /package/{esm2020 → esm2022}/static/testing/index.mjs +0 -0
  42. /package/{esm2020 → esm2022}/static/testing/public_api.mjs +0 -0
  43. /package/{esm2020 → esm2022}/static/testing/src/create_angularjs_testing_module.mjs +0 -0
  44. /package/{esm2020 → esm2022}/static/testing/testing.mjs +0 -0
  45. /package/{esm2020 → esm2022}/upgrade.mjs +0 -0
@@ -1,1841 +0,0 @@
1
- /**
2
- * @license Angular v16.0.0-next.4
3
- * (c) 2010-2022 Google LLC. https://angular.io/
4
- * License: MIT
5
- */
6
-
7
- import * as i0 from '@angular/core';
8
- import { ɵNG_MOD_DEF, Injector, ChangeDetectorRef, Testability, TestabilityRegistry, ApplicationRef, SimpleChange, NgZone, ComponentFactoryResolver, Version, ɵNOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR, PlatformRef, EventEmitter, Directive, NgModule } from '@angular/core';
9
- import { platformBrowser } from '@angular/platform-browser';
10
-
11
- function noNg() {
12
- throw new Error('AngularJS v1.x is not loaded!');
13
- }
14
- const noNgElement = (() => noNg());
15
- noNgElement.cleanData = noNg;
16
- let angular = {
17
- bootstrap: noNg,
18
- module: noNg,
19
- element: noNgElement,
20
- injector: noNg,
21
- version: undefined,
22
- resumeBootstrap: noNg,
23
- getTestability: noNg
24
- };
25
- try {
26
- if (window.hasOwnProperty('angular')) {
27
- angular = window.angular;
28
- }
29
- }
30
- catch (_a) {
31
- // ignore in CJS mode.
32
- }
33
- /**
34
- * @deprecated Use `setAngularJSGlobal` instead.
35
- *
36
- * @publicApi
37
- */
38
- function setAngularLib(ng) {
39
- setAngularJSGlobal(ng);
40
- }
41
- /**
42
- * @deprecated Use `getAngularJSGlobal` instead.
43
- *
44
- * @publicApi
45
- */
46
- function getAngularLib() {
47
- return getAngularJSGlobal();
48
- }
49
- /**
50
- * Resets the AngularJS global.
51
- *
52
- * Used when AngularJS is loaded lazily, and not available on `window`.
53
- *
54
- * @publicApi
55
- */
56
- function setAngularJSGlobal(ng) {
57
- angular = ng;
58
- }
59
- /**
60
- * Returns the current AngularJS global.
61
- *
62
- * @publicApi
63
- */
64
- function getAngularJSGlobal() {
65
- return angular;
66
- }
67
- const bootstrap = (e, modules, config) => angular.bootstrap(e, modules, config);
68
- // Do not declare as `module` to avoid webpack bug
69
- // (see https://github.com/angular/angular/issues/30050).
70
- const module_ = (prefix, dependencies) => angular.module(prefix, dependencies);
71
- const element = (e => angular.element(e));
72
- element.cleanData = nodes => angular.element.cleanData(nodes);
73
- const injector = (modules, strictDi) => angular.injector(modules, strictDi);
74
- const resumeBootstrap = () => angular.resumeBootstrap();
75
- const getTestability = e => angular.getTestability(e);
76
-
77
- const $COMPILE = '$compile';
78
- const $CONTROLLER = '$controller';
79
- const $DELEGATE = '$delegate';
80
- const $EXCEPTION_HANDLER = '$exceptionHandler';
81
- const $HTTP_BACKEND = '$httpBackend';
82
- const $INJECTOR = '$injector';
83
- const $INTERVAL = '$interval';
84
- const $PARSE = '$parse';
85
- const $PROVIDE = '$provide';
86
- const $ROOT_ELEMENT = '$rootElement';
87
- const $ROOT_SCOPE = '$rootScope';
88
- const $SCOPE = '$scope';
89
- const $TEMPLATE_CACHE = '$templateCache';
90
- const $TEMPLATE_REQUEST = '$templateRequest';
91
- const $$TESTABILITY = '$$testability';
92
- const COMPILER_KEY = '$$angularCompiler';
93
- const DOWNGRADED_MODULE_COUNT_KEY = '$$angularDowngradedModuleCount';
94
- const GROUP_PROJECTABLE_NODES_KEY = '$$angularGroupProjectableNodes';
95
- const INJECTOR_KEY = '$$angularInjector';
96
- const LAZY_MODULE_REF = '$$angularLazyModuleRef';
97
- const NG_ZONE_KEY = '$$angularNgZone';
98
- const UPGRADE_APP_TYPE_KEY = '$$angularUpgradeAppType';
99
- const REQUIRE_INJECTOR = '?^^' + INJECTOR_KEY;
100
- const REQUIRE_NG_MODEL = '?ngModel';
101
- const UPGRADE_MODULE_NAME = '$$UpgradeModule';
102
-
103
- /**
104
- * A `PropertyBinding` represents a mapping between a property name
105
- * and an attribute name. It is parsed from a string of the form
106
- * `"prop: attr"`; or simply `"propAndAttr" where the property
107
- * and attribute have the same identifier.
108
- */
109
- class PropertyBinding {
110
- constructor(prop, attr) {
111
- this.prop = prop;
112
- this.attr = attr;
113
- this.bracketAttr = `[${this.attr}]`;
114
- this.parenAttr = `(${this.attr})`;
115
- this.bracketParenAttr = `[(${this.attr})]`;
116
- const capitalAttr = this.attr.charAt(0).toUpperCase() + this.attr.slice(1);
117
- this.onAttr = `on${capitalAttr}`;
118
- this.bindAttr = `bind${capitalAttr}`;
119
- this.bindonAttr = `bindon${capitalAttr}`;
120
- }
121
- }
122
-
123
- const DIRECTIVE_PREFIX_REGEXP = /^(?:x|data)[:\-_]/i;
124
- const DIRECTIVE_SPECIAL_CHARS_REGEXP = /[:\-_]+(.)/g;
125
- function onError(e) {
126
- // TODO: (misko): We seem to not have a stack trace here!
127
- console.error(e, e.stack);
128
- throw e;
129
- }
130
- /**
131
- * Clean the jqLite/jQuery data on the element and all its descendants.
132
- * Equivalent to how jqLite/jQuery invoke `cleanData()` on an Element when removed:
133
- * https://github.com/angular/angular.js/blob/2e72ea13fa98bebf6ed4b5e3c45eaf5f990ed16f/src/jqLite.js#L349-L355
134
- * https://github.com/jquery/jquery/blob/6984d1747623dbc5e87fd6c261a5b6b1628c107c/src/manipulation.js#L182
135
- *
136
- * NOTE:
137
- * `cleanData()` will also invoke the AngularJS `$destroy` DOM event on the element:
138
- * https://github.com/angular/angular.js/blob/2e72ea13fa98bebf6ed4b5e3c45eaf5f990ed16f/src/Angular.js#L1932-L1945
139
- *
140
- * @param node The DOM node whose data needs to be cleaned.
141
- */
142
- function cleanData(node) {
143
- element.cleanData([node]);
144
- if (isParentNode(node)) {
145
- element.cleanData(node.querySelectorAll('*'));
146
- }
147
- }
148
- function controllerKey(name) {
149
- return '$' + name + 'Controller';
150
- }
151
- /**
152
- * Destroy an AngularJS app given the app `$injector`.
153
- *
154
- * NOTE: Destroying an app is not officially supported by AngularJS, but try to do our best by
155
- * destroying `$rootScope` and clean the jqLite/jQuery data on `$rootElement` and all
156
- * descendants.
157
- *
158
- * @param $injector The `$injector` of the AngularJS app to destroy.
159
- */
160
- function destroyApp($injector) {
161
- const $rootElement = $injector.get($ROOT_ELEMENT);
162
- const $rootScope = $injector.get($ROOT_SCOPE);
163
- $rootScope.$destroy();
164
- cleanData($rootElement[0]);
165
- }
166
- function directiveNormalize(name) {
167
- return name.replace(DIRECTIVE_PREFIX_REGEXP, '')
168
- .replace(DIRECTIVE_SPECIAL_CHARS_REGEXP, (_, letter) => letter.toUpperCase());
169
- }
170
- function getTypeName(type) {
171
- // Return the name of the type or the first line of its stringified version.
172
- return type.overriddenName || type.name || type.toString().split('\n')[0];
173
- }
174
- function getDowngradedModuleCount($injector) {
175
- return $injector.has(DOWNGRADED_MODULE_COUNT_KEY) ? $injector.get(DOWNGRADED_MODULE_COUNT_KEY) :
176
- 0;
177
- }
178
- function getUpgradeAppType($injector) {
179
- return $injector.has(UPGRADE_APP_TYPE_KEY) ? $injector.get(UPGRADE_APP_TYPE_KEY) :
180
- 0 /* UpgradeAppType.None */;
181
- }
182
- function isFunction(value) {
183
- return typeof value === 'function';
184
- }
185
- function isNgModuleType(value) {
186
- // NgModule class should have the `ɵmod` static property attached by AOT or JIT compiler.
187
- return isFunction(value) && !!value[ɵNG_MOD_DEF];
188
- }
189
- function isParentNode(node) {
190
- return isFunction(node.querySelectorAll);
191
- }
192
- function validateInjectionKey($injector, downgradedModule, injectionKey, attemptedAction) {
193
- const upgradeAppType = getUpgradeAppType($injector);
194
- const downgradedModuleCount = getDowngradedModuleCount($injector);
195
- // Check for common errors.
196
- switch (upgradeAppType) {
197
- case 1 /* UpgradeAppType.Dynamic */:
198
- case 2 /* UpgradeAppType.Static */:
199
- if (downgradedModule) {
200
- throw new Error(`Error while ${attemptedAction}: 'downgradedModule' unexpectedly specified.\n` +
201
- 'You should not specify a value for \'downgradedModule\', unless you are downgrading ' +
202
- 'more than one Angular module (via \'downgradeModule()\').');
203
- }
204
- break;
205
- case 3 /* UpgradeAppType.Lite */:
206
- if (!downgradedModule && (downgradedModuleCount >= 2)) {
207
- throw new Error(`Error while ${attemptedAction}: 'downgradedModule' not specified.\n` +
208
- 'This application contains more than one downgraded Angular module, thus you need to ' +
209
- 'always specify \'downgradedModule\' when downgrading components and injectables.');
210
- }
211
- if (!$injector.has(injectionKey)) {
212
- throw new Error(`Error while ${attemptedAction}: Unable to find the specified downgraded module.\n` +
213
- 'Did you forget to downgrade an Angular module or include it in the AngularJS ' +
214
- 'application?');
215
- }
216
- break;
217
- default:
218
- throw new Error(`Error while ${attemptedAction}: Not a valid '@angular/upgrade' application.\n` +
219
- 'Did you forget to downgrade an Angular module or include it in the AngularJS ' +
220
- 'application?');
221
- }
222
- }
223
- class Deferred {
224
- constructor() {
225
- this.promise = new Promise((res, rej) => {
226
- this.resolve = res;
227
- this.reject = rej;
228
- });
229
- }
230
- }
231
- /**
232
- * @return Whether the passed-in component implements the subset of the
233
- * `ControlValueAccessor` interface needed for AngularJS `ng-model`
234
- * compatibility.
235
- */
236
- function supportsNgModel(component) {
237
- return typeof component.writeValue === 'function' &&
238
- typeof component.registerOnChange === 'function';
239
- }
240
- /**
241
- * Glue the AngularJS `NgModelController` (if it exists) to the component
242
- * (if it implements the needed subset of the `ControlValueAccessor` interface).
243
- */
244
- function hookupNgModel(ngModel, component) {
245
- if (ngModel && supportsNgModel(component)) {
246
- ngModel.$render = () => {
247
- component.writeValue(ngModel.$viewValue);
248
- };
249
- component.registerOnChange(ngModel.$setViewValue.bind(ngModel));
250
- if (typeof component.registerOnTouched === 'function') {
251
- component.registerOnTouched(ngModel.$setTouched.bind(ngModel));
252
- }
253
- }
254
- }
255
- /**
256
- * Test two values for strict equality, accounting for the fact that `NaN !== NaN`.
257
- */
258
- function strictEquals(val1, val2) {
259
- return val1 === val2 || (val1 !== val1 && val2 !== val2);
260
- }
261
-
262
- const INITIAL_VALUE$1 = {
263
- __UNINITIALIZED__: true
264
- };
265
- class DowngradeComponentAdapter {
266
- constructor(element, attrs, scope, ngModel, parentInjector, $compile, $parse, componentFactory, wrapCallback) {
267
- this.element = element;
268
- this.attrs = attrs;
269
- this.scope = scope;
270
- this.ngModel = ngModel;
271
- this.parentInjector = parentInjector;
272
- this.$compile = $compile;
273
- this.$parse = $parse;
274
- this.componentFactory = componentFactory;
275
- this.wrapCallback = wrapCallback;
276
- this.implementsOnChanges = false;
277
- this.inputChangeCount = 0;
278
- this.inputChanges = {};
279
- this.componentScope = scope.$new();
280
- }
281
- compileContents() {
282
- const compiledProjectableNodes = [];
283
- const projectableNodes = this.groupProjectableNodes();
284
- const linkFns = projectableNodes.map(nodes => this.$compile(nodes));
285
- this.element.empty();
286
- linkFns.forEach(linkFn => {
287
- linkFn(this.scope, (clone) => {
288
- compiledProjectableNodes.push(clone);
289
- this.element.append(clone);
290
- });
291
- });
292
- return compiledProjectableNodes;
293
- }
294
- createComponentAndSetup(projectableNodes, manuallyAttachView = false, propagateDigest = true) {
295
- const component = this.createComponent(projectableNodes);
296
- this.setupInputs(manuallyAttachView, propagateDigest, component);
297
- this.setupOutputs(component.componentRef);
298
- this.registerCleanup(component.componentRef);
299
- return component.componentRef;
300
- }
301
- createComponent(projectableNodes) {
302
- const providers = [{ provide: $SCOPE, useValue: this.componentScope }];
303
- const childInjector = Injector.create({ providers: providers, parent: this.parentInjector, name: 'DowngradeComponentAdapter' });
304
- const componentRef = this.componentFactory.create(childInjector, projectableNodes, this.element[0]);
305
- const viewChangeDetector = componentRef.injector.get(ChangeDetectorRef);
306
- const changeDetector = componentRef.changeDetectorRef;
307
- // testability hook is commonly added during component bootstrap in
308
- // packages/core/src/application_ref.bootstrap()
309
- // in downgraded application, component creation will take place here as well as adding the
310
- // testability hook.
311
- const testability = componentRef.injector.get(Testability, null);
312
- if (testability) {
313
- componentRef.injector.get(TestabilityRegistry)
314
- .registerApplication(componentRef.location.nativeElement, testability);
315
- }
316
- hookupNgModel(this.ngModel, componentRef.instance);
317
- return { viewChangeDetector, componentRef, changeDetector };
318
- }
319
- setupInputs(manuallyAttachView, propagateDigest = true, { componentRef, changeDetector, viewChangeDetector }) {
320
- const attrs = this.attrs;
321
- const inputs = this.componentFactory.inputs || [];
322
- for (const input of inputs) {
323
- const inputBinding = new PropertyBinding(input.propName, input.templateName);
324
- let expr = null;
325
- if (attrs.hasOwnProperty(inputBinding.attr)) {
326
- const observeFn = (prop => {
327
- let prevValue = INITIAL_VALUE$1;
328
- return (currValue) => {
329
- // Initially, both `$observe()` and `$watch()` will call this function.
330
- if (!strictEquals(prevValue, currValue)) {
331
- if (prevValue === INITIAL_VALUE$1) {
332
- prevValue = currValue;
333
- }
334
- this.updateInput(componentRef, prop, prevValue, currValue);
335
- prevValue = currValue;
336
- }
337
- };
338
- })(inputBinding.prop);
339
- attrs.$observe(inputBinding.attr, observeFn);
340
- // Use `$watch()` (in addition to `$observe()`) in order to initialize the input in time
341
- // for `ngOnChanges()`. This is necessary if we are already in a `$digest`, which means that
342
- // `ngOnChanges()` (which is called by a watcher) will run before the `$observe()` callback.
343
- let unwatch = this.componentScope.$watch(() => {
344
- unwatch();
345
- unwatch = null;
346
- observeFn(attrs[inputBinding.attr]);
347
- });
348
- }
349
- else if (attrs.hasOwnProperty(inputBinding.bindAttr)) {
350
- expr = attrs[inputBinding.bindAttr];
351
- }
352
- else if (attrs.hasOwnProperty(inputBinding.bracketAttr)) {
353
- expr = attrs[inputBinding.bracketAttr];
354
- }
355
- else if (attrs.hasOwnProperty(inputBinding.bindonAttr)) {
356
- expr = attrs[inputBinding.bindonAttr];
357
- }
358
- else if (attrs.hasOwnProperty(inputBinding.bracketParenAttr)) {
359
- expr = attrs[inputBinding.bracketParenAttr];
360
- }
361
- if (expr != null) {
362
- const watchFn = ((prop) => (currValue, prevValue) => this.updateInput(componentRef, prop, prevValue, currValue))(inputBinding.prop);
363
- this.componentScope.$watch(expr, watchFn);
364
- }
365
- }
366
- // Invoke `ngOnChanges()` and Change Detection (when necessary)
367
- const detectChanges = () => changeDetector.detectChanges();
368
- const prototype = this.componentFactory.componentType.prototype;
369
- this.implementsOnChanges = !!(prototype && prototype.ngOnChanges);
370
- this.componentScope.$watch(() => this.inputChangeCount, this.wrapCallback(() => {
371
- // Invoke `ngOnChanges()`
372
- if (this.implementsOnChanges) {
373
- const inputChanges = this.inputChanges;
374
- this.inputChanges = {};
375
- componentRef.instance.ngOnChanges(inputChanges);
376
- }
377
- viewChangeDetector.markForCheck();
378
- // If opted out of propagating digests, invoke change detection when inputs change.
379
- if (!propagateDigest) {
380
- detectChanges();
381
- }
382
- }));
383
- // If not opted out of propagating digests, invoke change detection on every digest
384
- if (propagateDigest) {
385
- this.componentScope.$watch(this.wrapCallback(detectChanges));
386
- }
387
- // If necessary, attach the view so that it will be dirty-checked.
388
- // (Allow time for the initial input values to be set and `ngOnChanges()` to be called.)
389
- if (manuallyAttachView || !propagateDigest) {
390
- let unwatch = this.componentScope.$watch(() => {
391
- unwatch();
392
- unwatch = null;
393
- const appRef = this.parentInjector.get(ApplicationRef);
394
- appRef.attachView(componentRef.hostView);
395
- });
396
- }
397
- }
398
- setupOutputs(componentRef) {
399
- const attrs = this.attrs;
400
- const outputs = this.componentFactory.outputs || [];
401
- for (const output of outputs) {
402
- const outputBindings = new PropertyBinding(output.propName, output.templateName);
403
- const bindonAttr = outputBindings.bindonAttr.substring(0, outputBindings.bindonAttr.length - 6);
404
- const bracketParenAttr = `[(${outputBindings.bracketParenAttr.substring(2, outputBindings.bracketParenAttr.length - 8)})]`;
405
- // order below is important - first update bindings then evaluate expressions
406
- if (attrs.hasOwnProperty(bindonAttr)) {
407
- this.subscribeToOutput(componentRef, outputBindings, attrs[bindonAttr], true);
408
- }
409
- if (attrs.hasOwnProperty(bracketParenAttr)) {
410
- this.subscribeToOutput(componentRef, outputBindings, attrs[bracketParenAttr], true);
411
- }
412
- if (attrs.hasOwnProperty(outputBindings.onAttr)) {
413
- this.subscribeToOutput(componentRef, outputBindings, attrs[outputBindings.onAttr]);
414
- }
415
- if (attrs.hasOwnProperty(outputBindings.parenAttr)) {
416
- this.subscribeToOutput(componentRef, outputBindings, attrs[outputBindings.parenAttr]);
417
- }
418
- }
419
- }
420
- subscribeToOutput(componentRef, output, expr, isAssignment = false) {
421
- const getter = this.$parse(expr);
422
- const setter = getter.assign;
423
- if (isAssignment && !setter) {
424
- throw new Error(`Expression '${expr}' is not assignable!`);
425
- }
426
- const emitter = componentRef.instance[output.prop];
427
- if (emitter) {
428
- emitter.subscribe({
429
- next: isAssignment ? (v) => setter(this.scope, v) :
430
- (v) => getter(this.scope, { '$event': v })
431
- });
432
- }
433
- else {
434
- throw new Error(`Missing emitter '${output.prop}' on component '${getTypeName(this.componentFactory.componentType)}'!`);
435
- }
436
- }
437
- registerCleanup(componentRef) {
438
- const testabilityRegistry = componentRef.injector.get(TestabilityRegistry);
439
- const destroyComponentRef = this.wrapCallback(() => componentRef.destroy());
440
- let destroyed = false;
441
- this.element.on('$destroy', () => {
442
- // The `$destroy` event may have been triggered by the `cleanData()` call in the
443
- // `componentScope` `$destroy` handler below. In that case, we don't want to call
444
- // `componentScope.$destroy()` again.
445
- if (!destroyed)
446
- this.componentScope.$destroy();
447
- });
448
- this.componentScope.$on('$destroy', () => {
449
- if (!destroyed) {
450
- destroyed = true;
451
- testabilityRegistry.unregisterApplication(componentRef.location.nativeElement);
452
- // The `componentScope` might be getting destroyed, because an ancestor element is being
453
- // removed/destroyed. If that is the case, jqLite/jQuery would normally invoke `cleanData()`
454
- // on the removed element and all descendants.
455
- // https://github.com/angular/angular.js/blob/2e72ea13fa98bebf6ed4b5e3c45eaf5f990ed16f/src/jqLite.js#L349-L355
456
- // https://github.com/jquery/jquery/blob/6984d1747623dbc5e87fd6c261a5b6b1628c107c/src/manipulation.js#L182
457
- //
458
- // Here, however, `destroyComponentRef()` may under some circumstances remove the element
459
- // from the DOM and therefore it will no longer be a descendant of the removed element when
460
- // `cleanData()` is called. This would result in a memory leak, because the element's data
461
- // and event handlers (and all objects directly or indirectly referenced by them) would be
462
- // retained.
463
- //
464
- // To ensure the element is always properly cleaned up, we manually call `cleanData()` on
465
- // this element and its descendants before destroying the `ComponentRef`.
466
- cleanData(this.element[0]);
467
- destroyComponentRef();
468
- }
469
- });
470
- }
471
- updateInput(componentRef, prop, prevValue, currValue) {
472
- if (this.implementsOnChanges) {
473
- this.inputChanges[prop] = new SimpleChange(prevValue, currValue, prevValue === currValue);
474
- }
475
- this.inputChangeCount++;
476
- componentRef.instance[prop] = currValue;
477
- }
478
- groupProjectableNodes() {
479
- let ngContentSelectors = this.componentFactory.ngContentSelectors;
480
- return groupNodesBySelector(ngContentSelectors, this.element.contents());
481
- }
482
- }
483
- /**
484
- * Group a set of DOM nodes into `ngContent` groups, based on the given content selectors.
485
- */
486
- function groupNodesBySelector(ngContentSelectors, nodes) {
487
- const projectableNodes = [];
488
- for (let i = 0, ii = ngContentSelectors.length; i < ii; ++i) {
489
- projectableNodes[i] = [];
490
- }
491
- for (let j = 0, jj = nodes.length; j < jj; ++j) {
492
- const node = nodes[j];
493
- const ngContentIndex = findMatchingNgContentIndex(node, ngContentSelectors);
494
- if (ngContentIndex != null) {
495
- projectableNodes[ngContentIndex].push(node);
496
- }
497
- }
498
- return projectableNodes;
499
- }
500
- function findMatchingNgContentIndex(element, ngContentSelectors) {
501
- const ngContentIndices = [];
502
- let wildcardNgContentIndex = -1;
503
- for (let i = 0; i < ngContentSelectors.length; i++) {
504
- const selector = ngContentSelectors[i];
505
- if (selector === '*') {
506
- wildcardNgContentIndex = i;
507
- }
508
- else {
509
- if (matchesSelector(element, selector)) {
510
- ngContentIndices.push(i);
511
- }
512
- }
513
- }
514
- ngContentIndices.sort();
515
- if (wildcardNgContentIndex !== -1) {
516
- ngContentIndices.push(wildcardNgContentIndex);
517
- }
518
- return ngContentIndices.length ? ngContentIndices[0] : null;
519
- }
520
- function matchesSelector(el, selector) {
521
- var _a;
522
- const elProto = Element.prototype;
523
- return el.nodeType === Node.ELEMENT_NODE
524
- // matches is supported by all browsers from 2014 onwards except non-chromium edge
525
- ?
526
- ((_a = elProto.matches) !== null && _a !== void 0 ? _a : elProto.msMatchesSelector).call(el, selector) :
527
- false;
528
- }
529
-
530
- function isThenable(obj) {
531
- return !!obj && isFunction(obj.then);
532
- }
533
- /**
534
- * Synchronous, promise-like object.
535
- */
536
- class SyncPromise {
537
- constructor() {
538
- this.resolved = false;
539
- this.callbacks = [];
540
- }
541
- static all(valuesOrPromises) {
542
- const aggrPromise = new SyncPromise();
543
- let resolvedCount = 0;
544
- const results = [];
545
- const resolve = (idx, value) => {
546
- results[idx] = value;
547
- if (++resolvedCount === valuesOrPromises.length)
548
- aggrPromise.resolve(results);
549
- };
550
- valuesOrPromises.forEach((p, idx) => {
551
- if (isThenable(p)) {
552
- p.then(v => resolve(idx, v));
553
- }
554
- else {
555
- resolve(idx, p);
556
- }
557
- });
558
- return aggrPromise;
559
- }
560
- resolve(value) {
561
- // Do nothing, if already resolved.
562
- if (this.resolved)
563
- return;
564
- this.value = value;
565
- this.resolved = true;
566
- // Run the queued callbacks.
567
- this.callbacks.forEach(callback => callback(value));
568
- this.callbacks.length = 0;
569
- }
570
- then(callback) {
571
- if (this.resolved) {
572
- callback(this.value);
573
- }
574
- else {
575
- this.callbacks.push(callback);
576
- }
577
- }
578
- }
579
-
580
- /**
581
- * @description
582
- *
583
- * A helper function that allows an Angular component to be used from AngularJS.
584
- *
585
- * *Part of the [upgrade/static](api?query=upgrade%2Fstatic)
586
- * library for hybrid upgrade apps that support AOT compilation*
587
- *
588
- * This helper function returns a factory function to be used for registering
589
- * an AngularJS wrapper directive for "downgrading" an Angular component.
590
- *
591
- * @usageNotes
592
- * ### Examples
593
- *
594
- * Let's assume that you have an Angular component called `ng2Heroes` that needs
595
- * to be made available in AngularJS templates.
596
- *
597
- * {@example upgrade/static/ts/full/module.ts region="ng2-heroes"}
598
- *
599
- * We must create an AngularJS [directive](https://docs.angularjs.org/guide/directive)
600
- * that will make this Angular component available inside AngularJS templates.
601
- * The `downgradeComponent()` function returns a factory function that we
602
- * can use to define the AngularJS directive that wraps the "downgraded" component.
603
- *
604
- * {@example upgrade/static/ts/full/module.ts region="ng2-heroes-wrapper"}
605
- *
606
- * For more details and examples on downgrading Angular components to AngularJS components please
607
- * visit the [Upgrade guide](guide/upgrade#using-angular-components-from-angularjs-code).
608
- *
609
- * @param info contains information about the Component that is being downgraded:
610
- *
611
- * - `component: Type<any>`: The type of the Component that will be downgraded
612
- * - `downgradedModule?: string`: The name of the downgraded module (if any) that the component
613
- * "belongs to", as returned by a call to `downgradeModule()`. It is the module, whose
614
- * corresponding Angular module will be bootstrapped, when the component needs to be instantiated.
615
- * <br />
616
- * (This option is only necessary when using `downgradeModule()` to downgrade more than one
617
- * Angular module.)
618
- * - `propagateDigest?: boolean`: Whether to perform {@link ChangeDetectorRef#detectChanges
619
- * change detection} on the component on every
620
- * [$digest](https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$digest). If set to `false`,
621
- * change detection will still be performed when any of the component's inputs changes.
622
- * (Default: true)
623
- *
624
- * @returns a factory function that can be used to register the component in an
625
- * AngularJS module.
626
- *
627
- * @publicApi
628
- */
629
- function downgradeComponent(info) {
630
- const directiveFactory = function ($compile, $injector, $parse) {
631
- // When using `downgradeModule()`, we need to handle certain things specially. For example:
632
- // - We always need to attach the component view to the `ApplicationRef` for it to be
633
- // dirty-checked.
634
- // - We need to ensure callbacks to Angular APIs (e.g. change detection) are run inside the
635
- // Angular zone.
636
- // NOTE: This is not needed, when using `UpgradeModule`, because `$digest()` will be run
637
- // inside the Angular zone (except if explicitly escaped, in which case we shouldn't
638
- // force it back in).
639
- const isNgUpgradeLite = getUpgradeAppType($injector) === 3 /* UpgradeAppType.Lite */;
640
- const wrapCallback = !isNgUpgradeLite ? cb => cb : cb => () => NgZone.isInAngularZone() ? cb() : ngZone.run(cb);
641
- let ngZone;
642
- // When downgrading multiple modules, special handling is needed wrt injectors.
643
- const hasMultipleDowngradedModules = isNgUpgradeLite && (getDowngradedModuleCount($injector) > 1);
644
- return {
645
- restrict: 'E',
646
- terminal: true,
647
- require: [REQUIRE_INJECTOR, REQUIRE_NG_MODEL],
648
- link: (scope, element, attrs, required) => {
649
- var _a;
650
- // We might have to compile the contents asynchronously, because this might have been
651
- // triggered by `UpgradeNg1ComponentAdapterBuilder`, before the Angular templates have
652
- // been compiled.
653
- const ngModel = required[1];
654
- const parentInjector = required[0];
655
- let moduleInjector = undefined;
656
- let ranAsync = false;
657
- if (!parentInjector || hasMultipleDowngradedModules) {
658
- const downgradedModule = info.downgradedModule || '';
659
- const lazyModuleRefKey = `${LAZY_MODULE_REF}${downgradedModule}`;
660
- const attemptedAction = `instantiating component '${getTypeName(info.component)}'`;
661
- validateInjectionKey($injector, downgradedModule, lazyModuleRefKey, attemptedAction);
662
- const lazyModuleRef = $injector.get(lazyModuleRefKey);
663
- moduleInjector = (_a = lazyModuleRef.injector) !== null && _a !== void 0 ? _a : lazyModuleRef.promise;
664
- }
665
- // Notes:
666
- //
667
- // There are two injectors: `finalModuleInjector` and `finalParentInjector` (they might be
668
- // the same instance, but that is irrelevant):
669
- // - `finalModuleInjector` is used to retrieve `ComponentFactoryResolver`, thus it must be
670
- // on the same tree as the `NgModule` that declares this downgraded component.
671
- // - `finalParentInjector` is used for all other injection purposes.
672
- // (Note that Angular knows to only traverse the component-tree part of that injector,
673
- // when looking for an injectable and then switch to the module injector.)
674
- //
675
- // There are basically three cases:
676
- // - If there is no parent component (thus no `parentInjector`), we bootstrap the downgraded
677
- // `NgModule` and use its injector as both `finalModuleInjector` and
678
- // `finalParentInjector`.
679
- // - If there is a parent component (and thus a `parentInjector`) and we are sure that it
680
- // belongs to the same `NgModule` as this downgraded component (e.g. because there is only
681
- // one downgraded module, we use that `parentInjector` as both `finalModuleInjector` and
682
- // `finalParentInjector`.
683
- // - If there is a parent component, but it may belong to a different `NgModule`, then we
684
- // use the `parentInjector` as `finalParentInjector` and this downgraded component's
685
- // declaring `NgModule`'s injector as `finalModuleInjector`.
686
- // Note 1: If the `NgModule` is already bootstrapped, we just get its injector (we don't
687
- // bootstrap again).
688
- // Note 2: It is possible that (while there are multiple downgraded modules) this
689
- // downgraded component and its parent component both belong to the same NgModule.
690
- // In that case, we could have used the `parentInjector` as both
691
- // `finalModuleInjector` and `finalParentInjector`, but (for simplicity) we are
692
- // treating this case as if they belong to different `NgModule`s. That doesn't
693
- // really affect anything, since `parentInjector` has `moduleInjector` as ancestor
694
- // and trying to resolve `ComponentFactoryResolver` from either one will return
695
- // the same instance.
696
- // If there is a parent component, use its injector as parent injector.
697
- // If this is a "top-level" Angular component, use the module injector.
698
- const finalParentInjector = parentInjector || moduleInjector;
699
- // If this is a "top-level" Angular component or the parent component may belong to a
700
- // different `NgModule`, use the module injector for module-specific dependencies.
701
- // If there is a parent component that belongs to the same `NgModule`, use its injector.
702
- const finalModuleInjector = moduleInjector || parentInjector;
703
- const doDowngrade = (injector, moduleInjector) => {
704
- // Retrieve `ComponentFactoryResolver` from the injector tied to the `NgModule` this
705
- // component belongs to.
706
- const componentFactoryResolver = moduleInjector.get(ComponentFactoryResolver);
707
- const componentFactory = componentFactoryResolver.resolveComponentFactory(info.component);
708
- if (!componentFactory) {
709
- throw new Error(`Expecting ComponentFactory for: ${getTypeName(info.component)}`);
710
- }
711
- const injectorPromise = new ParentInjectorPromise(element);
712
- const facade = new DowngradeComponentAdapter(element, attrs, scope, ngModel, injector, $compile, $parse, componentFactory, wrapCallback);
713
- const projectableNodes = facade.compileContents();
714
- const componentRef = facade.createComponentAndSetup(projectableNodes, isNgUpgradeLite, info.propagateDigest);
715
- injectorPromise.resolve(componentRef.injector);
716
- if (ranAsync) {
717
- // If this is run async, it is possible that it is not run inside a
718
- // digest and initial input values will not be detected.
719
- scope.$evalAsync(() => { });
720
- }
721
- };
722
- const downgradeFn = !isNgUpgradeLite ? doDowngrade : (pInjector, mInjector) => {
723
- if (!ngZone) {
724
- ngZone = pInjector.get(NgZone);
725
- }
726
- wrapCallback(() => doDowngrade(pInjector, mInjector))();
727
- };
728
- // NOTE:
729
- // Not using `ParentInjectorPromise.all()` (which is inherited from `SyncPromise`), because
730
- // Closure Compiler (or some related tool) complains:
731
- // `TypeError: ...$src$downgrade_component_ParentInjectorPromise.all is not a function`
732
- SyncPromise.all([finalParentInjector, finalModuleInjector])
733
- .then(([pInjector, mInjector]) => downgradeFn(pInjector, mInjector));
734
- ranAsync = true;
735
- }
736
- };
737
- };
738
- // bracket-notation because of closure - see #14441
739
- directiveFactory['$inject'] = [$COMPILE, $INJECTOR, $PARSE];
740
- return directiveFactory;
741
- }
742
- /**
743
- * Synchronous promise-like object to wrap parent injectors,
744
- * to preserve the synchronous nature of AngularJS's `$compile`.
745
- */
746
- class ParentInjectorPromise extends SyncPromise {
747
- constructor(element) {
748
- super();
749
- this.element = element;
750
- this.injectorKey = controllerKey(INJECTOR_KEY);
751
- // Store the promise on the element.
752
- element.data(this.injectorKey, this);
753
- }
754
- resolve(injector) {
755
- // Store the real injector on the element.
756
- this.element.data(this.injectorKey, injector);
757
- // Release the element to prevent memory leaks.
758
- this.element = null;
759
- // Resolve the promise.
760
- super.resolve(injector);
761
- }
762
- }
763
-
764
- /**
765
- * @description
766
- *
767
- * A helper function to allow an Angular service to be accessible from AngularJS.
768
- *
769
- * *Part of the [upgrade/static](api?query=upgrade%2Fstatic)
770
- * library for hybrid upgrade apps that support AOT compilation*
771
- *
772
- * This helper function returns a factory function that provides access to the Angular
773
- * service identified by the `token` parameter.
774
- *
775
- * @usageNotes
776
- * ### Examples
777
- *
778
- * First ensure that the service to be downgraded is provided in an `NgModule`
779
- * that will be part of the upgrade application. For example, let's assume we have
780
- * defined `HeroesService`
781
- *
782
- * {@example upgrade/static/ts/full/module.ts region="ng2-heroes-service"}
783
- *
784
- * and that we have included this in our upgrade app `NgModule`
785
- *
786
- * {@example upgrade/static/ts/full/module.ts region="ng2-module"}
787
- *
788
- * Now we can register the `downgradeInjectable` factory function for the service
789
- * on an AngularJS module.
790
- *
791
- * {@example upgrade/static/ts/full/module.ts region="downgrade-ng2-heroes-service"}
792
- *
793
- * Inside an AngularJS component's controller we can get hold of the
794
- * downgraded service via the name we gave when downgrading.
795
- *
796
- * {@example upgrade/static/ts/full/module.ts region="example-app"}
797
- *
798
- * <div class="alert is-important">
799
- *
800
- * When using `downgradeModule()`, downgraded injectables will not be available until the Angular
801
- * module that provides them is instantiated. In order to be safe, you need to ensure that the
802
- * downgraded injectables are not used anywhere _outside_ the part of the app where it is
803
- * guaranteed that their module has been instantiated.
804
- *
805
- * For example, it is _OK_ to use a downgraded service in an upgraded component that is only used
806
- * from a downgraded Angular component provided by the same Angular module as the injectable, but
807
- * it is _not OK_ to use it in an AngularJS component that may be used independently of Angular or
808
- * use it in a downgraded Angular component from a different module.
809
- *
810
- * </div>
811
- *
812
- * @param token an `InjectionToken` that identifies a service provided from Angular.
813
- * @param downgradedModule the name of the downgraded module (if any) that the injectable
814
- * "belongs to", as returned by a call to `downgradeModule()`. It is the module, whose injector will
815
- * be used for instantiating the injectable.<br />
816
- * (This option is only necessary when using `downgradeModule()` to downgrade more than one Angular
817
- * module.)
818
- *
819
- * @returns a [factory function](https://docs.angularjs.org/guide/di) that can be
820
- * used to register the service on an AngularJS module.
821
- *
822
- * @publicApi
823
- */
824
- function downgradeInjectable(token, downgradedModule = '') {
825
- const factory = function ($injector) {
826
- const injectorKey = `${INJECTOR_KEY}${downgradedModule}`;
827
- const injectableName = isFunction(token) ? getTypeName(token) : String(token);
828
- const attemptedAction = `instantiating injectable '${injectableName}'`;
829
- validateInjectionKey($injector, downgradedModule, injectorKey, attemptedAction);
830
- try {
831
- const injector = $injector.get(injectorKey);
832
- return injector.get(token);
833
- }
834
- catch (err) {
835
- throw new Error(`Error while ${attemptedAction}: ${err.message || err}`);
836
- }
837
- };
838
- factory['$inject'] = [$INJECTOR];
839
- return factory;
840
- }
841
-
842
- /**
843
- * @module
844
- * @description
845
- * Entry point for all public APIs of the upgrade package.
846
- */
847
- /**
848
- * @publicApi
849
- */
850
- const VERSION = new Version('16.0.0-next.4');
851
-
852
- // We have to do a little dance to get the ng1 injector into the module injector.
853
- // We store the ng1 injector so that the provider in the module injector can access it
854
- // Then we "get" the ng1 injector from the module injector, which triggers the provider to read
855
- // the stored injector and release the reference to it.
856
- let tempInjectorRef = null;
857
- function setTempInjectorRef(injector) {
858
- tempInjectorRef = injector;
859
- }
860
- function injectorFactory() {
861
- if (!tempInjectorRef) {
862
- throw new Error('Trying to get the AngularJS injector before it being set.');
863
- }
864
- const injector = tempInjectorRef;
865
- tempInjectorRef = null; // clear the value to prevent memory leaks
866
- return injector;
867
- }
868
- function rootScopeFactory(i) {
869
- return i.get('$rootScope');
870
- }
871
- function compileFactory(i) {
872
- return i.get('$compile');
873
- }
874
- function parseFactory(i) {
875
- return i.get('$parse');
876
- }
877
- const angular1Providers = [
878
- // We must use exported named functions for the ng2 factories to keep the compiler happy:
879
- // > Metadata collected contains an error that will be reported at runtime:
880
- // > Function calls are not supported.
881
- // > Consider replacing the function or lambda with a reference to an exported function
882
- { provide: '$injector', useFactory: injectorFactory, deps: [] },
883
- { provide: '$rootScope', useFactory: rootScopeFactory, deps: ['$injector'] },
884
- { provide: '$compile', useFactory: compileFactory, deps: ['$injector'] },
885
- { provide: '$parse', useFactory: parseFactory, deps: ['$injector'] }
886
- ];
887
-
888
- class NgAdapterInjector {
889
- constructor(modInjector) {
890
- this.modInjector = modInjector;
891
- }
892
- // When Angular locate a service in the component injector tree, the not found value is set to
893
- // `NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR`. In such a case we should not walk up to the module
894
- // injector.
895
- // AngularJS only supports a single tree and should always check the module injector.
896
- get(token, notFoundValue) {
897
- if (notFoundValue === ɵNOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR) {
898
- return notFoundValue;
899
- }
900
- return this.modInjector.get(token, notFoundValue);
901
- }
902
- }
903
-
904
- let moduleUid = 0;
905
- /**
906
- * @description
907
- *
908
- * A helper function for creating an AngularJS module that can bootstrap an Angular module
909
- * "on-demand" (possibly lazily) when a {@link downgradeComponent downgraded component} needs to be
910
- * instantiated.
911
- *
912
- * *Part of the [upgrade/static](api?query=upgrade/static) library for hybrid upgrade apps that
913
- * support AOT compilation.*
914
- *
915
- * It allows loading/bootstrapping the Angular part of a hybrid application lazily and not having to
916
- * pay the cost up-front. For example, you can have an AngularJS application that uses Angular for
917
- * specific routes and only instantiate the Angular modules if/when the user visits one of these
918
- * routes.
919
- *
920
- * The Angular module will be bootstrapped once (when requested for the first time) and the same
921
- * reference will be used from that point onwards.
922
- *
923
- * `downgradeModule()` requires either an `NgModuleFactory`, `NgModule` class or a function:
924
- * - `NgModuleFactory`: If you pass an `NgModuleFactory`, it will be used to instantiate a module
925
- * using `platformBrowser`'s {@link PlatformRef#bootstrapModuleFactory bootstrapModuleFactory()}.
926
- * NOTE: this type of the argument is deprecated. Please either provide an `NgModule` class or a
927
- * bootstrap function instead.
928
- * - `NgModule` class: If you pass an NgModule class, it will be used to instantiate a module
929
- * using `platformBrowser`'s {@link PlatformRef#bootstrapModule bootstrapModule()}.
930
- * - `Function`: If you pass a function, it is expected to return a promise resolving to an
931
- * `NgModuleRef`. The function is called with an array of extra {@link StaticProvider Providers}
932
- * that are expected to be available from the returned `NgModuleRef`'s `Injector`.
933
- *
934
- * `downgradeModule()` returns the name of the created AngularJS wrapper module. You can use it to
935
- * declare a dependency in your main AngularJS module.
936
- *
937
- * {@example upgrade/static/ts/lite/module.ts region="basic-how-to"}
938
- *
939
- * For more details on how to use `downgradeModule()` see
940
- * [Upgrading for Performance](guide/upgrade-performance).
941
- *
942
- * @usageNotes
943
- *
944
- * Apart from `UpgradeModule`, you can use the rest of the `upgrade/static` helpers as usual to
945
- * build a hybrid application. Note that the Angular pieces (e.g. downgraded services) will not be
946
- * available until the downgraded module has been bootstrapped, i.e. by instantiating a downgraded
947
- * component.
948
- *
949
- * <div class="alert is-important">
950
- *
951
- * You cannot use `downgradeModule()` and `UpgradeModule` in the same hybrid application.<br />
952
- * Use one or the other.
953
- *
954
- * </div>
955
- *
956
- * ### Differences with `UpgradeModule`
957
- *
958
- * Besides their different API, there are two important internal differences between
959
- * `downgradeModule()` and `UpgradeModule` that affect the behavior of hybrid applications:
960
- *
961
- * 1. Unlike `UpgradeModule`, `downgradeModule()` does not bootstrap the main AngularJS module
962
- * inside the {@link NgZone Angular zone}.
963
- * 2. Unlike `UpgradeModule`, `downgradeModule()` does not automatically run a
964
- * [$digest()](https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$digest) when changes are
965
- * detected in the Angular part of the application.
966
- *
967
- * What this means is that applications using `UpgradeModule` will run change detection more
968
- * frequently in order to ensure that both frameworks are properly notified about possible changes.
969
- * This will inevitably result in more change detection runs than necessary.
970
- *
971
- * `downgradeModule()`, on the other side, does not try to tie the two change detection systems as
972
- * tightly, restricting the explicit change detection runs only to cases where it knows it is
973
- * necessary (e.g. when the inputs of a downgraded component change). This improves performance,
974
- * especially in change-detection-heavy applications, but leaves it up to the developer to manually
975
- * notify each framework as needed.
976
- *
977
- * For a more detailed discussion of the differences and their implications, see
978
- * [Upgrading for Performance](guide/upgrade-performance).
979
- *
980
- * <div class="alert is-helpful">
981
- *
982
- * You can manually trigger a change detection run in AngularJS using
983
- * [scope.$apply(...)](https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$apply) or
984
- * [$rootScope.$digest()](https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$digest).
985
- *
986
- * You can manually trigger a change detection run in Angular using {@link NgZone#run
987
- * ngZone.run(...)}.
988
- *
989
- * </div>
990
- *
991
- * ### Downgrading multiple modules
992
- *
993
- * It is possible to downgrade multiple modules and include them in an AngularJS application. In
994
- * that case, each downgraded module will be bootstrapped when an associated downgraded component or
995
- * injectable needs to be instantiated.
996
- *
997
- * Things to keep in mind, when downgrading multiple modules:
998
- *
999
- * - Each downgraded component/injectable needs to be explicitly associated with a downgraded
1000
- * module. See `downgradeComponent()` and `downgradeInjectable()` for more details.
1001
- *
1002
- * - If you want some injectables to be shared among all downgraded modules, you can provide them as
1003
- * `StaticProvider`s, when creating the `PlatformRef` (e.g. via `platformBrowser` or
1004
- * `platformBrowserDynamic`).
1005
- *
1006
- * - When using {@link PlatformRef#bootstrapmodule `bootstrapModule()`} or
1007
- * {@link PlatformRef#bootstrapmodulefactory `bootstrapModuleFactory()`} to bootstrap the
1008
- * downgraded modules, each one is considered a "root" module. As a consequence, a new instance
1009
- * will be created for every injectable provided in `"root"` (via
1010
- * {@link Injectable#providedIn `providedIn`}).
1011
- * If this is not your intention, you can have a shared module (that will act as act as the "root"
1012
- * module) and create all downgraded modules using that module's injector:
1013
- *
1014
- * {@example upgrade/static/ts/lite-multi-shared/module.ts region="shared-root-module"}
1015
- *
1016
- * @publicApi
1017
- */
1018
- function downgradeModule(moduleOrBootstrapFn) {
1019
- const lazyModuleName = `${UPGRADE_MODULE_NAME}.lazy${++moduleUid}`;
1020
- const lazyModuleRefKey = `${LAZY_MODULE_REF}${lazyModuleName}`;
1021
- const lazyInjectorKey = `${INJECTOR_KEY}${lazyModuleName}`;
1022
- let bootstrapFn;
1023
- if (isNgModuleType(moduleOrBootstrapFn)) {
1024
- // NgModule class
1025
- bootstrapFn = (extraProviders) => platformBrowser(extraProviders).bootstrapModule(moduleOrBootstrapFn);
1026
- }
1027
- else if (!isFunction(moduleOrBootstrapFn)) {
1028
- // NgModule factory
1029
- bootstrapFn = (extraProviders) => platformBrowser(extraProviders).bootstrapModuleFactory(moduleOrBootstrapFn);
1030
- }
1031
- else {
1032
- // bootstrap function
1033
- bootstrapFn = moduleOrBootstrapFn;
1034
- }
1035
- let injector;
1036
- // Create an ng1 module to bootstrap.
1037
- module_(lazyModuleName, [])
1038
- .constant(UPGRADE_APP_TYPE_KEY, 3 /* UpgradeAppType.Lite */)
1039
- .factory(INJECTOR_KEY, [lazyInjectorKey, identity])
1040
- .factory(lazyInjectorKey, () => {
1041
- if (!injector) {
1042
- throw new Error('Trying to get the Angular injector before bootstrapping the corresponding ' +
1043
- 'Angular module.');
1044
- }
1045
- return injector;
1046
- })
1047
- .factory(LAZY_MODULE_REF, [lazyModuleRefKey, identity])
1048
- .factory(lazyModuleRefKey, [
1049
- $INJECTOR,
1050
- ($injector) => {
1051
- setTempInjectorRef($injector);
1052
- const result = {
1053
- promise: bootstrapFn(angular1Providers).then(ref => {
1054
- injector = result.injector = new NgAdapterInjector(ref.injector);
1055
- injector.get($INJECTOR);
1056
- // Destroy the AngularJS app once the Angular `PlatformRef` is destroyed.
1057
- // This does not happen in a typical SPA scenario, but it might be useful for
1058
- // other use-cases where disposing of an Angular/AngularJS app is necessary
1059
- // (such as Hot Module Replacement (HMR)).
1060
- // See https://github.com/angular/angular/issues/39935.
1061
- injector.get(PlatformRef).onDestroy(() => destroyApp($injector));
1062
- return injector;
1063
- })
1064
- };
1065
- return result;
1066
- }
1067
- ])
1068
- .config([
1069
- $INJECTOR, $PROVIDE,
1070
- ($injector, $provide) => {
1071
- $provide.constant(DOWNGRADED_MODULE_COUNT_KEY, getDowngradedModuleCount($injector) + 1);
1072
- }
1073
- ]);
1074
- return lazyModuleName;
1075
- }
1076
- function identity(x) {
1077
- return x;
1078
- }
1079
-
1080
- // Constants
1081
- const REQUIRE_PREFIX_RE = /^(\^\^?)?(\?)?(\^\^?)?/;
1082
- // Classes
1083
- class UpgradeHelper {
1084
- constructor(injector, name, elementRef, directive) {
1085
- this.name = name;
1086
- this.$injector = injector.get($INJECTOR);
1087
- this.$compile = this.$injector.get($COMPILE);
1088
- this.$controller = this.$injector.get($CONTROLLER);
1089
- this.element = elementRef.nativeElement;
1090
- this.$element = element(this.element);
1091
- this.directive = directive !== null && directive !== void 0 ? directive : UpgradeHelper.getDirective(this.$injector, name);
1092
- }
1093
- static getDirective($injector, name) {
1094
- const directives = $injector.get(name + 'Directive');
1095
- if (directives.length > 1) {
1096
- throw new Error(`Only support single directive definition for: ${name}`);
1097
- }
1098
- const directive = directives[0];
1099
- // AngularJS will transform `link: xyz` to `compile: () => xyz`. So we can only tell there was a
1100
- // user-defined `compile` if there is no `link`. In other cases, we will just ignore `compile`.
1101
- if (directive.compile && !directive.link)
1102
- notSupported(name, 'compile');
1103
- if (directive.replace)
1104
- notSupported(name, 'replace');
1105
- if (directive.terminal)
1106
- notSupported(name, 'terminal');
1107
- return directive;
1108
- }
1109
- static getTemplate($injector, directive, fetchRemoteTemplate = false, $element) {
1110
- if (directive.template !== undefined) {
1111
- return getOrCall(directive.template, $element);
1112
- }
1113
- else if (directive.templateUrl) {
1114
- const $templateCache = $injector.get($TEMPLATE_CACHE);
1115
- const url = getOrCall(directive.templateUrl, $element);
1116
- const template = $templateCache.get(url);
1117
- if (template !== undefined) {
1118
- return template;
1119
- }
1120
- else if (!fetchRemoteTemplate) {
1121
- throw new Error('loading directive templates asynchronously is not supported');
1122
- }
1123
- return new Promise((resolve, reject) => {
1124
- const $httpBackend = $injector.get($HTTP_BACKEND);
1125
- $httpBackend('GET', url, null, (status, response) => {
1126
- if (status === 200) {
1127
- resolve($templateCache.put(url, response));
1128
- }
1129
- else {
1130
- reject(`GET component template from '${url}' returned '${status}: ${response}'`);
1131
- }
1132
- });
1133
- });
1134
- }
1135
- else {
1136
- throw new Error(`Directive '${directive.name}' is not a component, it is missing template.`);
1137
- }
1138
- }
1139
- buildController(controllerType, $scope) {
1140
- var _a, _b;
1141
- // TODO: Document that we do not pre-assign bindings on the controller instance.
1142
- // Quoted properties below so that this code can be optimized with Closure Compiler.
1143
- const locals = { '$scope': $scope, '$element': this.$element };
1144
- const controller = this.$controller(controllerType, locals, null, this.directive.controllerAs);
1145
- (_b = (_a = this.$element).data) === null || _b === void 0 ? void 0 : _b.call(_a, controllerKey(this.directive.name), controller);
1146
- return controller;
1147
- }
1148
- compileTemplate(template) {
1149
- if (template === undefined) {
1150
- template =
1151
- UpgradeHelper.getTemplate(this.$injector, this.directive, false, this.$element);
1152
- }
1153
- return this.compileHtml(template);
1154
- }
1155
- onDestroy($scope, controllerInstance) {
1156
- if (controllerInstance && isFunction(controllerInstance.$onDestroy)) {
1157
- controllerInstance.$onDestroy();
1158
- }
1159
- $scope.$destroy();
1160
- cleanData(this.element);
1161
- }
1162
- prepareTransclusion() {
1163
- const transclude = this.directive.transclude;
1164
- const contentChildNodes = this.extractChildNodes();
1165
- const attachChildrenFn = (scope, cloneAttachFn) => {
1166
- // Since AngularJS v1.5.8, `cloneAttachFn` will try to destroy the transclusion scope if
1167
- // `$template` is empty. Since the transcluded content comes from Angular, not AngularJS,
1168
- // there will be no transclusion scope here.
1169
- // Provide a dummy `scope.$destroy()` method to prevent `cloneAttachFn` from throwing.
1170
- scope = scope || { $destroy: () => undefined };
1171
- return cloneAttachFn($template, scope);
1172
- };
1173
- let $template = contentChildNodes;
1174
- if (transclude) {
1175
- const slots = Object.create(null);
1176
- if (typeof transclude === 'object') {
1177
- $template = [];
1178
- const slotMap = Object.create(null);
1179
- const filledSlots = Object.create(null);
1180
- // Parse the element selectors.
1181
- Object.keys(transclude).forEach(slotName => {
1182
- let selector = transclude[slotName];
1183
- const optional = selector.charAt(0) === '?';
1184
- selector = optional ? selector.substring(1) : selector;
1185
- slotMap[selector] = slotName;
1186
- slots[slotName] = null; // `null`: Defined but not yet filled.
1187
- filledSlots[slotName] = optional; // Consider optional slots as filled.
1188
- });
1189
- // Add the matching elements into their slot.
1190
- contentChildNodes.forEach(node => {
1191
- const slotName = slotMap[directiveNormalize(node.nodeName.toLowerCase())];
1192
- if (slotName) {
1193
- filledSlots[slotName] = true;
1194
- slots[slotName] = slots[slotName] || [];
1195
- slots[slotName].push(node);
1196
- }
1197
- else {
1198
- $template.push(node);
1199
- }
1200
- });
1201
- // Check for required slots that were not filled.
1202
- Object.keys(filledSlots).forEach(slotName => {
1203
- if (!filledSlots[slotName]) {
1204
- throw new Error(`Required transclusion slot '${slotName}' on directive: ${this.name}`);
1205
- }
1206
- });
1207
- Object.keys(slots).filter(slotName => slots[slotName]).forEach(slotName => {
1208
- const nodes = slots[slotName];
1209
- slots[slotName] = (scope, cloneAttach) => {
1210
- return cloneAttach(nodes, scope);
1211
- };
1212
- });
1213
- }
1214
- // Attach `$$slots` to default slot transclude fn.
1215
- attachChildrenFn.$$slots = slots;
1216
- // AngularJS v1.6+ ignores empty or whitespace-only transcluded text nodes. But Angular
1217
- // removes all text content after the first interpolation and updates it later, after
1218
- // evaluating the expressions. This would result in AngularJS failing to recognize text
1219
- // nodes that start with an interpolation as transcluded content and use the fallback
1220
- // content instead.
1221
- // To avoid this issue, we add a
1222
- // [zero-width non-joiner character](https://en.wikipedia.org/wiki/Zero-width_non-joiner)
1223
- // to empty text nodes (which can only be a result of Angular removing their initial content).
1224
- // NOTE: Transcluded text content that starts with whitespace followed by an interpolation
1225
- // will still fail to be detected by AngularJS v1.6+
1226
- $template.forEach(node => {
1227
- if (node.nodeType === Node.TEXT_NODE && !node.nodeValue) {
1228
- node.nodeValue = '\u200C';
1229
- }
1230
- });
1231
- }
1232
- return attachChildrenFn;
1233
- }
1234
- resolveAndBindRequiredControllers(controllerInstance) {
1235
- const directiveRequire = this.getDirectiveRequire();
1236
- const requiredControllers = this.resolveRequire(directiveRequire);
1237
- if (controllerInstance && this.directive.bindToController && isMap(directiveRequire)) {
1238
- const requiredControllersMap = requiredControllers;
1239
- Object.keys(requiredControllersMap).forEach(key => {
1240
- controllerInstance[key] = requiredControllersMap[key];
1241
- });
1242
- }
1243
- return requiredControllers;
1244
- }
1245
- compileHtml(html) {
1246
- this.element.innerHTML = html;
1247
- return this.$compile(this.element.childNodes);
1248
- }
1249
- extractChildNodes() {
1250
- const childNodes = [];
1251
- let childNode;
1252
- while (childNode = this.element.firstChild) {
1253
- this.element.removeChild(childNode);
1254
- childNodes.push(childNode);
1255
- }
1256
- return childNodes;
1257
- }
1258
- getDirectiveRequire() {
1259
- const require = this.directive.require || (this.directive.controller && this.directive.name);
1260
- if (isMap(require)) {
1261
- Object.entries(require).forEach(([key, value]) => {
1262
- const match = value.match(REQUIRE_PREFIX_RE);
1263
- const name = value.substring(match[0].length);
1264
- if (!name) {
1265
- require[key] = match[0] + key;
1266
- }
1267
- });
1268
- }
1269
- return require;
1270
- }
1271
- resolveRequire(require) {
1272
- if (!require) {
1273
- return null;
1274
- }
1275
- else if (Array.isArray(require)) {
1276
- return require.map(req => this.resolveRequire(req));
1277
- }
1278
- else if (typeof require === 'object') {
1279
- const value = {};
1280
- Object.keys(require).forEach(key => value[key] = this.resolveRequire(require[key]));
1281
- return value;
1282
- }
1283
- else if (typeof require === 'string') {
1284
- const match = require.match(REQUIRE_PREFIX_RE);
1285
- const inheritType = match[1] || match[3];
1286
- const name = require.substring(match[0].length);
1287
- const isOptional = !!match[2];
1288
- const searchParents = !!inheritType;
1289
- const startOnParent = inheritType === '^^';
1290
- const ctrlKey = controllerKey(name);
1291
- const elem = startOnParent ? this.$element.parent() : this.$element;
1292
- const value = searchParents ? elem.inheritedData(ctrlKey) : elem.data(ctrlKey);
1293
- if (!value && !isOptional) {
1294
- throw new Error(`Unable to find required '${require}' in upgraded directive '${this.name}'.`);
1295
- }
1296
- return value;
1297
- }
1298
- else {
1299
- throw new Error(`Unrecognized 'require' syntax on upgraded directive '${this.name}': ${require}`);
1300
- }
1301
- }
1302
- }
1303
- function getOrCall(property, ...args) {
1304
- return isFunction(property) ? property(...args) : property;
1305
- }
1306
- // NOTE: Only works for `typeof T !== 'object'`.
1307
- function isMap(value) {
1308
- return value && !Array.isArray(value) && typeof value === 'object';
1309
- }
1310
- function notSupported(name, feature) {
1311
- throw new Error(`Upgraded directive '${name}' contains unsupported feature: '${feature}'.`);
1312
- }
1313
-
1314
- const NOT_SUPPORTED = 'NOT_SUPPORTED';
1315
- const INITIAL_VALUE = {
1316
- __UNINITIALIZED__: true
1317
- };
1318
- class Bindings {
1319
- constructor() {
1320
- this.twoWayBoundProperties = [];
1321
- this.twoWayBoundLastValues = [];
1322
- this.expressionBoundProperties = [];
1323
- this.propertyToOutputMap = {};
1324
- }
1325
- }
1326
- /**
1327
- * @description
1328
- *
1329
- * A helper class that allows an AngularJS component to be used from Angular.
1330
- *
1331
- * *Part of the [upgrade/static](api?query=upgrade%2Fstatic)
1332
- * library for hybrid upgrade apps that support AOT compilation.*
1333
- *
1334
- * This helper class should be used as a base class for creating Angular directives
1335
- * that wrap AngularJS components that need to be "upgraded".
1336
- *
1337
- * @usageNotes
1338
- * ### Examples
1339
- *
1340
- * Let's assume that you have an AngularJS component called `ng1Hero` that needs
1341
- * to be made available in Angular templates.
1342
- *
1343
- * {@example upgrade/static/ts/full/module.ts region="ng1-hero"}
1344
- *
1345
- * We must create a `Directive` that will make this AngularJS component
1346
- * available inside Angular templates.
1347
- *
1348
- * {@example upgrade/static/ts/full/module.ts region="ng1-hero-wrapper"}
1349
- *
1350
- * In this example you can see that we must derive from the `UpgradeComponent`
1351
- * base class but also provide an {@link Directive `@Directive`} decorator. This is
1352
- * because the AOT compiler requires that this information is statically available at
1353
- * compile time.
1354
- *
1355
- * Note that we must do the following:
1356
- * * specify the directive's selector (`ng1-hero`)
1357
- * * specify all inputs and outputs that the AngularJS component expects
1358
- * * derive from `UpgradeComponent`
1359
- * * call the base class from the constructor, passing
1360
- * * the AngularJS name of the component (`ng1Hero`)
1361
- * * the `ElementRef` and `Injector` for the component wrapper
1362
- *
1363
- * @publicApi
1364
- * @extensible
1365
- */
1366
- class UpgradeComponent {
1367
- /**
1368
- * Create a new `UpgradeComponent` instance. You should not normally need to do this.
1369
- * Instead you should derive a new class from this one and call the super constructor
1370
- * from the base class.
1371
- *
1372
- * {@example upgrade/static/ts/full/module.ts region="ng1-hero-wrapper" }
1373
- *
1374
- * * The `name` parameter should be the name of the AngularJS directive.
1375
- * * The `elementRef` and `injector` parameters should be acquired from Angular by dependency
1376
- * injection into the base class constructor.
1377
- */
1378
- constructor(name, elementRef, injector) {
1379
- // We will be instantiating the controller in the `ngOnInit` hook, when the
1380
- // first `ngOnChanges` will have been already triggered. We store the
1381
- // `SimpleChanges` and "play them back" later.
1382
- this.pendingChanges = null;
1383
- this.helper = new UpgradeHelper(injector, name, elementRef);
1384
- this.$element = this.helper.$element;
1385
- this.directive = this.helper.directive;
1386
- this.bindings = this.initializeBindings(this.directive, name);
1387
- // We ask for the AngularJS scope from the Angular injector, since
1388
- // we will put the new component scope onto the new injector for each component
1389
- const $parentScope = injector.get($SCOPE);
1390
- // QUESTION 1: Should we create an isolated scope if the scope is only true?
1391
- // QUESTION 2: Should we make the scope accessible through `$element.scope()/isolateScope()`?
1392
- this.$componentScope = $parentScope.$new(!!this.directive.scope);
1393
- this.initializeOutputs();
1394
- }
1395
- /** @nodoc */
1396
- ngOnInit() {
1397
- // Collect contents, insert and compile template
1398
- const attachChildNodes = this.helper.prepareTransclusion();
1399
- const linkFn = this.helper.compileTemplate();
1400
- // Instantiate controller
1401
- const controllerType = this.directive.controller;
1402
- const bindToController = this.directive.bindToController;
1403
- let controllerInstance = controllerType ?
1404
- this.helper.buildController(controllerType, this.$componentScope) :
1405
- undefined;
1406
- let bindingDestination;
1407
- if (!bindToController) {
1408
- bindingDestination = this.$componentScope;
1409
- }
1410
- else if (controllerType && controllerInstance) {
1411
- bindingDestination = controllerInstance;
1412
- }
1413
- else {
1414
- throw new Error(`Upgraded directive '${this.directive.name}' specifies 'bindToController' but no controller.`);
1415
- }
1416
- this.controllerInstance = controllerInstance;
1417
- this.bindingDestination = bindingDestination;
1418
- // Set up outputs
1419
- this.bindOutputs(bindingDestination);
1420
- // Require other controllers
1421
- const requiredControllers = this.helper.resolveAndBindRequiredControllers(controllerInstance);
1422
- // Hook: $onChanges
1423
- if (this.pendingChanges) {
1424
- this.forwardChanges(this.pendingChanges, bindingDestination);
1425
- this.pendingChanges = null;
1426
- }
1427
- // Hook: $onInit
1428
- if (this.controllerInstance && isFunction(this.controllerInstance.$onInit)) {
1429
- this.controllerInstance.$onInit();
1430
- }
1431
- // Hook: $doCheck
1432
- if (controllerInstance && isFunction(controllerInstance.$doCheck)) {
1433
- const callDoCheck = () => { var _a; return (_a = controllerInstance === null || controllerInstance === void 0 ? void 0 : controllerInstance.$doCheck) === null || _a === void 0 ? void 0 : _a.call(controllerInstance); };
1434
- this.unregisterDoCheckWatcher = this.$componentScope.$parent.$watch(callDoCheck);
1435
- callDoCheck();
1436
- }
1437
- // Linking
1438
- const link = this.directive.link;
1439
- const preLink = typeof link == 'object' && link.pre;
1440
- const postLink = typeof link == 'object' ? link.post : link;
1441
- const attrs = NOT_SUPPORTED;
1442
- const transcludeFn = NOT_SUPPORTED;
1443
- if (preLink) {
1444
- preLink(this.$componentScope, this.$element, attrs, requiredControllers, transcludeFn);
1445
- }
1446
- linkFn(this.$componentScope, null, { parentBoundTranscludeFn: attachChildNodes });
1447
- if (postLink) {
1448
- postLink(this.$componentScope, this.$element, attrs, requiredControllers, transcludeFn);
1449
- }
1450
- // Hook: $postLink
1451
- if (this.controllerInstance && isFunction(this.controllerInstance.$postLink)) {
1452
- this.controllerInstance.$postLink();
1453
- }
1454
- }
1455
- /** @nodoc */
1456
- ngOnChanges(changes) {
1457
- if (!this.bindingDestination) {
1458
- this.pendingChanges = changes;
1459
- }
1460
- else {
1461
- this.forwardChanges(changes, this.bindingDestination);
1462
- }
1463
- }
1464
- /** @nodoc */
1465
- ngDoCheck() {
1466
- const twoWayBoundProperties = this.bindings.twoWayBoundProperties;
1467
- const twoWayBoundLastValues = this.bindings.twoWayBoundLastValues;
1468
- const propertyToOutputMap = this.bindings.propertyToOutputMap;
1469
- twoWayBoundProperties.forEach((propName, idx) => {
1470
- var _a;
1471
- const newValue = (_a = this.bindingDestination) === null || _a === void 0 ? void 0 : _a[propName];
1472
- const oldValue = twoWayBoundLastValues[idx];
1473
- if (!Object.is(newValue, oldValue)) {
1474
- const outputName = propertyToOutputMap[propName];
1475
- const eventEmitter = this[outputName];
1476
- eventEmitter.emit(newValue);
1477
- twoWayBoundLastValues[idx] = newValue;
1478
- }
1479
- });
1480
- }
1481
- /** @nodoc */
1482
- ngOnDestroy() {
1483
- if (isFunction(this.unregisterDoCheckWatcher)) {
1484
- this.unregisterDoCheckWatcher();
1485
- }
1486
- this.helper.onDestroy(this.$componentScope, this.controllerInstance);
1487
- }
1488
- initializeBindings(directive, name) {
1489
- const btcIsObject = typeof directive.bindToController === 'object';
1490
- if (btcIsObject && Object.keys(directive.scope).length) {
1491
- throw new Error(`Binding definitions on scope and controller at the same time is not supported.`);
1492
- }
1493
- const context = btcIsObject ? directive.bindToController : directive.scope;
1494
- const bindings = new Bindings();
1495
- if (typeof context == 'object') {
1496
- Object.keys(context).forEach(propName => {
1497
- const definition = context[propName];
1498
- const bindingType = definition.charAt(0);
1499
- // QUESTION: What about `=*`? Ignore? Throw? Support?
1500
- switch (bindingType) {
1501
- case '@':
1502
- case '<':
1503
- // We don't need to do anything special. They will be defined as inputs on the
1504
- // upgraded component facade and the change propagation will be handled by
1505
- // `ngOnChanges()`.
1506
- break;
1507
- case '=':
1508
- bindings.twoWayBoundProperties.push(propName);
1509
- bindings.twoWayBoundLastValues.push(INITIAL_VALUE);
1510
- bindings.propertyToOutputMap[propName] = propName + 'Change';
1511
- break;
1512
- case '&':
1513
- bindings.expressionBoundProperties.push(propName);
1514
- bindings.propertyToOutputMap[propName] = propName;
1515
- break;
1516
- default:
1517
- let json = JSON.stringify(context);
1518
- throw new Error(`Unexpected mapping '${bindingType}' in '${json}' in '${name}' directive.`);
1519
- }
1520
- });
1521
- }
1522
- return bindings;
1523
- }
1524
- initializeOutputs() {
1525
- // Initialize the outputs for `=` and `&` bindings
1526
- this.bindings.twoWayBoundProperties.concat(this.bindings.expressionBoundProperties)
1527
- .forEach(propName => {
1528
- const outputName = this.bindings.propertyToOutputMap[propName];
1529
- this[outputName] = new EventEmitter();
1530
- });
1531
- }
1532
- bindOutputs(bindingDestination) {
1533
- // Bind `&` bindings to the corresponding outputs
1534
- this.bindings.expressionBoundProperties.forEach(propName => {
1535
- const outputName = this.bindings.propertyToOutputMap[propName];
1536
- const emitter = this[outputName];
1537
- bindingDestination[propName] = (value) => emitter.emit(value);
1538
- });
1539
- }
1540
- forwardChanges(changes, bindingDestination) {
1541
- // Forward input changes to `bindingDestination`
1542
- Object.keys(changes).forEach(propName => bindingDestination[propName] = changes[propName].currentValue);
1543
- if (isFunction(bindingDestination.$onChanges)) {
1544
- bindingDestination.$onChanges(changes);
1545
- }
1546
- }
1547
- }
1548
- UpgradeComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0-next.4", ngImport: i0, type: UpgradeComponent, deps: "invalid", target: i0.ɵɵFactoryTarget.Directive });
1549
- UpgradeComponent.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.0.0-next.4", type: UpgradeComponent, usesOnChanges: true, ngImport: i0 });
1550
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0-next.4", ngImport: i0, type: UpgradeComponent, decorators: [{
1551
- type: Directive
1552
- }], ctorParameters: function () { return [{ type: undefined }, { type: i0.ElementRef }, { type: i0.Injector }]; } });
1553
-
1554
- /**
1555
- * @description
1556
- *
1557
- * An `NgModule`, which you import to provide AngularJS core services,
1558
- * and has an instance method used to bootstrap the hybrid upgrade application.
1559
- *
1560
- * *Part of the [upgrade/static](api?query=upgrade/static)
1561
- * library for hybrid upgrade apps that support AOT compilation*
1562
- *
1563
- * The `upgrade/static` package contains helpers that allow AngularJS and Angular components
1564
- * to be used together inside a hybrid upgrade application, which supports AOT compilation.
1565
- *
1566
- * Specifically, the classes and functions in the `upgrade/static` module allow the following:
1567
- *
1568
- * 1. Creation of an Angular directive that wraps and exposes an AngularJS component so
1569
- * that it can be used in an Angular template. See `UpgradeComponent`.
1570
- * 2. Creation of an AngularJS directive that wraps and exposes an Angular component so
1571
- * that it can be used in an AngularJS template. See `downgradeComponent`.
1572
- * 3. Creation of an Angular root injector provider that wraps and exposes an AngularJS
1573
- * service so that it can be injected into an Angular context. See
1574
- * {@link UpgradeModule#upgrading-an-angular-1-service Upgrading an AngularJS service} below.
1575
- * 4. Creation of an AngularJS service that wraps and exposes an Angular injectable
1576
- * so that it can be injected into an AngularJS context. See `downgradeInjectable`.
1577
- * 3. Bootstrapping of a hybrid Angular application which contains both of the frameworks
1578
- * coexisting in a single application.
1579
- *
1580
- * @usageNotes
1581
- *
1582
- * ```ts
1583
- * import {UpgradeModule} from '@angular/upgrade/static';
1584
- * ```
1585
- *
1586
- * See also the {@link UpgradeModule#examples examples} below.
1587
- *
1588
- * ### Mental Model
1589
- *
1590
- * When reasoning about how a hybrid application works it is useful to have a mental model which
1591
- * describes what is happening and explains what is happening at the lowest level.
1592
- *
1593
- * 1. There are two independent frameworks running in a single application, each framework treats
1594
- * the other as a black box.
1595
- * 2. Each DOM element on the page is owned exactly by one framework. Whichever framework
1596
- * instantiated the element is the owner. Each framework only updates/interacts with its own
1597
- * DOM elements and ignores others.
1598
- * 3. AngularJS directives always execute inside the AngularJS framework codebase regardless of
1599
- * where they are instantiated.
1600
- * 4. Angular components always execute inside the Angular framework codebase regardless of
1601
- * where they are instantiated.
1602
- * 5. An AngularJS component can be "upgraded"" to an Angular component. This is achieved by
1603
- * defining an Angular directive, which bootstraps the AngularJS component at its location
1604
- * in the DOM. See `UpgradeComponent`.
1605
- * 6. An Angular component can be "downgraded" to an AngularJS component. This is achieved by
1606
- * defining an AngularJS directive, which bootstraps the Angular component at its location
1607
- * in the DOM. See `downgradeComponent`.
1608
- * 7. Whenever an "upgraded"/"downgraded" component is instantiated the host element is owned by
1609
- * the framework doing the instantiation. The other framework then instantiates and owns the
1610
- * view for that component.
1611
- * 1. This implies that the component bindings will always follow the semantics of the
1612
- * instantiation framework.
1613
- * 2. The DOM attributes are parsed by the framework that owns the current template. So
1614
- * attributes in AngularJS templates must use kebab-case, while AngularJS templates must use
1615
- * camelCase.
1616
- * 3. However the template binding syntax will always use the Angular style, e.g. square
1617
- * brackets (`[...]`) for property binding.
1618
- * 8. Angular is bootstrapped first; AngularJS is bootstrapped second. AngularJS always owns the
1619
- * root component of the application.
1620
- * 9. The new application is running in an Angular zone, and therefore it no longer needs calls to
1621
- * `$apply()`.
1622
- *
1623
- * ### The `UpgradeModule` class
1624
- *
1625
- * This class is an `NgModule`, which you import to provide AngularJS core services,
1626
- * and has an instance method used to bootstrap the hybrid upgrade application.
1627
- *
1628
- * * Core AngularJS services<br />
1629
- * Importing this `NgModule` will add providers for the core
1630
- * [AngularJS services](https://docs.angularjs.org/api/ng/service) to the root injector.
1631
- *
1632
- * * Bootstrap<br />
1633
- * The runtime instance of this class contains a {@link UpgradeModule#bootstrap `bootstrap()`}
1634
- * method, which you use to bootstrap the top level AngularJS module onto an element in the
1635
- * DOM for the hybrid upgrade app.
1636
- *
1637
- * It also contains properties to access the {@link UpgradeModule#injector root injector}, the
1638
- * bootstrap `NgZone` and the
1639
- * [AngularJS $injector](https://docs.angularjs.org/api/auto/service/$injector).
1640
- *
1641
- * ### Examples
1642
- *
1643
- * Import the `UpgradeModule` into your top level {@link NgModule Angular `NgModule`}.
1644
- *
1645
- * {@example upgrade/static/ts/full/module.ts region='ng2-module'}
1646
- *
1647
- * Then inject `UpgradeModule` into your Angular `NgModule` and use it to bootstrap the top level
1648
- * [AngularJS module](https://docs.angularjs.org/api/ng/type/angular.Module) in the
1649
- * `ngDoBootstrap()` method.
1650
- *
1651
- * {@example upgrade/static/ts/full/module.ts region='bootstrap-ng1'}
1652
- *
1653
- * Finally, kick off the whole process, by bootstrapping your top level Angular `NgModule`.
1654
- *
1655
- * {@example upgrade/static/ts/full/module.ts region='bootstrap-ng2'}
1656
- *
1657
- * {@a upgrading-an-angular-1-service}
1658
- * ### Upgrading an AngularJS service
1659
- *
1660
- * There is no specific API for upgrading an AngularJS service. Instead you should just follow the
1661
- * following recipe:
1662
- *
1663
- * Let's say you have an AngularJS service:
1664
- *
1665
- * {@example upgrade/static/ts/full/module.ts region="ng1-text-formatter-service"}
1666
- *
1667
- * Then you should define an Angular provider to be included in your `NgModule` `providers`
1668
- * property.
1669
- *
1670
- * {@example upgrade/static/ts/full/module.ts region="upgrade-ng1-service"}
1671
- *
1672
- * Then you can use the "upgraded" AngularJS service by injecting it into an Angular component
1673
- * or service.
1674
- *
1675
- * {@example upgrade/static/ts/full/module.ts region="use-ng1-upgraded-service"}
1676
- *
1677
- * @publicApi
1678
- */
1679
- class UpgradeModule {
1680
- constructor(
1681
- /** The root `Injector` for the upgrade application. */
1682
- injector,
1683
- /** The bootstrap zone for the upgrade application */
1684
- ngZone,
1685
- /**
1686
- * The owning `NgModuleRef`s `PlatformRef` instance.
1687
- * This is used to tie the lifecycle of the bootstrapped AngularJS apps to that of the Angular
1688
- * `PlatformRef`.
1689
- */
1690
- platformRef) {
1691
- this.ngZone = ngZone;
1692
- this.platformRef = platformRef;
1693
- this.injector = new NgAdapterInjector(injector);
1694
- }
1695
- /**
1696
- * Bootstrap an AngularJS application from this NgModule
1697
- * @param element the element on which to bootstrap the AngularJS application
1698
- * @param [modules] the AngularJS modules to bootstrap for this application
1699
- * @param [config] optional extra AngularJS bootstrap configuration
1700
- * @return The value returned by
1701
- * [angular.bootstrap()](https://docs.angularjs.org/api/ng/function/angular.bootstrap).
1702
- */
1703
- bootstrap(element$1, modules = [], config /*angular.IAngularBootstrapConfig*/) {
1704
- const INIT_MODULE_NAME = UPGRADE_MODULE_NAME + '.init';
1705
- // Create an ng1 module to bootstrap
1706
- module_(INIT_MODULE_NAME, [])
1707
- .constant(UPGRADE_APP_TYPE_KEY, 2 /* UpgradeAppType.Static */)
1708
- .value(INJECTOR_KEY, this.injector)
1709
- .factory(LAZY_MODULE_REF, [INJECTOR_KEY, (injector) => ({ injector })])
1710
- .config([
1711
- $PROVIDE, $INJECTOR,
1712
- ($provide, $injector) => {
1713
- if ($injector.has($$TESTABILITY)) {
1714
- $provide.decorator($$TESTABILITY, [
1715
- $DELEGATE,
1716
- (testabilityDelegate) => {
1717
- const originalWhenStable = testabilityDelegate.whenStable;
1718
- const injector = this.injector;
1719
- // Cannot use arrow function below because we need the context
1720
- const newWhenStable = function (callback) {
1721
- originalWhenStable.call(testabilityDelegate, function () {
1722
- const ng2Testability = injector.get(Testability);
1723
- if (ng2Testability.isStable()) {
1724
- callback();
1725
- }
1726
- else {
1727
- ng2Testability.whenStable(newWhenStable.bind(testabilityDelegate, callback));
1728
- }
1729
- });
1730
- };
1731
- testabilityDelegate.whenStable = newWhenStable;
1732
- return testabilityDelegate;
1733
- }
1734
- ]);
1735
- }
1736
- if ($injector.has($INTERVAL)) {
1737
- $provide.decorator($INTERVAL, [
1738
- $DELEGATE,
1739
- (intervalDelegate) => {
1740
- // Wrap the $interval service so that setInterval is called outside NgZone,
1741
- // but the callback is still invoked within it. This is so that $interval
1742
- // won't block stability, which preserves the behavior from AngularJS.
1743
- let wrappedInterval = (fn, delay, count, invokeApply, ...pass) => {
1744
- return this.ngZone.runOutsideAngular(() => {
1745
- return intervalDelegate((...args) => {
1746
- // Run callback in the next VM turn - $interval calls
1747
- // $rootScope.$apply, and running the callback in NgZone will
1748
- // cause a '$digest already in progress' error if it's in the
1749
- // same vm turn.
1750
- setTimeout(() => {
1751
- this.ngZone.run(() => fn(...args));
1752
- });
1753
- }, delay, count, invokeApply, ...pass);
1754
- });
1755
- };
1756
- Object.keys(intervalDelegate)
1757
- .forEach(prop => wrappedInterval[prop] = intervalDelegate[prop]);
1758
- // the `flush` method will be present when ngMocks is used
1759
- if (intervalDelegate.hasOwnProperty('flush')) {
1760
- wrappedInterval['flush'] = () => {
1761
- intervalDelegate['flush']();
1762
- return wrappedInterval;
1763
- };
1764
- }
1765
- return wrappedInterval;
1766
- }
1767
- ]);
1768
- }
1769
- }
1770
- ])
1771
- .run([
1772
- $INJECTOR,
1773
- ($injector) => {
1774
- this.$injector = $injector;
1775
- const $rootScope = $injector.get('$rootScope');
1776
- // Initialize the ng1 $injector provider
1777
- setTempInjectorRef($injector);
1778
- this.injector.get($INJECTOR);
1779
- // Put the injector on the DOM, so that it can be "required"
1780
- element(element$1).data(controllerKey(INJECTOR_KEY), this.injector);
1781
- // Destroy the AngularJS app once the Angular `PlatformRef` is destroyed.
1782
- // This does not happen in a typical SPA scenario, but it might be useful for
1783
- // other use-cases where disposing of an Angular/AngularJS app is necessary
1784
- // (such as Hot Module Replacement (HMR)).
1785
- // See https://github.com/angular/angular/issues/39935.
1786
- this.platformRef.onDestroy(() => destroyApp($injector));
1787
- // Wire up the ng1 rootScope to run a digest cycle whenever the zone settles
1788
- // We need to do this in the next tick so that we don't prevent the bootup stabilizing
1789
- setTimeout(() => {
1790
- const subscription = this.ngZone.onMicrotaskEmpty.subscribe(() => {
1791
- if ($rootScope.$$phase) {
1792
- if (typeof ngDevMode === 'undefined' || ngDevMode) {
1793
- console.warn('A digest was triggered while one was already in progress. This may mean that something is triggering digests outside the Angular zone.');
1794
- }
1795
- return $rootScope.$evalAsync();
1796
- }
1797
- return $rootScope.$digest();
1798
- });
1799
- $rootScope.$on('$destroy', () => {
1800
- subscription.unsubscribe();
1801
- });
1802
- }, 0);
1803
- }
1804
- ]);
1805
- const upgradeModule = module_(UPGRADE_MODULE_NAME, [INIT_MODULE_NAME].concat(modules));
1806
- // Make sure resumeBootstrap() only exists if the current bootstrap is deferred
1807
- const windowAngular = window['angular'];
1808
- windowAngular.resumeBootstrap = undefined;
1809
- // Bootstrap the AngularJS application inside our zone
1810
- const returnValue = this.ngZone.run(() => bootstrap(element$1, [upgradeModule.name], config));
1811
- // Patch resumeBootstrap() to run inside the ngZone
1812
- if (windowAngular.resumeBootstrap) {
1813
- const originalResumeBootstrap = windowAngular.resumeBootstrap;
1814
- const ngZone = this.ngZone;
1815
- windowAngular.resumeBootstrap = function () {
1816
- let args = arguments;
1817
- windowAngular.resumeBootstrap = originalResumeBootstrap;
1818
- return ngZone.run(() => windowAngular.resumeBootstrap.apply(this, args));
1819
- };
1820
- }
1821
- return returnValue;
1822
- }
1823
- }
1824
- UpgradeModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0-next.4", ngImport: i0, type: UpgradeModule, deps: [{ token: i0.Injector }, { token: i0.NgZone }, { token: i0.PlatformRef }], target: i0.ɵɵFactoryTarget.NgModule });
1825
- UpgradeModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.0.0-next.4", ngImport: i0, type: UpgradeModule });
1826
- UpgradeModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.0.0-next.4", ngImport: i0, type: UpgradeModule, providers: [angular1Providers] });
1827
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0-next.4", ngImport: i0, type: UpgradeModule, decorators: [{
1828
- type: NgModule,
1829
- args: [{ providers: [angular1Providers] }]
1830
- }], ctorParameters: function () { return [{ type: i0.Injector }, { type: i0.NgZone }, { type: i0.PlatformRef }]; } });
1831
-
1832
- // This file only re-exports items to appear in the public api. Keep it that way.
1833
-
1834
- // This file is not used to build this module. It is only used during editing
1835
-
1836
- /**
1837
- * Generated bundle index. Do not edit.
1838
- */
1839
-
1840
- export { UpgradeComponent, UpgradeModule, VERSION, downgradeComponent, downgradeInjectable, downgradeModule, getAngularJSGlobal, getAngularLib, setAngularJSGlobal, setAngularLib };
1841
- //# sourceMappingURL=static.mjs.map