-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
420 lines (382 loc) · 12.5 KB
/
main.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
console.log("running main.js");
var db="jiangkangyur";
var name="head";
var uti="1.1a";
var timer1,timer2,tf1,tf2;
var toputi,bottomuti;
var prevbatch=0;
var batchstart=0;
var BATCHSIZE=30;
var searchresult;
var E=null;
var fetched=false;
var kdbArray=["jiangkangyur","tengyur","tibetan-masterpiece"];
var changeKdb=function(kdb){
db=kdbArray[kdb];
//$("#navbar").aria-expanded=false;
reloadToc(function(){
fetchText(20);
});
search();
}
var onSelect=function(e,treenode,seq,toc){
console.log("fetching by vpos",treenode.vpos);
fetchText(treenode.vpos);
/*console.log(arguments)*/
}
var onHitClick=function(e,treenode,seq,toc){
console.log("onHitClick:" + treenode.firstvpos);
fetchText(treenode.firstvpos);
}
var reloadBreadcrumb=function(vpos){
displaybreadcrumb(vpos);
}
var reloadToc=function(cb){
ksa.toc({db:db,q:tf2},function(err,data){
var conv=null;
if(toWylie)conv=wylie.toWylie;
ReactDOM.render(
E(
ksana2015treetoc.Component,
{toc:data.toc,hits:data.vhits,treename:db
,opened:openicon
,closed:closeicon
,nodeicons:nodeicons
,onSelect:onSelect
,conv:conv
,captionClass:"sutra"
,onHitClick:onHitClick}
),
document.getElementById("tree")
);
cb();
});
}
var showtotal=function(total){
document.getElementById("totalfound").innerHTML=total;
}
var showprevbatch=function(){
console.log("showprevbatch match count",searchresult.length)
if(tf2){//有全文摘要
if(prevbatch-BATCHSIZE>=0){//如果有上一頁內容
tf2Batch(prevbatch-BATCHSIZE);
}
}
else{//無全文,只列目錄
displaytitles(searchresult);
};
updateControls();
}
var showbatch=function(){
console.log("showbatch match count",searchresult.length)
if(tf2){//有全文摘要
if((searchresult.length-batchstart)>0){//如果下一頁內容
tf2Batch(batchstart);
/*
var uti=[];
for (var i=batchstart;i<searchresult.length;i++) {
uti.push(searchresult[i].uti);
if (uti.length>=BATCHSIZE) break;
}
ksa.fetch({db:db,q:tf2,uti:uti},function(err,res){
displayresult(res);
});
prevbatch=batchstart;
batchstart+=uti.length;*/
}
}
else{//無全文,只列目錄
displaytitles(searchresult);
};
updateControls();
}
var tf2Batch=function(batch){
console.log("batch:"+batch);
var uti=[];
for (var i=batch;i<searchresult.length;i++) {
uti.push(searchresult[i].uti);
if (uti.length>=BATCHSIZE) break;
}
ksa.fetch({db:db,q:tf2,uti:uti},function(err,res){
displayresult(res);
});
prevbatch=batch;
console.log("prevbatch:"+prevbatch);
batchstart=batch+uti.length;
}
var updateControls=function(){
console.log("left : " + (searchresult.length-batchstart));
document.getElementById("btnnext").style.visibility=(tf2 && searchresult.length>BATCHSIZE && (searchresult.length-batchstart)>0)?'visible':'hidden';
document.getElementById("btnprev").style.visibility=(tf2 && searchresult.length>BATCHSIZE && (prevbatch-BATCHSIZE)>=0)?'visible':'hidden';
}
var filter=function(ranges,regex){
ksa.filter({db:db,regex:regex,q:tf2,field:"head",ranges:ranges},function(err,data){
prevbatch=0;
batchstart=0;
searchresult=data||[];
showbatch(searchresult);
console.log(searchresult.length);
showtotal(searchresult.length);
reloadToc(function(){});
//updateControls();
});
};
/* 使用單經搜尋 */
var searchSid=function(tofind1){
var sid=tofind1;
console.log("searching sid :"+sid);
ksa.getFieldRange({db:db,field:"sutra",values:sid},function(err,ranges){
console.log("ranges:"+ranges);
filter(ranges,"");
});
}
var gotoSid=function(sid){
ksa.getFieldRange({db:db,field:"sutra",values:sid},function(err,ranges){
console.log("ranges:"+ranges);
if(err){console.log("gotosid err");}
fetchText(ranges[0][0]);
});
}
/* 使用頁碼搜尋 */
var searchUti=function(tofind1){
var searchUti = tofind1;
var isLong = tofind1.match(/(\d{1,3}[a-z]?)\.(\d{1,3}[ab]$)/);
if(!isLong)searchUti=searchUti+"a";//如果不是完整頁碼則補a
ksa.fetch({db:db,uti:searchUti,q:tf2},function(err,data){
if(data.length>0){
console.log("isUti uti:" + searchUti + " vpos:" + data[0].vpos);
if(data[0].vpos!=undefined){
fetchText(data[0].vpos);
}
else{
console.log("invalidUti");
}
}
else{
console.log("invalidUti");
}
});
}
/* 搜尋總函式 */
var search=function() {
var tofind1=document.getElementById("tofind1").value;
var tofind2=document.getElementById("tofind2").value;
tf1=wylie.fromWylieWithWildcard(tofind1);
tf2=wylie.fromWylieWithWildcard(tofind2);
console.log("tf2:" + tf2);
var isUti = tofind1.match(/^(\d{1,3}[a-z]?)\.(\d{1,3}[ab]?$)/);//確認是否以頁碼搜尋
var isSid = tofind1.match(/^([A-Z])(\d{1,4})([a-z]?)$/);//確認是否為單經搜尋
if(isSid && !tf2){//如果是跳單經
console.log("gotoSid:"+tofind1);
gotoSid(tofind1);
return;
}
if(isSid){//如果是單經搜尋
searchSid(tofind1);
return;
}
if(isUti){//如果是輸入頁碼
searchUti(tofind1);
return;
}
fetched=false;
//如果都不是則為一般搜尋
var ffield="";
if(!tf2)ffield="head";
ksa.filter({db:db,regex:tf1,q:tf2,field:ffield},function(err,data){
prevbatch=0;
batchstart=0;
searchresult=data||[];
showbatch(searchresult);
console.log(searchresult.length);
console.log("searchresult.length:"+searchresult.length);
showtotal(searchresult.length);
setTimeout(function(){
reloadToc(function(){
//reloadBreadcrumb(0);
});
},100);
//updateControls();
});
}
var tofind1input=function(e){
clearTimeout(timer1);
timer1=setTimeout(function(){
search();
},300);
};
var tofind2input=function(e){
clearTimeout(timer2);
timer2=setTimeout(function(){
search();
},300);
};
var scrollTo=function(uti){
if (uti.substr(0,1)==="_")return ;//suppress jquery warning
$("#mainContent").scrollTop(0);
console.log("scrollingTo:" + uti);
/*$("#uti_" + uti.replace(".","_")).scrollTop();*/
var to=$("#uti_" + uti.replace(".","_")).offset().top;
console.log(to);
$("#mainContent").scrollTop(to-250);
};
var nextMatch=function(currentvpos){
console.log("this vpos :"+currentvpos);
for(var i=0;i<searchresult.length;i++)
{
if(searchresult[i].vpos>currentvpos && i+1<searchresult.length){
console.log(i+" vpos :"+searchresult[i].vpos);
console.log("next vpos :"+searchresult[i+1].vpos);
fetchText(searchresult[i+1].vpos);
return;
}
}
console.log("next vpos :"+searchresult[0].vpos);
fetchText(searchresult[0].vpos);
return;
};
var highlightText2=function(text,hits,currentvpos){
console.log("highlightText");
if(!hits || !hits.length){
if(toWylie)text=wylie.toWylie(text);
return text;
}
console.log("hits:",hits);
return ksa.renderHits(text,hits,function(obj,text){
if(toWylie)text=wylie.toWylie(text);
//this is for React.js , convert to HTML
return obj.className?"<span onClick='nextMatch(\""+currentvpos+"\")' class='highlight'>"+text+"</span>":text;
}).join("");
};
var highlightText=function(text,hits){
console.log("highlightText");
if(!hits || !hits.length)return text;
console.log("hits:",hits);
return ksa.renderHits(text,hits,function(obj,text){
//this is for React.js , convert to HTML
return obj.className?"<span style='background:red;color:yellow'>"+text+"</span>":text;
}).join("");
}
var toggleWylie=function(){
var output=document.getElementById('contents').innerHTML;
if(toWylie){
//console.log("toWylie:false");
setWylie(false);
//output=wylie.fromWylie(output);
//document.getElementById('contents').innerHTML=output;
search();
setTimeout(function(){
fetchText(250);
},300);
}else{
console.log("toWylie:true");
setWylie(true);
//output=wylie.toWylie(output);
//document.getElementById('contents').innerHTML=output;
search();
setTimeout(function(){
fetchText(100);
},300);
}
}
var fetchText=function(vpos){
ksa.sibling({db:db,vpos:vpos},function(err,res){
console.log("fetchText:" + vpos);
var currentuti=res.sibling[res.idx];
/*if(toputi==res.sibling[0] && fetched){
scrollTo(currentuti);
reloadBreadcrumb(vpos);//vpos為此頁起點
return;
}*/
ksa.fetch({db:db,uti:res.sibling,q:tf2,fields:"sutra"},function(err,data){
var output="";
//var vposend = data[res.idx].vpos_end;
/*var currentSutraID = "J1";//經號
if(data[0].values[0]!=undefined)currentSutraID=data[0].values[0];
output+="<h1 id='sutraid'>"+currentSutraID+"</h1>";*/
//output+="<h1 id='sutraid'>"+(data[0].values[0]==undefined?"J1":data[0].values[0])+"</h1>";//經號
for(var i=0;i<data.length;i++){
output+="<div class='head-content'>";
if((data[i].uti).indexOf("_")==-1)output+="<h2 style='cursor:pointer' onClick='text4image(event)' id='uti_" + (data[i].uti).replace(".","_") + "'>" + data[i].uti + "</h2>";
//output+="<p>" + data[i].text + "</p>";
output+="<p>" + highlightText2(data[i].text,data[i].hits,data[i].vpos) + "</p>";
output+="<a class=\"btn-modal pic\" id='btn_" + (data[i].uti).replace(".","_") + "' onClick=\"text4image(event)\" title=\"open " + data[i].uti + " images\">image icon</a>";
output+="</div>";
}
output=output.replace(/[\r\n]/g,"");
document.getElementById('contents').innerHTML=output;/* innerHTML是很慢的動作,盡量避免執行多次 */
toputi=res.sibling[0];
bottomuti=res.sibling[res.sibling.length-1];
//console.log("vposend:"+vposend);
reloadBreadcrumb(data[res.idx].vpos);
scrollTo(currentuti);
//$("#bSutraID").html(currentSutraID);
fetched=true;
});
});
}
var updateHashTag=function(sid){
window.location.hash="#sid="+sid+"&db="+db;
}
var getSutraidFromHashAndSetDb=function(){
var sid=null;
var hash=window.location.hash;
if(!hash)return;
hash=hash.substr(1);
var params=hash.split("&");
for (var i=0;i<params.length;i+=1) {
var param=params[i].split("=");
if(param[0]==="sid"){
//console.log(param[1]);
sid=param[1];
}
if (param[0]==="db") {
db=param[1];//replacing global db, bad practice
}
}
return sid;
}
var showAbout=function(){
$('#aboutmodal').modal({
show: 'true'
});
}
var openicion,closeicon,nodeicons;
var systemReady=function(){
setFont(0,0);
E = React.createElement;
openicon=E("img",{src:"images/tree-open.png"});
closeicon=E("img",{src:"images/tree-close.png"});
nodeicons=[
"images/tree-lv0.png",
"images/tree-lv1.png",
"images/tree-lv2.png",
"images/tree-lv3.png",
"images/tree-lv4.png"
]
var Sid = getSutraidFromHashAndSetDb();//set db in this function
reloadToc(function(){
console.log("QQQQ");
if(!Sid){
//var DailyUti = localStorage.getItem("DailyUti");
//console.log("DailyUti:"+DailyUti);
/*if(DailyUti){
searchUti(DailyUti);
}
else{*/
fetchText(20);
return;
//}
}
else{
var isSid = Sid.match(/^([A-Z])(\d{1,4})([a-z]?)$/);
if(isSid){
gotoSid(Sid);
}
else{
fetchText(20);
}
}
});
initialAdvSearch();
}