1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-20 07:55:06 +00:00
seahub/frontend/src/pages/share-admin/libraries.js

279 lines
8.3 KiB
JavaScript
Raw Normal View History

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';
import { seafileAPI } from '../../utils/seafile-api';
import { Utils } from '../../utils/utils';
import { gettext, siteRoot, loginUrl, isPro } from '../../utils/constants';
2018-12-26 08:07:22 +00:00
import PermissionEditor from '../../components/permission-editor';
2018-12-27 02:24:34 +00:00
import SharedRepoInfo from '../../models/shared-repo-info';
class Content extends Component {
render() {
2018-12-27 02:24:34 +00:00
const {loading, errorMsg, items} = this.props;
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 libraries')}</h2>
<p>{gettext("You can share libraries with your friends and colleagues by clicking the share icon of your own libraries in your home page or creating a new library in groups you are in.")}</p>
</div>
);
const table = (
2018-12-26 08:07:22 +00:00
<table className="table-hover">
<thead>
<tr>
2018-12-26 08:07:22 +00:00
<th width="4%">{/*icon*/}</th>
<th width="34%">{gettext("Name")} <a className="table-sort-op by-name" href="#"><span className="sort-icon icon-caret-down hide"></span>{/* TODO: sort by name */}</a></th>
<th width="30%">{gettext("Share To")}</th>
2018-12-26 08:07:22 +00:00
<th width="24%">{gettext("Permission")}</th>
<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>
</table>
);
return items.length ? table : emptyTip;
}
}
}
class Item extends Component {
constructor(props) {
super(props);
2018-12-27 02:24:34 +00:00
let item = this.props.item;
this.state = {
2018-12-27 02:24:34 +00:00
share_permission: item.share_permission,
is_admin: item.is_admin,
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-12-26 08:07:22 +00:00
onMouseEnter = () => {
this.setState({showOpIcon: true});
}
2018-12-26 08:07:22 +00:00
onMouseLeave = () => {
this.setState({showOpIcon: false});
}
2018-12-26 08:07:22 +00:00
changePerm = (permission) => {
2018-12-27 02:24:34 +00:00
const item = this.props.item;
const share_type = item.share_type;
let options = {
'share_type': share_type,
2018-12-27 02:24:34 +00:00
'permission': permission
};
if (share_type == 'personal') {
2018-12-27 02:24:34 +00:00
options.user = item.user_email;
} else if (share_type == 'group') {
2018-12-27 02:24:34 +00:00
options.group_id = item.group_id;
} else if (share_type === 'public') {
// nothing todo
}
seafileAPI.updateRepoSharePerm(item.repo_id, options).then(() => {
this.setState({
2018-12-27 02:24:34 +00:00
share_permission: permission == 'admin' ? 'rw' : permission,
is_admin: permission == 'admin',
});
// TODO: show feedback msg
// gettext("Successfully modified permission")
}).catch((error) => {
// TODO: show feedback msg
});
}
2018-12-27 02:24:34 +00:00
unshare = () => {
this.props.unshareFolder(this.props.item);
}
2018-12-27 02:24:34 +00:00
getRepoParams = () => {
let item = this.props.item;
const { share_permission, is_admin } = this.state;
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.getLibIconUrl({
is_encrypted: item.encrypted,
is_readonly: is_readonly,
size: Utils.isHiDPI() ? 48 : 24
});
2018-12-27 02:24:34 +00:00
let iconTitle = Utils.getLibIconTitle({
'encrypted': item.encrypted,
'is_admin': is_admin,
'permission': share_permission
});
2018-12-27 02:24:34 +00:00
let repoUrl = `${siteRoot}library/${item.repo_id}/${item.repo_name}`;
return { iconUrl, iconTitle, repoUrl };
}
render() {
let { iconUrl, iconTitle, repoUrl } = this.getRepoParams();
let item = this.props.item;
let { share_permission, is_admin } = this.state;
let shareTo;
2018-12-27 02:24:34 +00:00
const shareType = item.share_type;
if (shareType == 'personal') {
2018-12-27 02:24:34 +00:00
shareTo = <td title={item.contact_email}>{item.user_name}</td>;
} else if (shareType == 'group') {
2018-12-27 02:24:34 +00:00
shareTo = <td>{item.group_name}</td>;
} else if (shareType == 'public') {
shareTo = <td>{gettext('all members')}</td>;
}
// show 'admin' perm or not
2018-12-27 02:24:34 +00:00
let showAdmin = isPro && (item.share_type !== 'public');
if (showAdmin && is_admin) {
share_permission = 'admin';
}
let iconVisibility = this.state.showOpIcon ? '' : ' invisible';
2018-12-26 08:07:22 +00:00
let unshareIconClassName = 'unshare op-icon sf2-icon-x3' + iconVisibility;
2018-12-27 02:24:34 +00:00
if (showAdmin && this.permissions.indexOf('admin') === -1) {
2018-12-26 08:07:22 +00:00
this.permissions.splice(2, 0, 'admin'); // add a item after 'r' permission;
}
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={repoUrl}>{item.repo_name}</Link></td>
{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={share_permission}
2018-12-26 08:07:22 +00:00
permissions={this.permissions}
onPermissionChangedHandler={this.changePerm}
/>
</td>
<td><a href="#" className={unshareIconClassName} title={gettext('Unshare')} onClick={this.unshare}></a></td>
</tr>
);
}
}
class ShareAdminLibraries extends Component {
2018-12-26 08:07:22 +00:00
constructor(props) {
super(props);
this.state = {
loading: true,
errorMsg: '',
items: []
};
}
componentDidMount() {
2018-11-19 06:26:23 +00:00
seafileAPI.listSharedRepos().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 SharedRepoInfo(item);
});
this.setState({
loading: false,
2018-12-27 02:24:34 +00:00
items: items
});
}).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) => {
const share_type = item.share_type;
let options = {
'share_type': share_type
};
if (share_type == 'personal') {
options.user = item.user_email;
} else if (share_type == 'group') {
options.group_id = item.group_id;
}
seafileAPI.unshareRepo(item.repo_id, options).then((res) => {
let items = [];
if (item.share_type === 'personal') {
items = this.state.items.filter(repoItem => {
return !(repoItem.user_email === item.user_email && repoItem.repo_id === item.repo_id);
});
} else if (item.share_type === 'group') {
items = this.state.items.filter(repoItem => {
return !(repoItem.group_id === item.group_id && repoItem.repo_id === item.repo_id);
});
} else if (item.share_type === 'public') {
items = this.state.items.filter(repoItem => {
return !(repoItem.share_type === 'public' && repoItem.repo_id === item.repo_id);
});
}
this.setState({items: items});
// TODO: show feedback msg
// gettext("Successfully deleted 1 item")
}).catch((error) => {
// TODO: show feedback msg
});
}
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("Libraries")}</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}
unshareFolder={this.unshareFolder}
/>
2018-11-26 06:00:32 +00:00
</div>
</div>
2018-11-26 09:53:18 +00:00
</div>
);
}
}
export default ShareAdminLibraries;