1 | import { EventEmitter, Injectable, NgZone } from '@angular/core';
|
2 | import { Config } from '../config/config';
|
3 | import { DomController } from './dom-controller';
|
4 | import { isTextInput } from '../util/dom';
|
5 | import { KEY_TAB } from './key';
|
6 | import { Platform } from './platform';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var Keyboard = (function () {
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function Keyboard(config, _plt, _zone, _dom) {
|
30 | this._plt = _plt;
|
31 | this._zone = _zone;
|
32 | this._dom = _dom;
|
33 | this.willShow = new EventEmitter();
|
34 | this.willHide = new EventEmitter();
|
35 | this.didShow = new EventEmitter();
|
36 | this.didHide = new EventEmitter();
|
37 | this.eventsAvailable = false;
|
38 | this.focusOutline(config.get('focusOutline'));
|
39 | var win = _plt.win();
|
40 | if (config.getBoolean('keyboardResizes', false)) {
|
41 | this.listenV2(win);
|
42 | }
|
43 | else {
|
44 | this.listenV1(win);
|
45 | }
|
46 | }
|
47 | |
48 |
|
49 |
|
50 |
|
51 | Keyboard.prototype.listenV2 = function (win) {
|
52 | var _this = this;
|
53 | var platform = this._plt;
|
54 | platform.registerListener(win, 'keyboardWillShow', function () {
|
55 | _this._zone.run(function () {
|
56 | _this.willShow.emit();
|
57 | });
|
58 | }, { zone: false, passive: true });
|
59 | platform.registerListener(win, 'keyboardWillHide', function () {
|
60 | _this._zone.run(function () {
|
61 | _this.willHide.emit();
|
62 | });
|
63 | }, { zone: false, passive: true });
|
64 | platform.registerListener(win, 'keyboardDidShow', function () {
|
65 | _this._zone.run(function () {
|
66 | _this.didShow.emit();
|
67 | });
|
68 | }, { zone: false, passive: true });
|
69 | platform.registerListener(win, 'keyboardDidHide', function () {
|
70 | _this._zone.run(function () {
|
71 | _this.didHide.emit();
|
72 | });
|
73 | }, { zone: false, passive: true });
|
74 | this.eventsAvailable = true;
|
75 | };
|
76 | |
77 |
|
78 |
|
79 |
|
80 | Keyboard.prototype.listenV1 = function (win) {
|
81 | var _this = this;
|
82 | var platform = this._plt;
|
83 | platform.registerListener(win, 'native.keyboardhide', function () {
|
84 | _this.blurActiveInput(true);
|
85 | }, { zone: false, passive: true });
|
86 | platform.registerListener(win, 'native.keyboardshow', function () {
|
87 | _this.blurActiveInput(false);
|
88 | }, { zone: false, passive: true });
|
89 | };
|
90 | |
91 |
|
92 |
|
93 |
|
94 | Keyboard.prototype.blurActiveInput = function (shouldBlur) {
|
95 | var _this = this;
|
96 | var platform = this._plt;
|
97 | platform.cancelTimeout(this._tmr);
|
98 | if (shouldBlur) {
|
99 | this._tmr = platform.timeout(function () {
|
100 |
|
101 |
|
102 |
|
103 | if (_this.isOpen()) {
|
104 | platform.focusOutActiveElement();
|
105 | }
|
106 | }, 80);
|
107 | }
|
108 | };
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | Keyboard.prototype.isOpen = function () {
|
127 | return this.hasFocusedTextInput();
|
128 | };
|
129 | |
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | Keyboard.prototype.onClose = function (callback, pollingInternval, pollingChecksMax) {
|
150 | if (pollingInternval === void 0) { pollingInternval = KEYBOARD_CLOSE_POLLING; }
|
151 | if (pollingChecksMax === void 0) { pollingChecksMax = KEYBOARD_POLLING_CHECKS_MAX; }
|
152 | (void 0) ;
|
153 | var self = this;
|
154 | var checks = 0;
|
155 | var promise = null;
|
156 | if (!callback) {
|
157 |
|
158 | promise = new Promise(function (resolve) { callback = resolve; });
|
159 | }
|
160 | |
161 |
|
162 |
|
163 | function checkKeyboard() {
|
164 | (void 0) ;
|
165 | if (!self.isOpen() || checks > pollingChecksMax) {
|
166 | self._plt.timeout(function () {
|
167 | self._zone.run(function () {
|
168 | (void 0) ;
|
169 | callback();
|
170 | });
|
171 | }, 400);
|
172 | }
|
173 | else {
|
174 | self._plt.timeout(checkKeyboard, pollingInternval);
|
175 | }
|
176 | checks++;
|
177 | }
|
178 | self._plt.timeout(checkKeyboard, pollingInternval);
|
179 | return promise;
|
180 | };
|
181 | |
182 |
|
183 |
|
184 |
|
185 | Keyboard.prototype.close = function () {
|
186 | var _this = this;
|
187 | this._dom.read(function () {
|
188 | if (_this.isOpen()) {
|
189 |
|
190 | (void 0) ;
|
191 | _this._dom.write(function () {
|
192 | _this._plt.focusOutActiveElement();
|
193 | });
|
194 | }
|
195 | });
|
196 | };
|
197 | |
198 |
|
199 |
|
200 |
|
201 |
|
202 | Keyboard.prototype.focusOutline = function (setting) {
|
203 | |
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | var self = this;
|
215 | var platform = self._plt;
|
216 | var doc = platform.doc();
|
217 | var isKeyInputEnabled = false;
|
218 | var unRegMouse;
|
219 | var unRegTouch;
|
220 | var evOpts = { passive: true, zone: false };
|
221 | |
222 |
|
223 |
|
224 | function cssClass() {
|
225 | self._dom.write(function () {
|
226 | ((platform.doc().body.classList))[isKeyInputEnabled ? 'add' : 'remove']('focus-outline');
|
227 | });
|
228 | }
|
229 | if (setting === true) {
|
230 | isKeyInputEnabled = true;
|
231 | return cssClass();
|
232 | }
|
233 | else if (setting === false) {
|
234 | return;
|
235 | }
|
236 | |
237 |
|
238 |
|
239 |
|
240 | function keyDown(ev) {
|
241 | if (!isKeyInputEnabled && ev.keyCode === KEY_TAB) {
|
242 | isKeyInputEnabled = true;
|
243 | enableKeyInput();
|
244 | }
|
245 | }
|
246 | |
247 |
|
248 |
|
249 | function pointerDown() {
|
250 | isKeyInputEnabled = false;
|
251 | enableKeyInput();
|
252 | }
|
253 | |
254 |
|
255 |
|
256 | function enableKeyInput() {
|
257 | cssClass();
|
258 | unRegMouse && unRegMouse();
|
259 | unRegTouch && unRegTouch();
|
260 | if (isKeyInputEnabled) {
|
261 |
|
262 | unRegMouse = platform.registerListener(doc, 'mousedown', pointerDown, evOpts);
|
263 | unRegTouch = platform.registerListener(doc, 'touchstart', pointerDown, evOpts);
|
264 | }
|
265 | }
|
266 |
|
267 | platform.registerListener(platform.doc(), 'keydown', keyDown, evOpts);
|
268 | };
|
269 | |
270 |
|
271 |
|
272 | Keyboard.prototype.hasFocusedTextInput = function () {
|
273 | var activeEle = this._plt.getActiveElement();
|
274 | if (isTextInput(activeEle)) {
|
275 | return (activeEle.parentElement.querySelector(':focus') === activeEle);
|
276 | }
|
277 | return false;
|
278 | };
|
279 | return Keyboard;
|
280 | }());
|
281 | export { Keyboard };
|
282 | Keyboard.decorators = [
|
283 | { type: Injectable },
|
284 | ];
|
285 |
|
286 |
|
287 |
|
288 | Keyboard.ctorParameters = function () { return [
|
289 | { type: Config, },
|
290 | { type: Platform, },
|
291 | { type: NgZone, },
|
292 | { type: DomController, },
|
293 | ]; };
|
294 | function Keyboard_tsickle_Closure_declarations() {
|
295 |
|
296 | Keyboard.decorators;
|
297 | |
298 |
|
299 |
|
300 |
|
301 | Keyboard.ctorParameters;
|
302 |
|
303 | Keyboard.prototype._tmr;
|
304 |
|
305 | Keyboard.prototype.willShow;
|
306 |
|
307 | Keyboard.prototype.willHide;
|
308 |
|
309 | Keyboard.prototype.didShow;
|
310 |
|
311 | Keyboard.prototype.didHide;
|
312 |
|
313 | Keyboard.prototype.eventsAvailable;
|
314 |
|
315 | Keyboard.prototype._plt;
|
316 |
|
317 | Keyboard.prototype._zone;
|
318 |
|
319 | Keyboard.prototype._dom;
|
320 | }
|
321 | var KEYBOARD_CLOSE_POLLING = 150;
|
322 | var KEYBOARD_POLLING_CHECKS_MAX = 100;
|
323 |
|
\ | No newline at end of file |