Files
Angular-Docs-Cn/modules/@angular/platform-browser/src/dom/debug/ng_probe.ts
T

74 lines
2.2 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
2016-11-09 14:58:40 -08:00
import * as core from '@angular/core';
2016-06-08 16:38:52 -07:00
2016-08-10 22:17:20 -07:00
import {StringMapWrapper} from '../../facade/collection';
import {DebugDomRootRenderer} from '../../private_import_core';
import {getDOM} from '../dom_adapter';
import {DomRootRenderer} from '../dom_renderer';
2016-06-08 16:38:52 -07:00
const CORE_TOKENS = {
2016-11-09 14:58:40 -08:00
'ApplicationRef': core.ApplicationRef,
'NgZone': core.NgZone
2016-06-08 16:38:52 -07:00
};
const INSPECT_GLOBAL_NAME = 'ng.probe';
const CORE_TOKENS_GLOBAL_NAME = 'ng.coreTokens';
/**
* Returns a {@link DebugElement} for the given native DOM element, or
* null if the given native element does not have an Angular view associated
* with it.
*/
2016-11-09 14:58:40 -08:00
export function inspectNativeElement(element: any /** TODO #9100 */): core.DebugNode {
return core.getDebugNode(element);
}
2016-08-10 22:17:20 -07:00
/**
2016-11-09 14:58:40 -08:00
* Deprecated. Use the one from '@angular/core'.
* @deprecated
2016-08-10 22:17:20 -07:00
*/
export class NgProbeToken {
2016-11-09 14:58:40 -08:00
constructor(public name: string, public token: any) {}
2016-08-10 22:17:20 -07:00
}
2016-11-09 14:58:40 -08:00
2016-08-10 22:17:20 -07:00
export function _createConditionalRootRenderer(
2016-11-09 14:58:40 -08:00
rootRenderer: any /** TODO #9100 */, extraTokens: NgProbeToken[],
coreTokens: core.NgProbeToken[]) {
if (core.isDevMode()) {
const tokens = (extraTokens || []).concat(coreTokens || []);
return _createRootRenderer(rootRenderer, tokens);
}
return rootRenderer;
}
2016-08-10 22:17:20 -07:00
function _createRootRenderer(rootRenderer: any /** TODO #9100 */, extraTokens: NgProbeToken[]) {
getDOM().setGlobalVar(INSPECT_GLOBAL_NAME, inspectNativeElement);
2016-08-10 22:17:20 -07:00
getDOM().setGlobalVar(
CORE_TOKENS_GLOBAL_NAME,
StringMapWrapper.merge(CORE_TOKENS, _ngProbeTokensToMap(extraTokens || [])));
return new DebugDomRootRenderer(rootRenderer);
}
2016-08-10 22:17:20 -07:00
function _ngProbeTokensToMap(tokens: NgProbeToken[]): {[name: string]: any} {
return tokens.reduce((prev: any, t: any) => (prev[t.name] = t.token, prev), {});
}
/**
* Providers which support debugging Angular applications (e.g. via `ng.probe`).
*/
2016-11-09 14:58:40 -08:00
export const ELEMENT_PROBE_PROVIDERS: core.Provider[] = [{
provide: core.RootRenderer,
2016-08-10 22:17:20 -07:00
useFactory: _createConditionalRootRenderer,
2016-11-09 14:58:40 -08:00
deps: [
DomRootRenderer, [NgProbeToken, new core.Optional()],
[core.NgProbeToken, new core.Optional()]
]
}];