-
Notifications
You must be signed in to change notification settings - Fork 0
/
DictController.js
166 lines (103 loc) · 3.2 KB
/
DictController.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* LAYOUT CONTROLLER THINGY */
var LayoutController = function(popId) {
this._popId = document.getElementById(popId);
//Used to store the nodes that need to be highlighted on hover
//Prob. doesn't need to be declared here, buuuttt I did
this.highlight = [];
}
LayoutController.prototype.traverseAllChildren = function(node) {
/* POSSIBLE OPTIMIZATIONS NEEDED:
1.) SKIP NON RELEVENT ELEMENTS (E.G. JAVASCRIPT)
*/
var next;
if (node.nodeType === 1) {
if (node = node.firstChild) {
do {
next = node.nextSibling;
this.traverseAllChildren(node);
} while(node=next);
}
}
else if (node.nodeType === 3) {
if (/[^\x00-\x80]+/.test(node.data)) {
console.log("should be here");
this.wrapInSpan(node);
}
}
}
LayoutController.prototype.wrapInSpan = function(textNode) {
var tmp = document.createElement('div');
tmp.innerHTML = textNode.data.replace(/[^\x00-\x80]/g, '<span class="cn" onmouseover="DIC_CONTROLLER.checkChinese(this)" onmouseout="LAYOUT.hidePop()">$&</span>');
while (tmp.firstChild) {
textNode.parentNode.insertBefore(tmp.firstChild,textNode);
}
textNode.parentNode.removeChild(textNode);
}
LayoutController.prototype.showPop = function(o) {
var lpos, tpos;
//For the record, I think this is a bad solution
this.highlight.reverse();
this.highlight.forEach(function(node) {
node.className = node.className + " CHINESE_HOVER_CLASS";
lpos = node.offsetLeft;
tpos = node.offsetTop + node.offsetHeight;
});
var text = "<span class='han'>" + o.han + " </span>";
text += "<span class='pinyin'> " + o.pinyin + "</span>";
text += "<br />";
text += "<span class='translation'>" + o.translation + "</span>";
this._popId.innerHTML = text;
this._popId.style.left = lpos;
this._popId.style.top = tpos;
this._popId.style.display = "block";
}
LayoutController.prototype.hidePop = function() {
this._popId.style.display = "none";
this._popId.innerHTML = "";
this.highlight.forEach(function(node) {
var reg = new RegExp('(\\s|^)'+"CHINESE_HOVER_CLASS"+'(\\s|$)');
node.className = node.className.replace(reg,'');
});
this.highlight = [];
}
LayoutController.prototype.pushToHighlight = function(node) {
this.highlight.push(node);
}
/* DICTIONARY CONTROLLER THINGY */
var DicController = function(dictionary) {
this._dic = dictionary._table;
this._pop = document.getElementById("pop");
}
DicController.prototype.contains = function(value) {
if (this._dic.hasOwnProperty(value))
{
return this._dic[value];
}
}
DicController.prototype.checkChinese = function(node) {
var han = tmp = tmp2 = pin = "";
while (node && node.className === "cn")
{
tmp += node.firstChild.data;
if (tmp2 = this.contains(tmp)) {
LAYOUT.pushToHighlight(node);
han += node.firstChild.data;
pin = tmp2;
node = node.nextSibling;
}
else {
break;
}
}
if (pin) {
LAYOUT.showPop(this.normalize(han, pin));
}
}
DicController.prototype.normalize = function(han, def) {
var ret = {};
var combined = def.split("|");
ret.han = han;
ret.pinyin = combined[0];
ret.translation = combined[1];
return ret;
}