jormungandr-bite/src/web/app/mobile/views/components/drive.vue

574 lines
14 KiB
Vue
Raw Normal View History

2018-02-16 08:36:28 -07:00
<template>
<div class="mk-drive">
2017-10-31 22:11:40 -06:00
<nav ref="nav">
2018-02-16 08:36:28 -07:00
<a @click.prevent="goRoot" href="/i/drive">%fa:cloud%%i18n:mobile.tags.mk-drive.drive%</a>
<template v-for="folder in hierarchyFolders">
<span :key="folder.id + '>'">%fa:angle-right%</span>
<a :key="folder.id" @click.prevent="cd(folder)" :href="`/i/drive/folder/${folder.id}`">{{ folder.name }}</a>
2018-02-07 22:50:18 -07:00
</template>
<template v-if="folder != null">
2017-12-07 10:44:50 -07:00
<span>%fa:angle-right%</span>
2018-02-16 08:36:28 -07:00
<p>{{ folder.name }}</p>
2018-02-07 22:50:18 -07:00
</template>
<template v-if="file != null">
2017-12-07 10:44:50 -07:00
<span>%fa:angle-right%</span>
2018-02-16 08:36:28 -07:00
<p>{{ file.name }}</p>
2018-02-07 22:50:18 -07:00
</template>
2017-01-11 13:55:38 -07:00
</nav>
2017-06-07 00:43:29 -06:00
<mk-uploader ref="uploader"/>
2018-02-16 08:36:28 -07:00
<div class="browser" :class="{ fetching }" v-if="file == null">
2018-02-07 02:34:43 -07:00
<div class="info" v-if="info">
2018-02-16 08:36:28 -07:00
<p v-if="folder == null">{{ (info.usage / info.capacity * 100).toFixed(1) }}% %i18n:mobile.tags.mk-drive.used%</p>
2018-02-07 02:34:43 -07:00
<p v-if="folder != null && (folder.folders_count > 0 || folder.files_count > 0)">
2018-02-16 08:36:28 -07:00
<template v-if="folder.folders_count > 0">{{ folder.folders_count }} %i18n:mobile.tags.mk-drive.folder-count%</template>
2018-02-07 22:50:18 -07:00
<template v-if="folder.folders_count > 0 && folder.files_count > 0">%i18n:mobile.tags.mk-drive.count-separator%</template>
2018-02-16 08:36:28 -07:00
<template v-if="folder.files_count > 0">{{ folder.files_count }} %i18n:mobile.tags.mk-drive.file-count%</template>
2017-03-18 09:02:45 -06:00
</p>
</div>
2018-02-07 02:34:43 -07:00
<div class="folders" v-if="folders.length > 0">
2018-02-16 08:36:28 -07:00
<mk-drive-folder v-for="folder in folders" :key="folder.id" :folder="folder"/>
2018-02-07 02:34:43 -07:00
<p v-if="moreFolders">%i18n:mobile.tags.mk-drive.load-more%</p>
2017-01-11 13:55:38 -07:00
</div>
2018-02-07 02:34:43 -07:00
<div class="files" v-if="files.length > 0">
2018-02-16 08:36:28 -07:00
<mk-drive-file v-for="file in files" :key="file.id" :file="file"/>
2018-02-07 02:34:43 -07:00
<button class="more" v-if="moreFiles" @click="fetchMoreFiles">
2018-02-16 08:36:28 -07:00
{{ fetchingMoreFiles ? '%i18n:common.loading%' : '%i18n:mobile.tags.mk-drive.load-more%' }}
2017-03-25 01:01:36 -06:00
</button>
2017-01-11 13:55:38 -07:00
</div>
2018-02-07 02:34:43 -07:00
<div class="empty" v-if="files.length == 0 && folders.length == 0 && !fetching">
<p v-if="folder == null">%i18n:mobile.tags.mk-drive.nothing-in-drive%</p>
<p v-if="folder != null">%i18n:mobile.tags.mk-drive.folder-is-empty%</p>
2017-01-11 13:55:38 -07:00
</div>
2017-03-18 09:02:45 -06:00
</div>
2018-02-07 02:34:43 -07:00
<div class="fetching" v-if="fetching && file == null && files.length == 0 && folders.length == 0">
2017-03-18 09:02:45 -06:00
<div class="spinner">
<div class="dot1"></div>
<div class="dot2"></div>
2017-01-11 13:55:38 -07:00
</div>
</div>
2018-02-16 08:36:28 -07:00
<input ref="file" type="file" multiple="multiple" @change="onChangeLocalFile"/>
<mk-drive-file-viewer v-if="file != null" :file="file"/>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
props: ['initFolder', 'initFile', 'selectFile', 'multiple', 'isNaked', 'top'],
data() {
return {
/**
* 現在の階層(フォルダ)
* * null でルートを表す
*/
folder: null,
file: null,
files: [],
folders: [],
moreFiles: false,
moreFolders: false,
hierarchyFolders: [],
selectedFiles: [],
info: null,
connection: null,
connectionId: null,
fetching: true,
fetchingMoreFiles: false,
fetchingMoreFolders: false
2017-02-21 09:05:23 -07:00
};
2018-02-16 08:36:28 -07:00
},
computed: {
isFileSelectMode(): boolean {
return this.selectFile;
}
},
mounted() {
this.connection = this.$root.$data.os.streams.driveStream.getConnection();
this.connectionId = this.$root.$data.os.streams.driveStream.use();
this.connection.on('file_created', this.onStreamDriveFileCreated);
this.connection.on('file_updated', this.onStreamDriveFileUpdated);
this.connection.on('folder_created', this.onStreamDriveFolderCreated);
this.connection.on('folder_updated', this.onStreamDriveFolderUpdated);
if (this.initFolder) {
this.cd(this.initFolder, true);
} else if (this.initFile) {
this.cf(this.initFile, true);
} else {
this.fetch();
}
if (this.isNaked) {
(this.$refs.nav as any).style.top = `${this.top}px`;
}
},
beforeDestroy() {
this.connection.off('file_created', this.onStreamDriveFileCreated);
this.connection.off('file_updated', this.onStreamDriveFileUpdated);
this.connection.off('folder_created', this.onStreamDriveFolderCreated);
this.connection.off('folder_updated', this.onStreamDriveFolderUpdated);
this.$root.$data.os.streams.driveStream.dispose(this.connectionId);
},
methods: {
onStreamDriveFileCreated(file) {
this.addFile(file, true);
},
2017-02-21 09:05:23 -07:00
2018-02-16 08:36:28 -07:00
onStreamDriveFileUpdated(file) {
2017-02-21 09:05:23 -07:00
const current = this.folder ? this.folder.id : null;
if (current != file.folder_id) {
this.removeFile(file);
} else {
this.addFile(file, true);
}
2018-02-16 08:36:28 -07:00
},
2017-02-21 09:05:23 -07:00
2018-02-16 08:36:28 -07:00
onStreamDriveFolderCreated(folder) {
2017-02-21 09:05:23 -07:00
this.addFolder(folder, true);
2018-02-16 08:36:28 -07:00
},
2017-02-21 09:05:23 -07:00
2018-02-16 08:36:28 -07:00
onStreamDriveFolderUpdated(folder) {
2017-02-21 09:05:23 -07:00
const current = this.folder ? this.folder.id : null;
if (current != folder.parent_id) {
this.removeFolder(folder);
} else {
this.addFolder(folder, true);
}
2018-02-16 08:36:28 -07:00
},
2017-02-21 09:05:23 -07:00
2018-02-16 08:36:28 -07:00
dive(folder) {
this.hierarchyFolders.unshift(folder);
if (folder.parent) this.dive(folder.parent);
},
2017-02-21 09:05:23 -07:00
2018-02-16 08:36:28 -07:00
cd(target, silent = false) {
2017-02-21 09:05:23 -07:00
this.file = null;
if (target == null) {
this.goRoot();
return;
2018-02-16 08:36:28 -07:00
} else if (typeof target == 'object') {
target = target.id;
}
2017-02-21 09:05:23 -07:00
2018-02-16 08:36:28 -07:00
this.fetching = true;
2016-12-28 15:49:51 -07:00
2018-02-15 01:24:52 -07:00
this.$root.$data.os.api('drive/folders/show', {
2017-02-21 09:05:23 -07:00
folder_id: target
}).then(folder => {
this.folder = folder;
this.hierarchyFolders = [];
2017-01-11 13:55:38 -07:00
2018-02-16 08:36:28 -07:00
if (folder.parent) this.dive(folder.parent);
2017-01-11 13:55:38 -07:00
2018-02-08 21:11:30 -07:00
this.$emit('open-folder', this.folder, silent);
this.fetch();
2017-02-21 09:05:23 -07:00
});
2018-02-16 08:36:28 -07:00
},
2017-01-11 13:55:38 -07:00
2018-02-16 08:36:28 -07:00
addFolder(folder, unshift = false) {
2017-02-21 09:05:23 -07:00
const current = this.folder ? this.folder.id : null;
// 追加しようとしているフォルダが、今居る階層とは違う階層のものだったら中断
if (current != folder.parent_id) return;
2017-01-11 13:55:38 -07:00
2017-02-21 09:05:23 -07:00
// 追加しようとしているフォルダを既に所有してたら中断
if (this.folders.some(f => f.id == folder.id)) return;
2017-01-11 13:55:38 -07:00
2017-02-21 09:05:23 -07:00
if (unshift) {
this.folders.unshift(folder);
} else {
this.folders.push(folder);
}
2018-02-16 08:36:28 -07:00
},
2016-12-28 15:49:51 -07:00
2018-02-16 08:36:28 -07:00
addFile(file, unshift = false) {
2017-02-21 09:05:23 -07:00
const current = this.folder ? this.folder.id : null;
// 追加しようとしているファイルが、今居る階層とは違う階層のものだったら中断
if (current != file.folder_id) return;
2017-01-11 13:55:38 -07:00
2017-02-21 09:05:23 -07:00
if (this.files.some(f => f.id == file.id)) {
const exist = this.files.map(f => f.id).indexOf(file.id);
2018-02-16 08:36:28 -07:00
this.files[exist] = file; // TODO
2017-02-21 09:05:23 -07:00
return;
}
2016-12-28 15:49:51 -07:00
2017-02-21 09:05:23 -07:00
if (unshift) {
this.files.unshift(file);
} else {
this.files.push(file);
}
2018-02-16 08:36:28 -07:00
},
2017-01-11 13:55:38 -07:00
2018-02-16 08:36:28 -07:00
removeFolder(folder) {
2017-02-21 09:05:23 -07:00
if (typeof folder == 'object') folder = folder.id;
this.folders = this.folders.filter(f => f.id != folder);
2018-02-16 08:36:28 -07:00
},
2017-01-11 13:55:38 -07:00
2018-02-16 08:36:28 -07:00
removeFile(file) {
2017-02-21 09:05:23 -07:00
if (typeof file == 'object') file = file.id;
this.files = this.files.filter(f => f.id != file);
2018-02-16 08:36:28 -07:00
},
appendFile(file) {
this.addFile(file);
},
appendFolder(folder) {
this.addFolder(folder);
},
prependFile(file) {
this.addFile(file, true);
},
prependFolder(folder) {
this.addFolder(folder, true);
},
2017-12-08 10:11:48 -07:00
2018-02-16 08:36:28 -07:00
goRoot() {
2017-02-21 09:05:23 -07:00
if (this.folder || this.file) {
2018-02-16 08:36:28 -07:00
this.file = null;
this.folder = null;
this.hierarchyFolders = [];
2018-02-08 21:11:30 -07:00
this.$emit('move-root');
this.fetch();
2017-02-21 09:05:23 -07:00
}
2018-02-16 08:36:28 -07:00
},
2017-12-08 09:51:05 -07:00
2018-02-16 08:36:28 -07:00
fetch() {
this.folders = [];
this.files = [];
this.moreFolders = false;
this.moreFiles = false;
this.fetching = true;
2017-01-11 13:55:38 -07:00
2018-02-08 21:11:30 -07:00
this.$emit('begin-fetch');
2017-01-11 13:55:38 -07:00
2017-02-21 09:05:23 -07:00
let fetchedFolders = null;
let fetchedFiles = null;
2017-01-11 13:55:38 -07:00
2017-02-21 09:05:23 -07:00
const foldersMax = 20;
const filesMax = 20;
2017-01-11 13:55:38 -07:00
2017-02-19 17:53:57 -07:00
// フォルダ一覧取得
2018-02-15 01:24:52 -07:00
this.$root.$data.os.api('drive/folders', {
2017-02-21 09:05:23 -07:00
folder_id: this.folder ? this.folder.id : null,
limit: foldersMax + 1
}).then(folders => {
if (folders.length == foldersMax + 1) {
this.moreFolders = true;
folders.pop();
}
fetchedFolders = folders;
complete();
});
2017-01-11 13:55:38 -07:00
2017-02-19 17:53:57 -07:00
// ファイル一覧取得
2018-02-15 01:24:52 -07:00
this.$root.$data.os.api('drive/files', {
2017-02-21 09:05:23 -07:00
folder_id: this.folder ? this.folder.id : null,
limit: filesMax + 1
}).then(files => {
if (files.length == filesMax + 1) {
this.moreFiles = true;
files.pop();
}
fetchedFiles = files;
complete();
});
let flag = false;
2017-02-21 10:05:44 -07:00
const complete = () => {
2017-02-21 09:05:23 -07:00
if (flag) {
2017-02-23 08:18:33 -07:00
fetchedFolders.forEach(this.appendFolder);
fetchedFiles.forEach(this.appendFile);
2018-02-16 08:36:28 -07:00
this.fetching = false;
2017-02-21 09:05:23 -07:00
// 一連の読み込みが完了したイベントを発行
2018-02-08 21:11:30 -07:00
this.$emit('fetched');
2017-02-21 09:05:23 -07:00
} else {
flag = true;
// 一連の読み込みが半分完了したイベントを発行
2018-02-08 21:11:30 -07:00
this.$emit('fetch-mid');
2017-02-21 09:05:23 -07:00
}
};
2017-03-18 09:02:45 -06:00
if (this.folder == null) {
// Fetch addtional drive info
2018-02-15 01:24:52 -07:00
this.$root.$data.os.api('drive').then(info => {
2018-02-16 08:36:28 -07:00
this.info = info;
2017-03-18 09:02:45 -06:00
});
}
2018-02-16 08:36:28 -07:00
},
2017-02-21 09:05:23 -07:00
2018-02-16 08:36:28 -07:00
fetchMoreFiles() {
this.fetching = true;
this.fetchingMoreFiles = true;
const max = 30;
// ファイル一覧取得
2018-02-15 01:24:52 -07:00
this.$root.$data.os.api('drive/files', {
folder_id: this.folder ? this.folder.id : null,
2017-06-06 10:55:02 -06:00
limit: max + 1,
2017-12-20 10:20:02 -07:00
until_id: this.files[this.files.length - 1].id
}).then(files => {
if (files.length == max + 1) {
this.moreFiles = true;
files.pop();
} else {
this.moreFiles = false;
}
files.forEach(this.appendFile);
2018-02-16 08:36:28 -07:00
this.fetching = false;
this.fetchingMoreFiles = false;
});
2018-02-16 08:36:28 -07:00
},
2018-02-16 08:36:28 -07:00
chooseFile(file) {
if (this.isFileSelectMode) {
2017-08-28 05:23:47 -06:00
if (this.multiple) {
if (this.selectedFiles.some(f => f.id == file.id)) {
this.selectedFiles = this.selectedFiles.filter(f => f.id != file.id);
} else {
this.selectedFiles.push(file);
}
2018-02-08 21:11:30 -07:00
this.$emit('change-selection', this.selectedFiles);
2017-02-21 09:05:23 -07:00
} else {
2018-02-08 21:11:30 -07:00
this.$emit('selected', file);
2017-02-21 09:05:23 -07:00
}
} else {
this.cf(file);
}
2018-02-16 08:36:28 -07:00
},
2017-02-16 01:20:45 -07:00
2018-02-16 08:36:28 -07:00
cf(file, silent = false) {
2017-02-21 09:05:23 -07:00
if (typeof file == 'object') file = file.id;
2017-02-16 01:20:45 -07:00
2018-02-16 08:36:28 -07:00
this.fetching = true;
2017-02-16 01:20:45 -07:00
2018-02-15 01:24:52 -07:00
this.$root.$data.os.api('drive/files/show', {
2017-02-16 01:20:45 -07:00
file_id: file
2017-02-21 09:05:23 -07:00
}).then(file => {
2017-03-18 10:16:55 -06:00
this.fetching = false;
2017-02-21 09:05:23 -07:00
this.file = file;
this.folder = null;
this.hierarchyFolders = [];
2017-02-16 01:20:45 -07:00
2018-02-16 08:36:28 -07:00
if (file.folder) this.dive(file.folder);
2017-02-16 01:20:45 -07:00
2018-02-08 21:11:30 -07:00
this.$emit('open-file', this.file, silent);
2017-02-21 09:05:23 -07:00
});
2018-02-16 08:36:28 -07:00
},
2017-03-12 23:56:53 -06:00
2018-02-16 08:36:28 -07:00
openContextMenu() {
2017-03-12 23:56:53 -06:00
const fn = window.prompt('何をしますか?(数字を入力してください): <1 → ファイルをアップロード | 2 → ファイルをURLでアップロード | 3 → フォルダ作成 | 4 → このフォルダ名を変更 | 5 → このフォルダを移動 | 6 → このフォルダを削除>');
if (fn == null || fn == '') return;
switch (fn) {
case '1':
2017-10-31 12:31:36 -06:00
this.selectLocalFile();
2017-03-12 23:56:53 -06:00
break;
case '2':
this.urlUpload();
break;
case '3':
this.createFolder();
break;
case '4':
this.renameFolder();
break;
case '5':
this.moveFolder();
break;
case '6':
alert('ごめんなさい!フォルダの削除は未実装です...。');
break;
}
2018-02-16 08:36:28 -07:00
},
2017-03-12 23:56:53 -06:00
2018-02-16 08:36:28 -07:00
selectLocalFile() {
(this.$refs.file as any).click();
},
2017-10-31 12:31:36 -06:00
2018-02-16 08:36:28 -07:00
createFolder() {
2017-03-12 23:56:53 -06:00
const name = window.prompt('フォルダー名');
if (name == null || name == '') return;
2018-02-15 01:24:52 -07:00
this.$root.$data.os.api('drive/folders/create', {
2017-03-12 23:56:53 -06:00
name: name,
2017-03-13 07:07:14 -06:00
parent_id: this.folder ? this.folder.id : undefined
2017-03-12 23:56:53 -06:00
}).then(folder => {
this.addFolder(folder, true);
});
2018-02-16 08:36:28 -07:00
},
2017-03-12 23:56:53 -06:00
2018-02-16 08:36:28 -07:00
renameFolder() {
2017-03-12 23:56:53 -06:00
if (this.folder == null) {
alert('現在いる場所はルートで、フォルダではないため名前の変更はできません。名前を変更したいフォルダに移動してからやってください。');
return;
}
const name = window.prompt('フォルダー名', this.folder.name);
if (name == null || name == '') return;
2018-02-15 01:24:52 -07:00
this.$root.$data.os.api('drive/folders/update', {
2017-03-12 23:56:53 -06:00
name: name,
folder_id: this.folder.id
}).then(folder => {
this.cd(folder);
});
2018-02-16 08:36:28 -07:00
},
2017-03-12 23:56:53 -06:00
2018-02-16 08:36:28 -07:00
moveFolder() {
2017-03-12 23:56:53 -06:00
if (this.folder == null) {
alert('現在いる場所はルートで、フォルダではないため移動はできません。移動したいフォルダに移動してからやってください。');
return;
}
const dialog = riot.mount(document.body.appendChild(document.createElement('mk-drive-folder-selector')))[0];
dialog.one('selected', folder => {
2018-02-15 01:24:52 -07:00
this.$root.$data.os.api('drive/folders/update', {
parent_id: folder ? folder.id : null,
2017-03-12 23:56:53 -06:00
folder_id: this.folder.id
}).then(folder => {
this.cd(folder);
});
});
2018-02-16 08:36:28 -07:00
},
2017-03-12 23:56:53 -06:00
2018-02-16 08:36:28 -07:00
urlUpload() {
2017-03-12 23:56:53 -06:00
const url = window.prompt('アップロードしたいファイルのURL');
if (url == null || url == '') return;
2018-02-15 01:24:52 -07:00
this.$root.$data.os.api('drive/files/upload_from_url', {
2017-03-12 23:56:53 -06:00
url: url,
folder_id: this.folder ? this.folder.id : undefined
});
alert('アップロードをリクエストしました。アップロードが完了するまで時間がかかる場合があります。');
2018-02-16 08:36:28 -07:00
},
onChangeLocalFile() {
Array.from((this.$refs.file as any).files)
.forEach(f => (this.$refs.uploader as any).upload(f, this.folder));
}
}
});
</script>
<style lang="stylus" scoped>
.mk-drive
background #fff
> nav
display block
position sticky
position -webkit-sticky
top 0
z-index 1
width 100%
padding 10px 12px
overflow auto
white-space nowrap
font-size 0.9em
color rgba(0, 0, 0, 0.67)
-webkit-backdrop-filter blur(12px)
backdrop-filter blur(12px)
background-color rgba(#fff, 0.75)
border-bottom solid 1px rgba(0, 0, 0, 0.13)
> p
> a
display inline
margin 0
padding 0
text-decoration none !important
color inherit
&:last-child
font-weight bold
> [data-fa]
margin-right 4px
> span
margin 0 8px
opacity 0.5
> .browser
&.fetching
opacity 0.5
> .info
border-bottom solid 1px #eee
&:empty
display none
2017-03-12 23:56:53 -06:00
2018-02-16 08:36:28 -07:00
> p
display block
max-width 500px
margin 0 auto
padding 4px 16px
font-size 10px
color #777
> .folders
> mk-drive-folder
border-bottom solid 1px #eee
> .files
> mk-drive-file
border-bottom solid 1px #eee
> .more
display block
width 100%
padding 16px
font-size 16px
color #555
> .empty
padding 16px
text-align center
color #999
pointer-events none
> p
margin 0
> .fetching
.spinner
margin 100px auto
width 40px
height 40px
text-align center
animation sk-rotate 2.0s infinite linear
.dot1, .dot2
width 60%
height 60%
display inline-block
position absolute
top 0
background rgba(0, 0, 0, 0.2)
border-radius 100%
animation sk-bounce 2.0s infinite ease-in-out
.dot2
top auto
bottom 0
animation-delay -1.0s
@keyframes sk-rotate { 100% { transform: rotate(360deg); }}
@keyframes sk-bounce {
0%, 100% {
transform: scale(0.0);
} 50% {
transform: scale(1.0);
}
}
> [ref='file']
display none
</style>