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

@ -363,7 +363,6 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
} }
getComputedStyle(element: any): any { return getComputedStyle(element); } getComputedStyle(element: any): any { return getComputedStyle(element); }
// TODO(tbosch): move this into a separate environment class once we have it // TODO(tbosch): move this into a separate environment class once we have it
setGlobalVar(path: string, value: any) { setValueOnPath(global, path, value); }
supportsWebAnimation(): boolean { supportsWebAnimation(): boolean {
return typeof(<any>Element).prototype['animate'] === 'function'; return typeof(<any>Element).prototype['animate'] === 'function';
} }
@ -419,20 +418,3 @@ export function parseCookieValue(cookieStr: string, name: string): string|null {
} }
return null; return null;
} }
export function setValueOnPath(global: any, path: string, value: any) {
const parts = path.split('.');
let obj: any = global;
while (parts.length > 1) {
const name = parts.shift() !;
if (obj.hasOwnProperty(name) && obj[name] != null) {
obj = obj[name];
} else {
obj = obj[name] = {};
}
}
if (obj === undefined || obj === null) {
obj = {};
}
obj[parts.shift() !] = value;
}

View File

@ -7,11 +7,10 @@
*/ */
import {ComponentRef} from '@angular/core'; import {ComponentRef} from '@angular/core';
import {getDOM} from '../../dom/dom_adapter'; import {exportNgVar} from '../../dom/util';
import {AngularProfiler} from './common_tools'; import {AngularProfiler} from './common_tools';
const PROFILER_GLOBAL_NAME = 'ng.profiler'; const PROFILER_GLOBAL_NAME = 'profiler';
/** /**
* Enabled Angular debug tools that are accessible via your browser's * Enabled Angular debug tools that are accessible via your browser's
@ -27,7 +26,7 @@ const PROFILER_GLOBAL_NAME = 'ng.profiler';
* @experimental All debugging apis are currently experimental. * @experimental All debugging apis are currently experimental.
*/ */
export function enableDebugTools<T>(ref: ComponentRef<T>): ComponentRef<T> { export function enableDebugTools<T>(ref: ComponentRef<T>): ComponentRef<T> {
getDOM().setGlobalVar(PROFILER_GLOBAL_NAME, new AngularProfiler(ref)); exportNgVar(PROFILER_GLOBAL_NAME, new AngularProfiler(ref));
return ref; return ref;
} }
@ -37,5 +36,5 @@ export function enableDebugTools<T>(ref: ComponentRef<T>): ComponentRef<T> {
* @experimental All debugging apis are currently experimental. * @experimental All debugging apis are currently experimental.
*/ */
export function disableDebugTools(): void { export function disableDebugTools(): void {
getDOM().setGlobalVar(PROFILER_GLOBAL_NAME, null); exportNgVar(PROFILER_GLOBAL_NAME, null);
} }

View File

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

View File

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

View File

@ -6,6 +6,8 @@
* found in the LICENSE file at https://angular.io/license * 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 CAMEL_CASE_REGEXP = /([A-Z])/g;
const DASH_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 { export function dashCaseToCamelCase(input: string): string {
return input.replace(DASH_CASE_REGEXP, (...m: string[]) => m[1].toUpperCase()); 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;

View File

@ -8,7 +8,6 @@
export {BROWSER_SANITIZATION_PROVIDERS as ɵBROWSER_SANITIZATION_PROVIDERS, INTERNAL_BROWSER_PLATFORM_PROVIDERS as ɵINTERNAL_BROWSER_PLATFORM_PROVIDERS, initDomAdapter as ɵinitDomAdapter} from './browser'; export {BROWSER_SANITIZATION_PROVIDERS as ɵBROWSER_SANITIZATION_PROVIDERS, INTERNAL_BROWSER_PLATFORM_PROVIDERS as ɵINTERNAL_BROWSER_PLATFORM_PROVIDERS, initDomAdapter as ɵinitDomAdapter} from './browser';
export {BrowserDomAdapter as ɵBrowserDomAdapter} from './browser/browser_adapter'; export {BrowserDomAdapter as ɵBrowserDomAdapter} from './browser/browser_adapter';
export {setValueOnPath as ɵsetValueOnPath} from './browser/browser_adapter';
export {BrowserPlatformLocation as ɵBrowserPlatformLocation} from './browser/location/browser_platform_location'; export {BrowserPlatformLocation as ɵBrowserPlatformLocation} from './browser/location/browser_platform_location';
export {TRANSITION_ID as ɵTRANSITION_ID} from './browser/server-transition'; export {TRANSITION_ID as ɵTRANSITION_ID} from './browser/server-transition';
export {BrowserGetTestability as ɵBrowserGetTestability} from './browser/testability'; export {BrowserGetTestability as ɵBrowserGetTestability} from './browser/testability';

View File

@ -9,7 +9,7 @@
const parse5 = require('parse5'); const parse5 = require('parse5');
import {ɵglobal as global} from '@angular/core'; import {ɵglobal as global} from '@angular/core';
import {ɵDomAdapter as DomAdapter, ɵsetRootDomAdapter as setRootDomAdapter, ɵsetValueOnPath as setValueOnPath} from '@angular/platform-browser'; import {ɵDomAdapter as DomAdapter, ɵsetRootDomAdapter as setRootDomAdapter} from '@angular/platform-browser';
import {SelectorMatcher, CssSelector} from '@angular/compiler'; import {SelectorMatcher, CssSelector} from '@angular/compiler';
let treeAdapter: any; let treeAdapter: any;
@ -611,7 +611,6 @@ export class Parse5DomAdapter extends DomAdapter {
getComputedStyle(el: any): any { throw 'not implemented'; } getComputedStyle(el: any): any { throw 'not implemented'; }
setData(el: any, name: string, value: string) { this.setAttribute(el, 'data-' + name, value); } setData(el: any, name: string, value: string) { this.setAttribute(el, 'data-' + name, value); }
// TODO(tbosch): move this into a separate environment class once we have it // TODO(tbosch): move this into a separate environment class once we have it
setGlobalVar(path: string, value: any) { setValueOnPath(global, path, value); }
supportsWebAnimation(): boolean { return false; } supportsWebAnimation(): boolean { return false; }
performanceNow(): number { return Date.now(); } performanceNow(): number { return Date.now(); }
getAnimationPrefix(): string { return ''; } getAnimationPrefix(): string { return ''; }

View File

@ -154,7 +154,6 @@ export class WorkerDomAdapter extends DomAdapter {
setData(element: any, name: string, value: string) { throw 'not implemented'; } setData(element: any, name: string, value: string) { throw 'not implemented'; }
getComputedStyle(element: any): any { throw 'not implemented'; } getComputedStyle(element: any): any { throw 'not implemented'; }
getData(element: any, name: string): string { throw 'not implemented'; } getData(element: any, name: string): string { throw 'not implemented'; }
setGlobalVar(name: string, value: any) { throw 'not implemented'; }
performanceNow(): number { throw 'not implemented'; } performanceNow(): number { throw 'not implemented'; }
getAnimationPrefix(): string { throw 'not implemented'; } getAnimationPrefix(): string { throw 'not implemented'; }
getTransitionEnd(): string { throw 'not implemented'; } getTransitionEnd(): string { throw 'not implemented'; }