2018-11-26 09:53:18 +00:00
|
|
|
import React, { Component } from 'react';
|
2018-12-26 08:07:22 +00:00
|
|
|
import { Link } from '@reach/router';
|
2018-11-19 03:53:44 +00:00
|
|
|
import { seafileAPI } from '../../utils/seafile-api';
|
|
|
|
import { Utils } from '../../utils/utils';
|
|
|
|
import { gettext, siteRoot, loginUrl, isPro } from '../../utils/constants';
|
2019-01-09 06:39:17 +00:00
|
|
|
import PermissionEditor from '../../components/select-editor/permission-editor';
|
2018-12-27 02:24:34 +00:00
|
|
|
import SharedFolderInfo from '../../models/shared-folder-info';
|
2018-11-19 03:53:44 +00:00
|
|
|
|
|
|
|
class Content extends Component {
|
|
|
|
|
2019-01-02 08:17:39 +00:00
|
|
|
sortByName = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const sortBy = 'name';
|
|
|
|
const sortOrder = this.props.sortOrder == 'asc' ? 'desc' : 'asc';
|
|
|
|
this.props.sortItems(sortBy, sortOrder);
|
|
|
|
}
|
|
|
|
|
2018-11-19 03:53:44 +00:00
|
|
|
render() {
|
2019-01-02 08:17:39 +00:00
|
|
|
const { loading, errorMsg, items, sortBy, sortOrder } = this.props;
|
2018-11-19 03:53:44 +00:00
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return <span className="loading-icon loading-tip"></span>;
|
|
|
|
} else if (errorMsg) {
|
|
|
|
return <p className="error text-center">{errorMsg}</p>;
|
|
|
|
} else {
|
|
|
|
const emptyTip = (
|
|
|
|
<div className="empty-tip">
|
|
|
|
<h2>{gettext('You have not shared any folders')}</h2>
|
|
|
|
<p>{gettext("You can share a single folder with a registered user if you don't want to share a whole library.")}</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2019-01-02 08:17:39 +00:00
|
|
|
// sort
|
|
|
|
const sortByName = sortBy == 'name';
|
|
|
|
const sortIcon = sortOrder == 'asc' ? <span className="fas fa-caret-up"></span> : <span className="fas fa-caret-down"></span>;
|
|
|
|
|
2018-11-19 03:53:44 +00:00
|
|
|
const table = (
|
2018-12-26 08:07:22 +00:00
|
|
|
<table className="table-hover">
|
2018-11-19 03:53:44 +00:00
|
|
|
<thead>
|
|
|
|
<tr>
|
2018-12-26 08:07:22 +00:00
|
|
|
<th width="4%">{/*icon*/}</th>
|
2019-01-02 08:17:39 +00:00
|
|
|
<th width="34%"><a className="d-block table-sort-op" href="#" onClick={this.sortByName}>{gettext('Name')} {sortByName && sortIcon}</a></th>
|
2018-11-19 03:53:44 +00:00
|
|
|
<th width="30%">{gettext("Share To")}</th>
|
2018-12-26 08:07:22 +00:00
|
|
|
<th width="24%">{gettext("Permission")}</th>
|
2018-11-19 03:53:44 +00:00
|
|
|
<th width="8%"></th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
2018-12-27 02:24:34 +00:00
|
|
|
<tbody>
|
|
|
|
{items.map((item, index) => {
|
|
|
|
return (<Item key={index} item={item} unshareFolder={this.props.unshareFolder}/>);
|
|
|
|
})}
|
|
|
|
</tbody>
|
2018-11-19 03:53:44 +00:00
|
|
|
</table>
|
|
|
|
);
|
|
|
|
|
|
|
|
return items.length ? table : emptyTip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Item extends Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2018-12-27 02:24:34 +00:00
|
|
|
share_permission: this.props.item.share_permission,
|
2018-11-19 03:53:44 +00:00
|
|
|
showOpIcon: false,
|
|
|
|
unshared: false
|
|
|
|
};
|
|
|
|
|
2018-12-26 08:07:22 +00:00
|
|
|
this.permissions = ['rw', 'r'];
|
|
|
|
if (isPro) {
|
|
|
|
this.permissions = ['rw', 'r', 'cloud-edit', 'preview'];
|
|
|
|
}
|
2018-11-19 03:53:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 08:07:22 +00:00
|
|
|
onMouseEnter = () => {
|
|
|
|
this.setState({showOpIcon: true});
|
2018-11-19 03:53:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 08:07:22 +00:00
|
|
|
onMouseLeave = () => {
|
|
|
|
this.setState({showOpIcon: false});
|
2018-11-19 03:53:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 08:07:22 +00:00
|
|
|
unshare = (e) => {
|
2018-11-19 03:53:44 +00:00
|
|
|
e.preventDefault();
|
2018-12-27 02:24:34 +00:00
|
|
|
const item = this.props.item;
|
|
|
|
this.props.unshareFolder(item);
|
2018-11-19 03:53:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 08:07:22 +00:00
|
|
|
changePerm = (permission) => {
|
|
|
|
const perm = permission;
|
2018-11-19 03:53:44 +00:00
|
|
|
const postData = {
|
|
|
|
'permission': perm
|
|
|
|
};
|
2018-12-27 02:24:34 +00:00
|
|
|
const item = this.props.item;
|
2018-11-19 03:53:44 +00:00
|
|
|
let options = {
|
2018-12-27 02:24:34 +00:00
|
|
|
'p': item.path,
|
|
|
|
'share_type': item.share_type
|
2018-11-19 03:53:44 +00:00
|
|
|
};
|
2018-12-27 02:24:34 +00:00
|
|
|
if (item.share_type == 'user') {
|
|
|
|
options.username = item.user_email;
|
|
|
|
} else {
|
|
|
|
options.group_id = item.group_id;
|
2018-11-19 03:53:44 +00:00
|
|
|
}
|
2018-12-27 02:24:34 +00:00
|
|
|
|
|
|
|
seafileAPI.updateFolderSharePerm(item.repo_id, postData, options).then((res) => {
|
|
|
|
this.setState({share_permission: perm});
|
2018-11-19 03:53:44 +00:00
|
|
|
// TODO: show feedback msg
|
|
|
|
// gettext("Successfully modified permission")
|
|
|
|
}).catch((error) => {
|
|
|
|
// TODO: show feedback msg
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-27 02:24:34 +00:00
|
|
|
getFolderParams = () => {
|
|
|
|
let item = this.props.item;
|
|
|
|
let share_permission = this.state.share_permission;
|
2018-11-19 03:53:44 +00:00
|
|
|
let is_readonly = false;
|
|
|
|
if (share_permission == 'r' || share_permission == 'preview') {
|
|
|
|
is_readonly = true;
|
|
|
|
}
|
2018-12-27 02:24:34 +00:00
|
|
|
let iconUrl = Utils.getFolderIconUrl({
|
2018-11-19 03:53:44 +00:00
|
|
|
is_readonly: is_readonly,
|
|
|
|
size: Utils.isHiDPI() ? 48 : 24
|
|
|
|
});
|
2018-12-27 02:24:34 +00:00
|
|
|
let iconTitle = Utils.getFolderIconTitle({
|
2018-11-19 03:53:44 +00:00
|
|
|
'permission': share_permission
|
|
|
|
});
|
2018-12-27 02:24:34 +00:00
|
|
|
let folderUrl = `${siteRoot}library/${item.repo_id}/${item.repo_name}${Utils.encodePath(item.path)}`;
|
|
|
|
|
|
|
|
return { iconUrl, iconTitle, folderUrl };
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const item = this.props.item;
|
|
|
|
let { iconUrl, iconTitle, folderUrl } = this.getFolderParams();
|
2018-11-19 03:53:44 +00:00
|
|
|
|
|
|
|
let shareTo;
|
2018-12-27 02:24:34 +00:00
|
|
|
const shareType = item.share_type;
|
|
|
|
if (shareType == 'user') {
|
|
|
|
shareTo = <td title={item.contact_email}>{item.user_name}</td>;
|
2018-11-19 03:53:44 +00:00
|
|
|
} else if (shareType == 'group') {
|
2018-12-27 02:24:34 +00:00
|
|
|
shareTo = <td>{item.group_name}</td>;
|
2018-11-19 03:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let iconVisibility = this.state.showOpIcon ? '' : ' invisible';
|
2018-12-28 03:12:24 +00:00
|
|
|
let unshareIconClassName = 'unshare action-icon sf2-icon-x3' + iconVisibility;
|
2018-11-19 03:53:44 +00:00
|
|
|
|
2018-12-27 02:24:34 +00:00
|
|
|
return (
|
2018-12-26 08:07:22 +00:00
|
|
|
<tr onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}>
|
2018-12-27 02:24:34 +00:00
|
|
|
<td><img src={iconUrl} title={iconTitle} alt={iconTitle} width="24" /></td>
|
|
|
|
<td><Link to={folderUrl}>{item.folder_name}</Link></td>
|
2018-11-19 03:53:44 +00:00
|
|
|
{shareTo}
|
2018-12-26 08:07:22 +00:00
|
|
|
<td>
|
|
|
|
<PermissionEditor
|
|
|
|
isTextMode={true}
|
|
|
|
isEditIconShow={this.state.showOpIcon}
|
2018-12-27 02:24:34 +00:00
|
|
|
currentPermission={this.state.share_permission}
|
2018-12-26 08:07:22 +00:00
|
|
|
permissions={this.permissions}
|
|
|
|
onPermissionChangedHandler={this.changePerm}
|
|
|
|
/>
|
|
|
|
</td>
|
2018-11-19 03:53:44 +00:00
|
|
|
<td><a href="#" className={unshareIconClassName} title={gettext('Unshare')} onClick={this.unshare}></a></td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ShareAdminFolders extends Component {
|
2018-12-26 08:07:22 +00:00
|
|
|
|
2018-11-19 03:53:44 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
errorMsg: '',
|
2019-01-02 08:17:39 +00:00
|
|
|
items: [],
|
|
|
|
sortBy: 'name',
|
|
|
|
sortOrder: 'asc' // 'asc' or 'desc'
|
2018-11-19 03:53:44 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-02 08:17:39 +00:00
|
|
|
_sortItems = (items, sortBy, sortOrder) => {
|
|
|
|
let comparator;
|
|
|
|
|
|
|
|
switch (`${sortBy}-${sortOrder}`) {
|
|
|
|
case 'name-asc':
|
|
|
|
comparator = function(a, b) {
|
|
|
|
var result = Utils.compareTwoWord(a.folder_name, b.folder_name);
|
|
|
|
return result;
|
2019-01-02 09:49:30 +00:00
|
|
|
};
|
2019-01-02 08:17:39 +00:00
|
|
|
break;
|
|
|
|
case 'name-desc':
|
|
|
|
comparator = function(a, b) {
|
|
|
|
var result = Utils.compareTwoWord(a.folder_name, b.folder_name);
|
|
|
|
return -result;
|
2019-01-02 09:49:30 +00:00
|
|
|
};
|
2019-01-02 08:17:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
items.sort(comparator);
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
sortItems = (sortBy, sortOrder) => {
|
|
|
|
this.setState({
|
|
|
|
sortBy: sortBy,
|
|
|
|
sortOrder: sortOrder,
|
|
|
|
items: this._sortItems(this.state.items, sortBy, sortOrder)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-19 03:53:44 +00:00
|
|
|
componentDidMount() {
|
|
|
|
seafileAPI.listSharedFolders().then((res) => {
|
|
|
|
// res: {data: Array(2), status: 200, statusText: "OK", headers: {…}, config: {…}, …}
|
2018-12-27 02:24:34 +00:00
|
|
|
let items = res.data.map(item => {
|
|
|
|
return new SharedFolderInfo(item);
|
|
|
|
});
|
2018-11-19 03:53:44 +00:00
|
|
|
this.setState({
|
|
|
|
loading: false,
|
2019-01-02 08:17:39 +00:00
|
|
|
items: this._sortItems(items, this.state.sortBy, this.state.sortOrder)
|
2018-11-19 03:53:44 +00:00
|
|
|
});
|
|
|
|
}).catch((error) => {
|
|
|
|
if (error.response) {
|
|
|
|
if (error.response.status == 403) {
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
errorMsg: gettext("Permission denied")
|
|
|
|
});
|
|
|
|
location.href = `${loginUrl}?next=${encodeURIComponent(location.href)}`;
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
errorMsg: gettext("Error")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
errorMsg: gettext("Please check the network.")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-27 02:24:34 +00:00
|
|
|
unshareFolder = (item) => {
|
|
|
|
let options = {};
|
|
|
|
options['p'] = item.path;
|
|
|
|
options.share_type = item.share_type;
|
|
|
|
if (item.share_type == 'user') { // or group
|
|
|
|
options.username = item.user_email;
|
|
|
|
} else {
|
|
|
|
options.group_id = item.group_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
seafileAPI.unshareFolder(item.repo_id, options).then((res) => {
|
|
|
|
let items = this.state.items.filter(folderItem => {
|
|
|
|
if (item.share_type === 'user') {
|
|
|
|
return folderItem.user_email !== item.user_email;
|
|
|
|
} else {
|
|
|
|
return folderItem.group_id !== item.group_id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.setState({items: items});
|
|
|
|
// TODO: show feedback msg
|
|
|
|
// gettext("Successfully deleted 1 item")
|
|
|
|
}).catch((error) => {
|
|
|
|
// TODO: show feedback msg
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-19 03:53:44 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2018-11-26 09:53:18 +00:00
|
|
|
<div className="main-panel-center">
|
|
|
|
<div className="cur-view-container" id="share-admin-libs">
|
|
|
|
<div className="cur-view-path">
|
|
|
|
<h3 className="sf-heading">{gettext("Folders")}</h3>
|
|
|
|
</div>
|
|
|
|
<div className="cur-view-content">
|
2018-12-27 02:24:34 +00:00
|
|
|
<Content
|
|
|
|
errorMsg={this.state.errorMsg}
|
|
|
|
loading={this.state.loading}
|
|
|
|
items={this.state.items}
|
2019-01-02 08:17:39 +00:00
|
|
|
sortBy={this.state.sortBy}
|
|
|
|
sortOrder={this.state.sortOrder}
|
|
|
|
sortItems={this.sortItems}
|
2018-12-27 02:24:34 +00:00
|
|
|
unshareFolder={this.unshareFolder}
|
|
|
|
/>
|
2018-11-26 06:00:32 +00:00
|
|
|
</div>
|
2018-11-19 03:53:44 +00:00
|
|
|
</div>
|
2018-11-26 09:53:18 +00:00
|
|
|
</div>
|
2018-11-19 03:53:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ShareAdminFolders;
|