forked from hunterlord/phaser-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputElement.ts
142 lines (110 loc) · 4.07 KB
/
InputElement.ts
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
module Fabrique {
export enum InputType {
text,
password,
number
}
export class InputElement {
private element: HTMLInputElement;
private callback: () => void;
private type: InputType;
private id: string;
private game: Phaser.Game;
public focusIn: Phaser.Signal = new Phaser.Signal();
public focusOut: Phaser.Signal = new Phaser.Signal();
constructor(game: Phaser.Game, id: string, type: InputType = InputType.text, value: string = '') {
this.id = id;
this.type = type;
this.game = game;
this.element = document.createElement('input');
this.element.id = id;
this.element.style.position = 'absolute';
this.element.style.top = (-100).toString() + 'px';
this.element.style.left = (-100).toString() + 'px';
this.element.value = this.value;
this.element.type = InputType[type];
this.element.addEventListener('focusin', (): void => {
this.focusIn.dispatch();
});
this.element.addEventListener('focusout', (): void => {
this.focusOut.dispatch()
});
document.body.appendChild(this.element);
}
public addKeyUpListener(callback: () => void): void {
this.callback = callback;
document.addEventListener('keyup', this.callback);
}
public removeEventListener(): void {
document.removeEventListener('keyup', this.callback);
}
public destroy() {
document.body.removeChild(this.element);
}
public setMax(max: string, min?: string) {
if (max === undefined) {
return;
}
if (this.type === InputType.text || this.type === InputType.password) {
this.element.maxLength = parseInt(max, 10);
} else if (this.type === InputType.number) {
this.element.max = max;
if (min === undefined) {
return;
}
this.element.min = min;
}
}
get value(): string {
return this.element.value;
}
set value(value: string) {
this.element.value = value;
}
public focus(): void {
this.element.focus();
if (!this.game.device.desktop && this.game.device.chrome) {
let originalWidth = window.innerWidth,
originalHeight = window.innerHeight;
let kbAppeared: boolean = false;
let interval: number = setInterval((): void => {
//console.log(originalWidth, window.innerWidth, originalHeight, window.innerHeight)
if (originalWidth > window.innerWidth || originalHeight > window.innerHeight) {
kbAppeared = true;
}
if (kbAppeared && originalWidth === window.innerWidth && originalHeight === window.innerHeight) {
this.focusOut.dispatch();
clearInterval(interval);
}
}, 50);
}
}
public blur(): void {
this.element.blur();
}
get hasSelection () {
if (this.type === InputType.number) {
return false;
}
return this.element.selectionStart !== this.element.selectionEnd;
}
get caretStart() {
return this.element.selectionEnd;
}
get caretEnd() {
return this.element.selectionStart;
}
public getCaretPosition() {
if (this.type === InputType.number) {
return -1;
}
return this.element.selectionStart;
}
public setCaretPosition(pos: number) {
if (this.type === InputType.number) {
return ;
}
this.element.setSelectionRange(pos, pos);
}
}
}