-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
212 lines (186 loc) · 6.6 KB
/
common.js
File metadata and controls
212 lines (186 loc) · 6.6 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
/*
* 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 for UI element.
*/
goog.provide('firebaseui.auth.ui.element');
goog.require('goog.dom');
goog.require('goog.dom.classlist');
goog.require('goog.dom.forms');
goog.require('goog.events.ActionHandler');
goog.require('goog.events.ActionHandler.EventType');
goog.require('goog.events.FocusHandler');
goog.require('goog.events.FocusHandler.EventType');
goog.require('goog.events.InputHandler');
goog.require('goog.events.InputHandler.EventType');
goog.require('goog.events.KeyCodes');
goog.require('goog.events.KeyHandler');
goog.require('goog.events.KeyHandler.EventType');
goog.require('goog.ui.Component');
/**
* Sets the valid state of the element.
* @param {Element} e The element.
* @param {boolean} valid Whether the element is valid or not.
*/
firebaseui.auth.ui.element.setValid = function(e, valid) {
// The parent textfield element, if applicable.
var textfield = goog.dom.getAncestorByClass(e, 'firebaseui-textfield');
if (valid) {
goog.dom.classlist.addRemove(
e, 'firebaseui-input-invalid', 'firebaseui-input');
if (textfield) {
goog.dom.classlist.remove(textfield, 'firebaseui-textfield-invalid');
}
} else {
goog.dom.classlist.addRemove(
e, 'firebaseui-input', 'firebaseui-input-invalid');
if (textfield) {
goog.dom.classlist.add(textfield, 'firebaseui-textfield-invalid');
}
}
};
/**
* @param {goog.ui.Component} component The component containing the element.
* @param {Element} e The input element.
* @param {function(this:SCOPE, EVENTOBJ)} cb Callback to invoke when the event
* is fired.
* @template SCOPE,EVENTOBJ
* @suppress {accessControls}
*/
firebaseui.auth.ui.element.listenForInputEvent = function(component, e, cb) {
var handler = new goog.events.InputHandler(e);
component.registerDisposable(handler);
component.getHandler().listen(
handler, goog.events.InputHandler.EventType.INPUT, cb);
};
/**
* @param {goog.ui.Component} component The component containing the element.
* @param {Element} e The input element.
* @param {function(this:SCOPE, EVENTOBJ)} cb Callback to invoke when the event
* is fired.
* @template SCOPE,EVENTOBJ
* @suppress {accessControls}
*/
firebaseui.auth.ui.element.listenForEnterEvent = function(component, e, cb) {
var handler = new goog.events.KeyHandler(e);
component.registerDisposable(handler);
component.getHandler().listen(
handler, goog.events.KeyHandler.EventType.KEY, function(e) {
if (e.keyCode == goog.events.KeyCodes.ENTER) {
// Stop event propagation and disable the default action since it
// could cause a form submission.
e.stopPropagation();
e.preventDefault();
cb(e);
}
});
};
/**
* @param {goog.ui.Component} component The component containing the element.
* @param {Element} e The input element.
* @param {function(this:SCOPE, EVENTOBJ)} cb Callback to invoke when the
* element is focused.
* @template SCOPE,EVENTOBJ
* @suppress {accessControls}
*/
firebaseui.auth.ui.element.listenForFocusInEvent = function(component, e, cb) {
var handler = new goog.events.FocusHandler(e);
component.registerDisposable(handler);
component.getHandler().listen(
handler, goog.events.FocusHandler.EventType.FOCUSIN, cb);
};
/**
* @param {goog.ui.Component} component The component containing the element.
* @param {Element} e The input element.
* @param {function(this:SCOPE, EVENTOBJ)} cb Callback to invoke when the
* element is blurred.
* @template SCOPE,EVENTOBJ
* @suppress {accessControls}
*/
firebaseui.auth.ui.element.listenForFocusOutEvent = function(component, e, cb) {
var handler = new goog.events.FocusHandler(e);
component.registerDisposable(handler);
component.getHandler().listen(
handler, goog.events.FocusHandler.EventType.FOCUSOUT, cb);
};
/**
* @param {goog.ui.Component} component The component containing the element.
* @param {Element} e The actionable element.
* @param {function(this:SCOPE, EVENTOBJ)} cb Callback to invoke when the event
* is fired.
* @template SCOPE,EVENTOBJ
* @suppress {accessControls}
*/
firebaseui.auth.ui.element.listenForActionEvent = function(component, e, cb) {
var handler = new goog.events.ActionHandler(e);
component.registerDisposable(handler);
component.getHandler().listen(
handler, goog.events.ActionHandler.EventType.ACTION, function(e) {
// Stop event propagation and disable the default action since it
// could cause a form submission.
e.stopPropagation();
e.preventDefault();
cb(e);
});
};
/**
* @param {Element} e The input element.
* @return {?string} The text in the input.
*/
firebaseui.auth.ui.element.getInputValue = function(e) {
return /** @type {?string} */ (goog.dom.forms.getValue(e));
};
/**
* Hides the element.
* @param {Element} e The element to hide.
*/
firebaseui.auth.ui.element.hide = function(e) {
goog.dom.classlist.add(e, 'firebaseui-hidden');
};
/**
* Shows the element.
* @param {Element} e The element to show.
* @param {string=} opt_text The text to show.
*/
firebaseui.auth.ui.element.show = function(e, opt_text) {
if (opt_text) {
goog.dom.setTextContent(e, opt_text);
}
goog.dom.classlist.remove(e, 'firebaseui-hidden');
};
/**
* Checks if the element is shown or not. The element is considered shown if it
* doesn't have 'firebaseui-hidden' class or 'display:none' style.
* @param {Element} e The element to check.
* @return {boolean} True if the element is shown.
*/
firebaseui.auth.ui.element.isShown = function(e) {
return !goog.dom.classlist.contains(e, 'firebaseui-hidden') &&
e.style.display != 'none';
};
/**
* Checks if the element is deeply hidden because it or one of its ancestors is
* hidden.
* @param {Element} e The element to check.
* @return {boolean} True if the element is hidden.
*/
firebaseui.auth.ui.element.isDeeplyHidden = function(e) {
while (e) {
if (!firebaseui.auth.ui.element.isShown(e)) {
return true;
}
e = e.parentElement;
}
return false;
};