From 7a8d66e90f6fe73bd5c9777cd0dadb138a84e991 Mon Sep 17 00:00:00 2001 From: ShallowAi <1402320551@qq.com> Date: Fri, 31 Jan 2020 16:53:56 +0800 Subject: [PATCH] Create apptest3.js --- themes/material/apptest3.js | 489 ++++++++++++++++++++++++++++++++++++ 1 file changed, 489 insertions(+) create mode 100644 themes/material/apptest3.js diff --git a/themes/material/apptest3.js b/themes/material/apptest3.js new file mode 100644 index 0000000..66bf05e --- /dev/null +++ b/themes/material/apptest3.js @@ -0,0 +1,489 @@ +// 在 head 中 加载必要静态 +document.write(''); +// markdown支持 +document.write(''); +// mdui定义 +document.write(''); +// 初始化页面, 并载入必要资源 +function init(){ + document.siteName = $('title').html(); + $('body').addClass("mdui-theme-primary-blue-grey mdui-theme-accent-blue"); + var html = ` +
+ +
+
+
+ `; + $('body').html(html); +} + +function render(path){ + if(path.indexOf("?") > 0){ + path = path.substr(0,path.indexOf("?")); + } + title(path); + nav(path); + if(path.substr(-1) == '/'){ + list(path); + }else{ + file(path); + } +} + + +// 渲染 title +function title(path){ + path = decodeURI(path); + $('title').html(document.siteName+' - '+path); +} + +// 渲染导航栏 +function nav(path){ + var html = ""; + html += `${document.siteName}`; + var arr = path.trim('/').split('/'); + var p = '/'; + if(arr.length > 0){ + for(i in arr){ + var n = arr[i]; + n = decodeURI(n); + p += n+'/'; + if(n == ''){ + break; + } + html += `chevron_right${n}`; + } + } + $('#nav').html(html); +} + +// 渲染文件列表 +function list(path){ + var content = ` + + +
+ +
+
+ +
+ + `; + $('#content').html(content); + + var password = localStorage.getItem('password'+path); + $('#list').html(`
`); + $('#readme_md').hide().html(''); + $('#head_md').hide().html(''); + $.post(path,'{"password":"'+password+'"}', function(data,status){ + var obj = jQuery.parseJSON(data); + if(typeof obj != 'null' && obj.hasOwnProperty('error') && obj.error.code == '401'){ + var pass = prompt("目录加密, 请输入密码",""); + localStorage.setItem('password'+path, pass); + if(pass != null && pass != ""){ + list(path); + }else{ + history.go(-1); + } + }else if(typeof obj != 'null'){ + list_files(path,obj.files); + } + }); +} + +function list_files(path,files){ + html = ""; + for(i in files){ + var item = files[i]; + var p = path+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 +=`
  • +
    + folder_open + ${item.name} +
    +
    ${item['modifiedTime']}
    +
    ${item['size']}
    +
    +
  • `; + }else{ + var p = path+item.name; + var c = "file"; + if(item.name == "README.md"){ + get_file(p, item, function(data){ + markdown("#readme_md",data); + }); + } + if(item.name == "HEAD.md"){ + get_file(p, item, function(data){ + markdown("#head_md",data); + }); + } + var ext = p.split('.').pop(); + if("|html|php|css|go|java|js|json|txt|sh|md|mp4|bmp|jpg|jpeg|png|gif|webp|m4a|mp3|wav|ogg|webm|avi|rm|rmvb|mov|wmv|asf|ts|flv|f4v|m3u8|m4s|mpd|".indexOf(`|${ext}|`) >= 0){ + p += "?a=view"; + c += " view"; + } + html += `
  • +
    + insert_drive_file + ${item.name} +
    +
    ${item['modifiedTime']}
    +
    ${item['size']}
    +
    +
  • `; + } + } + $('#list').html(html); +} + + +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); + }); + } +} + + + +// 文件展示 ?a=view +function file(path){ + var name = path.split('/').pop(); + var ext = name.split('.').pop().toLowerCase().replace(`?a=view`,""); + if("|html|php|css|go|java|js|json|txt|sh|md|".indexOf(`|${ext}|`) >= 0){ + return file_code(path); + } + + if("|mp4|webm|avi|".indexOf(`|${ext}|`) >= 0){ + return file_video(path); + } + + if("|rm|rmvb|mov|wmv|asf|mkv|".indexOf(`|${ext}|`) >= 0){ + $.getScript('//cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.js',function(){ + return file_dpvideo(path); + }); + } + + if("|flv|f4v|".indexOf(`|${ext}|`) >= 0){ + $.getScript('//cdn.jsdelivr.net/npm/flv.js/dist/flv.min.js',function(){ + $.getScript('//cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.js',function(){ + return file_dpvideo(path); + }); + }); + } + + if("|m3u8|ts|".indexOf(`|${ext}|`) >= 0){ + $.getScript('//cdn.jsdelivr.net/npm/hls.js/dist/hls.min.js',function(){ + $.getScript('//cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.js',function(){ + return file_dpvideo(path); + }); + }); + } + + if("|m4s|mpd|".indexOf(`|${ext}|`) >= 0){ + $.getScript('//cdn.jsdelivr.net/npm/dashjs/dist/dash.all.min.js',function(){ + $.getScript('//cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.js',function(){ + return file_dpvideo(path); + }); + }); + } + + if("|mp3|wav|ogg|m4a|".indexOf(`|${ext}|`) >= 0){ + return file_audio(path); + } + + if("|bmp|jpg|jpeg|png|gif|webp|".indexOf(`|${ext}|`) >= 0){ + return file_image(path); + } +} + +// 文件展示 |html|php|css|go|java|js|json|txt|sh|md| +function file_code(path){ + var type = { + "html":"html", + "php":"php", + "css":"css", + "go":"golang", + "java":"java", + "js":"javascript", + "json":"json", + "txt":"text", + "sh":"sh", + "md":"markdown", + }; + var name = path.split('/').pop(); + var ext = name.split('.').pop(); + var href = window.location.origin + path; + var content = ` +
    +
    
    +
    +
    + + +
    +file_download + + + + `; + $('#content').html(content); + + $.get(path, function(data){ + $('#editor').html($('
    ').text(data).html()); + var code_type = "Text"; + if(type[ext] != undefined ){ + code_type = type[ext]; + } + var editor = ace.edit("editor"); + editor.setTheme("ace/theme/ambiance"); + editor.setFontSize(18); + editor.session.setMode("ace/mode/"+code_type); + + //Autocompletion + editor.setOptions({ + enableBasicAutocompletion: true, + enableSnippets: true, + enableLiveAutocompletion: true, + maxLines: Infinity + }); + }); +} + +// 文件展示 视频 |mp4|webm|mpg|mpeg| +function file_video(path){ + var url = window.location.origin + path; + var content = ` +
    +
    + +
    + +
    + + +
    +
    + + +
    +
    +file_download + `; + $('#content').html(content); +} + +// 文件展示 视频 DPlayer |avi|rm|rmvb|mov|wmv|asf|ts|flv|f4v|m3u8|m4s|mpd| +function file_dpvideo(path){ + var url = window.location.origin + path; + var content = ` + +
    +
    +
    +
    + +
    + + +
    +
    + + +
    +
    + + file_download + `; + $('#content').html(content); +} + +// 文件展示 音频 |mp3|m4a|wav|ogg| +function file_audio(path){ + var url = window.location.origin + path; + var content = ` +
    +
    + +
    + +
    + + +
    +
    + + +
    +
    +file_download + `; + $('#content').html(content); +} + + +// 图片展示 +function file_image(path){ + var url = window.location.origin + path; + var content = ` +
    +
    + +
    +
    + + +
    +
    + + +
    +
    + + +
    +
    +
    +file_download + `; + $('#content').html(content); +} + + +//时间转换 +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>=1000000000) {bytes=(bytes/1000000000).toFixed(2)+' GB';} + else if (bytes>=1000000) {bytes=(bytes/1000000).toFixed(2)+' MB';} + else if (bytes>=1000) {bytes=(bytes/1000).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/markdown-it@10.0.0/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); +});