-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
319 lines (276 loc) · 9.31 KB
/
util.js
File metadata and controls
319 lines (276 loc) · 9.31 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
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
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Common utilities.
*/
goog.provide('firebaseui.auth.util');
goog.require('goog.Promise');
goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.html.SafeUrl');
goog.require('goog.userAgent');
goog.require('goog.window');
/**
* Navigates the current page to the given URL.
* It simply wraps the window.location.assign and is meant for testing since
* some browsers don't allow overwriting of the native object.
*
* @param {string} url The target URL.
* @param {?Window=} opt_win Optional window whose location is to be reassigned.
*/
firebaseui.auth.util.goTo = function(url, opt_win) {
var win = opt_win || window;
// Sanitize URL before redirect.
win.location.assign(firebaseui.auth.util.sanitizeUrl(url));
};
/**
* @param {string} url The plain unsafe URL.
* @return {string} The sanitized safe version of the provided URL.
*/
firebaseui.auth.util.sanitizeUrl = function(url) {
return goog.html.SafeUrl.unwrap(goog.html.SafeUrl.sanitize(url));
};
/** @return {?string} The current URL scheme. */
firebaseui.auth.util.getScheme = function() {
return window.location && window.location.protocol;
};
/** @return {boolean} Whether Cordova InAppBrowser plugin is installed. */
firebaseui.auth.util.isCordovaInAppBrowserInstalled = function() {
return !!(window['cordova'] && window['cordova']['InAppBrowser']);
};
/** @return {boolean} Whether current scheme is HTTP or HTTPS. */
firebaseui.auth.util.isHttpOrHttps = function() {
return firebaseui.auth.util.getScheme() === 'http:' ||
firebaseui.auth.util.getScheme() === 'https:';
};
/**
* Navigates to the previous page.
* It simply wraps the window.history.back and is meant for testing since
* some browsers don't allow to overwrite the native object.
*/
firebaseui.auth.util.goBack = function() {
window.history.back();
};
/**
* Navigates the opener page (parent window) to the given URL.
* It simply wraps the window.opener.location.assign and is meant for testing
* since some browsers don't allow to overwrite the native object.
*
* @param {string} url The target URL.
* @param {?Window=} opt_win Optional window whose parent's location is to
* be reassigned.
*/
firebaseui.auth.util.openerGoTo = function(url, opt_win) {
var win = opt_win || window;
// Sanitize URL before redirect.
win.opener.location.assign(firebaseui.auth.util.sanitizeUrl(url));
};
/**
* Detects whether there is an opener (parent) window whose location is
* reassignable.
*
* @return {boolean} The opener window.
*/
firebaseui.auth.util.hasOpener = function() {
// TODO: consider completely removing this ability and just always redirecting
// the current page.
try {
// Some browsers do not allow reassignment of the location of the opener if
// the url is a different origin than the current one. Confirm hostname and
// protocol match between current page and opener.
return !!(window.opener &&
window.opener.location &&
window.opener.location.assign &&
window.opener.location.hostname === window.location.hostname &&
window.opener.location.protocol === window.location.protocol);
} catch (e) {}
return false;
};
/**
* Loads the URL into the window with the specified name. If the name doesn't
* exist, then a new window is opened.
* It simply wraps the window.open and is meant for testing since some browsers
* don't allow overwriting of the native object.
*
* @param {string} url The target URL.
* @param {string} windowName The window name.
* @param {?number=} opt_width width of the popup
* @param {?number=} opt_height height of the popup
* @param {?Window=} opt_parentWin Parent window that should be used to open the
* new window.
*/
firebaseui.auth.util.open =
function(url, windowName, opt_width, opt_height, opt_parentWin) {
var options = {
'target': windowName
};
if (opt_width) {
options['width'] = opt_width;
}
if (opt_height) {
options['height'] = opt_height;
}
goog.window.open(url, options, opt_parentWin);
};
/**
* Detects mobile browser.
*
* @return {boolean} True if the browser is on mobile.
*/
firebaseui.auth.util.isMobileBrowser = function() {
return goog.userAgent.MOBILE;
};
/**
* Detects CORS support.
*
* @return {boolean} True if the browser supports CORS.
*/
firebaseui.auth.util.supportsCors = function() {
// Among all supported browsers, onluy IE8 and IE9 don't support CORS.
return !goog.userAgent.IE || // Not IE.
!goog.userAgent.DOCUMENT_MODE || // No document mode == IE Edge
goog.userAgent.DOCUMENT_MODE > 9;
};
/**
* The default timeout for browser redirecting in mobile flow.
*
* @const {number}
* @private
*/
firebaseui.auth.util.BROWSER_REDIRECT_TIMEOUT_ = 500;
/**
* Closes a window.
* @param {Window} window The window to close.
*/
firebaseui.auth.util.close = function(window) {
window.close();
};
/**
* Opens a popup window.
* @param {string=} opt_url initial URL of the popup window
* @param {string=} opt_name title of the popup
* @param {number=} opt_width width of the popup
* @param {number=} opt_height height of the popup
*/
firebaseui.auth.util.popup =
function(opt_url, opt_name, opt_width, opt_height) {
var width = opt_width || 500;
var height = opt_height || 600;
var top = (window.screen.availHeight - height) / 2;
var left = (window.screen.availWidth - width) / 2;
var options = {
'width': width,
'height': height,
'top': top > 0 ? top : 0,
'left': left > 0 ? left : 0,
'location': true,
'resizable': true,
'statusbar': true,
'toolbar': false
};
if (opt_name) {
options['target'] = opt_name;
}
goog.window.popup(opt_url || 'about:blank', options);
};
/**
* Gets the element in the current document by the query selector.
* If an Element is passed in, it is returned.
* An `Error` is thrown if the element can not be found.
*
* @param {string|Element} element The element or the query selector.
* @param {string=} opt_notFoundDesc Error description when element not
* found.
* @return {Element} The HTML element.
*/
firebaseui.auth.util.getElement = function(element, opt_notFoundDesc) {
element = goog.dom.isElement(element) ?
element : document.querySelector(String(element));
if (element == null) {
// If more detailed description provided it, use it instead of default
// description.
var notFoundDesc = opt_notFoundDesc || 'Cannot find element.';
throw new Error(notFoundDesc);
}
return /** @type {Element} */ (element);
};
/**
* Replaces the current history state with the provided one.
*
* @param {!Object} state The new history state.
* @param {string} title The associated document title.
* @param {string} url The associated URL.
*/
firebaseui.auth.util.replaceHistoryState = function(state, title, url) {
// History API should be supported by all browser in our support matrix.
if (goog.global.history &&
goog.global.history.replaceState) {
goog.global.history.replaceState(state, title, url);
}
};
/**
* @return {string} The current location URL.
*/
firebaseui.auth.util.getCurrentUrl = function() {
return window.location.href;
};
/**
* @return {string} The country code in canonical Unicode format.
*/
firebaseui.auth.util.getUnicodeLocale = function() {
return goog.LOCALE.replace(/_/g, '-');
};
/**
* @return {!goog.Promise<void>} A promise that resolves when DOM is ready.
*/
firebaseui.auth.util.onDomReady = function() {
var resolver = null;
return new goog.Promise(function(resolve, reject) {
var doc = goog.global.document;
// If document already loaded, resolve immediately.
if (doc.readyState == 'complete') {
resolve();
} else {
// Document not ready, wait for load before resolving.
// Save resolver, so we can remove listener in case it was externally
// cancelled.
resolver = function() {
resolve();
};
goog.events.listenOnce(window, goog.events.EventType.LOAD, resolver);
}
}).thenCatch(function(error) {
// In case this promise was cancelled, make sure it unlistens to load.
goog.events.unlisten(window, goog.events.EventType.LOAD, resolver);
throw error;
});
};
/**
* Generates a random alpha numeric string.
* @param {number} numOfChars The number of random characters within the string.
* @return {string} A string with a specific number of random characters.
*/
firebaseui.auth.util.generateRandomAlphaNumericString = function(numOfChars) {
var chars = [];
var allowedChars =
'1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
while (numOfChars > 0) {
chars.push(
allowedChars.charAt(
Math.floor(Math.random() * allowedChars.length)));
numOfChars--;
}
return chars.join('');
};