1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-01 23:20:51 +00:00

optimized getIcon interface (#2904)

* optimized getIcon interface

* optimzied get lib-icon code

* repair share-repo bug
This commit is contained in:
杨顺强
2019-01-29 15:22:02 +08:00
committed by Daniel Pan
parent 6b24da7fda
commit 8a16b4013d
17 changed files with 88 additions and 196 deletions

View File

@@ -1,11 +1,11 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import moment from 'moment'; import moment from 'moment';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter, Input } from 'reactstrap'; import { Button, Modal, ModalHeader, ModalBody, ModalFooter, Input, Alert } from 'reactstrap';
import { gettext, siteRoot } from '../../utils/constants';
import { seafileAPI } from '../../utils/seafile-api'; import { seafileAPI } from '../../utils/seafile-api';
import { gettext } from '../../utils/constants';
import { Utils } from '../../utils/utils';
import Repo from '../../models/repo'; import Repo from '../../models/repo';
import toaster from '../toast';
const shareRepoListItemProps = { const shareRepoListItemProps = {
repo: PropTypes.object.isRequired, repo: PropTypes.object.isRequired,
@@ -28,10 +28,11 @@ class ShareRepoListItem extends React.Component {
render() { render() {
let repo = this.props.repo; let repo = this.props.repo;
let iconUrl = Utils.getLibIconUrl(repo);
return ( return (
<tr> <tr>
<td className="text-center"><input type="checkbox" className="vam" name="repo" onChange={this.onRepoSelect} /></td> <td className="text-center"><input type="checkbox" className="vam" name="repo" onChange={this.onRepoSelect} /></td>
<td className="text-center"><img src={siteRoot + 'media/img/lib/48/lib.png'} alt={gettext('icon')} /></td> <td className="text-center"><img src={iconUrl} width="24" alt={gettext('icon')} /></td>
<td className="name">{repo.repo_name}</td> <td className="name">{repo.repo_name}</td>
<td>{moment(repo.last_modified).fromNow()}</td> <td>{moment(repo.last_modified).fromNow()}</td>
<td> <td>
@@ -61,6 +62,7 @@ class ShareRepoDialog extends React.Component {
currentRepo: null, currentRepo: null,
permission: 'rw', permission: 'rw',
selectedRepoList: [], selectedRepoList: [],
errMessage: '',
}; };
} }
@@ -100,7 +102,8 @@ class ShareRepoDialog extends React.Component {
handleSubmit = () => { handleSubmit = () => {
if (this.state.selectedRepoList.length === 0) { if (this.state.selectedRepoList.length === 0) {
toaster.danger(gettext('Please select a library to share.')); let errMessage = gettext('Please select a library to share.')
this.setState({errMessage: errMessage});
return; return;
} }
@@ -141,6 +144,7 @@ class ShareRepoDialog extends React.Component {
</tbody> </tbody>
</table> </table>
</ModalBody> </ModalBody>
{this.state.errMessage && <Alert color="danger" className="mt-2">{this.state.errMessage}</Alert>}
<ModalFooter> <ModalFooter>
<Button color="secondary" onClick={this.onCloseDialog}>{gettext('Close')}</Button> <Button color="secondary" onClick={this.onCloseDialog}>{gettext('Close')}</Button>
<Button color="primary" onClick={this.handleSubmit}>{gettext('Submit')}</Button> <Button color="primary" onClick={this.handleSubmit}>{gettext('Submit')}</Button>

View File

@@ -94,24 +94,8 @@ class DirentDetail extends React.Component {
render() { render() {
let { dirent } = this.props; let { dirent } = this.props;
let smallIconUrl = Utils.getDirentIcon(dirent);
let smallIconUrl = ''; let bigIconUrl = Utils.getDirentIcon(dirent, true);
let bigIconUrl = '';
let smallIconSize = Utils.isHiDPI() ? 48 : 24;
let bigIconSize = 192;
if (dirent.type === 'file') {
smallIconUrl = Utils.getFileIconUrl(dirent.name, smallIconSize);
bigIconUrl = Utils.getFileIconUrl(dirent.name, bigIconSize);
} else {
let isReadOnly = false;
if (dirent.permission === 'r' || dirent.permission === 'preview') {
isReadOnly = true;
}
let options = {isReadOnly: isReadOnly, size: smallIconSize};
smallIconUrl = Utils.getFolderIconUrl(options);
options.size = bigIconSize;
bigIconUrl = Utils.getFolderIconUrl(options);
}
return ( return (
<div className="detail-container"> <div className="detail-container">

View File

@@ -38,32 +38,21 @@ class LibDetail extends React.Component {
render() { render() {
let repo = this.props.currentRepo; let repo = this.props.currentRepo;
let isReadOnly = false; let smallIconUrl = Utils.getLibIconUrl(repo);
if (repo.permission === 'r' || repo.permission === 'preview') { let bigIconUrl = Utils.getLibIconUrl(repo, true);
isReadOnly = true;
}
let titleIcon = Utils.getLibIconUrl({
is_encrypted: repo.encrypted,
is_readonly: isReadOnly,
size: Utils.isHiDPI() ? 48 : 24
});
let iconUrl = Utils.getLibIconUrl({
is_encrypted: repo.encrypted,
is_readonly: isReadOnly,
});
return ( return (
<div className="detail-container"> <div className="detail-container">
<div className="detail-header"> <div className="detail-header">
<div className="detail-control sf2-icon-x1" onClick={this.props.closeDetails}></div> <div className="detail-control sf2-icon-x1" onClick={this.props.closeDetails}></div>
<div className="detail-title dirent-title"> <div className="detail-title dirent-title">
<img src={titleIcon} width="24" height="24" alt="" />{' '} <img src={smallIconUrl} width="24" height="24" alt="" />{' '}
<span className="name ellipsis" title={repo.repo_name}>{repo.repo_name}</span> <span className="name ellipsis" title={repo.repo_name}>{repo.repo_name}</span>
</div> </div>
</div> </div>
<div className="detail-body dirent-info"> <div className="detail-body dirent-info">
<div className="img"> <div className="img">
<img src={iconUrl} height="96" alt="" /> <img src={bigIconUrl} height="96" alt="" />
</div> </div>
<div className="dirent-table-container"> <div className="dirent-table-container">
<table className="table-thead-hidden"> <table className="table-thead-hidden">

View File

@@ -353,17 +353,7 @@ class DirentListItem extends React.Component {
}); });
} }
let size = Utils.isHiDPI() ? 48 : 24; let iconUrl = Utils.getDirentIcon(dirent);
let iconUrl = '';
if (dirent.type === 'file') {
iconUrl = Utils.getFileIconUrl(dirent.name, size);
} else {
let isReadOnly = false;
if (dirent.permission === 'r' || dirent.permission === 'preview') {
isReadOnly = true;
}
iconUrl = Utils.getFolderIconUrl({isReadOnly, size});
}
const {repoEncrypted, isRepoOwner, isAdmin} = this.props; const {repoEncrypted, isRepoOwner, isAdmin} = this.props;
let showShare = false; let showShare = false;

View File

@@ -57,8 +57,7 @@ class DraftListItem extends React.Component {
let localTime = moment.utc(draft.updated_at).toDate(); let localTime = moment.utc(draft.updated_at).toDate();
localTime = moment(localTime).fromNow(); localTime = moment(localTime).fromNow();
let size = Utils.isHiDPI() ? 48 : 24; let iconUrl = Utils.getFileIconUrl(fileName);
let iconUrl = Utils.getFileIconUrl(fileName, size);
return ( return (
<tr className={this.state.highlight} onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}> <tr className={this.state.highlight} onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}>
<td className="text-center"><img src={iconUrl} width="24" alt='' /></td> <td className="text-center"><img src={iconUrl} width="24" alt='' /></td>

View File

@@ -102,8 +102,7 @@ class ReviewListItem extends React.Component {
let localTime = moment.utc(item.updated_at).toDate(); let localTime = moment.utc(item.updated_at).toDate();
localTime = moment(localTime).fromNow(); localTime = moment(localTime).fromNow();
let size = Utils.isHiDPI() ? 48 : 24; let iconUrl = Utils.getFileIconUrl(fileName);
let iconUrl = Utils.getFileIconUrl(fileName, size);
return ( return (
<tr className={this.state.highlight} onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}> <tr className={this.state.highlight} onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}>

View File

@@ -1,6 +1,5 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { siteRoot } from '../../utils/constants';
import { Utils } from '../../utils/utils'; import { Utils } from '../../utils/utils';
const propTypes = { const propTypes = {
@@ -17,8 +16,7 @@ class SearchResultItem extends React.Component {
render() { render() {
let item = this.props.item; let item = this.props.item;
let fileIconSize = Utils.isHiDPI() ? 48 : 24; let fileIconUrl = item.is_dir ? Utils.getFolderIconUrl(false, 192) : Utils.getFileIconUrl(item.name, 192);
let fileIconUrl = item.is_dir ? siteRoot + 'media/img/folder-192.png' : Utils.getFileIconUrl(item.name, fileIconSize);
return ( return (
<li className="search-result-item" onClick={this.onClickHandler}> <li className="search-result-item" onClick={this.onClickHandler}>
<img className="item-img" src={fileIconUrl} alt="" /> <img className="item-img" src={fileIconUrl} alt="" />

View File

@@ -80,21 +80,9 @@ class SharedRepoListItem extends React.Component {
getRepoComputeParams = () => { getRepoComputeParams = () => {
let repo = this.props.repo; let repo = this.props.repo;
let isReadOnly = false;
if (repo.permission === 'r' || repo.permission === 'preview') { let iconUrl = Utils.getLibIconUrl(repo);
isReadOnly = true; let iconTitle = Utils.getLibIconTitle(repo);
}
let iconUrl = Utils.getLibIconUrl({
is_encrypted: repo.encrypted,
is_readonly: isReadOnly,
size: Utils.isHiDPI() ? 48 : 24
});
let iconTitle = Utils.getLibIconTitle({
'encrypted': repo.encrypted,
'is_admin': repo.is_admin,
'permission': repo.permission
});
let libPath = `${siteRoot}library/${repo.repo_id}/${Utils.encodePath(repo.repo_name)}/`; let libPath = `${siteRoot}library/${repo.repo_id}/${Utils.encodePath(repo.repo_name)}/`;
return { iconUrl, iconTitle, libPath }; return { iconUrl, iconTitle, libPath };

View File

@@ -34,18 +34,7 @@ class WikiDirListItem extends React.Component {
render() { render() {
let { path, dirent } = this.props; let { path, dirent } = this.props;
let href = siteRoot + 'wikis' + Utils.joinPath(path, dirent.name); let href = siteRoot + 'wikis' + Utils.joinPath(path, dirent.name);
let iconUrl = Utils.getDirentIcon(dirent);
let size = Utils.isHiDPI() ? 48 : 24;
let iconUrl = '';
if (dirent.type === 'file') {
iconUrl = Utils.getFileIconUrl(dirent.name, size);
} else {
let isReadOnly = false;
if (dirent.permission === 'r' || dirent.permission === 'preview') {
isReadOnly = true;
}
iconUrl = Utils.getFolderIconUrl({isReadOnly, size});
}
return ( return (
<tr className={this.state.highlight ? 'tr-highlight' : ''} onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}> <tr className={this.state.highlight ? 'tr-highlight' : ''} onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}>

View File

@@ -164,22 +164,9 @@ class Item extends Component {
render() { render() {
const data = this.props.data; const data = this.props.data;
const permission = data.permission;
data.icon_url = Utils.getLibIconUrl(data);
let is_readonly = false; data.icon_title = Utils.getLibIconTitle(data);
if (permission == 'r' || permission == 'preview') {
is_readonly = true;
}
data.icon_url = Utils.getLibIconUrl({
is_encrypted: data.encrypted,
is_readonly: is_readonly,
size: Utils.isHiDPI() ? 48 : 24
});
data.icon_title = Utils.getLibIconTitle({
'encrypted': data.encrypted,
'is_admin': data.is_admin,
'permission': permission
});
data.url = `${siteRoot}library/${data.repo_id}/${Utils.encodePath(data.repo_name)}/`; data.url = `${siteRoot}library/${data.repo_id}/${Utils.encodePath(data.repo_name)}/`;
let iconVisibility = this.state.showOpIcon ? '' : ' invisible'; let iconVisibility = this.state.showOpIcon ? '' : ' invisible';

View File

@@ -120,10 +120,7 @@ class Item extends Component {
if (share_permission == 'r' || share_permission == 'preview') { if (share_permission == 'r' || share_permission == 'preview') {
is_readonly = true; is_readonly = true;
} }
let iconUrl = Utils.getFolderIconUrl({ let iconUrl = Utils.getFolderIconUrl(is_readonly);
is_readonly: is_readonly,
size: Utils.isHiDPI() ? 48 : 24
});
let iconTitle = Utils.getFolderIconTitle({ let iconTitle = Utils.getFolderIconTitle({
'permission': share_permission 'permission': share_permission
}); });

View File

@@ -117,22 +117,9 @@ class Item extends Component {
getRepoParams = () => { getRepoParams = () => {
let item = this.props.item; let item = this.props.item;
const { share_permission, is_admin } = this.state;
let iconUrl = Utils.getLibIconUrl(item);
let is_readonly = false; let iconTitle = Utils.getLibIconTitle(item);
if (share_permission == 'r' || share_permission == 'preview') {
is_readonly = true;
}
let iconUrl = Utils.getLibIconUrl({
is_encrypted: item.encrypted,
is_readonly: is_readonly,
size: Utils.isHiDPI() ? 48 : 24
});
let iconTitle = Utils.getLibIconTitle({
'encrypted': item.encrypted,
'is_admin': is_admin,
'permission': share_permission
});
let repoUrl = `${siteRoot}library/${item.repo_id}/${item.repo_name}`; let repoUrl = `${siteRoot}library/${item.repo_id}/${item.repo_name}`;
return { iconUrl, iconTitle, repoUrl }; return { iconUrl, iconTitle, repoUrl };

View File

@@ -125,17 +125,13 @@ class Item extends Component {
getLinkParams = () => { getLinkParams = () => {
let item = this.props.item; let item = this.props.item;
let icon_size = Utils.isHiDPI() ? 48 : 24;
let iconUrl = ''; let iconUrl = '';
let linkUrl = ''; let linkUrl = '';
if (item.is_dir) { if (item.is_dir) {
iconUrl = Utils.getFolderIconUrl({ iconUrl = Utils.getFolderIconUrl(false);
is_readonly: false,
size: icon_size
});
linkUrl = `${siteRoot}library/${item.repo_id}/${item.repo_name}${Utils.encodePath(item.path)}`; linkUrl = `${siteRoot}library/${item.repo_id}/${item.repo_name}${Utils.encodePath(item.path)}`;
} else { } else {
iconUrl = Utils.getFileIconUrl(item.obj_name, icon_size); iconUrl = Utils.getFileIconUrl(item.obj_name);
linkUrl = `${siteRoot}lib/${item.repo_id}/file${Utils.encodePath(item.path)}`; linkUrl = `${siteRoot}lib/${item.repo_id}/file${Utils.encodePath(item.path)}`;
} }

View File

@@ -107,12 +107,7 @@ class Item extends Component {
getUploadParams = () => { getUploadParams = () => {
let item = this.props.item; let item = this.props.item;
let icon_size = Utils.isHiDPI() ? 48 : 24; let iconUrl = Utils.getFolderIconUrl(false);
let iconUrl = Utils.getFolderIconUrl({
is_readonly: false,
size: icon_size
});
let uploadUrl = `${siteRoot}library/${item.repo_id}/${item.repo_name}${Utils.encodePath(item.path)}`; let uploadUrl = `${siteRoot}library/${item.repo_id}/${item.repo_name}${Utils.encodePath(item.path)}`;
return { iconUrl, uploadUrl }; return { iconUrl, uploadUrl };

View File

@@ -182,22 +182,9 @@ class Item extends Component {
} }
const data = this.props.data; const data = this.props.data;
const permission = data.permission;
let is_readonly = false; data.icon_url = Utils.getLibIconUrl(data);
if (permission == 'r' || permission == 'preview') { data.icon_title = Utils.getLibIconTitle(data);
is_readonly = true;
}
data.icon_url = Utils.getLibIconUrl({
is_encrypted: data.encrypted,
is_readonly: is_readonly,
size: Utils.isHiDPI() ? 48 : 24
});
data.icon_title = Utils.getLibIconTitle({
'encrypted': data.encrypted,
'is_admin': data.is_admin,
'permission': permission
});
data.url = `${siteRoot}#shared-libs/lib/${data.repo_id}/`; data.url = `${siteRoot}#shared-libs/lib/${data.repo_id}/`;
let iconVisibility = this.state.showOpIcon ? '' : ' invisible'; let iconVisibility = this.state.showOpIcon ? '' : ' invisible';

View File

@@ -91,9 +91,8 @@ class TableBody extends Component {
render() { render() {
let listFilesActivities = this.state.items.map(function(item, index) { let listFilesActivities = this.state.items.map(function(item, index) {
let fileIconSize = Utils.isHiDPI() ? 48 : 24;
item.file_icon_url = Utils.getFileIconUrl(item.file_name, fileIconSize); item.file_icon_url = Utils.getFileIconUrl(item.file_name);
item.encoded_path = Utils.encodePath(item.path); item.encoded_path = Utils.encodePath(item.path);
item.thumbnail_url = item.encoded_thumbnail_src ? `${siteRoot}${item.encoded_thumbnail_src}` : ''; item.thumbnail_url = item.encoded_thumbnail_src ? `${siteRoot}${item.encoded_thumbnail_src}` : '';

View File

@@ -89,28 +89,6 @@ export const Utils = {
'default' : 'file.png' 'default' : 'file.png'
}, },
getFileIconUrl: function(filename, size) {
if (size > 24) {
size = 192;
} else {
size = 24;
}
var file_ext;
if (filename.lastIndexOf('.') == -1) {
return mediaUrl + 'img/file/' + size + '/'
+ this.FILEEXT_ICON_MAP['default'];
} else {
file_ext = filename.substr(filename.lastIndexOf('.') + 1).toLowerCase();
}
if (this.FILEEXT_ICON_MAP[file_ext]) {
return mediaUrl + 'img/file/' + size + '/' + this.FILEEXT_ICON_MAP[file_ext];
} else {
return mediaUrl + 'img/file/' + size + '/' + this.FILEEXT_ICON_MAP['default'];
}
},
// check if a file is an image // check if a file is an image
imageCheck: function (filename) { imageCheck: function (filename) {
// no file ext // no file ext
@@ -242,45 +220,71 @@ export const Utils = {
navigator.userAgent.indexOf('Chrome') > -1; navigator.userAgent.indexOf('Chrome') > -1;
}, },
getLibIconUrl: function(options) { getLibIconUrl: function(repo, isBig) {
/* let permission = repo.permission || repo.share_permission; //Compatible with regular repo and repo shared
* param: {is_encrypted, is_readonly, size} let size = Utils.isHiDPI() ? 48 : 24;
*/ size = isBig ? 256 : size;
// icon name let icon_name = 'lib.png';
var icon_name = 'lib.png'; if (repo.encrypted) {
if (options.is_encrypted) {
icon_name = 'lib-encrypted.png'; icon_name = 'lib-encrypted.png';
} }
if (options.is_readonly) { if (permission === 'r' || permission === 'perview') {
icon_name = 'lib-readonly.png'; icon_name = 'lib-readonly.png';
} }
// icon size return mediaUrl + 'img/lib/' + size + '/' + icon_name;
var icon_size = options.size || 256; // 'size' can be 24, 48, or undefined. (2017.7.31)
return mediaUrl + 'img/lib/' + icon_size + '/' + icon_name;
}, },
getFolderIconUrl: function(options) { getDirentIcon: function (dirent, isBig) {
/* let size = this.isHiDPI() ? 48 : 24;
* param: {is_readonly, size} size = isBig ? 192 : size;
*/ if (dirent.isDir()) {
const readonly = options.is_readonly; let readonly = false;
const size = options.size; if (dirent.permission && (dirent.permission === 'r' || dirent.permission === 'preview')) {
return `${mediaUrl}img/folder${readonly ? '-read-only' : ''}${size > 24 ? '-192' : '-24'}.png`; readonly = true;
}
return this.getFolderIconUrl(readonly, size);
} else {
return this.getFileIconUrl(dirent.name, size);
}
}, },
getLibIconTitle: function(options) { getFolderIconUrl: function(readonly = false, size) {
/* if (!size) {
* param: {encrypted, is_admin, permission} size = Utils.isHiDPI() ? 48 : 24;
*/ }
size = size > 24 ? 192 : 24;
return `${mediaUrl}img/folder${readonly ? '-read-only-' : '-'}${size}.png`;
},
getFileIconUrl: function(filename, size) {
if (!size) {
size = Utils.isHiDPI() ? 48 : 24;
}
size = size > 24 ? 192 : 24;
let file_ext = '';
if (filename.lastIndexOf('.') == -1) {
return mediaUrl + 'img/file/' + size + '/' + this.FILEEXT_ICON_MAP['default'];
} else {
file_ext = filename.substr(filename.lastIndexOf('.') + 1).toLowerCase();
}
if (this.FILEEXT_ICON_MAP[file_ext]) {
return mediaUrl + 'img/file/' + size + '/' + this.FILEEXT_ICON_MAP[file_ext];
} else {
return mediaUrl + 'img/file/' + size + '/' + this.FILEEXT_ICON_MAP['default'];
}
},
getLibIconTitle: function(repo) {
var title; var title;
if (options.encrypted) { let permission = repo.permission || repo.share_permission; //Compatible with regular repo and repo shared
if (repo.encrypted) {
title = gettext("Encrypted library"); title = gettext("Encrypted library");
} else if (options.is_admin) { // shared with 'admin' permission } else if (repo.is_admin) { // shared with 'admin' permission
title = gettext("Admin access"); title = gettext("Admin access");
} else { } else {
switch(options.permission) { switch(permission) {
case 'rw': case 'rw':
title = gettext("Read-Write library"); title = gettext("Read-Write library");
break; break;