UNPKG

2.56 kBJavaScriptView Raw
1//Requires
2import { getNodeById } from './dom-node';
3import { mainThreadify } from '../utils';
4// Use lazy requires for core modules
5const getAppRootView = () => require('../application').getRootView();
6let unsetValue;
7function unsetViewValue(view, name) {
8 if (!unsetValue) {
9 unsetValue = require('../ui/core/properties').unsetValue;
10 }
11 view[name] = unsetValue;
12}
13function getViewById(nodeId) {
14 const node = getNodeById(nodeId);
15 let view;
16 if (node) {
17 view = node.viewRef.get();
18 }
19 return view;
20}
21export function getDocument() {
22 const appRoot = getAppRootView();
23 if (!appRoot) {
24 return undefined;
25 }
26 try {
27 appRoot.ensureDomNode();
28 }
29 catch (e) {
30 console.log('ERROR in getDocument(): ' + e);
31 }
32 return appRoot.domNode.toObject();
33}
34export function getComputedStylesForNode(nodeId) {
35 const view = getViewById(nodeId);
36 if (view) {
37 return view.domNode.getComputedProperties();
38 }
39 return [];
40}
41export const removeNode = mainThreadify(function removeNode(nodeId) {
42 const view = getViewById(nodeId);
43 if (view) {
44 // Avoid importing layout and content view
45 const parent = view.parent;
46 if (parent.removeChild) {
47 parent.removeChild(view);
48 }
49 else if (parent.content === view) {
50 parent.content = null;
51 }
52 else {
53 console.log("Can't remove child from " + parent);
54 }
55 }
56});
57export const setAttributeAsText = mainThreadify(function setAttributeAsText(nodeId, text, name) {
58 const view = getViewById(nodeId);
59 if (view) {
60 // attribute is registered for the view instance
61 const hasOriginalAttribute = !!name.trim();
62 if (text) {
63 const textParts = text.split('=');
64 if (textParts.length === 2) {
65 const attrName = textParts[0];
66 const attrValue = textParts[1].replace(/['"]+/g, '');
67 // if attr name is being replaced with another
68 if (name !== attrName && hasOriginalAttribute) {
69 unsetViewValue(view, name);
70 view[attrName] = attrValue;
71 }
72 else {
73 view[hasOriginalAttribute ? name : attrName] = attrValue;
74 }
75 }
76 }
77 else {
78 // delete attribute
79 unsetViewValue(view, name);
80 }
81 view.domNode.loadAttributes();
82 }
83});
84//# sourceMappingURL=devtools-elements.common.js.map
\No newline at end of file