refactor(platform-browser): Remove setGlobalVar from DOM adapter

This commit is contained in:
Miško Hevery
2017-05-22 15:02:07 -07:00
committed by Alex Rickabaugh
parent d4e196035c
commit bb2fc6b8da
8 changed files with 28 additions and 34 deletions

View File

@ -7,15 +7,15 @@
*/
import * as core from '@angular/core';
import {getDOM} from '../dom_adapter';
import {exportNgVar} from '../util';
const CORE_TOKENS = {
'ApplicationRef': core.ApplicationRef,
'NgZone': core.NgZone,
};
const INSPECT_GLOBAL_NAME = 'ng.probe';
const CORE_TOKENS_GLOBAL_NAME = 'ng.coreTokens';
const INSPECT_GLOBAL_NAME = 'probe';
const CORE_TOKENS_GLOBAL_NAME = 'coreTokens';
/**
* Returns a {@link DebugElement} for the given native DOM element, or
@ -36,9 +36,8 @@ export class NgProbeToken {
export function _createNgProbe(extraTokens: NgProbeToken[], coreTokens: core.NgProbeToken[]): any {
const tokens = (extraTokens || []).concat(coreTokens || []);
getDOM().setGlobalVar(INSPECT_GLOBAL_NAME, inspectNativeElement);
getDOM().setGlobalVar(
CORE_TOKENS_GLOBAL_NAME, {...CORE_TOKENS, ..._ngProbeTokensToMap(tokens || [])});
exportNgVar(INSPECT_GLOBAL_NAME, inspectNativeElement);
exportNgVar(CORE_TOKENS_GLOBAL_NAME, {...CORE_TOKENS, ..._ngProbeTokensToMap(tokens || [])});
return () => inspectNativeElement;
}

View File

@ -151,7 +151,6 @@ export abstract class DomAdapter {
abstract setData(element: any, name: string, value: string): any;
abstract getComputedStyle(element: any): any;
abstract getData(element: any, name: string): string|null;
abstract setGlobalVar(name: string, value: any): any;
abstract supportsWebAnimation(): boolean;
abstract performanceNow(): number;
abstract getAnimationPrefix(): string;

View File

@ -6,6 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/
import {ɵglobal as global} from '@angular/core';
const CAMEL_CASE_REGEXP = /([A-Z])/g;
const DASH_CASE_REGEXP = /-([a-z])/g;
@ -17,3 +19,19 @@ export function camelCaseToDashCase(input: string): string {
export function dashCaseToCamelCase(input: string): string {
return input.replace(DASH_CASE_REGEXP, (...m: string[]) => m[1].toUpperCase());
}
/**
* Exports the value under a given `name` in the global property `ng`. For example `ng.probe` if
* `name` is `'probe'`.
* @param name Name under which it will be exported. Keep in mind this will be a property of the
* global `ng` object.
* @param value The value to export.
*/
export function exportNgVar(name: string, value: any): void {
if (!ng) {
global['ng'] = ng = (global['ng'] as{[key: string]: any} | undefined) || {};
}
ng[name] = value;
}
let ng: {[key: string]: any}|undefined;