forked from firebase/firebaseui-web
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.js
More file actions
195 lines (166 loc) · 5.42 KB
/
Copy pathutil.js
File metadata and controls
195 lines (166 loc) · 5.42 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
/*
* 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.dom');
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.
*/
firebaseui.auth.util.goTo = function(url) {
window.location.assign(url);
};
/** @return {?string} The current URL scheme. */
firebaseui.auth.util.getScheme = function() {
return window.location && window.location.protocol;
};
/** @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.
*/
firebaseui.auth.util.openerGoTo = function(url) {
window.opener.location.assign(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;
};
/**
* 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 {@code 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);
};
/**
* @return {string} The current location URL.
*/
firebaseui.auth.util.getCurrentUrl = function() {
return window.location.href;
};