-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
154 lines (119 loc) · 3.35 KB
/
index.jsx
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React, { Component } from 'react';
import { render } from 'react-dom';
import PropTypes from 'prop-types';
import { ifElse, is, always, omit } from 'ramda';
// https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Window_features
const defaultWindowOptions = [
'about:blank',
'My Awesome Title',
'toolbar=false',
'menubar=false',
];
const PopoutWrapper = ({ children }) => (
<div>
{children}
</div>
);
class ChildWindow extends Component {
static displayName = 'ChildWindow'
static propTypes = {
options: PropTypes.arrayOf(PropTypes.string),
open: PropTypes.bool,
onUnload: PropTypes.func,
onLoad: PropTypes.func,
mountID: PropTypes.string,
}
constructor(props) {
super(props);
const defaultRef = (el) => console.dir(el);
this.setRef = props.setRef || defaultRef;
}
componentWillMount = () => (
this.props.open
? this.openWindow(this.props)
: this.closeWindow()
)
componentWillReceiveProps = (props) => {
const { open } = props;
if (open) {
this.openWindow(props);
} else {
this.closeWindow();
}
}
componentWillUnmount = () => this.closeWindow()
getChildMountElement = (id, win) => {
const alreadyMounted = win.document.getElementById(id);
if (alreadyMounted) {
return alreadyMounted;
}
const el = win.document.createElement('div');
el.setAttribute('id', id);
win.document.body.appendChild(el);
this.setRef(el);
return el;
}
withSplitScreen = (options, win) => {
if (!win) {
return options;
}
return [...options];
}
openWindow = (props) => {
if (this.childWindow) {
return this.renderToChild(this.updateChildren(props));
}
const parentWindow = props.window || window;
if (!parentWindow) {
return null;
}
const { options = defaultWindowOptions } = props;
this.childWindow = this.createChild(
this.withSplitScreen(options, parentWindow),
parentWindow
);
return this.childWindow;
}
createChild = ([url, title, ...attrs], win) => {
const childWindow = win.open(url, title, attrs.join(','));
// eslint-disable-next-line
const defaultHandleUnload = (event, self) => self.childWindow = null;
const defaultHandleLoad = () => console.log('The content has loaded');
const {
onUnload = defaultHandleUnload,
onLoad = defaultHandleLoad,
mountID = 'mount',
} = this.props;
const mountElement = this.getChildMountElement(mountID, childWindow);
const renderToChild = comp => render(comp, mountElement);
this.renderToChild = ifElse(
is(Array),
comp => renderToChild(<PopoutWrapper>{comp}</PopoutWrapper>),
renderToChild
);
/**
* window.componentDidMount
*/
childWindow.addEventListener(
'load',
event => onLoad(event, this)
);
/**
* window.componentWillUnmount
*/
childWindow.addEventListener(
'beforeunload',
event => onUnload(event, this)
);
this.renderToChild(this.updateChildren(this.props));
return childWindow;
}
closeWindow = () => this.childWindow && this.childWindow.close()
updateChildren = ({ children, ...props }) => React.Children
.map(
children,
child => React.cloneElement(child, omit(['onLoad','onUnload'], props))
)
render = always(null)
}
export default ChildWindow;