-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDLapp.js
750 lines (684 loc) · 26.7 KB
/
DLapp.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
// 在head 中 加载 必要静态
document.write('<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/[email protected]/dist/css/mdui.min.css">');
// markdown支持
document.write('<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/markdown-it.min.js"></script>');
document.write('<style>.mdui-appbar .mdui-toolbar{height:56px;font-size:1pc}.mdui-toolbar>*{padding:0 6px;margin:0 2px}.mdui-toolbar>i{opacity:.5}.mdui-toolbar>.mdui-typo-headline{padding:0 1pc 0 0}.mdui-toolbar>i{padding:0}.mdui-toolbar>a:hover,a.active,a.mdui-typo-headline{opacity:1}.mdui-container{max-width:980px}.mdui-list-item{transition:none}.mdui-list>.th{background-color:initial}.mdui-list-item>a{width:100%;line-height:3pc}.mdui-list-item{margin:2px 0;padding:0}.mdui-toolbar>a:last-child{opacity:1}@media screen and (max-width:980px){.mdui-list-item .mdui-text-right{display:none}.mdui-container{width:100%!important;margin:0}.mdui-toolbar>.mdui-typo-headline,.mdui-toolbar>a:last-child,.mdui-toolbar>i:first-child{display:block}}</style>');
// add custome theme and darkmode
if (UI.dark_mode) {
document.write(`<style>* {box-sizing: border-box}body{color:rgba(255,255,255,.87);background-color:#333232}.mdui-theme-primary-${UI.main_color} .mdui-color-theme{background-color:#232427!important}</style>`);
}
// 初始化页面,并载入必要资源
function init() {
document.siteName = $('title').html();
$('body').addClass(`mdui-theme-primary-${UI.main_color} mdui-theme-accent-${UI.accent_color}`);
var html = `
<header class="mdui-appbar mdui-color-theme">
<div id="nav" class="mdui-toolbar mdui-container${UI.fluid_navigation_bar ? '-fluid' : ''} ${UI.dark_mode ? 'mdui-text-color-white-text' : ''}">
</div>
</header>
<div id="content" class="mdui-container">
</div>
`;
$('body').html(html);
}
const Os = {
isWindows: navigator.platform.toUpperCase().indexOf('WIN') > -1, // .includes
isMac: navigator.platform.toUpperCase().indexOf('MAC') > -1,
isMacLike: /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform),
isIos: /(iPhone|iPod|iPad)/i.test(navigator.platform),
isMobile: /Android|webOS|iPhone|iPad|iPod|iOS|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
};
function getDocumentHeight() {
var D = document;
return Math.max(
D.body.scrollHeight, D.documentElement.scrollHeight,
D.body.offsetHeight, D.documentElement.offsetHeight,
D.body.clientHeight, D.documentElement.clientHeight
);
}
function render(path) {
if (path.indexOf("?") > 0) {
path = path.substr(0, path.indexOf("?"));
}
title(path);
nav(path);
// .../0: 这种
var reg = /\/\d+:$/g;
if (window.MODEL.is_search_page) {
// 用来存储一些滚动事件的状态
window.scroll_status = {
// 滚动事件是否已经绑定
event_bound: false,
// "滚动到底部,正在加载更多数据" 事件的锁
loading_lock: false
};
render_search_result_list()
} else if (path.match(reg) || path.substr(-1) == '/') {
// 用来存储一些滚动事件的状态
window.scroll_status = {
// 滚动事件是否已经绑定
event_bound: false,
// "滚动到底部,正在加载更多数据" 事件的锁
loading_lock: false
};
list(path);
} else {
file(path);
}
}
// 渲染 title
function title(path) {
path = decodeURI(path);
var cur = window.current_drive_order || 0;
var drive_name = window.drive_names[cur];
path = path.replace(`/${cur}:`, '');
// $('title').html(document.siteName + ' - ' + path);
var model = window.MODEL;
if (model.is_search_page)
$('title').html(`${document.siteName} - ${drive_name} - 搜索 ${model.q} 的结果`);
else
$('title').html(`${document.siteName} - ${drive_name} - ${path}`);
}
// 渲染导航栏
function nav(path) {
var model = window.MODEL;
var html = "";
var cur = window.current_drive_order || 0;
html += `<a href="/${cur}:/" class="mdui-typo-headline folder">${document.siteName}</a>`;
var names = window.drive_names;
/*html += `<button class="mdui-btn mdui-btn-raised" mdui-menu="{target: '#drive-names'}"><i class="mdui-icon mdui-icon-left material-icons">share</i> ${names[cur]}</button>`;
html += `<ul class="mdui-menu" id="drive-names" style="transform-origin: 0px 0px; position: fixed;">`;
names.forEach((name, idx) => {
html += `<li class="mdui-menu-item ${(idx === cur) ? 'mdui-list-item-active' : ''} "><a href="/${idx}:/" class="mdui-ripple">${name}</a></li>`;
});
html += `</ul>`;*/
// 修改为 select
html += `<select class="mdui-select" onchange="window.location.href=this.value" mdui-select style="overflow:visible;padding-left:8px;padding-right:8px">`;
names.forEach((name, idx) => {
html += `<option value="/${idx}:/" ${idx === cur ? 'selected="selected"' : ''} >${name}</option>`;
});
html += `</select>`;
if (!model.is_search_page) {
var arr = path.trim('/').split('/');
var p = '/';
if (arr.length > 1) {
arr.shift();
for (i in arr) {
var n = arr[i];
n = decodeURI(n);
p += n + '/';
if (n == '') {
break;
}
html += `<i class="mdui-icon material-icons mdui-icon-dark folder" style="margin:0;">chevron_right</i><a class="folder" href="/${cur}:${p}">${n}</a>`;
}
}
}
var search_text = model.is_search_page ? (model.q || '') : '';
const isMobile = Os.isMobile;
var search_bar = `<div class="mdui-toolbar-spacer"></div>
<div id="search_bar" class="mdui-textfield mdui-textfield-expandable mdui-float-right ${model.is_search_page ? 'mdui-textfield-expanded' : ''}" style="max-width:${isMobile ? 300 : 400}px">
<button class="mdui-textfield-icon mdui-btn mdui-btn-icon" onclick="if($('#search_bar').hasClass('mdui-textfield-expanded') && $('#search_bar_form>input').val()) $('#search_bar_form').submit();">
<i class="mdui-icon material-icons">search</i>
</button>
<form id="search_bar_form" method="get" action="/${cur}:search">
<input class="mdui-textfield-input" type="text" name="q" placeholder="Search in current drive" value="${search_text}"/>
</form>
<button class="mdui-textfield-close mdui-btn mdui-btn-icon"><i class="mdui-icon material-icons">close</i></button>
</div>`;
// 个人盘 或 团队盘
if (model.root_type < 2) {
// 显示搜索框
html += search_bar;
}
$('#nav').html(html);
mdui.mutation();
mdui.updateTextFields();
}
/**
* 发起列目录的 POST 请求
* @param path Path
* @param params Form params
* @param resultCallback Success Result Callback
* @param authErrorCallback Pass Error Callback
*/
function requestListPath(path, params, resultCallback, authErrorCallback) {
var p = {
password: params['password'] || null,
page_token: params['page_token'] || null,
page_index: params['page_index'] || 0
};
$.post(path, p, function (data, status) {
var res = jQuery.parseJSON(data);
if (res && res.error && res.error.code == '401') {
// 密码验证失败
if (authErrorCallback) authErrorCallback(path)
} else if (res && res.data) {
if (resultCallback) resultCallback(res, path, p)
}
})
}
/**
* 搜索 POST 请求
* @param params Form params
* @param resultCallback Success callback
*/
function requestSearch(params, resultCallback) {
var p = {
q: params['q'] || null,
page_token: params['page_token'] || null,
page_index: params['page_index'] || 0
};
$.post(`/${window.current_drive_order}:search`, p, function (data, status) {
var res = jQuery.parseJSON(data);
if (res && res.data) {
if (resultCallback) resultCallback(res, p)
}
})
}
// 渲染文件列表
function list(path) {
var content = `
<div id="head_md" class="mdui-typo" style="display:none;padding: 20px 0;"></div>
<div class="mdui-row">
<ul class="mdui-list">
<li class="mdui-list-item th">
<div class="mdui-col-xs-12 mdui-col-sm-7">
名稱
<i class="mdui-icon material-icons icon-sort" data-sort="name" data-order="more">expand_more</i>
</div>
<div class="mdui-col-sm-3 mdui-text-right">
時間
<i class="mdui-icon material-icons icon-sort" data-sort="date" data-order="downward">expand_more</i>
</div>
<div class="mdui-col-sm-2 mdui-text-right">
大小
<i class="mdui-icon material-icons icon-sort" data-sort="size" data-order="downward">expand_more</i>
</div>
</li>
</ul>
</div>
<div class="mdui-row">
<ul id="list" class="mdui-list">
</ul>
<div id="count" class="mdui-hidden mdui-center mdui-text-center mdui-m-b-3 mdui-typo-subheading mdui-text-color-blue-grey-500">共 <span class="number"></span> 項</div><p>群組Discord: <a style="color:#00BB00;" href = "https://discord.com/invite/XrAa7RDBhw">https://discord.com/invite/XrAa7RDBhw</a></p>
</div>
<div id="readme_md" class="mdui-typo" style="display:none; padding: 20px 0;"></div>
`;
$('#content').html(content);
var password = localStorage.getItem('password' + path);
$('#list').html(`<div class="mdui-progress"><div class="mdui-progress-indeterminate"></div></div>`);
$('#readme_md').hide().html('');
$('#head_md').hide().html('');
/**
* 列目录请求成功返回数据后的回调
* @param res 返回的结果(object)
* @param path 请求的路径
* @param prevReqParams 请求时所用的参数
*/
function successResultCallback(res, path, prevReqParams) {
// 把 nextPageToken 和 currentPageIndex 暂存在 list元素 中
$('#list')
.data('nextPageToken', res['nextPageToken'])
.data('curPageIndex', res['curPageIndex']);
// 移除 loading spinner
$('#spinner').remove();
if (res['nextPageToken'] === null) {
// 如果是最后一页,取消绑定 scroll 事件,重置 scroll_status ,并 append 数据
$(window).off('scroll');
window.scroll_status.event_bound = false;
window.scroll_status.loading_lock = false;
append_files_to_list(path, res['data']['files']);
} else {
// 如果不是最后一页,append数据 ,并绑定 scroll 事件(如果还未绑定),更新 scroll_status
append_files_to_list(path, res['data']['files']);
if (window.scroll_status.event_bound !== true) {
// 绑定事件,如果还未绑定
$(window).on('scroll', function () {
var scrollTop = $(this).scrollTop();
var scrollHeight = getDocumentHeight();
var windowHeight = $(this).height();
// 滚到底部
if (scrollTop + windowHeight > scrollHeight - (Os.isMobile ? 130 : 80)) {
/*
滚到底部事件触发时,如果此时已经正在 loading 中,则忽略此次事件;
否则,去 loading,并占据 loading锁,表明 正在 loading 中
*/
if (window.scroll_status.loading_lock === true) {
return;
}
window.scroll_status.loading_lock = true;
// 展示一个 loading spinner
$(`<div id="spinner" class="mdui-spinner mdui-spinner-colorful mdui-center"></div>`)
.insertBefore('#readme_md');
mdui.updateSpinners();
// mdui.mutation();
let $list = $('#list');
requestListPath(path, {
password: prevReqParams['password'],
page_token: $list.data('nextPageToken'),
// 请求下一页
page_index: $list.data('curPageIndex') + 1
},
successResultCallback,
// 密码和之前相同。不会出现 authError
null
)
}
});
window.scroll_status.event_bound = true
}
}
// loading 成功,并成功渲染了新数据之后,释放 loading 锁,以便能继续处理 "滚动到底部" 事件
if (window.scroll_status.loading_lock === true) {
window.scroll_status.loading_lock = false
}
}
// 开始从第1页请求数据
requestListPath(path, {password: password},
successResultCallback,
function (path) {
$('#spinner').remove();
var pass = prompt("通關密語", "");
localStorage.setItem('password' + path, pass);
if (pass != null && pass != "") {
list(path);
} else {
history.go(-1);
}
});
}
/**
* 把请求得来的新一页的数据追加到 list 中
* @param path 路径
* @param files 请求得来的结果
*/
function append_files_to_list(path, files) {
var $list = $('#list');
// 是最后一页数据了吗?
var is_lastpage_loaded = null === $list.data('nextPageToken');
var is_firstpage = '0' == $list.data('curPageIndex');
html = "";
let targetFiles = [];
for (i in files) {
var item = files[i];
var p = path+encodeURIComponent(item.name) + '/';
if (item['size'] == undefined) {
item['size'] = "";
}
item['modifiedTime'] = utc2beijing(item['modifiedTime']);
item['size'] = formatFileSize(item['size']);
if (item['mimeType'] == 'application/vnd.google-apps.folder') {
html += `<li class="mdui-list-item mdui-ripple"><a href="${p}" class="folder">
<div class="mdui-col-xs-12 mdui-col-sm-7 mdui-text-truncate" title="${item.name}">
<i class="mdui-icon material-icons">folder_open</i>
${item.name}
</div>
<div class="mdui-col-sm-3 mdui-text-right">${item['modifiedTime']}</div>
<div class="mdui-col-sm-2 mdui-text-right">${item['size']}</div>
</a>
</li>`;
} else {
var p = path+encodeURIComponent(item.name);
const filepath = path+encodeURIComponent(item.name);
var c = "file";
// 渲染head.md
get_file('/0:/HEAD.md', item, (data) => {
markdown('#head_md', data)
})
// 当加载完最后一页后,才显示 README ,否则会影响滚动事件 + 隱藏項目
if (is_lastpage_loaded && item.name == "README.md") {
get_file(p, item, function (data) {
markdown("#readme_md", data);
});
continue
}
if (item.name == "HEAD.md") {
// get_file(p, item, function (data) {
// markdown("#head_md", data);
// });
continue
}
var ext = p.split('.').pop().toLowerCase();
html += `<li class="mdui-list-item file mdui-ripple" target="_blank"><a gd-type="${item.mimeType}" href="${p}" class="${c}">
<div class="mdui-col-xs-12 mdui-col-sm-7 mdui-text-truncate" title="${item.name}">
<i class="mdui-icon material-icons">insert_drive_file</i>
${item.name}
</div>
<div class="mdui-col-sm-3 mdui-text-right">${item['modifiedTime']}</div>
<div class="mdui-col-sm-2 mdui-text-right">${item['size']}</div>
</a>
</li>`;
}
}
if (targetFiles.length > 0) {
let old = localStorage.getItem(path);
let new_children = targetFiles;
// 第1页重设;否则追加
if (!is_firstpage && old) {
let old_children;
try {
old_children = JSON.parse(old);
if (!Array.isArray(old_children)) {
old_children = []
}
} catch (e) {
old_children = [];
}
new_children = old_children.concat(targetFiles)
}
localStorage.setItem(path, JSON.stringify(new_children))
}
// 是第1页时,去除横向loading条
$list.html(($list.data('curPageIndex') == '0' ? '' : $list.html()) + html);
// 是最后一页时,统计并显示出总项目数
if (is_lastpage_loaded) {
$('#count').removeClass('mdui-hidden').find('.number').text($list.find('li.mdui-list-item').length);
}
}
/**
* 渲染搜索结果列表。有大量重复代码,但是里面有不一样的逻辑,暂时先这样分开弄吧
*/
function render_search_result_list() {
var content = `
<div id="head_md" class="mdui-typo" style="display:none;padding: 20px 0;"></div>
<div class="mdui-row">
<ul class="mdui-list">
<li class="mdui-list-item th">
<div class="mdui-col-xs-12 mdui-col-sm-7">
名稱
<i class="mdui-icon material-icons icon-sort" data-sort="name" data-order="more">expand_more</i>
</div>
<div class="mdui-col-sm-3 mdui-text-right">
時間
<i class="mdui-icon material-icons icon-sort" data-sort="date" data-order="downward">expand_more</i>
</div>
<div class="mdui-col-sm-2 mdui-text-right">
大小
<i class="mdui-icon material-icons icon-sort" data-sort="size" data-order="downward">expand_more</i>
</div>
</li>
</ul>
</div>
<div class="mdui-row">
<ul id="list" class="mdui-list">
</ul>
<div id="count" class="mdui-hidden mdui-center mdui-text-center mdui-m-b-3 mdui-typo-subheading mdui-text-color-blue-grey-500">共 <span class="number"></span> 項</div><p>群組Discord: <a style="color:#00BB00;" href = "https://discord.com/invite/XrAa7RDBhw">https://discord.com/invite/XrAa7RDBhw</a></p>
</div>
<div id="readme_md" class="mdui-typo" style="display:none; padding: 20px 0;"></div>
`;
$('#content').html(content);
$('#list').html(`<div class="mdui-progress"><div class="mdui-progress-indeterminate"></div></div>`);
$('#readme_md').hide().html('');
$('#head_md').hide().html('');
/**
* 搜索请求成功返回数据后的回调
* @param res 返回的结果(object)
* @param path 请求的路径
* @param prevReqParams 请求时所用的参数
*/
function searchSuccessCallback(res, prevReqParams) {
// 把 nextPageToken 和 currentPageIndex 暂存在 list元素 中
$('#list')
.data('nextPageToken', res['nextPageToken'])
.data('curPageIndex', res['curPageIndex']);
// 移除 loading spinner
$('#spinner').remove();
if (res['nextPageToken'] === null) {
// 如果是最后一页,取消绑定 scroll 事件,重置 scroll_status ,并 append 数据
$(window).off('scroll');
window.scroll_status.event_bound = false;
window.scroll_status.loading_lock = false;
append_search_result_to_list(res['data']['files']);
} else {
// 如果不是最后一页,append数据 ,并绑定 scroll 事件(如果还未绑定),更新 scroll_status
append_search_result_to_list(res['data']['files']);
if (window.scroll_status.event_bound !== true) {
// 绑定事件,如果还未绑定
$(window).on('scroll', function () {
var scrollTop = $(this).scrollTop();
var scrollHeight = getDocumentHeight();
var windowHeight = $(this).height();
// 滚到底部
if (scrollTop + windowHeight > scrollHeight - (Os.isMobile ? 130 : 80)) {
/*
滚到底部事件触发时,如果此时已经正在 loading 中,则忽略此次事件;
否则,去 loading,并占据 loading锁,表明 正在 loading 中
*/
if (window.scroll_status.loading_lock === true) {
return;
}
window.scroll_status.loading_lock = true;
// 展示一个 loading spinner
$(`<div id="spinner" class="mdui-spinner mdui-spinner-colorful mdui-center"></div>`)
.insertBefore('#readme_md');
mdui.updateSpinners();
// mdui.mutation();
let $list = $('#list');
requestSearch({
q: window.MODEL.q,
page_token: $list.data('nextPageToken'),
// 请求下一页
page_index: $list.data('curPageIndex') + 1
},
searchSuccessCallback
)
}
});
window.scroll_status.event_bound = true
}
}
// loading 成功,并成功渲染了新数据之后,释放 loading 锁,以便能继续处理 "滚动到底部" 事件
if (window.scroll_status.loading_lock === true) {
window.scroll_status.loading_lock = false
}
}
// 开始从第1页请求数据
requestSearch({q: window.MODEL.q}, searchSuccessCallback);
}
/**
* 追加新一页的搜索结果
* @param files
*/
function append_search_result_to_list(files) {
var $list = $('#list');
// 是最后一页数据了吗?
var is_lastpage_loaded = null === $list.data('nextPageToken');
// var is_firstpage = '0' == $list.data('curPageIndex');
html = "";
for (i in files) {
var item = files[i];
if (item['size'] == undefined) {
item['size'] = "";
}
item['modifiedTime'] = utc2beijing(item['modifiedTime']);
item['size'] = formatFileSize(item['size']);
if (item['mimeType'] == 'application/vnd.google-apps.folder') {
html += `<li class="mdui-list-item mdui-ripple"><a id="${item['id']}" onclick="onSearchResultItemClick(this)" class="folder">
<div class="mdui-col-xs-12 mdui-col-sm-7 mdui-text-truncate" title="${item.name}">
<i class="mdui-icon material-icons">folder_open</i>
${item.name}
</div>
<div class="mdui-col-sm-3 mdui-text-right">${item['modifiedTime']}</div>
<div class="mdui-col-sm-2 mdui-text-right">${item['size']}</div>
</a>
</li>`;
} else {
switch(item.name) { // 隱藏項目
case 'README.md':
continue
case 'HEAD.md':
continue
}
var c = "file";
var ext = item.name.split('.').pop().toLowerCase();
html += `<li class="mdui-list-item file mdui-ripple" target="_blank"><a id="${item['id']}" gd-type="${item.mimeType}" onclick="onSearchResultItemClick(this)" class="${c}">
<div class="mdui-col-xs-12 mdui-col-sm-7 mdui-text-truncate" title="${item.name}">
<i class="mdui-icon material-icons">insert_drive_file</i>
${item.name}
</div>
<div class="mdui-col-sm-3 mdui-text-right">${item['modifiedTime']}</div>
<div class="mdui-col-sm-2 mdui-text-right">${item['size']}</div>
</a>
</li>`;
}
}
// 是第1页时,去除横向loading条
$list.html(($list.data('curPageIndex') == '0' ? '' : $list.html()) + html);
// 是最后一页时,统计并显示出总项目数
if (is_lastpage_loaded) {
$('#count').removeClass('mdui-hidden').find('.number').text($list.find('li.mdui-list-item').length);
}
}
/**
* 搜索结果项目点击事件
* @param a_ele 点击的元素
*/
function onSearchResultItemClick(a_ele) {
var me = $(a_ele);
var can_preview = me.hasClass('view');
var cur = window.current_drive_order;
var dialog = mdui.dialog({
title: '',
content: '<div class="mdui-text-center mdui-typo-title mdui-m-b-1">喔逆醬人家在讀取目標路徑...</div><div class="mdui-spinner mdui-spinner-colorful mdui-center"></div>',
// content: '<div class="mdui-spinner mdui-spinner-colorful mdui-center"></div>',
history: false,
modal: true,
closeOnEsc: true
});
mdui.updateSpinners();
// 请求获取路径
$.post(`/${cur}:id2path`, {id: a_ele.id}, function (data) {
if (data) {
dialog.close();
var href = `/${cur}:${data}${can_preview ? '?a=view' : ''}`;
dialog = mdui.dialog({
title: '<i class="mdui-icon material-icons"></i>目標路徑',
content: `<a href="${href}">${data}</a>`,
history: false,
modal: true,
closeOnEsc: true,
buttons: [
{
text: '打開', onClick: function () {
window.location.href = href
}
}, {
text: '在新的分頁打開吧', onClick: function () {
window.open(href)
}
}
, {text: '取消'}
]
});
return;
}
dialog.close();
dialog = mdui.dialog({
title: '<i class="mdui-icon material-icons"></i>喔逆醬人家讀取不到',
content: 'o(╯□╰)o 喔逆醬的次元空間不存在此項目',
history: false,
modal: true,
closeOnEsc: true,
buttons: [
{text: 'WTF ???'}
]
});
})
}
function get_file(path, file, callback) {
var key = "file_path_" + path + file['modifiedTime'];
var data = localStorage.getItem(key);
if (data != undefined) {
return callback(data);
} else {
$.get(path, function (d) {
localStorage.setItem(key, d);
callback(d);
});
}
}
//时间转换
function utc2beijing(utc_datetime) {
// 转为正常的时间格式 年-月-日 时:分:秒
var T_pos = utc_datetime.indexOf('T');
var Z_pos = utc_datetime.indexOf('Z');
var year_month_day = utc_datetime.substr(0, T_pos);
var hour_minute_second = utc_datetime.substr(T_pos + 1, Z_pos - T_pos - 1);
var new_datetime = year_month_day + " " + hour_minute_second; // 2017-03-31 08:02:06
// 处理成为时间戳
timestamp = new Date(Date.parse(new_datetime));
timestamp = timestamp.getTime();
timestamp = timestamp / 1000;
// 增加8个小时,北京时间比utc时间多八个时区
var unixtimestamp = timestamp + 8 * 60 * 60;
// 时间戳转为时间
var unixtimestamp = new Date(unixtimestamp * 1000);
var year = 1900 + unixtimestamp.getYear();
var month = "0" + (unixtimestamp.getMonth() + 1);
var date = "0" + unixtimestamp.getDate();
var hour = "0" + unixtimestamp.getHours();
var minute = "0" + unixtimestamp.getMinutes();
var second = "0" + unixtimestamp.getSeconds();
return year + "-" + month.substring(month.length - 2, month.length) + "-" + date.substring(date.length - 2, date.length)
+ " " + hour.substring(hour.length - 2, hour.length) + ":"
+ minute.substring(minute.length - 2, minute.length) + ":"
+ second.substring(second.length - 2, second.length);
}
// bytes自适应转换到KB,MB,GB
function formatFileSize(bytes) {
if (bytes >= 1073741824) {
bytes = (bytes / 1073741824).toFixed(2) + ' GB';
} else if (bytes >= 1048576) {
bytes = (bytes / 1048576).toFixed(2) + ' MB';
} else if (bytes >= 1024) {
bytes = (bytes / 1024).toFixed(2) + ' KB';
} else if (bytes > 1) {
bytes = bytes + ' bytes';
} else if (bytes == 1) {
bytes = bytes + ' byte';
} else {
bytes = '';
}
return bytes;
}
String.prototype.trim = function (char) {
if (char) {
return this.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');
}
return this.replace(/^\s+|\s+$/g, '');
};
// README.md HEAD.md 支持
function markdown(el, data) {
if (window.md == undefined) {
//$.getScript('https://cdn.jsdelivr.net/npm/[email protected]/dist/markdown-it.min.js',function(){
window.md = window.markdownit();
markdown(el, data);
//});
} else {
var html = md.render(data);
$(el).show().html(html);
}
}
// 监听回退事件
window.onpopstate = function () {
var path = window.location.pathname;
render(path);
}
$(function () {
init();
var path = window.location.pathname;
/*$("body").on("click", '.folder', function () {
var url = $(this).attr('href');
history.pushState(null, null, url);
render(url);
return false;
});
$("body").on("click", '.view', function () {
var url = $(this).attr('href');
history.pushState(null, null, url);
render(url);
return false;
});*/
render(path);
});