-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup-oauth.js
executable file
·180 lines (163 loc) · 5.33 KB
/
popup-oauth.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
(function (factory) {
if (typeof window.define != "undefined" && define.amd) {
define([], factory);
} else {
window.popupOauth = factory();
}
}(function () {
function setDefaults(options, defaults) {
for (var key in defaults) {
if (options[key] === undefined) {
options[key] = defaults[key];
}
}
}
function logError(message) {
if (typeof window.console !== "undefined") {
console.log("[popup-oauth] " + message);
}
}
var popupId = 0;
function Popup(url, options) {
if (options === undefined) {
options = {};
}
popupId = popupId + 1;
var id = 'popup' + popupId;
var defaultFeatures = {
height: 300,
width: 500
};
var optionsFeatures = options.features || {};
var defaults = {
pollWindowInterval: 100
};
setDefaults(options, defaults);
var self = this;
var _pollWindowTimeout;
var _openWindow;
var _pollWindowShouldEmitClosed;
var _pollWindowLastLocationEmitted;
var eventListeners = {};
var emitEvent = function (eventName) {
var data = Array.prototype.slice.call(arguments, 1);
var listeners = eventListeners[eventName] || [];
for (var i = 0; i < listeners.length; i++) {
var listener = listeners[i];
try {
listener.apply(self, data);
} catch (e) {
logError(e);
}
}
};
var listenEvent = function (eventName, listener) {
if (!eventListeners[eventName]) {
eventListeners[eventName] = [];
}
eventListeners[eventName].push(listener);
}
var resetEventListeners = function () {
eventListeners = {};
}
var getFeatures = function () {
var featuresString = "";
var features = options.features || {};
for (var key in features) {
if (features.hasOwnProperty(key)) {
var value = features[key];
featuresString += key + "=" + value + ',';
}
}
if (featuresString.length > 0) {
featuresString = featuresString.substring(0, featuresString.length - 1);
}
return featuresString;
};
var pollWindow = function (initialize) {
if (initialize) {
_pollWindowShouldEmitClosed = true;
_pollWindowLastLocationEmitted = undefined;
}
if (!_openWindow || _openWindow.closed) {
if (_pollWindowShouldEmitClosed) {
emitEvent("close", false);
}
return;
}
try {
var canAccessLocation = _openWindow.location && _openWindow.location.hostname;
} catch (e) {
canAccessLocation = false;
}
if (canAccessLocation && _pollWindowLastLocationEmitted != _openWindow.location.href) {
emitEvent("location", _openWindow.location);
_pollWindowLastLocationEmitted = _openWindow.location.href;
}
_pollWindowTimeout = setTimeout(function () {
pollWindow();
}, options.pollWindowInterval);
};
this.isPopped = function () {
if (_openWindow && !_openWindow.closed) {
return true;
}
return false;
}
this.focus = function () {
if (_openWindow) {
_openWindow.focus();
}
}
this.pop = function () {
if (_pollWindowTimeout) {
clearTimeout(_pollWindowTimeout);
}
_openWindow = window.open(url, id, getFeatures());
pollWindow(true);
listenEvent("close", function () {
resetEventListeners();
});
return this;
};
this.on = function (eventName, callback) {
listenEvent(eventName, callback);
return this;
};
this.close = function () {
if (_pollWindowTimeout) {
clearTimeout(_pollWindowTimeout);
}
_pollWindowShouldEmitClosed = false;
_openWindow.close();
emitEvent("close", true);
return this;
};
}
var popups = [];
var popupOauth = function (url, options) {
var popup = new Popup(url, options);
popup.on("close", function () {
var popupIndex;
for (var i = 0; i < popups.length; i++) {
if (popups[i] == popup) {
popupIndex = i;
break;
}
}
popups.splice(popupIndex, 1);
});
popups.push(popup);
return popup;
};
popupOauth.getByUrl = function (url) {
var returnPopups = [];
for (var i=0; i < popups.length; i++) {
if (popups[i].url == url) {
returnPopups.push(popups[i]);
}
}
return returnPopups;
};
return popupOauth;
}));