Skip to content

Commit

Permalink
Update Vue components. 'Not support IE 8 or eariler'. now pluggable.
Browse files Browse the repository at this point in the history
  • Loading branch information
ganlvtech committed Sep 21, 2018
1 parent 8562be8 commit 157c42f
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 77 deletions.
14 changes: 13 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@
</div>
</div>
</noscript>
<!--[if lt IE 9]>
<div class="container">
<div class="alert alert-danger">
<strong>非常抱歉:使用 IE 8 或者更早版本的浏览器访问本网站可能给您带来较差的交互体验,请您将浏览器升级至最新版本。推荐使用 <a href="https://www.google.cn/chrome/">Chrome 浏览器</a> 访问本页面。</strong>
</div>
</div>
<![endif]-->
<div class="container">
<div class="alert alert-primary alert-dismissible fade show" role="alert">
<ul>
Expand All @@ -104,11 +111,16 @@ <h1 class="current-path"><%= VUE_APP_DOWNLOAD_BASE_URL %>/</h1>
</p>
</div>
<script>
(function f() {
(function () {
var ths = document.querySelectorAll('th');
for (var i = 0; i < ths.length; ++i) {
ths[i].style.width = '';
}
window.addEventListener('DOMContentLoaded', function () {
window.down52PojieCn = new Down52PojieCn({
requestType: 'jsonp'
});
});
})();
</script>
</body>
Expand Down
149 changes: 149 additions & 0 deletions src/Down52PojieCn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import axios from 'axios';
import jQuery from 'jquery';
import toastr from 'toastr';
import Vue from 'vue';
import Router from 'vue-router';
import Home from './views/Home.vue';
import App from './App.vue';
import {cacheTimestamp} from './helpers';

const $ = jQuery;

Vue.config.productionTip = false;
Vue.use(Router);

export default function Down52PojieCn(config = {}) {
/**
* Vue 实例的挂载目标
*
* @default '#app'
* @type string
* @see https://cn.vuejs.org/v2/api/#el
*/
this.vueElement = config.vueElement || '#app';

/**
* Vue Router 历史记录模式
*
* 'hash' or 'history'
*
* @default 'hash'
* @type string
* @see https://router.vuejs.org/api/#router-mode
*/
this.routerMode = config.routerMode || 'hash';

/**
* 基础网址(末尾不加斜线)
*
* @type string
* @default 'https://down.52pojie.cn'
*/
this.baseUrl = config.baseUrl || 'https://down.52pojie.cn';

/**
* 请求数据文件的方法
*
* 'json' or 'jsonp'
*
* @default 'json'
* @type string
*/
this.requestType = config.requestType || 'json';

/**
* json 文件的 URL
*
* @type string
* @default '/list.json'
*/
this.jsonUrl = config.jsonUrl || '/list.json';

/**
* jsonp 请求的 URL
*
* @type string
* @default '/list.js'
*/
this.jsonpUrl = config.jsonpUrl || '/list.js';

/**
* jsonp 请求的回调函数名,
*
* @type string
* @default '__jsonpCallbackDown52PojieCn'
*/
this.jsonpCallback = config.jsonpCallback || '__jsonpCallbackDown52PojieCn';

/**
* 数据文件缓存时间
*
* 数字表示缓存时间(秒),请求后面会加上类似 ?t=153xxxxxxx 的时间戳,这个时间戳是通过整除得到的,通常是可以通过 CDN 缓存的。
* falsy value 表示不加 ?t= 的时间戳,是否读取缓存靠服务器的缓存控制
*
* @type {int}
* @default 0
*/
this.cacheTime = config.cacheTime || 0;

this.router = null;
toastr.info('爱盘搜索扩展插件加载完成,正在加载文件列表');
this[this.requestType]();
}

Down52PojieCn.prototype.json = function () {
let url = this.jsonUrl;
if (this.cacheTime) {
url += '?t=' + cacheTimestamp(this.cacheTime);
}
axios.get(url)
.then(response => {
let data = response.data;
this.callback(data);
})
.catch(error => {
toastr.error(`通过 ajax 加载文件出错,请刷新页面或联系管理员。错误信息:${error.message}`);
throw error;
});
};

Down52PojieCn.prototype.jsonp = function () {
window[this.jsonpCallback] = data => {
clearTimeout(timeout);
this.callback(data);
window[this.jsonpCallback] = undefined;
};
let script = document.createElement('script');
script.src = this.jsonpUrl;
document.getElementsByTagName('head')[0].appendChild(script);
let timeout = setTimeout(function () {
toastr.error('通过 jsonp 加载文件列表超时,请刷新页面或联系管理员。');
}, 20000);
};

Down52PojieCn.prototype.callback = function (data) {
let router = new Router({
mode: this.routerMode,
base: '/',
routes: [
{
path: '(.*)',
name: 'home',
component: Home,
props: {
baseUrl: this.baseUrl,
list: data
}
}
]
});
this.router = router;

this.vm = new Vue({
el: this.vueElement,
router,
render: h => h(App)
});

$('#main').hide();
};
33 changes: 11 additions & 22 deletions src/components/FileExplorer.vue
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
<template>
<div>
<FilePath :file="currentFile" :is-search="isSearch" :query="searchQuery"/>
<form class="form-inline" action="/" method="post" @submit.prevent="submitForm">
<div class="form-group mb-2">
<label for="search-input">查找文件:</label>
</div>
<div class="form-group mr-2 mb-2">
<input id="search-input" ref="searchInput" :value="searchQuery" type="search" name="query" class="form-control" @input="submitDebounce">
</div>
<button type="submit" class="btn btn-primary mr-2 mb-2">搜索</button>
<a class="btn btn-outline-primary mb-2" @click="submitAll">全部文件</a></form>
<FilePath :base-url="baseUrl" :file="currentFile" :is-search="isSearch" :query="searchQuery"/>
<SearchBox :query="searchQuery" @submit="submit"/>
<FileTable :files="files" :desc="false" order-by="name"/>
</div>
</template>

<script>
import FilePath from '../components/FilePath.vue';
import SearchBox from '../components/SearchBox.vue';
import FileTable from '../components/FileTable.vue';
import {flattenFiles} from '../helpers';
import toastr from 'toastr';
export default {
components: {
FilePath,
SearchBox,
FileTable
},
props: {
baseUrl: {
type: String,
default: ''
},
root: {
type: Object,
default() {
Expand All @@ -47,7 +45,7 @@
},
computed: {
path() {
return decodeURI(this.$route.path);
return this.$route.params[0];
},
stats() {
let lastUpdate = this.root.timeFromNowForHuman;
Expand Down Expand Up @@ -106,21 +104,12 @@
}
},
mounted() {
toastr.success(`文件列表加载完成。最后更新于 ${this.stats.lastUpdate}爱盘包含 ${this.stats.fileCount} 个文件、${this.stats.dirCount} 个文件夹,共计 ${this.stats.size}`);
toastr.success(`文件列表加载完成。最后更新于 ${this.stats.lastUpdate}包含 ${this.stats.fileCount} 个文件、${this.stats.dirCount} 个文件夹,共计 ${this.stats.size}`);
},
methods: {
submit(query) {
this.$router.push({name: 'home', query: {query: query}});
},
submitDebounce: _.debounce(function () {
this.submit(this.$refs.searchInput.value);
}, 500),
submitForm() {
this.submit(this.$refs.searchInput.value);
this.$router.push({name: 'home', query: {query: query}, params: {0: ''}});
},
submitAll() {
this.submit('');
}
}
};
</script>
18 changes: 13 additions & 5 deletions src/components/FilePath.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<router-link to="/">{{ baseUrl }}</router-link>
</li>
<li v-for="part in parts" :key="part.i" :class="{active: part.active}" class="breadcrumb-item">
<router-link :to="part.file.path">{{ part.file.name }}</router-link>
<span v-if="part.active">{{ part.file.name }}</span>
<router-link v-else :to="part.file.path">{{ part.file.name }}</router-link>
</li>
<li v-if="isSearch" class="breadcrumb-item">
<span>搜索文件:{{ query }}</span>
Expand All @@ -20,6 +21,10 @@
export default {
components: {},
props: {
baseUrl: {
type: String,
default: ''
},
file: {
type: Object,
default() {
Expand Down Expand Up @@ -51,17 +56,20 @@
if (file.hasOwnProperty('parent')) {
parts.push({
key: i,
file: file
file: file,
active: false
});
file = file.parent;
} else {
file = null;
}
}
if (parts.length > 0) {
if (!this.isSearch) {
parts[0].active = true;
}
}
return _.reverse(parts);
},
baseUrl() {
return process.env.VUE_APP_DOWNLOAD_BASE_URL;
}
}
};
Expand Down
39 changes: 39 additions & 0 deletions src/components/SearchBox.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<template>
<div>
<form class="form-inline" action="/" method="post" @submit.prevent="submitForm">
<div class="form-group mb-2">
<label for="search-input">查找文件:</label>
</div>
<div class="form-group mr-2 mb-2">
<input id="search-input" ref="searchInput" :value="query" type="search" name="query" class="form-control" @input="submitDebounce">
</div>
<button type="submit" class="btn btn-primary mr-2 mb-2">搜索</button>
<a class="btn btn-outline-primary mb-2" @click="submitAll">全部文件</a>
</form>
</div>
</template>

<script>
export default {
props: {
query: {
type: String,
default: ''
}
},
methods: {
submit(query) {
this.$emit('submit', query);
},
submitDebounce: _.debounce(function () {
this.submit(this.$refs.searchInput.value);
}, 500),
submitForm() {
this.submit(this.$refs.searchInput.value);
},
submitAll() {
this.submit('');
}
}
};
</script>
3 changes: 3 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ export function flattenFiles(file, all = []) {
return all;
}

export function cacheTimestamp(cacheTime = 86400) {
return Math.floor(Date.now() / 1000 / cacheTime) * cacheTime;
}
25 changes: 2 additions & 23 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
import './styles/index.scss';
import Down52PojieCn from './Down52PojieCn';

import Vue from 'vue';
import Router from 'vue-router';
import Home from './views/Home.vue';
import App from './App.vue';

window.$ = window.jQuery = require('jquery');
window.Popper = require('popper.js');
require('bootstrap');

Vue.config.productionTip = false;
Vue.use(Router);

const router = new Router({
mode: process.env.VUE_APP_ROUTER_MODE || 'hash',
base: '/',
routes: [
{path: '(.*)', name:'home', component: Home}
]
});

window.vm = new Vue({
el: '#app',
router,
render: h => h(App)
});
window.Down52PojieCn = Down52PojieCn;
Loading

0 comments on commit 157c42f

Please sign in to comment.