1 |
|
2 | import { getNodeById } from './dom-node';
|
3 | import { mainThreadify } from '../utils';
|
4 |
|
5 | const getAppRootView = () => require('../application').getRootView();
|
6 | let unsetValue;
|
7 | function unsetViewValue(view, name) {
|
8 | if (!unsetValue) {
|
9 | unsetValue = require('../ui/core/properties').unsetValue;
|
10 | }
|
11 | view[name] = unsetValue;
|
12 | }
|
13 | function getViewById(nodeId) {
|
14 | const node = getNodeById(nodeId);
|
15 | let view;
|
16 | if (node) {
|
17 | view = node.viewRef.get();
|
18 | }
|
19 | return view;
|
20 | }
|
21 | export 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 | }
|
34 | export function getComputedStylesForNode(nodeId) {
|
35 | const view = getViewById(nodeId);
|
36 | if (view) {
|
37 | return view.domNode.getComputedProperties();
|
38 | }
|
39 | return [];
|
40 | }
|
41 | export const removeNode = mainThreadify(function removeNode(nodeId) {
|
42 | const view = getViewById(nodeId);
|
43 | if (view) {
|
44 |
|
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 | });
|
57 | export const setAttributeAsText = mainThreadify(function setAttributeAsText(nodeId, text, name) {
|
58 | const view = getViewById(nodeId);
|
59 | if (view) {
|
60 |
|
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 |
|
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 |
|
79 | unsetViewValue(view, name);
|
80 | }
|
81 | view.domNode.loadAttributes();
|
82 | }
|
83 | });
|
84 |
|
\ | No newline at end of file |