-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
295 lines (253 loc) · 7.59 KB
/
app.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
var learn = getDataLearn();
var lessons = getDataLesson();
var oldList = [];
var max = learn.length;
var colourMap = {prep:"info",conj:"secondary",adj:"primary",adv:"danger",verb:"warning",noun:"success",other:"dark"};
var enableLog = false;
var numberWord = 15;
var isVocaCkb = false;
var isStcCkb = true;
var isRandomCkb = true;
var isAnswerCkb = false;
$(function () {
genTableContent();
setChecked("isVocaCkb",isVocaCkb);
setChecked("isStcCkb",isStcCkb);
setChecked("isRandomCkb",isRandomCkb);
setChecked("isAnswerCkb",isAnswerCkb);
getWord();
});
function allLesson() {
setChecked("isRandomCkb",true);
getWord();
}
function getVocaCkb(){
return getCheckBox("isVocaCkb");
}
function getStcCkb(){
return getCheckBox("isStcCkb");
}
function getRandomCkb(){
return getCheckBox("isRandomCkb");
}
function getAnswerCkb(){
return getCheckBox("isAnswerCkb");
}
function getCheckBox(id){
return $("#"+id).is(":checked");
}
function onChangeCheckBox(id) {
window[id] = $("#"+id).is(":checked");
}
function setChecked(id,value) {
$("#"+id).prop("checked", value);
window[id] = value;
}
function genTableContent(){
var tableContent = $('#tableContent');
for(var code in lessons){
tableContent.append(getHtmlTableContentTag(code,lessons[code]));
}
}
function getHtmlTableContentTag(code,name){
var query = "'less="+code+"'";
return '<button type="button" class="btn btn-primary mb-1 ml-1" onclick="getWord('+query+')">'+name+'</button>'
}
function getLearnProperty(data,prop,less){
return prop === "less" ? less[data[prop]] : data[prop];
}
function genTag(data,less){
logD("genTag","data",data);
switch(data.type){
case "stc": return genSentence(data,less);
case "voca": return genVoca(data,less)
}
}
function genVoca(data,less){
var format = {primary:["en"],title:["less"],content:["vi","note"],small:["kind2","less"]};
data.kind2=data.kind2.toUpperCase();
var dataFormat = formatData(format,data,less);
return getHtmlVocaTag(dataFormat);
}
function genSentence(data,less){
var format = {primary:["vi"],title:["less"],content:["en"],small:["less"]};
if(getAnswerCkb()){
format.small.push("en");
}
var dataFormat = formatData(format,data,less);
return getHtmlVocaTag(dataFormat);
}
function formatData(format,data,less){
logD("formatData","format",format);
var vocaKind = colourMap.hasOwnProperty(data["kind1"]) ? data["kind1"] : "other";
var colourValues = Object.values(colourMap);
var colour = data["type"] === "stc" ? colourValues[getRandom(colourValues.length)] : colourMap[vocaKind];
var dataTemp = {colour:colour,title:'',content:'',primary:'',small:''};
var join;
for (var property in format) {
join = [];
join.push(getLearnProperty(data,format[property][0],less));
for (var i = 1; i < format[property].length; i++){
var dt = getLearnProperty(data,format[property][i],less);
if (dt != null && dt.length > 0){
join.push(getLearnProperty(data,format[property][i],less));
}
}
dataTemp[property] = join;
}
logD("formatData","dataTemp",dataTemp);
return dataTemp;
}
var separate = " - ";
function getHtmlVocaTag(data){
return '<div class="col-12 alert alert-'+data.colour+'" data-toggle="popover" data-placement="top" data-trigger="hover" title="'+data.title.join(separate)+'" data-placement="bottom" data-content="'+data.content.join(separate)+'"><lead>'+data.primary.join(separate)+'</lead>' + gentHtmlSmall(data.small) + '</div>'
}
function gentHtmlSmall(data) {
var html = "";
for(var i in data){
if(data[i] !== ""){
html += "<br/><small>"+data[i]+"</small>";
}
}
return html;
}
function defaultIfError(value,vDefault) {
return value === undefined || value === "" ? vDefault : value;
}
function getWord(queries){
var fullQuery;
var queryWordType = ";type=";
if(getStcCkb()){
queryWordType += ",stc";
}
if(getVocaCkb()){
queryWordType += ",voca";
}
fullQuery = defaultIfError(queries,"") + queryWordType;
var vocaMain = $('#main');
vocaMain.empty();
var word;
var number = numberWord;
var isRandomWord = getRandomCkb();
for(var i = 0; i < max; i++){
logD("======="+i+"=======");
if(isRandomWord){
word = learn[getRandomNotCoincide(oldList,max,maxHandle)];
} else {
word = learn[i];
}
if (filter(word,fullQuery)) {
vocaMain.append(genTag(word, lessons));
if (isRandomWord && --number === 0) break;
}
}
$('[data-toggle="popover"]').popover();
logD("==============================================================");
}
function maxHandle(oldList,max){
if(oldList.length>=max) {
logD("maxHandle","max",max);
logD("maxHandle","oldList.length",oldList.length);
logD("maxHandle","oldList",oldList);
emptyArr(oldList);
}
}
// function logE(){
// if(enableLog){
// console.error(getGenMessLog(arguments));
// }
// }
//
// function logW(){
// if(enableLog){
// console.warn(getGenMessLog(arguments));
// }
// }
function logD(){
if(enableLog){
console.debug(getGenMessLog(arguments));
}
}
// function log(){
// if(enableLog){
// console.log(getGenMessLog(arguments));
// }
// }
function getGenMessLog(messArr) {
var message = messArr[0];
for(var i = 1; i < messArr.length; i++){
message += " : " + getObjectJson(messArr[i]);
}
return message;
}
function getObjectJson(data) {
return data instanceof Object ? JSON.stringify(data) : data;
}
function emptyArr(arr){
logD("emptyArr","arr.length",arr.length);
while(arr.length>0){
arr.pop();
}
logD("emptyArr","arr.length",arr.length)
}
function filter(data, query) {
if (query === null) return true;
var subQueries = query.split(";");
var kv, arrValue;
for (var i in subQueries) {
if (subQueries[i] !== "") {
kv = subQueries[i].split("=");
arrValue = ("," + kv[1]).split(",");
if (!arrValue.includes(data[kv[0]])) {
return false;
}
}
}
return true;
}
function getRandomNotCoincide(oldList,max,maxHandle){
logD("getRandomNotCoincide","oldList.length",oldList.length);
if(maxHandle !== undefined){
var handledValue = maxHandle(oldList,max);
if(!isNaN(handledValue)) return handledValue;
}else {
if(oldList.length>=max)emptyArr(oldList);
}
var randomNumber;
for(var i = 0; i < max; i++){
randomNumber = getRandom(max);
if(!oldList.includes(randomNumber)){
oldList.push(randomNumber);
logD("getRandomNotCoincide","randomNumber",randomNumber);
return randomNumber;
}
}
logD("getRandomNotCoincide","return-default",1);
return 1;
}
//get a random number, which is smaller than max
function getRandom(max){
return Math.floor((Math.random() * max));
}
// function httpGet(url) {
// var req = new XMLHttpRequest();
//
// req.addEventListener('load', onLoad);
// req.addEventListener('error', onFail);
// req.addEventListener('abort', onFail);
//
// req.open('GET', url);
// req.send();
//
// function onLoad(event) {
// if (req.status >= 400) {
// onFail(event);
// } else {
// learn = JSON.parse(this.responseText);
// }
// }
//
// function onFail(event) {
// callback(new Error('...'));
// }
// }