import React from 'react';
import ReactDOM from 'react-dom';
import { Button } from 'reactstrap';
import moment from 'moment';
import Account from './components/common/account';
import { gettext, siteRoot, mediaUrl, logoPath, logoWidth, logoHeight, siteTitle, thumbnailSizeForOriginal } from './utils/constants';
import { Utils } from './utils/utils';
import { seafileAPI } from './utils/seafile-api';
import Loading from './components/loading';
import toaster from './components/toast';
import ModalPortal from './components/modal-portal';
import ZipDownloadDialog from './components/dialog/zip-download-dialog';
import ImageDialog from './components/dialog/image-dialog';
import './css/shared-dir-view.css';
import './css/grid-view.css';
let loginUser = window.app.pageOptions.name;
const { token, trafficOverLimit, dirName, sharedBy, path, canDownload, mode, thumbnailSize } = window.shared.pageOptions;
const showDownloadIcon = !trafficOverLimit && canDownload;
class SharedDirView extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
errorMsg: '',
items: [],
isZipDialogOpen: false,
zipFolderPath: '',
isImagePopupOpen: false,
imageItems: [],
imageIndex: 0
};
}
componentDidMount() {
if (trafficOverLimit) {
toaster.danger(gettext('File download is disabled: the share link traffic of owner is used up.'), {
duration: 3
});
}
seafileAPI.listSharedDir(token, path, thumbnailSize).then((res) => {
const items = res.data['dirent_list'];
this.setState({
isLoading: false,
errorMsg: '',
items: items
});
this.getThumbnails();
}).catch((error) => {
let errorMsg = Utils.getErrorMsg(error);
this.setState({
isLoading: false,
errorMsg: errorMsg
});
});
}
getThumbnails = () => {
let items = this.state.items.filter((item) => {
return !item.is_dir && Utils.imageCheck(item.file_name) && !item.encoded_thumbnail_src;
});
if (items.length == 0) {
return ;
}
const len = items.length;
const _this = this;
let getThumbnail = function(i) {
const curItem = items[i];
seafileAPI.getShareLinkThumbnail(token, curItem.file_path, thumbnailSize).then((res) => {
curItem.encoded_thumbnail_src = res.data.encoded_thumbnail_src;
}).catch((error) => {
// do nothing
}).then(() => {
if (i < len - 1) {
getThumbnail(++i);
} else {
// when done, `setState()`
_this.setState({
items: _this.state.items
});
}
});
};
getThumbnail(0);
}
renderPath = () => {
// path: '/', or '/g/'
if (path == '/') {
return dirName;
}
let pathList = path.substr(0, path.length -1).split('/');
return (
{dirName}
/
{pathList.map((item, index) => {
if (index > 0 && index != pathList.length - 1) {
return (
{pathList[index]}
/
);
}
}
)}
{pathList[pathList.length - 1]}
);
}
zipDownloadFolder = (folderPath) => {
this.setState({
isZipDialogOpen: true,
zipFolderPath: folderPath
});
}
closeZipDialog = () => {
this.setState({
isZipDialogOpen: false,
zipFolderPath: ''
});
}
// for image popup
prepareImageItem = (item) => {
const name = item.file_name;
const fileExt = name.substr(name.lastIndexOf('.') + 1).toLowerCase();
const isGIF = fileExt == 'gif';
let src;
const fileURL = `${siteRoot}d/${token}/files/?p=${encodeURIComponent(item.file_path)}`;
if (!isGIF) {
src = `${siteRoot}thumbnail/${token}/${thumbnailSizeForOriginal}${Utils.encodePath(item.file_path)}`;
} else {
src = `${fileURL}&raw=1`;
}
return {
'name': name,
'url': fileURL,
'src': src
};
}
showImagePopup = (curItem) => {
const items = this.state.items.filter((item) => {
return !item.is_dir && Utils.imageCheck(item.file_name);
});
const imageItems = items.map((item) => {
return this.prepareImageItem(item);
});
this.setState({
isImagePopupOpen: true,
imageItems: imageItems,
imageIndex: items.indexOf(curItem)
});
}
closeImagePopup = () => {
this.setState({
isImagePopupOpen: false
});
}
moveToPrevImage = () => {
const imageItemsLength = this.state.imageItems.length;
this.setState((prevState) => ({
imageIndex: (prevState.imageIndex + imageItemsLength - 1) % imageItemsLength
}));
}
moveToNextImage = () => {
const imageItemsLength = this.state.imageItems.length;
this.setState((prevState) => ({
imageIndex: (prevState.imageIndex + 1) % imageItemsLength
}));
}
render() {
const modeBaseClass = 'btn btn-secondary btn-icon sf-view-mode-btn';
return (
{loginUser &&
}
{dirName}
{gettext('Shared by: ')}{sharedBy}
{gettext('Current path: ')}{this.renderPath()}
{showDownloadIcon &&
}
{this.state.isZipDialogOpen &&
}
{this.state.isImagePopupOpen &&
}
);
}
}
class Content extends React.Component {
render() {
const { isLoading, errorMsg, items } = this.props.data;
if (isLoading) {
return ;
}
if (errorMsg) {
return
{errorMsg}
;
}
return mode == 'list' ? (
|
{gettext('Name')} |
{gettext('Size')} |
{gettext('Last Update')} |
{gettext('Operations')} |
{items.map((item, index) => {
return ;
})}
) : (
{items.map((item, index) => {
return ;
})}
);
}
}
class Item extends React.Component {
constructor(props) {
super(props);
this.state = {
isIconShown: false
};
}
handleMouseOver = () => {
this.setState({isIconShown: true});
}
handleMouseOut = () => {
this.setState({isIconShown: false});
}
zipDownloadFolder = (e) => {
e.preventDefault();
this.props.zipDownloadFolder.bind(this, this.props.item.folder_path)();
}
handleFileClick = (e) => {
const item = this.props.item;
if (!Utils.imageCheck(item.file_name)) {
return;
}
e.preventDefault();
this.props.showImagePopup(item);
}
render() {
const item = this.props.item;
const { isIconShown } = this.state;
if (item.is_dir) {
return (
}) |
{item.folder_name}
|
|
{moment(item.last_modified).format('YYYY-MM-DD')} |
{showDownloadIcon &&
}
|
);
} else {
const fileURL = `${siteRoot}d/${token}/files/?p=${encodeURIComponent(item.file_path)}`;
const thumbnailURL = item.encoded_thumbnail_src ? `${siteRoot}${item.encoded_thumbnail_src}` : '';
return (
{thumbnailURL ?
:
}
|
{item.file_name}
|
{Utils.bytesToSize(item.size)} |
{moment(item.last_modified).format('YYYY-MM-DD')} |
{showDownloadIcon &&
}
|
);
}
}
}
class GridItem extends React.Component {
constructor(props) {
super(props);
this.state = {
isIconShown: false
};
}
handleMouseOver = () => {
this.setState({isIconShown: true});
}
handleMouseOut = () => {
this.setState({isIconShown: false});
}
zipDownloadFolder = (e) => {
e.preventDefault();
this.props.zipDownloadFolder.bind(this, this.props.item.folder_path)();
}
handleFileClick = (e) => {
const item = this.props.item;
if (!Utils.imageCheck(item.file_name)) {
return;
}
e.preventDefault();
this.props.showImagePopup(item);
}
render() {
const item = this.props.item;
const { isIconShown } = this.state;
if (item.is_dir) {
const folderURL = `?p=${encodeURIComponent(item.folder_path.substr(0, item.folder_path.length - 1))}&mode=${mode}`;
return (
{item.folder_name}
{showDownloadIcon &&
}
);
} else {
const fileURL = `${siteRoot}d/${token}/files/?p=${encodeURIComponent(item.file_path)}`;
const thumbnailURL = item.encoded_thumbnail_src ? `${siteRoot}${item.encoded_thumbnail_src}` : '';
return (
{thumbnailURL ?
:
}
{item.file_name}
{showDownloadIcon &&
}
);
}
}
}
ReactDOM.render(
,
document.getElementById('wrapper')
);