angular/devtools/projects/shell-chrome/src/app/chrome-window-extensions.ts
AleksanderBodurri 2a1ff17b42 refactor(devtools): run tslint --fix on devtools codebase
This commit runs tslint --fix with the angular/angular tslint configuration on the files inside the devtools codebase.

Notably, the file-header rule in `tslint.json` was missing a default attribute. This commit adds that default attribute and sets it to the
license header that is present in all files in this repo. After running tslint --fix with this default added, this commit added the license header to all files in the devtools directory. Note for the reviewer: the automatically added license headers were added as comments with the "/*!" prefix. Since we want these comments removed in builds, and the rest of the codebase uses "/**", a simple find and replace was performed on the devtools directory to change these prefixes to "/**".
2022-01-26 16:35:31 -05:00

76 lines
2.5 KiB
TypeScript

/**
* @license
* Copyright Google LLC 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
*/
import {findNodeFromSerializedPosition} from 'ng-devtools-backend';
import {buildDirectiveForest, queryDirectiveForest} from '../../../ng-devtools-backend/src/lib/component-tree';
export const initializeExtendedWindowOperations = () => {
extendWindowOperations(window, {inspectedApplication: chromeWindowExtensions});
};
const extendWindowOperations = <T>(target, classImpl: T) => {
for (const key of Object.keys(classImpl)) {
if (target[key] != null) {
console.warn(`A window function or object named ${key} would be overwritten`);
}
}
Object.assign(target, classImpl);
};
const chromeWindowExtensions = {
findConstructorByPosition: (serializedId: string): Element | undefined => {
const node = findNodeFromSerializedPosition(serializedId);
if (node === null) {
console.error(`Cannot find element associated with node ${serializedId}`);
return;
}
if (node.component) {
return node.component.instance.constructor;
} else {
console.error('This component has no instance and therefore no constructor');
}
},
findDomElementByPosition: (serializedId: string): Node | undefined => {
const node = findNodeFromSerializedPosition(serializedId);
if (node === null) {
console.error(`Cannot find element associated with node ${serializedId}`);
return undefined;
}
return node.nativeElement;
},
findPropertyByPosition: (args): any => {
const {directivePosition, objectPath} = JSON.parse(args);
const node = queryDirectiveForest(directivePosition.element, buildDirectiveForest());
if (node === null) {
console.error(`Cannot find element associated with node ${directivePosition}`);
return undefined;
}
const isDirective = directivePosition.directive !== undefined &&
node.directives[directivePosition.directive] &&
typeof node.directives[directivePosition.directive] === 'object';
if (isDirective) {
return traverseDirective(node.directives[directivePosition.directive].instance, objectPath);
}
if (node.component) {
return traverseDirective(node.component.instance, objectPath);
}
},
};
const traverseDirective = (dir: any, objectPath: string[]): any => {
for (const key of objectPath) {
if (!dir[key]) {
return;
}
dir = dir[key];
}
return dir;
};