iceshrimp/src/web/app/desktop/-tags/drive/browser.tag

737 lines
18 KiB
HTML
Raw Normal View History

2017-01-11 21:55:38 +01:00
<mk-drive-browser>
<nav>
<div class="path" oncontextmenu={ pathOncontextmenu }>
2018-02-08 06:02:08 +01:00
<mk-drive-browser-nav-folder :class="{ current: folder == null }" folder={ null }/>
2018-02-08 06:50:18 +01:00
<template each={ folder in hierarchyFolders }>
2017-12-07 18:44:50 +01:00
<span class="separator">%fa:angle-right%</span>
2017-06-07 08:43:29 +02:00
<mk-drive-browser-nav-folder folder={ folder }/>
2018-02-08 06:50:18 +01:00
</template>
2018-02-07 10:34:43 +01:00
<span class="separator" v-if="folder != null">%fa:angle-right%</span>
<span class="folder current" v-if="folder != null">{ folder.name }</span>
2017-01-11 21:55:38 +01:00
</div>
2017-04-04 13:07:13 +02:00
<input class="search" type="search" placeholder="&#xf002; %i18n:desktop.tags.mk-drive-browser.search%"/>
2017-01-11 21:55:38 +01:00
</nav>
2017-02-21 01:49:35 +01:00
<div class="main { uploading: uploads.length > 0, fetching: fetching }" ref="main" onmousedown={ onmousedown } ondragover={ ondragover } ondragenter={ ondragenter } ondragleave={ ondragleave } ondrop={ ondrop } oncontextmenu={ oncontextmenu }>
2017-01-11 21:55:38 +01:00
<div class="selection" ref="selection"></div>
<div class="contents" ref="contents">
2018-02-07 10:34:43 +01:00
<div class="folders" ref="foldersContainer" v-if="folders.length > 0">
2018-02-08 06:50:18 +01:00
<template each={ folder in folders }>
2017-06-07 08:43:29 +02:00
<mk-drive-browser-folder class="folder" folder={ folder }/>
2018-02-08 06:50:18 +01:00
</template>
2017-12-19 05:18:17 +01:00
<!-- SEE: https://stackoverflow.com/questions/18744164/flex-box-align-last-row-to-grid -->
<div class="padding" each={ Array(10).fill(16) }></div>
2018-02-07 10:34:43 +01:00
<button v-if="moreFolders">%i18n:desktop.tags.mk-drive-browser.load-more%</button>
2017-01-11 21:55:38 +01:00
</div>
2018-02-07 10:34:43 +01:00
<div class="files" ref="filesContainer" v-if="files.length > 0">
2018-02-08 06:50:18 +01:00
<template each={ file in files }>
2017-06-07 08:43:29 +02:00
<mk-drive-browser-file class="file" file={ file }/>
2018-02-08 06:50:18 +01:00
</template>
2017-12-19 05:18:17 +01:00
<!-- SEE: https://stackoverflow.com/questions/18744164/flex-box-align-last-row-to-grid -->
<div class="padding" each={ Array(10).fill(16) }></div>
2018-02-07 10:34:43 +01:00
<button v-if="moreFiles" @click="fetchMoreFiles">%i18n:desktop.tags.mk-drive-browser.load-more%</button>
2017-01-11 21:55:38 +01:00
</div>
2018-02-07 10:34:43 +01:00
<div class="empty" v-if="files.length == 0 && folders.length == 0 && !fetching">
<p v-if="draghover">%i18n:desktop.tags.mk-drive-browser.empty-draghover%</p>
<p v-if="!draghover && folder == null"><strong>%i18n:desktop.tags.mk-drive-browser.empty-drive%</strong><br/>%i18n:desktop.tags.mk-drive-browser.empty-drive-description%</p>
<p v-if="!draghover && folder != null">%i18n:desktop.tags.mk-drive-browser.empty-folder%</p>
2017-01-11 21:55:38 +01:00
</div>
</div>
2018-02-07 10:34:43 +01:00
<div class="fetching" v-if="fetching">
2016-12-28 23:49:51 +01:00
<div class="spinner">
<div class="dot1"></div>
<div class="dot2"></div>
</div>
2017-01-11 21:55:38 +01:00
</div>
</div>
2018-02-07 10:34:43 +01:00
<div class="dropzone" v-if="draghover"></div>
2017-06-07 08:43:29 +02:00
<mk-uploader ref="uploader"/>
<input ref="fileInput" type="file" accept="*/*" multiple="multiple" tabindex="-1" onchange={ changeFileInput }/>
2018-02-07 10:30:17 +01:00
<style lang="stylus" scoped>
2017-01-11 21:55:38 +01:00
:scope
display block
> nav
display block
z-index 2
width 100%
overflow auto
font-size 0.9em
color #555
background #fff
//border-bottom 1px solid #dfdfdf
box-shadow 0 1px 0 rgba(0, 0, 0, 0.05)
2016-12-28 23:49:51 +01:00
2017-01-11 21:55:38 +01:00
&, *
user-select none
2016-12-28 23:49:51 +01:00
2017-01-11 21:55:38 +01:00
> .path
display inline-block
vertical-align bottom
2016-12-28 23:49:51 +01:00
margin 0
2017-01-11 21:55:38 +01:00
padding 0 8px
width calc(100% - 200px)
line-height 38px
white-space nowrap
2016-12-28 23:49:51 +01:00
2017-01-11 21:55:38 +01:00
> *
display inline-block
2016-12-28 23:49:51 +01:00
margin 0
2017-01-11 21:55:38 +01:00
padding 0 8px
line-height 38px
cursor pointer
2016-12-28 23:49:51 +01:00
2017-01-11 21:55:38 +01:00
i
margin-right 4px
*
pointer-events none
2016-12-28 23:49:51 +01:00
2017-01-11 21:55:38 +01:00
&:hover
text-decoration underline
2016-12-28 23:49:51 +01:00
2017-01-11 21:55:38 +01:00
&.current
font-weight bold
cursor default
2016-12-28 23:49:51 +01:00
2017-01-11 21:55:38 +01:00
&:hover
text-decoration none
2016-12-28 23:49:51 +01:00
2017-01-11 21:55:38 +01:00
&.separator
margin 0
padding 0
opacity 0.5
cursor default
2016-12-28 23:49:51 +01:00
2017-12-07 18:44:50 +01:00
> [data-fa]
2017-01-11 21:55:38 +01:00
margin 0
> .search
display inline-block
vertical-align bottom
user-select text
cursor auto
margin 0
padding 0 18px
width 200px
font-size 1em
line-height 38px
background transparent
outline none
//border solid 1px #ddd
border none
border-radius 0
box-shadow none
transition color 0.5s ease, border 0.5s ease
font-family FontAwesome, sans-serif
&[data-active='true']
background #fff
&::-webkit-input-placeholder,
&:-ms-input-placeholder,
&:-moz-placeholder
2017-02-27 09:26:32 +01:00
color $ui-control-foreground-color
2017-01-11 21:55:38 +01:00
> .main
padding 8px
height calc(100% - 38px)
overflow auto
&, *
user-select none
2017-02-21 01:49:35 +01:00
&.fetching
2017-01-11 21:55:38 +01:00
cursor wait !important
*
pointer-events none
> .contents
opacity 0.5
&.uploading
height calc(100% - 38px - 100px)
> .selection
display none
position absolute
z-index 128
top 0
left 0
border solid 1px $theme-color
background rgba($theme-color, 0.5)
pointer-events none
> .contents
> .folders
> .files
2017-11-20 00:47:03 +01:00
display flex
flex-wrap wrap
2017-01-11 21:55:38 +01:00
2017-11-20 00:47:03 +01:00
> .folder
2017-01-11 21:55:38 +01:00
> .file
2017-11-20 00:47:03 +01:00
flex-grow 1
width 144px
margin 4px
> .padding
flex-grow 1
pointer-events none
width 144px + 8px // 8px is margin
2017-01-11 21:55:38 +01:00
> .empty
padding 16px
text-align center
color #999
pointer-events none
> p
margin 0
2017-02-21 01:49:35 +01:00
> .fetching
2017-01-11 21:55:38 +01:00
.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-color rgba(0, 0, 0, 0.3)
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);
}
}
> .dropzone
position absolute
left 0
top 38px
width 100%
height calc(100% - 38px)
border dashed 2px rgba($theme-color, 0.5)
2016-12-28 23:49:51 +01:00
pointer-events none
2017-01-11 21:55:38 +01:00
> mk-uploader
height 100px
padding 16px
background #fff
2016-12-28 23:49:51 +01:00
2017-01-11 21:55:38 +01:00
> input
display none
2016-12-28 23:49:51 +01:00
2017-01-11 21:55:38 +01:00
</style>
2018-02-07 10:47:29 +01:00
<script lang="typescript">
2017-03-18 12:05:11 +01:00
import contains from '../../../common/scripts/contains';
import dialog from '../../scripts/dialog';
import inputDialog from '../../scripts/input-dialog';
2017-02-20 17:02:32 +01:00
2017-03-18 12:05:11 +01:00
this.mixin('i');
2017-02-20 01:53:57 +01:00
this.mixin('api');
2017-11-16 15:46:36 +01:00
this.mixin('drive-stream');
this.connection = this.driveStream.getConnection();
this.connectionId = this.driveStream.use();
2016-12-28 23:49:51 +01:00
2017-02-20 03:02:43 +01:00
this.files = [];
this.folders = [];
this.hierarchyFolders = [];
2017-02-23 09:12:09 +01:00
this.selectedFiles = [];
2016-12-28 23:49:51 +01:00
2017-02-20 03:02:43 +01:00
this.uploads = [];
2016-12-28 23:49:51 +01:00
2017-02-20 01:53:57 +01:00
// 現在の階層(フォルダ)
// * null でルートを表す
2017-02-20 03:02:43 +01:00
this.folder = null;
2016-12-28 23:49:51 +01:00
2017-02-20 17:02:32 +01:00
this.multiple = this.opts.multiple != null ? this.opts.multiple : false;
2016-12-28 23:49:51 +01:00
2017-02-20 01:53:57 +01:00
// ドロップされようとしているか
2017-02-20 03:02:43 +01:00
this.draghover = false;
2016-12-28 23:49:51 +01:00
2017-02-20 01:53:57 +01:00
// 自信の所有するアイテムがドラッグをスタートさせたか
// (自分自身の階層にドロップできないようにするためのフラグ)
2017-02-20 17:02:32 +01:00
this.isDragSource = false;
2016-12-28 23:49:51 +01:00
2017-02-20 01:53:57 +01:00
this.on('mount', () => {
2018-02-07 10:17:59 +01:00
this.$refs.uploader.on('uploaded', file => {
2017-02-20 17:02:32 +01:00
this.addFile(file, true);
});
2016-12-28 23:49:51 +01:00
2018-02-07 10:17:59 +01:00
this.$refs.uploader.on('change-uploads', uploads => {
2017-02-20 17:02:32 +01:00
this.update({
uploads: uploads
});
});
2016-12-28 23:49:51 +01:00
2017-11-16 15:46:36 +01:00
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);
2016-12-28 23:49:51 +01:00
2017-03-26 21:39:46 +02:00
if (this.opts.folder) {
2017-02-21 01:49:35 +01:00
this.move(this.opts.folder);
} else {
this.fetch();
2017-02-21 01:49:35 +01:00
}
});
2016-12-28 23:49:51 +01:00
2017-02-20 01:53:57 +01:00
this.on('unmount', () => {
2017-11-16 15:46:36 +01:00
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.driveStream.dispose(this.connectionId);
2017-02-21 01:49:35 +01:00
});
this.onStreamDriveFileCreated = file => {
this.addFile(file, true);
};
this.onStreamDriveFileUpdated = file => {
const current = this.folder ? this.folder.id : null;
if (current != file.folder_id) {
this.removeFile(file);
} else {
this.addFile(file, true);
}
};
this.onStreamDriveFolderCreated = folder => {
this.addFolder(folder, true);
};
this.onStreamDriveFolderUpdated = folder => {
const current = this.folder ? this.folder.id : null;
if (current != folder.parent_id) {
this.removeFolder(folder);
} else {
this.addFolder(folder, true);
}
};
this.onmousedown = e => {
2018-02-07 10:17:59 +01:00
if (contains(this.$refs.foldersContainer, e.target) || contains(this.$refs.filesContainer, e.target)) return true;
2017-02-21 01:49:35 +01:00
2018-02-07 10:17:59 +01:00
const rect = this.$refs.main.getBoundingClientRect();
2017-02-21 01:49:35 +01:00
2018-02-07 10:17:59 +01:00
const left = e.pageX + this.$refs.main.scrollLeft - rect.left - window.pageXOffset
const top = e.pageY + this.$refs.main.scrollTop - rect.top - window.pageYOffset
2017-02-21 01:49:35 +01:00
const move = e => {
2018-02-07 10:17:59 +01:00
this.$refs.selection.style.display = 'block';
2017-02-21 01:49:35 +01:00
2018-02-07 10:17:59 +01:00
const cursorX = e.pageX + this.$refs.main.scrollLeft - rect.left - window.pageXOffset;
const cursorY = e.pageY + this.$refs.main.scrollTop - rect.top - window.pageYOffset;
2017-02-21 01:49:35 +01:00
const w = cursorX - left;
const h = cursorY - top;
if (w > 0) {
2018-02-07 10:17:59 +01:00
this.$refs.selection.style.width = w + 'px';
this.$refs.selection.style.left = left + 'px';
2017-02-21 01:49:35 +01:00
} else {
2018-02-07 10:17:59 +01:00
this.$refs.selection.style.width = -w + 'px';
this.$refs.selection.style.left = cursorX + 'px';
2017-02-21 01:49:35 +01:00
}
if (h > 0) {
2018-02-07 10:17:59 +01:00
this.$refs.selection.style.height = h + 'px';
this.$refs.selection.style.top = top + 'px';
2017-02-21 01:49:35 +01:00
} else {
2018-02-07 10:17:59 +01:00
this.$refs.selection.style.height = -h + 'px';
this.$refs.selection.style.top = cursorY + 'px';
2017-02-21 01:49:35 +01:00
}
};
2017-02-26 09:27:34 +01:00
const up = e => {
2017-02-21 01:49:35 +01:00
document.documentElement.removeEventListener('mousemove', move);
document.documentElement.removeEventListener('mouseup', up);
2018-02-07 10:17:59 +01:00
this.$refs.selection.style.display = 'none';
2017-02-21 01:49:35 +01:00
};
document.documentElement.addEventListener('mousemove', move);
document.documentElement.addEventListener('mouseup', up);
};
this.pathOncontextmenu = e => {
2017-02-20 02:27:44 +01:00
e.preventDefault();
2017-02-20 03:02:43 +01:00
e.stopImmediatePropagation();
2017-02-21 01:49:35 +01:00
return false;
};
2016-12-28 23:49:51 +01:00
2017-02-21 01:49:35 +01:00
this.ondragover = e => {
2017-02-20 02:27:44 +01:00
e.preventDefault();
e.stopPropagation();
2017-01-11 21:55:38 +01:00
2017-02-20 01:53:57 +01:00
// ドラッグ元が自分自身の所有するアイテムかどうか
2017-02-21 01:49:35 +01:00
if (!this.isDragSource) {
2017-02-20 01:53:57 +01:00
// ドラッグされてきたものがファイルだったら
2017-02-21 01:49:35 +01:00
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
this.draghover = true;
} else {
2017-02-20 01:53:57 +01:00
// 自分自身にはドロップさせない
2017-02-21 01:49:35 +01:00
e.dataTransfer.dropEffect = 'none';
return false;
}
};
2016-12-28 23:49:51 +01:00
2017-02-21 01:49:35 +01:00
this.ondragenter = e => {
2017-02-20 02:27:44 +01:00
e.preventDefault();
2017-02-21 01:49:35 +01:00
if (!this.isDragSource) this.draghover = true;
};
2016-12-28 23:49:51 +01:00
2017-02-21 01:49:35 +01:00
this.ondragleave = e => {
this.draghover = false;
};
2017-01-11 21:55:38 +01:00
2017-02-21 01:49:35 +01:00
this.ondrop = e => {
2017-02-20 02:27:44 +01:00
e.preventDefault();
e.stopPropagation();
2017-01-11 21:55:38 +01:00
2017-02-21 01:49:35 +01:00
this.draghover = false;
2017-01-11 21:55:38 +01:00
2017-02-20 01:53:57 +01:00
// ドロップされてきたものがファイルだったら
2017-02-21 01:49:35 +01:00
if (e.dataTransfer.files.length > 0) {
2017-11-13 10:05:35 +01:00
Array.from(e.dataTransfer.files).forEach(file => {
2017-02-21 01:49:35 +01:00
this.upload(file, this.folder);
});
return false;
}
2017-01-11 21:55:38 +01:00
2017-02-20 01:53:57 +01:00
// データ取得
2017-02-21 01:49:35 +01:00
const data = e.dataTransfer.getData('text');
if (data == null) return false;
2017-01-11 21:55:38 +01:00
2017-02-20 01:53:57 +01:00
// パース
2017-02-21 01:49:35 +01:00
// TODO: JSONじゃなかったら中断
const obj = JSON.parse(data);
2017-01-11 21:55:38 +01:00
2017-02-20 01:53:57 +01:00
// (ドライブの)ファイルだったら
2017-02-21 01:49:35 +01:00
if (obj.type == 'file') {
const file = obj.id;
if (this.files.some(f => f.id == file)) return false;
this.removeFile(file);
2017-02-20 08:09:46 +01:00
this.api('drive/files/update', {
2017-02-21 01:49:35 +01:00
file_id: file,
folder_id: this.folder ? this.folder.id : null
});
2017-02-20 01:53:57 +01:00
// (ドライブの)フォルダーだったら
2017-02-21 01:49:35 +01:00
} else if (obj.type == 'folder') {
const folder = obj.id;
2017-02-20 01:53:57 +01:00
// 移動先が自分自身ならreject
2017-02-21 01:49:35 +01:00
if (this.folder && folder == this.folder.id) return false;
if (this.folders.some(f => f.id == folder)) return false;
this.removeFolder(folder);
2017-02-20 08:09:46 +01:00
this.api('drive/folders/update', {
2017-02-21 01:49:35 +01:00
folder_id: folder,
parent_id: this.folder ? this.folder.id : null
2017-02-20 12:13:42 +01:00
}).then(() => {
2017-02-20 01:53:57 +01:00
// something
2017-02-21 01:49:35 +01:00
}).catch(err => {
switch (err) {
case 'detected-circular-definition':
2017-12-07 18:44:50 +01:00
dialog('%fa:exclamation-triangle%%i18n:desktop.tags.mk-drive-browser.unable-to-process%',
2017-04-04 13:07:13 +02:00
'%i18n:desktop.tags.mk-drive-browser.circular-reference-detected%', [{
2017-04-04 13:45:35 +02:00
text: '%i18n:common.ok%'
2017-02-21 01:49:35 +01:00
}]);
break;
default:
2017-04-04 13:07:13 +02:00
alert('%i18n:desktop.tags.mk-drive-browser.unhandled-error% ' + err);
2017-02-21 01:49:35 +01:00
}
});
}
return false;
};
this.oncontextmenu = e => {
2017-02-20 02:27:44 +01:00
e.preventDefault();
2017-02-20 03:02:43 +01:00
e.stopImmediatePropagation();
2017-01-11 21:55:38 +01:00
2017-02-21 01:49:35 +01:00
const ctx = riot.mount(document.body.appendChild(document.createElement('mk-drive-browser-base-contextmenu')), {
2017-02-20 03:02:43 +01:00
browser: this
2017-02-21 01:49:35 +01:00
})[0];
ctx.open({
x: e.pageX - window.pageXOffset,
2017-02-20 03:02:43 +01:00
y: e.pageY - window.pageYOffset
2017-02-21 01:49:35 +01:00
});
2017-01-11 21:55:38 +01:00
2017-02-21 01:49:35 +01:00
return false;
};
2016-12-28 23:49:51 +01:00
2017-02-21 01:49:35 +01:00
this.selectLocalFile = () => {
2018-02-07 10:17:59 +01:00
this.$refs.fileInput.click();
2017-02-21 01:49:35 +01:00
};
2017-02-16 21:46:14 +01:00
2017-02-21 01:49:35 +01:00
this.urlUpload = () => {
2017-04-26 09:36:29 +02:00
inputDialog('%i18n:desktop.tags.mk-drive-browser.url-upload%',
'%i18n:desktop.tags.mk-drive-browser.url-of-file%', null, url => {
2017-02-20 08:09:46 +01:00
this.api('drive/files/upload_from_url', {
2017-02-21 01:49:35 +01:00
url: url,
folder_id: this.folder ? this.folder.id : undefined
});
2017-02-16 21:46:14 +01:00
2017-12-07 18:44:50 +01:00
dialog('%fa:check%%i18n:desktop.tags.mk-drive-browser.url-upload-requested%',
2017-04-04 13:07:13 +02:00
'%i18n:desktop.tags.mk-drive-browser.may-take-time%', [{
2017-04-04 13:45:35 +02:00
text: '%i18n:common.ok%'
2017-02-21 01:49:35 +01:00
}]);
});
};
2017-02-16 21:46:14 +01:00
2017-02-20 08:09:46 +01:00
this.createFolder = () => {
2017-04-26 09:36:29 +02:00
inputDialog('%i18n:desktop.tags.mk-drive-browser.create-folder%',
'%i18n:desktop.tags.mk-drive-browser.folder-name%', null, name => {
2017-02-21 01:49:35 +01:00
this.api('drive/folders/create', {
name: name,
folder_id: this.folder ? this.folder.id : undefined
}).then(folder => {
this.addFolder(folder, true);
this.update();
});
});
};
2016-12-28 23:49:51 +01:00
2017-02-21 01:49:35 +01:00
this.changeFileInput = () => {
2018-02-07 10:17:59 +01:00
Array.from(this.$refs.fileInput.files).forEach(file => {
2017-02-21 01:49:35 +01:00
this.upload(file, this.folder);
});
};
2016-12-28 23:49:51 +01:00
2017-02-20 02:34:57 +01:00
this.upload = (file, folder) => {
2017-02-21 01:49:35 +01:00
if (folder && typeof folder == 'object') folder = folder.id;
2018-02-07 10:17:59 +01:00
this.$refs.uploader.upload(file, folder);
2017-02-21 01:49:35 +01:00
};
2016-12-28 23:49:51 +01:00
2017-02-23 09:12:09 +01:00
this.chooseFile = file => {
2017-02-25 05:23:54 +01:00
const isAlreadySelected = this.selectedFiles.some(f => f.id == file.id);
if (this.multiple) {
if (isAlreadySelected) {
this.selectedFiles = this.selectedFiles.filter(f => f.id != file.id);
} else {
this.selectedFiles.push(file);
}
this.update();
2018-02-09 05:11:30 +01:00
this.$emit('change-selection', this.selectedFiles);
2017-02-23 09:12:09 +01:00
} else {
2017-02-25 05:23:54 +01:00
if (isAlreadySelected) {
2018-02-09 05:11:30 +01:00
this.$emit('selected', file);
2017-02-25 05:23:54 +01:00
} else {
this.selectedFiles = [file];
2018-02-09 05:11:30 +01:00
this.$emit('change-selection', [file]);
2017-02-25 05:23:54 +01:00
}
2017-02-23 09:12:09 +01:00
}
2017-02-21 01:49:35 +01:00
};
2016-12-28 23:49:51 +01:00
2017-02-21 01:49:35 +01:00
this.newWindow = folderId => {
riot.mount(document.body.appendChild(document.createElement('mk-drive-browser-window')), {
folder: folderId
});
};
this.move = target => {
if (target == null) {
this.goRoot();
return;
} else if (typeof target == 'object') {
target = target.id;
}
this.update({
fetching: true
});
2016-12-28 23:49:51 +01:00
2017-02-20 08:09:46 +01:00
this.api('drive/folders/show', {
2017-02-21 01:49:35 +01:00
folder_id: target
}).then(folder => {
this.folder = folder;
this.hierarchyFolders = [];
2016-12-28 23:49:51 +01:00
2017-02-21 01:49:35 +01:00
const dive = folder => {
this.hierarchyFolders.unshift(folder);
if (folder.parent) dive(folder.parent);
};
2016-12-28 23:49:51 +01:00
2017-02-21 01:49:35 +01:00
if (folder.parent) dive(folder.parent);
2017-01-11 21:55:38 +01:00
2017-02-20 01:53:57 +01:00
this.update();
2018-02-09 05:11:30 +01:00
this.$emit('open-folder', folder);
this.fetch();
2017-02-21 01:49:35 +01:00
});
};
2017-01-11 21:55:38 +01:00
2017-02-21 01:49:35 +01:00
this.addFolder = (folder, unshift = false) => {
const current = this.folder ? this.folder.id : null;
if (current != folder.parent_id) return;
2017-01-11 21:55:38 +01:00
2017-02-21 01:49:35 +01:00
if (this.folders.some(f => f.id == folder.id)) {
const exist = this.folders.map(f => f.id).indexOf(folder.id);
this.folders[exist] = folder;
2017-02-20 01:53:57 +01:00
this.update();
2017-02-21 01:49:35 +01:00
return;
}
2017-01-11 21:55:38 +01:00
2017-02-21 01:49:35 +01:00
if (unshift) {
this.folders.unshift(folder);
} else {
this.folders.push(folder);
}
2016-12-28 23:49:51 +01:00
2017-02-20 01:53:57 +01:00
this.update();
2017-02-21 01:49:35 +01:00
};
2016-12-28 23:49:51 +01:00
2017-02-21 01:49:35 +01:00
this.addFile = (file, unshift = false) => {
const current = this.folder ? this.folder.id : null;
if (current != file.folder_id) return;
2017-01-11 21:55:38 +01:00
2017-02-21 01:49:35 +01:00
if (this.files.some(f => f.id == file.id)) {
const exist = this.files.map(f => f.id).indexOf(file.id);
this.files[exist] = file;
2017-02-20 01:53:57 +01:00
this.update();
2017-02-21 01:49:35 +01:00
return;
}
2016-12-28 23:49:51 +01:00
2017-02-21 01:49:35 +01:00
if (unshift) {
this.files.unshift(file);
} else {
this.files.push(file);
}
2016-12-28 23:49:51 +01:00
2017-02-20 01:53:57 +01:00
this.update();
2017-02-21 01:49:35 +01:00
};
2016-12-28 23:49:51 +01:00
2017-02-21 01:49:35 +01:00
this.removeFolder = folder => {
if (typeof folder == 'object') folder = folder.id;
this.folders = this.folders.filter(f => f.id != folder);
2017-02-20 01:53:57 +01:00
this.update();
2017-02-21 01:49:35 +01:00
};
2017-01-11 21:55:38 +01:00
2017-02-21 01:49:35 +01:00
this.removeFile = file => {
if (typeof file == 'object') file = file.id;
this.files = this.files.filter(f => f.id != file);
2017-02-20 01:53:57 +01:00
this.update();
2017-02-21 01:49:35 +01:00
};
2016-12-28 23:49:51 +01:00
2017-02-23 16:18:33 +01:00
this.appendFile = file => this.addFile(file);
this.appendFolder = file => this.addFolder(file);
this.prependFile = file => this.addFile(file, true);
this.prependFolder = file => this.addFolder(file, true);
2017-02-21 01:49:35 +01:00
this.goRoot = () => {
// 既にrootにいるなら何もしない
if (this.folder == null) return;
this.update({
folder: null,
hierarchyFolders: []
});
2018-02-09 05:11:30 +01:00
this.$emit('move-root');
this.fetch();
2017-02-21 01:49:35 +01:00
};
2017-01-11 21:55:38 +01:00
this.fetch = () => {
2017-02-21 01:49:35 +01:00
this.update({
folders: [],
files: [],
moreFolders: false,
moreFiles: false,
fetching: true
});
2016-12-28 23:49:51 +01:00
2017-02-21 01:49:35 +01:00
let fetchedFolders = null;
let fetchedFiles = null;
2017-01-11 21:55:38 +01:00
2017-02-21 01:49:35 +01:00
const foldersMax = 30;
const filesMax = 30;
2017-01-11 21:55:38 +01:00
2017-02-20 01:53:57 +01:00
// フォルダ一覧取得
2017-02-20 08:09:46 +01:00
this.api('drive/folders', {
2017-02-21 01:49:35 +01: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 21:55:38 +01:00
2017-02-20 01:53:57 +01:00
// ファイル一覧取得
2017-02-20 08:09:46 +01:00
this.api('drive/files', {
2017-02-21 01:49:35 +01: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 18:05:44 +01:00
const complete = () => {
2017-02-21 01:49:35 +01:00
if (flag) {
2017-02-23 16:18:33 +01:00
fetchedFolders.forEach(this.appendFolder);
fetchedFiles.forEach(this.appendFile);
2017-02-21 01:49:35 +01:00
this.update({
fetching: false
});
} else {
flag = true;
}
};
};
2017-01-11 21:55:38 +01:00
this.fetchMoreFiles = () => {
this.update({
fetching: true
});
const max = 30;
// ファイル一覧取得
this.api('drive/files', {
folder_id: this.folder ? this.folder.id : null,
limit: max + 1
}).then(files => {
if (files.length == max + 1) {
this.moreFiles = true;
files.pop();
} else {
this.moreFiles = false;
}
files.forEach(this.appendFile);
this.update({
fetching: false
});
});
};
2017-01-11 21:55:38 +01:00
</script>
</mk-drive-browser>