-
Notifications
You must be signed in to change notification settings - Fork 123
/
detachWindow.js
99 lines (77 loc) · 2.83 KB
/
detachWindow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const _ = require('../../lodash');
function detachWindowGUI(gui, K3D) {
let newWindow;
let intervalID;
const originalDom = K3D.getWorld().targetDOMNode;
const detachedDom = document.createElement('div');
const detachedDomText = 'K3D works in detached mode. Please close window or click on this text to attach it here.';
function removeByTagName(DOM, tag) {
const elements = DOM.getElementsByTagName(tag);
while (elements[0]) {
elements[0].parentNode.removeChild(elements[0]);
}
}
function reinitializeK3D(DOM) {
const world = K3D.getWorld();
const newK3D = new K3D.constructor(K3D.Provider, DOM, K3D.parameters);
const objects = world.K3DObjects.children.reduce((prev, object) => {
prev.push(world.ObjectsListJson[object.K3DIdentifier]);
return prev;
}, []);
K3D.disable();
newK3D.load({ objects });
newK3D.setCamera(K3D.getWorld().controls.getCameraArray());
_.assign(K3D, newK3D);
}
function attach() {
if (newWindow) {
newWindow.close();
newWindow = null;
originalDom.removeChild(detachedDom);
reinitializeK3D(originalDom);
}
}
function checkWindow() {
if (newWindow && newWindow.closed) {
clearInterval(intervalID);
attach();
}
}
detachedDom.className = 'detachedInfo';
detachedDom.innerHTML = detachedDomText;
detachedDom.style.cssText = [
'cursor: pointer',
'padding: 2em',
].join(';');
const obj = {
detachWidget() {
newWindow = window.open('', '_blank', 'width=800,height=600,resizable=1');
newWindow.document.body.innerHTML = require('./helpers/detachedWindowHtml');
// copy css
const styles = document.getElementsByTagName('style');
for (let i = 0; i < styles.length; i++) {
newWindow.document.body.appendChild(
styles[i].cloneNode(true),
);
}
setTimeout(() => {
reinitializeK3D(newWindow.document.getElementById('canvasTarget'));
removeByTagName(originalDom, 'canvas');
removeByTagName(originalDom, 'div');
removeByTagName(originalDom, 'svg');
originalDom.appendChild(detachedDom);
}, 100);
newWindow.opener.addEventListener('unload', () => {
if (newWindow) {
newWindow.close();
}
}, false);
intervalID = setInterval(checkWindow, 500);
},
};
gui.add(obj, 'detachWidget').name('Detach widget');
detachedDom.addEventListener('click', () => {
attach();
}, false);
}
module.exports = detachWindowGUI;