forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.ts
More file actions
84 lines (70 loc) · 1.73 KB
/
form.ts
File metadata and controls
84 lines (70 loc) · 1.73 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
import {Injectable} from 'angular2/core';
/**
* @private
*/
@Injectable()
export class Form {
private _blur: HTMLElement;
private _focused = null;
private _ids: number = -1;
private _inputs: Array<any> = [];
constructor() {
this.focusCtrl(document);
}
register(input) {
this._inputs.push(input);
}
deregister(input) {
let index = this._inputs.indexOf(input);
if (index > -1) {
this._inputs.splice(index, 1);
}
if (input === this._focused) {
this._focused = null;
}
}
focusCtrl(document) {
// raw DOM fun
let focusCtrl = document.createElement('focus-ctrl');
focusCtrl.setAttribute('aria-hidden', true);
this._blur = document.createElement('button');
this._blur.tabIndex = -1;
focusCtrl.appendChild(this._blur);
document.body.appendChild(focusCtrl);
}
focusOut() {
console.debug('focusOut');
let activeElement: any = document.activeElement;
if (activeElement) {
activeElement.blur();
}
this._blur.focus();
}
setAsFocused(input) {
this._focused = input;
}
/**
* Focuses the next input element, if it exists.
*/
tabFocus(currentInput) {
let index = this._inputs.indexOf(currentInput);
if (index > -1 && (index + 1) < this._inputs.length) {
let nextInput = this._inputs[index + 1];
if (nextInput !== this._focused) {
console.debug('tabFocus, next');
return nextInput.initFocus();
}
}
index = this._inputs.indexOf(this._focused);
if (index > 0) {
let previousInput = this._inputs[index - 1];
if (previousInput) {
console.debug('tabFocus, previous');
previousInput.initFocus();
}
}
}
nextId() {
return ++this._ids;
}
}