1 | const registeredDomNodes = {};
|
2 | const ELEMENT_NODE_TYPE = 1;
|
3 | const ROOT_NODE_TYPE = 9;
|
4 | const propertyBlacklist = ['effectivePaddingLeft', 'effectivePaddingBottom', 'effectivePaddingRight', 'effectivePaddingTop', 'effectiveBorderTopWidth', 'effectiveBorderRightWidth', 'effectiveBorderBottomWidth', 'effectiveBorderLeftWidth', 'effectiveMinWidth', 'effectiveMinHeight', 'effectiveWidth', 'effectiveHeight', 'effectiveMarginLeft', 'effectiveMarginTop', 'effectiveMarginRight', 'effectiveMarginBottom', 'nodeName', 'nodeType', 'decodeWidth', 'decodeHeight', 'ng-reflect-items', 'domNode', 'touchListenerIsSet', 'bindingContext', 'nativeView'];
|
5 | function lazy(action) {
|
6 | let _value;
|
7 | return () => _value || (_value = action());
|
8 | }
|
9 | const percentLengthToStringLazy = lazy(() => require('../ui/styling/style-properties').PercentLength.convertToString);
|
10 | const getSetPropertiesLazy = lazy(() => require('../ui/core/properties').getSetProperties);
|
11 | const getComputedCssValuesLazy = lazy(() => require('../ui/core/properties').getComputedCssValues);
|
12 | export function registerInspectorEvents(inspector) {
|
13 | inspectorFrontendInstance = inspector;
|
14 | }
|
15 | let inspectorFrontendInstance;
|
16 | function notifyInspector(callback) {
|
17 | if (inspectorFrontendInstance) {
|
18 | callback(inspectorFrontendInstance);
|
19 | }
|
20 | }
|
21 | function valueToString(value) {
|
22 | if (typeof value === 'undefined' || value === null) {
|
23 | return '';
|
24 | }
|
25 | else if (typeof value === 'object' && value.unit) {
|
26 | return percentLengthToStringLazy()(value);
|
27 | }
|
28 | else {
|
29 | return value + '';
|
30 | }
|
31 | }
|
32 | function propertyFilter([name, value]) {
|
33 | if (name[0] === '_') {
|
34 | return false;
|
35 | }
|
36 | if (value !== null && typeof value === 'object') {
|
37 | return false;
|
38 | }
|
39 | if (propertyBlacklist.indexOf(name) >= 0) {
|
40 | return false;
|
41 | }
|
42 | return true;
|
43 | }
|
44 | function registerNode(domNode) {
|
45 | registeredDomNodes[domNode.nodeId] = domNode;
|
46 | }
|
47 | function unregisterNode(domNode) {
|
48 | delete registeredDomNodes[domNode.nodeId];
|
49 | }
|
50 | export function getNodeById(id) {
|
51 | return registeredDomNodes[id];
|
52 | }
|
53 | export class DOMNode {
|
54 | constructor(view) {
|
55 | this.nodeValue = '';
|
56 | this.attributes = [];
|
57 | this.viewRef = new WeakRef(view);
|
58 | this.nodeType = view.typeName === 'Frame' ? ROOT_NODE_TYPE : ELEMENT_NODE_TYPE;
|
59 | this.nodeId = view._domId;
|
60 | this.nodeName = view.typeName;
|
61 | this.localName = this.nodeName;
|
62 |
|
63 | this.loadAttributes();
|
64 | registerNode(this);
|
65 | }
|
66 | loadAttributes() {
|
67 | this.attributes = [];
|
68 | getSetPropertiesLazy()(this.viewRef.get())
|
69 | .filter(propertyFilter)
|
70 | .forEach((pair) => this.attributes.push(pair[0], pair[1] + ''));
|
71 | }
|
72 | get children() {
|
73 | const view = this.viewRef.get();
|
74 | if (!view) {
|
75 | return [];
|
76 | }
|
77 | const res = [];
|
78 | view.eachChild((child) => {
|
79 | child.ensureDomNode();
|
80 | res.push(child.domNode);
|
81 | return true;
|
82 | });
|
83 | return res;
|
84 | }
|
85 | onChildAdded(childView) {
|
86 | notifyInspector((ins) => {
|
87 | const view = this.viewRef.get();
|
88 | let previousChild;
|
89 | view.eachChild((child) => {
|
90 | if (child === childView) {
|
91 | return false;
|
92 | }
|
93 | previousChild = child;
|
94 | return true;
|
95 | });
|
96 | const index = previousChild ? previousChild._domId : 0;
|
97 | childView.ensureDomNode();
|
98 | ins.childNodeInserted(this.nodeId, index, childView.domNode);
|
99 | });
|
100 | }
|
101 | onChildRemoved(view) {
|
102 | notifyInspector((ins) => {
|
103 | ins.childNodeRemoved(this.nodeId, view._domId);
|
104 | });
|
105 | }
|
106 | attributeModified(name, value) {
|
107 | notifyInspector((ins) => {
|
108 | if (propertyBlacklist.indexOf(name) < 0) {
|
109 | ins.attributeModified(this.nodeId, name, valueToString(value));
|
110 | }
|
111 | });
|
112 | }
|
113 | attributeRemoved(name) {
|
114 | notifyInspector((ins) => {
|
115 | ins.attributeRemoved(this.nodeId, name);
|
116 | });
|
117 | }
|
118 | getComputedProperties() {
|
119 | const view = this.viewRef.get();
|
120 | if (!view) {
|
121 | return [];
|
122 | }
|
123 | const result = getComputedCssValuesLazy()(view)
|
124 | .filter((pair) => pair[0][0] !== '_')
|
125 | .map((pair) => {
|
126 | return {
|
127 | name: pair[0],
|
128 | value: valueToString(pair[1]),
|
129 | };
|
130 | });
|
131 | return result;
|
132 | }
|
133 | dispose() {
|
134 | unregisterNode(this);
|
135 |
|
136 | }
|
137 | toObject() {
|
138 | return {
|
139 | nodeId: this.nodeId,
|
140 | nodeType: this.nodeType,
|
141 | nodeName: this.nodeName,
|
142 | localName: this.localName,
|
143 | nodeValue: this.nodeValue,
|
144 | children: this.children.map((c) => c.toObject()),
|
145 | attributes: this.attributes,
|
146 | backendNodeId: 0,
|
147 | };
|
148 | }
|
149 | }
|
150 |
|
\ | No newline at end of file |