-
Notifications
You must be signed in to change notification settings - Fork 248
/
index.js
1035 lines (795 loc) · 36.3 KB
/
index.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _desc, _value, _class;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
var desc = {};
Object['ke' + 'ys'](descriptor).forEach(function (key) {
desc[key] = descriptor[key];
});
desc.enumerable = !!desc.enumerable;
desc.configurable = !!desc.configurable;
if ('value' in desc || desc.initializer) {
desc.writable = true;
}
desc = decorators.slice().reverse().reduce(function (desc, decorator) {
return decorator(target, property, desc) || desc;
}, desc);
if (context && desc.initializer !== void 0) {
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
if (desc.initializer === void 0) {
Object['define' + 'Property'](target, property, desc);
desc = null;
}
return desc;
}
/* eslint max-lines: 0 */
import { send, bridge, serializeMessage, ProxyWindow } from 'post-robot/src';
import { isSameDomain, isSameTopWindow, matchDomain, getDomainFromUrl, isBlankDomain, onCloseWindow, getDomain, getDistanceFromTop, isTop, normalizeMockUrl } from 'cross-domain-utils/src';
import { ZalgoPromise } from 'zalgo-promise/src';
import { addEventListener, uniqueID, elementReady as _elementReady, writeElementToWindow, noop, showAndAnimate, animateAndHide, showElement, hideElement, onResize, addClass, extend, extendUrl, getElement, memoized, appendChild, once, stringify, stringifyError, eventEmitter } from 'belter/src';
import { node, dom, ElementNode } from 'jsx-pragmatic/src';
import { buildChildWindowName } from '../window';
import { POST_MESSAGE, CONTEXT, CLASS_NAMES, ANIMATION_NAMES, CLOSE_REASONS, DELEGATE, INITIAL_PROPS, WINDOW_REFERENCES, EVENTS } from '../../constants';
import { global, cleanup } from '../../lib';
import { RENDER_DRIVERS } from './drivers';
import { propsToQuery, normalizeProps } from './props';
global.props = global.props || {};
global.windows = global.windows || {};
/* Parent Component
----------------
This manages the state of the component on the parent window side - i.e. the window the component is being rendered into.
It handles opening the necessary windows/iframes, launching the component's url, and listening for messages back from the component.
*/
export var ParentComponent = (_class = function () {
// eslint-disable-line no-undef
function ParentComponent(component, context, _ref) {
var _this = this;
var props = _ref.props;
_classCallCheck(this, ParentComponent);
ZalgoPromise['try'](function () {
_this.onInit = new ZalgoPromise();
_this.clean = cleanup(_this);
_this.event = eventEmitter();
_this.component = component;
_this.driver = RENDER_DRIVERS[context];
_this.setProps(props);
_this.registerActiveComponent();
_this.watchForUnload();
return _this.onInit;
})['catch'](function (err) {
return _this.error(err, props);
});
}
ParentComponent.prototype.render = function render(context, element) {
var _this2 = this;
var target = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : window;
return this.tryInit(function () {
_this2.component.log('render');
var uid = uniqueID();
var tasks = {};
tasks.onRender = _this2.props.onRender();
var domain = _this2.getDomain();
var initialDomain = _this2.getInitialDomain();
tasks.elementReady = ZalgoPromise['try'](function () {
if (element) {
return _this2.elementReady(element);
}
});
var focus = function focus() {
return tasks.open.then(function (proxyWin) {
return proxyWin.focus();
});
};
tasks.openContainer = tasks.elementReady.then(function () {
return _this2.openContainer(element, { context: context, uid: uid, focus: focus });
});
tasks.open = _this2.driver.renderedIntoContainer ? tasks.openContainer.then(function () {
return _this2.open();
}) : _this2.open();
tasks.showContainer = tasks.openContainer.then(function () {
return _this2.showContainer();
});
tasks.buildWindowName = tasks.open.then(function (proxyWin) {
return _this2.buildWindowName({ proxyWin: proxyWin, initialDomain: initialDomain, domain: domain, target: target, context: context, uid: uid });
});
tasks.setWindowName = ZalgoPromise.all([tasks.open, tasks.buildWindowName]).then(function (_ref2) {
var proxyWin = _ref2[0],
windowName = _ref2[1];
return _this2.setWindowName(proxyWin, windowName);
});
tasks.watchForClose = tasks.open.then(function (proxyWin) {
return _this2.watchForClose(proxyWin);
});
tasks.prerender = ZalgoPromise.all([tasks.open, tasks.openContainer]).then(function (_ref3) {
var proxyWin = _ref3[0];
return _this2.prerender(proxyWin, { context: context, uid: uid });
});
tasks.showComponent = tasks.prerender.then(function () {
return _this2.showComponent();
});
tasks.buildUrl = _this2.buildUrl();
tasks.openBridge = ZalgoPromise.all([tasks.open, tasks.buildUrl]).then(function (_ref4) {
var proxyWin = _ref4[0],
url = _ref4[1];
return _this2.openBridge(proxyWin, getDomainFromUrl(url), context);
});
tasks.loadUrl = ZalgoPromise.all([tasks.open, tasks.buildUrl, tasks.setWindowName]).then(function (_ref5) {
var proxyWin = _ref5[0],
url = _ref5[1];
return _this2.loadUrl(proxyWin, url);
});
tasks.switchPrerender = ZalgoPromise.all([tasks.prerender, _this2.onInit]).then(function () {
return _this2.switchPrerender();
});
tasks.runTimeout = tasks.loadUrl.then(function () {
return _this2.runTimeout();
});
return ZalgoPromise.hash(tasks);
}).then(function () {
return _this2.props.onEnter();
}).then(function () {
return _this2;
});
};
ParentComponent.prototype.renderTo = function renderTo(context, target, element) {
var _this3 = this;
return this.tryInit(function () {
if (target === window) {
return _this3.render(context, element);
}
if (element && typeof element !== 'string') {
throw new Error('Element passed to renderTo must be a string selector, got ' + (typeof element === 'undefined' ? 'undefined' : _typeof(element)) + ' ' + element);
}
_this3.checkAllowRemoteRender(target);
_this3.component.log('render_' + context + '_to_win', { element: stringify(element), context: context });
_this3.delegate(context, target);
return _this3.render(context, element, target);
});
};
ParentComponent.prototype.on = function on(eventName, handler) {
return this.event.on(eventName, handler);
};
ParentComponent.prototype.checkAllowRemoteRender = function checkAllowRemoteRender(target) {
if (!target) {
throw this.component.createError('Must pass window to renderTo');
}
if (!isSameTopWindow(window, target)) {
throw new Error('Can only renderTo an adjacent frame');
}
var origin = getDomain();
var domain = this.getDomain();
if (!matchDomain(domain, origin) && !isSameDomain(target)) {
throw new Error('Can not render remotely to ' + domain.toString() + ' - can only render to ' + origin);
}
};
ParentComponent.prototype.registerActiveComponent = function registerActiveComponent() {
var _this4 = this;
ParentComponent.activeComponents.push(this);
this.clean.register(function () {
ParentComponent.activeComponents.splice(ParentComponent.activeComponents.indexOf(_this4), 1);
});
};
ParentComponent.prototype.getWindowRef = function getWindowRef(target, domain, uid, context) {
if (domain === getDomain(window)) {
global.windows[uid] = window;
this.clean.register(function () {
delete global.windows[uid];
});
return { type: WINDOW_REFERENCES.GLOBAL, uid: uid };
}
if (target !== window) {
throw new Error('Can not currently create window reference for different target with a different domain');
}
if (context === CONTEXT.POPUP) {
return { type: WINDOW_REFERENCES.OPENER };
}
if (isTop(window)) {
return { type: WINDOW_REFERENCES.TOP };
}
return { type: WINDOW_REFERENCES.PARENT, distance: getDistanceFromTop(window) };
};
ParentComponent.prototype.buildWindowName = function buildWindowName(_ref6) {
var proxyWin = _ref6.proxyWin,
initialDomain = _ref6.initialDomain,
domain = _ref6.domain,
target = _ref6.target,
uid = _ref6.uid,
context = _ref6.context;
return buildChildWindowName(this.component.name, this.buildChildPayload({ proxyWin: proxyWin, initialDomain: initialDomain, domain: domain, target: target, context: context, uid: uid }));
};
ParentComponent.prototype.getPropsRef = function getPropsRef(proxyWin, target, domain, uid) {
var value = serializeMessage(proxyWin, domain, this.getPropsForChild(domain));
var propRef = isSameDomain(target) ? { type: INITIAL_PROPS.RAW, value: value } : { type: INITIAL_PROPS.UID, uid: uid };
if (propRef.type === INITIAL_PROPS.UID) {
global.props[uid] = value;
this.clean.register(function () {
delete global.props[uid];
});
}
return propRef;
};
ParentComponent.prototype.buildChildPayload = function buildChildPayload() {
var _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
proxyWin = _ref7.proxyWin,
initialDomain = _ref7.initialDomain,
domain = _ref7.domain,
_ref7$target = _ref7.target,
target = _ref7$target === undefined ? window : _ref7$target,
context = _ref7.context,
uid = _ref7.uid;
var childPayload = {
uid: uid,
context: context,
domain: getDomain(window),
tag: this.component.tag,
parent: this.getWindowRef(target, initialDomain, uid, context),
props: this.getPropsRef(proxyWin, target, domain, uid),
exports: serializeMessage(proxyWin, domain, this.buildParentExports(proxyWin))
};
return childPayload;
};
ParentComponent.prototype.setProps = function setProps(props) {
var isUpdate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
if (this.component.validate) {
this.component.validate(this.component, props);
}
// $FlowFixMe
this.props = this.props || {};
extend(this.props, normalizeProps(this.component, this, props, isUpdate));
};
ParentComponent.prototype.buildUrl = function buildUrl() {
var _this5 = this;
return propsToQuery(_extends({}, this.component.props, this.component.builtinProps), this.props).then(function (query) {
var url = normalizeMockUrl(_this5.component.getUrl(_this5.props));
return extendUrl(url, { query: _extends({}, query) });
});
};
ParentComponent.prototype.getDomain = function getDomain() {
return this.component.getDomain(this.props);
};
ParentComponent.prototype.getInitialDomain = function getInitialDomain() {
return this.component.getInitialDomain(this.props);
};
ParentComponent.prototype.getPropsForChild = function getPropsForChild(domain) {
var result = {};
for (var _i2 = 0, _Object$keys2 = Object.keys(this.props), _length2 = _Object$keys2 == null ? 0 : _Object$keys2.length; _i2 < _length2; _i2++) {
var key = _Object$keys2[_i2];
var prop = this.component.getProp(key);
if (prop && prop.sendToChild === false) {
continue;
}
if (prop && prop.sameDomain && !matchDomain(domain, getDomain(window))) {
continue;
}
// $FlowFixMe
result[key] = this.props[key];
}
// $FlowFixMe
return result;
};
ParentComponent.prototype.updateProps = function updateProps(props) {
var _this6 = this;
this.setProps(props, true);
return this.onInit.then(function () {
if (_this6.childExports) {
return _this6.childExports.updateProps(_this6.getPropsForChild(_this6.getDomain()));
} else {
throw new Error('Child exports were not available');
}
});
};
ParentComponent.prototype.openBridge = function openBridge(proxyWin, domain, context) {
var _this7 = this;
return ZalgoPromise['try'](function () {
return proxyWin.awaitWindow();
}).then(function (win) {
if (!bridge || !bridge.needsBridge({ win: win, domain: domain }) || bridge.hasBridge(domain, domain)) {
return;
}
var bridgeUrl = _this7.component.getBridgeUrl(_this7.props);
if (!bridgeUrl) {
throw new Error('Bridge url and domain needed to render ' + context);
}
var bridgeDomain = getDomainFromUrl(bridgeUrl);
bridge.linkUrl(win, domain);
return bridge.openBridge(bridgeUrl, bridgeDomain);
});
};
ParentComponent.prototype.open = function open() {
var _this8 = this;
return ZalgoPromise['try'](function () {
_this8.component.log('open');
var windowProp = _this8.props.window;
if (windowProp) {
_this8.clean.register('destroyProxyWindow', function () {
return windowProp.close();
});
return windowProp;
}
return _this8.driver.open.call(_this8);
});
};
ParentComponent.prototype.setWindowName = function setWindowName(proxyWin, name) {
return proxyWin.setName(name);
};
ParentComponent.prototype.switchPrerender = function switchPrerender() {
var _this9 = this;
return ZalgoPromise['try'](function () {
if (_this9.component.prerenderTemplate && _this9.driver.switchPrerender) {
return _this9.driver.switchPrerender.call(_this9);
}
});
};
ParentComponent.prototype.elementReady = function elementReady(element) {
return _elementReady(element).then(noop);
};
ParentComponent.prototype.delegate = function delegate(context, target) {
var _this10 = this;
this.component.log('delegate');
var props = {};
for (var _i4 = 0, _component$getPropNam2 = this.component.getPropNames(), _length4 = _component$getPropNam2 == null ? 0 : _component$getPropNam2.length; _i4 < _length4; _i4++) {
var propName = _component$getPropNam2[_i4];
if (this.component.getProp(propName).allowDelegate) {
props[propName] = this.props[propName];
}
}
var delegate = send(target, POST_MESSAGE.DELEGATE + '_' + this.component.name, {
context: context,
props: props,
overrides: {
userClose: function userClose() {
return _this10.userClose();
},
error: function error(err) {
return _this10.error(err);
},
on: function on(eventName, handler) {
return _this10.on(eventName, handler);
}
}
}).then(function (_ref8) {
var data = _ref8.data;
_this10.clean.register(data.destroy);
return data;
})['catch'](function (err) {
throw new Error('Unable to delegate rendering. Possibly the component is not loaded in the target window.\n\n' + stringifyError(err));
});
var overrides = this.driver.delegateOverrides;
var _loop = function _loop(_i6, _Object$keys4, _length6) {
var key = _Object$keys4[_i6];
var val = overrides[key];
if (val === DELEGATE.CALL_DELEGATE) {
// $FlowFixMe
_this10[key] = function overridenFunction() {
var _this11 = this,
_arguments = arguments;
return delegate.then(function (data) {
return data.overrides[key].apply(_this11, _arguments);
});
};
}
};
for (var _i6 = 0, _Object$keys4 = Object.keys(overrides), _length6 = _Object$keys4 == null ? 0 : _Object$keys4.length; _i6 < _length6; _i6++) {
_loop(_i6, _Object$keys4, _length6);
}
};
ParentComponent.prototype.watchForClose = function watchForClose(proxyWin) {
var _this12 = this;
return proxyWin.awaitWindow().then(function (win) {
var closeWindowListener = onCloseWindow(win, function () {
_this12.component.log('detect_close_child');
return ZalgoPromise['try'](function () {
return _this12.props.onClose(CLOSE_REASONS.CLOSE_DETECTED);
})['finally'](function () {
return _this12.destroy();
});
}, 3000);
_this12.clean.register('destroyCloseWindowListener', closeWindowListener.cancel);
});
};
ParentComponent.prototype.watchForUnload = function watchForUnload() {
var _this13 = this;
var unloadWindowListener = addEventListener(window, 'unload', once(function () {
_this13.component.log('navigate_away');
_this13.destroy();
}));
this.clean.register('destroyUnloadWindowListener', unloadWindowListener.cancel);
};
ParentComponent.prototype.loadUrl = function loadUrl(proxyWin, url) {
this.component.log('load_url');
return proxyWin.setLocation(url);
};
ParentComponent.prototype.runTimeout = function runTimeout() {
var _this14 = this;
var timeout = this.props.timeout;
if (timeout) {
var _id = this.timeout = setTimeout(function () {
_this14.component.log('timed_out', { timeout: timeout.toString() });
_this14.error(_this14.component.createError('Loading component timed out after ' + timeout + ' milliseconds'));
}, timeout);
this.clean.register(function () {
clearTimeout(_id);
delete _this14.timeout;
});
}
};
ParentComponent.prototype.initChild = function initChild(childExports) {
var _this15 = this;
return ZalgoPromise['try'](function () {
_this15.childExports = childExports;
_this15.onInit.resolve(_this15);
if (_this15.timeout) {
clearTimeout(_this15.timeout);
}
});
};
ParentComponent.prototype.buildParentExports = function buildParentExports(win) {
var _this16 = this;
return {
init: function init(childExports) {
return _this16.initChild(childExports);
},
close: function close(reason) {
return _this16.close(reason);
},
checkClose: function checkClose() {
return _this16.checkClose(win);
},
resize: function resize(_ref9) {
var width = _ref9.width,
height = _ref9.height;
return _this16.resize({ width: width, height: height });
},
trigger: function trigger(name) {
return ZalgoPromise['try'](function () {
return _this16.event.trigger(name);
});
},
hide: function hide() {
return ZalgoPromise['try'](function () {
return _this16.hide();
});
},
show: function show() {
return ZalgoPromise['try'](function () {
return _this16.show();
});
},
error: function error(err) {
return _this16.error(err);
}
};
};
ParentComponent.prototype.resize = function resize(_ref10) {
var _this17 = this;
var width = _ref10.width,
height = _ref10.height;
return ZalgoPromise['try'](function () {
_this17.driver.resize.call(_this17, { width: width, height: height });
});
};
ParentComponent.prototype.hide = function hide() {
if (this.container) {
hideElement(this.container);
}
return this.driver.hide.call(this);
};
ParentComponent.prototype.show = function show() {
if (this.container) {
showElement(this.container);
}
return this.driver.show.call(this);
};
ParentComponent.prototype.checkClose = function checkClose(win) {
var _this18 = this;
return win.isClosed().then(function (closed) {
if (closed) {
return _this18.userClose();
}
return ZalgoPromise.delay(200).then(function () {
return win.isClosed();
}).then(function (secondClosed) {
if (secondClosed) {
return _this18.userClose();
}
});
});
};
ParentComponent.prototype.userClose = function userClose() {
return this.close(CLOSE_REASONS.USER_CLOSED);
};
/* Close
-----
Close the child component
*/
ParentComponent.prototype.close = function close() {
var _this19 = this;
var reason = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : CLOSE_REASONS.PARENT_CALL;
return ZalgoPromise['try'](function () {
_this19.component.log('close', { reason: reason });
_this19.event.triggerOnce(EVENTS.CLOSE);
return _this19.props.onClose(reason);
}).then(function () {
return ZalgoPromise.all([_this19.closeComponent(), _this19.closeContainer()]);
}).then(function () {
return _this19.destroy();
});
};
ParentComponent.prototype.closeContainer = function closeContainer() {
var _this20 = this;
var reason = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : CLOSE_REASONS.PARENT_CALL;
return ZalgoPromise['try'](function () {
_this20.event.triggerOnce(EVENTS.CLOSE);
return _this20.props.onClose(reason);
}).then(function () {
return ZalgoPromise.all([_this20.closeComponent(reason), _this20.hideContainer()]);
}).then(function () {
return _this20.destroyContainer();
});
};
ParentComponent.prototype.destroyContainer = function destroyContainer() {
var _this21 = this;
return ZalgoPromise['try'](function () {
_this21.clean.run('destroyContainerEvents');
_this21.clean.run('destroyContainerTemplate');
});
};
ParentComponent.prototype.closeComponent = function closeComponent() {
var _this22 = this;
var reason = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : CLOSE_REASONS.PARENT_CALL;
return ZalgoPromise['try'](function () {
return _this22.cancelContainerEvents();
}).then(function () {
_this22.event.triggerOnce(EVENTS.CLOSE);
return _this22.props.onClose(reason);
}).then(function () {
return _this22.hideComponent();
}).then(function () {
return _this22.destroyComponent();
}).then(function () {
// IE in metro mode -- child window needs to close itself, or close will hang
if (_this22.childExports && _this22.driver.callChildToClose) {
_this22.childExports.close()['catch'](noop);
}
});
};
ParentComponent.prototype.destroyComponent = function destroyComponent() {
this.clean.run('destroyUnloadWindowListener');
this.clean.run('destroyCloseWindowListener');
this.clean.run('destroyContainerEvents');
this.clean.run('destroyWindow');
this.clean.run('destroyProxyWindow');
};
ParentComponent.prototype.showContainer = function showContainer() {
var _this23 = this;
return ZalgoPromise['try'](function () {
if (_this23.props.onDisplay) {
return _this23.props.onDisplay();
}
}).then(function () {
if (_this23.container) {
return showAndAnimate(_this23.container, ANIMATION_NAMES.SHOW_CONTAINER, _this23.clean.register);
}
});
};
ParentComponent.prototype.showComponent = function showComponent() {
var _this24 = this;
return ZalgoPromise['try'](function () {
if (_this24.props.onDisplay) {
return _this24.props.onDisplay();
}
}).then(function () {
if (_this24.element) {
return showAndAnimate(_this24.element, ANIMATION_NAMES.SHOW_COMPONENT, _this24.clean.register);
}
});
};
ParentComponent.prototype.hideContainer = function hideContainer() {
var _this25 = this;
return ZalgoPromise['try'](function () {
if (_this25.container) {
return animateAndHide(_this25.container, ANIMATION_NAMES.HIDE_CONTAINER, _this25.clean.register);
}
});
};
ParentComponent.prototype.hideComponent = function hideComponent() {
var _this26 = this;
return ZalgoPromise['try'](function () {
if (_this26.element) {
return animateAndHide(_this26.element, ANIMATION_NAMES.HIDE_COMPONENT, _this26.clean.register);
}
});
};
/* Create Component Template
-------------------------
Creates an initial template and stylesheet which are loaded into the child window, to be displayed before the url is loaded
*/
ParentComponent.prototype.prerender = function prerender(proxyWin, _ref11) {
var _this27 = this;
var context = _ref11.context,
uid = _ref11.uid;
return ZalgoPromise['try'](function () {
if (!_this27.component.prerenderTemplate) {
return;
}
return ZalgoPromise['try'](function () {
return proxyWin.awaitWindow();
}).then(function (win) {
return _this27.driver.openPrerender.call(_this27, win);
}).then(function (prerenderWindow) {
if (!prerenderWindow || !isSameDomain(prerenderWindow) || !isBlankDomain(prerenderWindow)) {
return;
}
var doc = prerenderWindow.document;
var el = _this27.renderTemplate(_this27.component.prerenderTemplate, { context: context, uid: uid, document: doc });
if (el instanceof ElementNode) {
el = el.render(dom({ doc: doc }));
}
try {
writeElementToWindow(prerenderWindow, el);
} catch (err) {
return;
}
var _ref12 = _this27.component.autoResize || {},
_ref12$width = _ref12.width,
width = _ref12$width === undefined ? false : _ref12$width,
_ref12$height = _ref12.height,
height = _ref12$height === undefined ? false : _ref12$height,
_ref12$element = _ref12.element,
element = _ref12$element === undefined ? 'body' : _ref12$element;
if (width || height) {
onResize(getElement(element, prerenderWindow.document), function (_ref13) {
var newWidth = _ref13.width,
newHeight = _ref13.height;
_this27.resize({
width: width ? newWidth : undefined,
height: height ? newHeight : undefined
});
}, { width: width, height: height, win: prerenderWindow });
}
});
});
};
ParentComponent.prototype.renderTemplate = function renderTemplate(renderer, _ref14) {
var _this28 = this;
var context = _ref14.context,
uid = _ref14.uid,
focus = _ref14.focus,
container = _ref14.container,
document = _ref14.document,
outlet = _ref14.outlet;
focus = focus || function () {
return ZalgoPromise.resolve();
};
// $FlowFixMe
return renderer.call(this, {
context: context,
uid: uid,
id: CLASS_NAMES.ZOID + '-' + this.component.tag + '-' + uid,
props: renderer.__xdomain__ ? null : this.props,
tag: this.component.tag,
CLASS: CLASS_NAMES,
ANIMATION: ANIMATION_NAMES,
CONTEXT: CONTEXT,
EVENT: EVENTS,
actions: {
focus: focus,
close: function close() {
return _this28.userClose();
}
},
on: function on(eventName, handler) {
return _this28.on(eventName, handler);
},
jsxDom: node,
document: document,
dimensions: this.component.dimensions,
container: container,
outlet: outlet
});
};
ParentComponent.prototype.openContainer = function openContainer(element, _ref15) {
var _this29 = this;
var context = _ref15.context,
uid = _ref15.uid,
focus = _ref15.focus;
return ZalgoPromise['try'](function () {
var el = void 0;
if (element) {
el = getElement(element);
} else {
el = document.body;
}
if (!el) {
throw new Error('Could not find element to open container into');
}
if (!_this29.component.containerTemplate) {
if (_this29.driver.renderedIntoContainer) {
throw new Error('containerTemplate needed to render ' + context);
}
return;
}
var outlet = document.createElement('div');
addClass(outlet, CLASS_NAMES.OUTLET);
var container = _this29.renderTemplate(_this29.component.containerTemplate, { context: context, uid: uid, container: el, focus: focus, outlet: outlet });
if (container instanceof ElementNode) {
container = container.render(dom({ doc: document }));
}
_this29.container = container;
hideElement(_this29.container);
appendChild(el, _this29.container);
if (_this29.driver.renderedIntoContainer) {
_this29.element = outlet;
hideElement(_this29.element);
if (!_this29.element) {
throw new Error('Could not find element to render component into');
}
hideElement(_this29.element);
}
_this29.clean.register('destroyContainerTemplate', function () {
if (_this29.container && _this29.container.parentNode) {
_this29.container.parentNode.removeChild(_this29.container);
}
delete _this29.container;
});
});
};
ParentComponent.prototype.cancelContainerEvents = function cancelContainerEvents() {
this.clean.run('destroyContainerEvents');
};
ParentComponent.prototype.destroy = function destroy() {
var _this30 = this;
return ZalgoPromise['try'](function () {
if (_this30.clean.hasTasks()) {
_this30.component.log('destroy');
return _this30.clean.all();
}
});
};
ParentComponent.prototype.tryInit = function tryInit(method) {
var _this31 = this;
return ZalgoPromise['try'](method)['catch'](function (err) {
_this31.onInit.reject(err);
}).then(function () {
return _this31.onInit;
});
};
// $FlowFixMe
ParentComponent.prototype.error = function error(err) {
var _this32 = this;
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.props;
if (this.errored) {
return;
}