-
Notifications
You must be signed in to change notification settings - Fork 2
/
translation_box.js
107 lines (97 loc) · 2.88 KB
/
translation_box.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 TranslationBox() {
this.div = document.createElement('div');
this.opacity = 0;
this.enabled = false;
with (this.div.style) {
zIndex = 2147483647;
position = 'absolute';
background = '#000000';
color = '#ffffff';
fontFamily = 'Hiragino Mincho Pro';
fontSize = '11px';
opacity = '0';
textAlign = 'left';
display = 'block';
borderRadius = '5px';
lineHeight = 'normal';
webkitFontSmoothing = 'auto';
}
document.body.appendChild(this.div);
}
TranslationBox.prototype.Enabled = function() {
return this.enabled;
};
TranslationBox.prototype.SetEnabled = function(enabled) {
this.enabled = enabled;
};
TranslationBox.prototype.Fadein = function() {
var self = this;
clearInterval(self.fadeinSt);
clearInterval(self.fadeoutSt);
self.fadeinSt = setInterval(function() {
self.div.style.opacity = self.opacity / 100;
if (self.opacity == 75) {
clearInterval(self.fadeinSt);
} else {
self.opacity += 5;
}
}, 10);
};
TranslationBox.prototype.Fadeout = function() {
var self = this;
clearInterval(self.fadeoutSt);
clearInterval(self.fadeinSt);
self.fadeoutSt = setInterval(function() {
self.div.style.opacity = self.opacity / 100;
if (self.opacity == 0) {
self.div.innerHTML = '';
clearInterval(self.fadeoutSt);
} else {
self.opacity -= 5;
}
}, 10);
};
TranslationBox.prototype.SetContent = function(results) {
this.content = [];
for (var i = 0; i < results.length; ++i) {
var key = results[i].originalKey || results[i].key;
var value = results[i].value;
this.content.push({word: key, translation: value});
}
this.div.innerHTML = "";
for (var i = 0; i < this.content.length; ++i) {
var c = this.content[i];
this.div.innerHTML +=
('<span style="font-size:14px;">' + (i + 1) + '. ' + c.word +
'</span>\n<div style="margin:5px;">' + c.translation + '</div>').
replace(/\n/g, '<br />');
}
};
TranslationBox.prototype.GetContent = function() {
return this.content;
};
TranslationBox.prototype.SetLocation = function(mouse) {
this.mouse = mouse;
this.AdjustLocation();
};
TranslationBox.prototype.AdjustLocation = function() {
// Redraw translation box.
var box_width = Math.min(400, window.innerWidth);
var box_left = Math.min(window.innerWidth - box_width, this.mouse.clientX) +
this.mouse.pageX - this.mouse.clientX + 2;
var box_top = this.mouse.pageY + 10;
with (this.div.style) {
width = box_width + 'px';
left = box_left + 'px';
top = box_top + 'px';
}
// Adjust when bottom of the translation box is out of the window.
var bcr = this.div.getBoundingClientRect();
with (this.div.style) {
if (bcr.height >= window.innerHeight) {
top = window.pageYOffset + 'px';
} else if (bcr.bottom > window.innerHeight) {
top = (window.pageYOffset + window.innerHeight - bcr.height) + 'px';
}
}
};