forked from DuendeArchive/identity-model-oidc-client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopupWindow.js
More file actions
139 lines (108 loc) · 3.81 KB
/
PopupWindow.js
File metadata and controls
139 lines (108 loc) · 3.81 KB
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
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
import Log from './Log';
import UrlUtility from './UrlUtility';
const CheckForPopupClosedInterval = 500;
const DefaultPopupFeatures = 'location=no,toolbar=no,width=500,height=500,left=100,top=100;';
//const DefaultPopupFeatures = 'location=no,toolbar=no,width=500,height=500,left=100,top=100;resizable=yes';
const DefaultPopupTarget = "_blank";
export default class PopupWindow {
constructor(params) {
Log.debug("PopupWindow.ctor");
this._promise = new Promise((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
});
let target = params.popupWindowTarget || DefaultPopupTarget;
let features = params.popupWindowFeatures || DefaultPopupFeatures;
this._popup = window.open('', target, features);
if (this._popup) {
Log.debug("popup successfully created");
this._checkForPopupClosedTimer = window.setInterval(this._checkForPopupClosed.bind(this), CheckForPopupClosedInterval);
}
}
get promise() {
return this._promise;
}
navigate(params) {
Log.debug("PopupWindow.navigate");
if (!this._popup) {
this._error("Error opening popup window");
}
else if (!params || !params.url) {
this._error("No url provided");
}
else {
Log.debug("Setting URL in popup");
this._id = params.id;
if (this._id) {
window["popupCallback_" + params.id] = this._callback.bind(this);
}
this._popup.focus();
this._popup.window.location = params.url;
}
return this.promise;
}
_success(data) {
this._cleanup();
Log.debug("Successful response from popup window");
this._resolve(data);
}
_error(message) {
this._cleanup();
Log.error(message);
this._reject(new Error(message));
}
close() {
this._cleanup(false);
}
_cleanup(keepOpen) {
Log.debug("PopupWindow._cleanup");
window.clearInterval(this._checkForPopupClosedTimer);
this._checkForPopupClosedTimer = null;
delete window["popupCallback_" + this._id];
if (this._popup && !keepOpen) {
this._popup.close();
}
this._popup = null;
}
_checkForPopupClosed() {
Log.debug("PopupWindow._checkForPopupClosed");
if (!this._popup || this._popup.closed) {
this._error("Popup window closed");
}
}
_callback(url, keepOpen) {
Log.debug("PopupWindow._callback");
this._cleanup(keepOpen);
if (url) {
this._success({ url: url });
}
else {
this._error("Invalid response from popup");
}
}
static notifyOpener(url, keepOpen, delimiter) {
Log.debug("PopupWindow.notifyOpener");
if (window.opener) {
url = url || window.location.href;
if (url) {
var data = UrlUtility.parseUrlFragment(url, delimiter);
if (data.state) {
var name = "popupCallback_" + data.state;
var callback = window.opener[name];
if (callback) {
Log.debug("passing url message to opener");
callback(url, keepOpen);
}
else {
Log.warn("no matching callback found on opener");
}
}
else {
Log.warn("no state found in response url");
}
}
}
}
}