-
Notifications
You must be signed in to change notification settings - Fork 2
/
float.js
107 lines (94 loc) · 2.4 KB
/
float.js
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
function Float() {
this.oEl = null;
this.oSelected = null;
this.oReturnFocus = null;
this.onChange = null; // function(mItem)
this.create();
}
Float.prototype.create = function() {
this.oEl = document.createElement('div');
this.oEl.className = 'float';
this.oEl.style.display = 'none';
this.oEl.style.position = 'absolute';
document.body.appendChild(this.oEl);
var oFloat = this;
this.oEl.onkeydown = function(e) {
return oFloat.handleKeyDown(e);
}
this.oEl.onkeyup = function(e) {
return oFloat.handleKeyUp(e);
}
this.oEl.onblur = function() {
oFloat.hide();
}
}
Float.prototype.isActive = function() {
return this.oEl.style.display == 'block';
}
Float.prototype.focus = function(oReturnTo) {
this.oReturnFocus = oReturnTo;
this.oEl.focus();
if (!this.oSelected && this.oEl.firstChild) {
this.oSelected = this.oEl.firstChild;
this.oSelected.className = 'floatItemSelected';
}
}
Float.prototype.handleKeyUp = function(e) {
e = e || window.event;
var c = (is_ie) ? e.keyCode : e.which;
//log('float keyup ' + c);
switch (c) {
case 13:
if (this.oSelected && this.onChange)
this.onChange(this.oSelected.oData);
this.hide();
return false;
}
}
Float.prototype.handleKeyDown = function(e) {
e = e || window.event;
var c = (is_ie) ? e.keyCode : e.which;
//log('float keydown ' + c);
switch (c) {
case 38: // up
case 40: // down
if (this.oSelected) {
this.oSelected.className = 'floatItem';
if (c == 38) {
this.oSelected = (this.oSelected.previousSibling) ?
this.oSelected.previousSibling : this.oEl.lastChild;
} else {
this.oSelected = (this.oSelected.nextSibling) ?
this.oSelected.nextSibling : this.oEl.firstChild;
}
if (this.oSelected)
this.oSelected.className = 'floatItemSelected';
}
break;
case 27:
this.hide();
break;
}
}
Float.prototype.setup = function(aItems) {
this.oSelected = null;
while (this.oEl.firstChild)
this.oEl.removeChild(this.oEl.firstChild);
for (var sDesc in aItems) {
var oDiv = document.createElement('div');
this.oEl.appendChild(oDiv);
oDiv.className = 'floatItem';
oDiv.innerHTML = sDesc;
oDiv.oData = aItems[sDesc];
}
}
Float.prototype.show = function(x, y) {
this.oEl.style.left = x;
this.oEl.style.top = y;
this.oEl.style.display = 'block';
}
Float.prototype.hide = function() {
this.oEl.style.display = 'none';
if (this.oReturnFocus)
this.oReturnFocus.focus();
}