-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXM.js
executable file
·584 lines (565 loc) · 16.7 KB
/
XM.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
.import "qrc:/qml/com/com.js" as Com
.import "qrc:/qml/com/tag.js" as Tag
/// col
function refreshAll(key_changed) {
if($l.isDebug()) {
Com.debug("refreshAll", key_changed);
}
let k = search_bar.text.trim();
if(key_changed && k.length === 0 && list_model_category.count > 0) {
pre_cid = getCurrentCategoryId();
pre_xmid = getCurrentXMId();
}
category_list_view.currentIndex = 0;
list_model_category.clear();
list_model_xm.clear();
loadCategory();
}
function loadCategory() {
$xm.getCategories(search_bar.text.trim(), root_xm);
}
function submitCol(add) {
if(add) {
addCol();
} else {
let i = category_list_view.currentIndex;
let c = list_model_category.get(i);
if(c) {
editCol(c.id);
}
}
}
function addCol() {
let name = col_edit_popup.text.trim();
if(name === '') {
return;
}
let n = list_model_category.count;
for(let x = 0; x < n; x++) {
let m = list_model_category.get(x);
if(m.name === name) {
tipsInfo($a.tr("Exists Already"));
return;
}
}
$xm.addCategory(name, col_edit_popup);
}
function editCol(id) {
let name = col_edit_popup.text.trim();
if(name.length > 30) {
tipsInfo($a.tr("too long"));
} else {
$xm.editCategory(id, name, col_edit_popup);
}
}
function delCol() {
let i = category_list_view.currentIndex;
if(i > 0) {
let c = list_model_category.get(i);
if(c.total>0) {
tipsInfo($a.tr("Can not delete, it not empty."));
} else {
$xm.delCategory(c.id, Com.putFunc(function(){
let i = category_list_view.currentIndex;
list_model_category.remove(i);
loadXM();
}));
}
} else {
tipsInfo($a.tr("Can not delete the default folder."));
}
}
function moveToCategory(i) {
let currentPKIndex = xm_list_view.currentIndex;
let pk = list_model_xm.get(currentPKIndex);
if(pk) {
let c = list_model_category.get(i);
if(c.id !== pk.cid) {
$xm.updateXMCid(i, pk.id, c.id, root_xm);
}
}
}
function colTotalIncrement() {
let i = category_list_view.currentIndex;
let c = list_model_category.get(i);
c.total = c.total + 1;
}
function colTotalDecrement() {
let i = category_list_view.currentIndex;
let c = list_model_category.get(i);
c.total = Com.max(0, c.total - 1);
}
function addOrUpdateCol(col) {
let n = list_model_category.count;
let add = true;
for(let i = 0; i < n; i++) {
let m = list_model_category.get(i);
if(m.id === col.id) {
m.total = col.total;
add = false;
}
}
if(add) {
list_model_category.append(col);
}
}
/// pk
function loadXM(clear, cb) {
let index = category_list_view.currentIndex;
xm_list_view.footer = xm_list_footer;
if(clear) {
list_model_xm.clear();
}
let c = list_model_category.get(index);
if(c){
let cid = c.id;
let k = search_bar.text.trim();
let lsId = getPKLastId();
xm_list_view.loading = true;
$xm.getXMList(k, cid, lsId, xm_list.width, Com.putFunc(function(list){
loadXM0(list);
if(cb) {
cb();
}
xm_list_view.loading = false;
}));
}
}
function loadXM0(list) {
if($l.isDebug()) {
Com.debug("pushXM list", list.length);
}
let ar = Com.parseTime(getPKLastTime(), 1);
let preDateStr = ar[0];
let preTimeStr = ar[1];
for(let i in list) {
let e = list[i];
let xm = Com.convXM(preDateStr, preTimeStr, e);
if($l.isDebug()) {
Com.debug("xm", JSON.stringify(e));
}
list_model_xm.append(xm);
preDateStr = xm.date_str;
preTimeStr = xm.time_str;
}
if(list.length < $a.pageSize) {
xm_list_view.footer = pkNoMoreBtn;
} else {
xm_list_view.footer = xm_list_footer;
}
//定位当前选中项
if(pre_cid > 0) {
let i = getColIndexByCid(pre_cid);
if(i >= 0) {
category_list_view.currentIndex = i;
pre_cid = 0;
}
}
if(pre_xmid > 0) {
let arr = getPKByIdInCurrentList(pre_xmid);
if(arr) {
let i = arr[0];
xm_list_view.currentIndex = i;
pre_xmid = 0;
}
}
$a.setUIVal(0, xm_list.width);
}
function deletePK(target) {
let i = xm_list_view.currentIndex;
if(i>=0){
let p = list_model_xm.get(i);
p.img_path = "";
$xm.deleteXM(p.id, target);
}
}
function openEditPopup(add, pk) {
if(add) {
let cid = getCurrentCategoryId();
edit_pk_popup.op(cid);
} else {
if(!pk) {
pk = getCurrentXM();
}
if(pk) {
$xm.getXM(pk.id, xm_list.width, Com.putFunc(function(pk2) {
edit_pk_popup.op(pk.cid, pk.id, pk2.cont);
}));
}
}
}
function openOutEdit() {
let pk = getCurrentXM();
if(pk && !pk.jm) {
$a.openInExternal(1, pk.id);
}
}
function submitXM(cb, upeditor=1) {
let txt = edit_pk_popup.text;
if(edit_pk_popup.bid <= 0) {
if(txt==='') {
if(cb) {
cb();
}
return;
}
$xm.addXM(getCurrentCategoryId(), txt, xm_list.width, Com.putFunc(function(xm){
xm = Com.convXM(1, 1, xm);
colTotalIncrement();
edit_pk_popup.bid = xm.id;
if(cb) {
cb(xm);
}
}));
} else {
let k = search_bar.text.trim();
if($l.isDebug()) {
$l.debug("updateXM "+ edit_pk_popup.bid + " " + txt + " " + k);
}
$xm.updateXM(edit_pk_popup.bid, txt, k, xm_list.width, Com.putFunc(function(xm) {
xm = Com.convXM(1, 1, xm);
onUpdateXM(xm, upeditor);
if(cb) {
cb(xm);
}
}));
}
}
function closeEditPK() {
edit_pk_popup.cl();
category_list_view.forceActiveFocus();
if(detailView.visible) {
let suc = detailView.reopen();
if(!suc) {
openDetail(getCurrentXM(), xm_list);
}
}
}
function onKeysPressed(event) {
// console.log("onKeysPressed", event.key, event.modifiers, ctrlVal, Qt.MetaModifier, event.modifiers === ctrlVal);
event.accepted = true;
let jump = false;
if(event.modifiers === Qt.ShiftModifier && event.key === Qt.Key_J) {
category_list_view.currentIndex = (category_list_view.currentIndex+1) % category_list_view.count;
} else if(event.modifiers === ctrlVal && event.key === Qt.Key_E) {
openEncryptPopup();
} else if(event.modifiers === ctrlVal && event.key === Qt.Key_Home) {
let xm = getCurrentXM();
if(xm.sticky) {
cancelSticky(xm.id);
} else {
sticky(xm.id);
}
} else if(event.modifiers === Qt.ShiftModifier && event.key === Qt.Key_K) {
let i = category_list_view.currentIndex - 1;
if(i < 0){
i = category_list_view.count - 1;
}
category_list_view.currentIndex = i;
} else if(event.key === Qt.Key_J || event.key === Qt.Key_Down) {
xm_list.next();
jump = true;
} else if(event.key === Qt.Key_K || event.key === Qt.Key_Up) {
xm_list.previous();
jump = true;
} else if(event.modifiers === (ctrlVal | Qt.ShiftModifier) && event.key === Qt.Key_E) {
openColEditPopup();
} else if(event.modifiers === ctrlVal && event.key === Qt.Key_Return) {
openEditPopup();
} else if(event.modifiers === (ctrlVal | Qt.AltModifier) && event.key === Qt.Key_Return) {
openOutEdit();
} else if(event.key === Qt.Key_Delete || event.key === Qt.Key_Backspace) {
if(list_model_xm.count > 0){
ensure_popup.open();
}
} else if(event.modifiers === ctrlVal && event.key === Qt.Key_F) {
search_bar.forceActiveFocus();
} else if(event.key === Qt.Key_Return) {
openImgView();
} else if(event.modifiers === Qt.ShiftModifier && event.key === Qt.Key_Space) {
detailWin();
} else if(event.modifiers === ctrlVal && event.key === Qt.Key_Space) {
} else if(event.key === Qt.Key_Space) {
detail();
} else if(event.modifiers === ctrlVal && event.key === Qt.Key_N) {
navBtnClick('add');
} else if(event.modifiers === (ctrlVal | Qt.ShiftModifier) && event.key === Qt.Key_N) {
onBtnClick_addCol();
} else if(event.modifiers === (Qt.AltModifier|Qt.ShiftModifier) && event.key === Qt.Key_C) {
copyPK(0);
} else if(event.modifiers === (ctrlVal|Qt.AltModifier) && event.key === Qt.Key_C) {
copyPK(1);
} else if(event.modifiers === (ctrlVal|Qt.ShiftModifier) && event.key === Qt.Key_C) {
copyPK(2);
} else if(event.modifiers === ctrlVal && event.key === Qt.Key_T) {
navBtnClick('tag');
} else if(event.key === Qt.Key_Home) {
xm_list_view.currentIndex = 0;
} else if(event.key === Qt.Key_End) {
xm_list_view.currentIndex = xm_list_view.count - 1;
} else if(event.key === Qt.Key_Escape || event.modifiers === ctrlVal) {
if(list_model_xm.count > 0 && xm_list_view.currentItem.btn_no) {
xm_list_view.currentItem.btn_no.click();
}
jump = true;
} else {
jump = true;
}
if(jump) {
Com.jump(event, list_model_category, category_list_view);
}
root_xm.key(event);
}
function detail() {
openDetail(getCurrentXM(), xm_list);
}
function detailWin() {
let com = Qt.createComponent("qrc:/qml/com/DetailWin.qml");
let win = com.createObject();
win.open(getCurrentXM(), xm_list);
win_detail_refs[win_detail_refs.length] = win;
}
function openColEditPopup() {
let i = category_list_view.currentIndex;
let c = list_model_category.get(i);
if(c) {
col_edit_popup.text = c.name;
col_edit_popup.open();
}
}
function openEncryptPopup() {
if(list_model_xm.count > 0) {
let pk = getCurrentXM();
if(pk) {
let twiceCheck = !pk.jm;
popup_input.delegate = {
onSubmit:function() {
enOrDecrypt();
category_list_view.forceActiveFocus();
},
onCancel:function() {
category_list_view.forceActiveFocus();
}
};
popup_input.op(twiceCheck);
}
}
}
let img_view_delegate = {
setIndex:function(i) {
xm_list_view.currentIndex = i;
},
getCurrentIndex:function() {
return xm_list_view.currentIndex;
},
previous:function(){
return xm_list.previous();
},
next:function(){
return xm_list.next();
},
onClose:function() {
category_list_view.forceActiveFocus();
let pk = getCurrentXM();
if(pk) {
let tmp = pk.img_path;
pk.img_path = "";
pk.img_path = tmp;
let tmp_qmls = pk.simple_qmls;
pk.simple_qmls = "";
pk.simple_qmls = tmp_qmls;
}
}
}
function openImgView(img) {
let pk = getCurrentXM();
if(pk && !pk.jm) {
imgViewer.delegate = img_view_delegate;
let n = imgViewer.append(pk);
if(n>0) {
if(img) {
imgViewer.setCurIndexByImg(img);
}
imgViewer.open();
}
}
}
function copyPK(type) {
let pk = getCurrentXM();
if(pk) {
$xm.copyXM(type, pk.id);
}
}
function getPKByIdInCurrentList(id) {
let ct = list_model_xm.count;
for(let i = 0; i < ct; i++) {
let pk = list_model_xm.get(i);
if(pk.id === id) {
return [i,pk];
}
}
return null;
}
function getCurrentCategoryId() {
let ci = category_list_view.currentIndex;
let c = list_model_category.get(ci);
if(c) {
return c.id;
}
return 0;
}
function getCurrentCategory() {
let i = category_list_view.currentIndex;
return list_model_category.get(i);
}
function getCurrentXM() {
let i = xm_list_view.currentIndex;
let pk = list_model_xm.get(i);
return pk;
}
function getCurrentXMId() {
let pk = getCurrentXM();
if(pk){
return pk.id;
}
return 0;
}
function getColIndexByCid(cid) {
let len = list_model_category.count;
for(let i = 0; i < len; i++){
let c = list_model_category.get(i);
if(c && c.id === cid) {
return i;
}
}
return -1;
}
function onBtnClick_addCol() {
if(!col_edit_popup.visible) {
col_edit_popup.text = '';
col_edit_popup.add = true;
col_edit_popup.open();
}
}
function getPKLastTime() {
if(list_model_xm.count > 0) {
let last = list_model_xm.get(list_model_xm.count - 1);
return last.time;
} else {
return 0;
}
}
function getPKLastId() {
if(list_model_xm.count > 0) {
let last = list_model_xm.get(list_model_xm.count - 1);
return last.id;
} else {
return 0;
}
}
function test() {
// let col = "fuck";
// for(let i = 0; i < 1000; i++) {
// $a.addCol(col+i, col_edit_popup);
// }
// let pk = "fuck";
// for(let i = 0; i < 10000; i++) {
// $xm.addXM(8, pk+i, edit_pk_popup);
// }
}
//test();
///
class PKTagDelegate extends Tag.TagDelegate {
getCurrentItem() {
return getCurrentXM();
}
updateTags(id,tags) {
$xm.updateXMTags(id, tags, Com.putFunc(function(r) {
let pk = getCurrentXM();
if(pk) {
pk.tags = r.tags;
pk.stime = r.stime;
xm_list_view.currentItem.updateTags(r.tags);
if(detailView.visible) {
detailView.updateTags(r.tags, r.stime);
}
if($l.isDebug()) {
Com.debug("updateTags id", id, "tags", r.tags);
}
}
}));
}
}
function enOrDecrypt() {
let pk = getCurrentXM();
if(pk) {
let pwd = popup_input.text.trim();
if(pk.jm) {
Com.st(0, $a.tr("decrypting..."));
$a.decrypt(pk.id, pwd, xm_list.width, Com.putFunc(function(pk2) {
if($l.isTrace()) {
Com.trace("decrypt xm", JSON.stringify(pk2));
}
if(getCurrentCategoryId() === pk2.cid) {
let pk = getCurrentXM();
if(pk) {
pk.jm = 0;
pk.jm_ensure = true;
pk.simple_cont = pk2.simple_cont;
pk.cont = pk2.cont;
pk.simple_qmls = JSON.stringify(pk2.simple_qmls);
pk.img_path = Com.file_pre + $a.getCfgVal("tmp_dir") + "/" + pk2.img;
pk.qmls_ = JSON.stringify(pk2.qmls);//这里直接赋值为数组不行,就暂时转成字符串
pk.forceMaxWidth = true;
xm_list_view.currentItem.refresh(pk);
list_model_xm.set(xm_list_view.currentIndex, pk);
}
}
popup_input.cancel();
Com.st(1, $a.tr("Finished"));
}));
} else {
Com.st(0, $a.tr("encrypting..."));
$a.encrypt(pk.id, pwd, xm_list.width, Com.putFunc(function(pk2) {
if($l.isTrace()) {
Com.trace("encrypt xm", JSON.stringify(pk2));
}
if(getCurrentCategoryId() === pk2.cid) {
let pk = getCurrentXM();
if(pk) {
pk.jm = 1;
pk.jm_ensure = true;
pk.cont = pk2.cont;
pk.qmls_ = JSON.stringify(pk2.qmls);
pk.simple_qmls = JSON.stringify(pk2.simple_qmls);
xm_list_view.currentItem.refresh(pk);
list_model_xm.set(xm_list_view.currentIndex, pk);
}
}
popup_input.cancel();
Com.st(1, $a.tr("Finished"));
}));
}
popup_input.clear();
}
}
function sticky(id) {
let xm = getCurrentXM();
$xm.sticky(id, Com.putFunc(function(){
xm.sticky = 1;
alert("置顶成功");
loadXM(true, function(){
xm_list_view.currentIndex = 0;
});
}));
}
function cancelSticky(id) {
let xm = getCurrentXM();
$xm.cancelSticky(id, Com.putFunc(function(){
xm.sticky = 0;
alert("取消置顶成功");
loadXM(true, function(){
xm_list_view.currentIndex = 0;
});
}));
}